Data Structures Practfile File
Data Structures Practfile File
CODE:
#include<stdio.h> #include<conio.h>
void insert_ele(int i,int arr[],int len)
{
int ele,loc;
printf("\n enter the element to be inserted \n");
scanf("%d",&ele);
printf("\n enter the location\n");
scanf("%d",&loc);
if(i==19)
{
printf("\nOverflow\n");
} else {
while(i>=loc)
{
arr[i+1]=arr[i]; i--;
} } arr[loc]=ele; len++;
printf("\n New array: ");
for(i=0;i<len;i++)
{
printf("%d ",arr[i]);
}
}
void delete_ele(int i,int arr[],int len)
{
int loc;
printf("enter the location of element to be deleted \n"); scanf("%d ",&loc);
if(len==0)
{ printf("\n Underflow");
} else {
for(i=loc;i<len;i++)
{ arr[i]=arr[i+1];
} len--;
printf("\n new array
is\n");
for(i=0;i<len;i++)
{
printf("\t%d ",arr[i]);
}
}
} void main(){
int arr[20], i=0, len=0, ch; char choice;
printf("\nEnter number of elements you want to insert : "); scanf("%d",&len);
for(i=0;i<len;i++)
{
printf("enter the element %d :",i); scanf("%d",&arr[i]);
} i--; label:
printf("\n 1.To insert element,2.To delete \n");
scanf("%d",&ch);
switch(ch)
{ case 1: insert_ele(i,arr,len); break;
case 2: delete_ele(i,arr,len); break; default:
printf("\n Invalid operation\n");
break;
}
printf("\n press y to continue or any other key to quit : "); scanf("%s",&choice);
if(choice=='y'||choice=='Y')
{
goto label;
}
}
OUTPUT:
EXPERIMENT - 2
CODE:
#include<stdio.h>
void main()
{
int matrix[10][10]; int i,j,m,n; int
sparse_counter=0;
printf("enter the orer of matrix : \n"); scanf("%d%d",&m,&n);
printf("Enter the elements of the matrix\n"); for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
scanf("%d",&matrix[i][j]); if(matrix[i][j]==0)
{
++sparse_counter;
}
}
}
if(sparse_counter>((m*n)/2))
{
printf("The given matrix is sparse matrix!!!\n");
} else
{
printf("The given matrix is not a sparse matrix\n");
}
printf("There are %d no of zeroes.",sparse_counter); }
OUTPUT
EXPERIMENT - 3
CODE:
#include<stdio.h>
int main()
{
int arr[100],search,c,n; printf("Enter no of elements\n
"); scanf("%d",&n);
printf("Enter %d integer(s)\n",n);
for(c=0;c<n;c++)
{
scanf("%d",&arr[c]);
}
printf("Enter the element to search for \n"); scanf("%d",&search);
for(c=0;c<n;c++)
{
if(arr[c]==search)
{
printf("%d is present at location %d\n",search,c+1);
break;
}
} if(c==n)
{
printf("%d isnt present in the array.\n",search);
} return 0;
}
OUTPUT:
EXPERIMENT - 4
CODE:
#include<stdio.h>
int main()
{
int c,first,l,last,middle,n,search,arr[100]; printf("Enter No of
elements \n"); scanf("%d",&n); printf("Enter %d
Integers\n",n); for(c=0;c<n;c++)
{
scanf("%d",&arr[c]);
}
printf("Enter the value to find :\n"); scanf("%d",&search);
first=0; last=n-1;
middle=(first+last)/2;
while(first<=last)
{
if(arr[middle<search])
{
first=middle+1;
}
else if(arr[middle]==search)
{
printf("%d found at location %d.\n",search,middle+1); break;
} else
{ last=middle-1;
middle=(first+last)/2;
} }
if(first>last)
{
printf("Not found ! %d isn't present in the list\n",search);
} return 0;
}
OUTPUT:
EXPERIMENT - 5
CODE:
top=-1;
printf("\n Enter the size of STACK[MAX=100]:"); scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT"); do {
printf("\n Enter the Choice:"); scanf("%d",&choice);
switch(choice)
{ case 1: {
push(); break;
} case 2:
{ pop();
break;
} case 3: {
display(); break;
} case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
} void push() {
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
} else
{
printf(" Enter a value to be pushed:"); scanf("%d",&x);
top++; stack[top]=x;
}
} void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
} else
{
printf("\n\t The popped elements is %d",stack[top]); top--;
}
} void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--) printf("\n%d",stack[i]);
printf("\n Press Next Choice");
} else
{
printf("\n The STACK is empty");
}
OUTPUT:
EXPERIMENT - 6
CODE:
#include<stdio.h> #include<stdlib.h>
struct node
{
int data; struct node*next;
CODE:
#include<stdio.h> #include<stdlib.h>
struct node
{
int data; struct node*next;
OUTPUT:
EXPERIMENT - 8
AIM: Implementation of Queue.
CODE:
#include<stdio.h> #include<stdlib.h>
struct node
{
int data; struct node *next;
}; struct node*head; void beginsert();
void lastinsert(); void randominsert();
void begin_delete(); void last_delete();
void random_delete(); void display();
void main()
{
int choice=0;
while(choice!=9)
{
printf("\n\n *****Main menu*****\n"); printf("\n choose one of the
option\n"); printf("\n-------------\n"); printf("\n1.Insert in
beginning\n2.insert at last\n3.Insert in the middle\n4.delete in the
beginning\n5.delete in the last\n6.delete in the middle\n7.display the list\n");
printf("\n Enter the choice \n"); scanf("\n %d",&choice);
switch(choice)
{ case 1:
beginsert(); break; case
2: lastinsert(); break;
case 3: randominsert();
break; case 4:
begin_delete();
break; case 5:
last_delete(); break;
case 6:
random_delete();
break; case 7:
display(); break;
case 8: exit(0);
break; default:
printf("\n Enter any valid choice\n");
}
}
}
void beginsert()
{
struct node*ptr; int item;
ptr=(struct node*)malloc(sizeof(struct node*));
if(ptr==NULL)
{
printf("OVERFLOW");
} else {
printf("\n Enter the value");
scanf("%d",&item); ptr->data=item; ptr-
>next=head; head=ptr;
printf("\n Node inserted\n");
}
}
void lastinsert() {
struct node*ptr,*temp;
int item;
ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL)
{
printf("\nOVERFLOW\n");
} else {
printf("\n Enter the value?\n"); scanf("%d",&item); ptr->data=item;
if(head==NULL)
{
ptr->next=NULL;
head=ptr;
printf("\n Node inserted");
} else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=ptr; ptr->next=NULL;
printf("\n Node inserted");
}
}
}
void randominsert()
{
int i,loc,item; struct node*ptr,*temp;
ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL)
{
printf("\n OVERFLOW");
} else {
printf("\n Enter the value\n");
scanf("%d",&item); ptr->data=item;
printf("\n Enter the location of the element to be inserted\n"); scanf("\n %d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp=temp->next;
if(temp==NULL)
{
printf("\n can't insert");
return;
}
}
ptr->next=temp->next; temp->next=ptr;
printf("\n Node inserted");
}
} void display()
{
struct node*ptr; ptr=head;
if(ptr==NULL)
{
printf("\nNothing to print");
} else {
printf("\n Printing values. . . . .\n");
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
}
} void begin_delete() {
struct node*ptr;
if(head==NULL)
{
printf("\n list is empty\n");
} else { ptr=head;
head=ptr->next;
free(ptr);
printf("\n Node deleted from the beginning ..\n");
}
}
void last_delete()
{
struct node*ptr,*ptr1;
if(head==NULL)
{
printf("\n the list is empty\n");
}
else if(head->next==NULL)
{
head=NULL; free(head);
printf("\nOnly node of the list deleted ..\n");
} else {
ptr=head;
while(ptr->next!=NULL)
{ ptr1=ptr;
ptr=ptr->next;
}
ptr1->next=NULL;
free(ptr);
printf("\n Deleted node from the last..\n"); }
}
void random_delete()
{
struct node*ptr,*ptr1; int loc,i;
printf("\n Enter the location of the node after which you want to delete\n"); scanf("%d",&loc);
ptr=head; for(i=0;i<loc;i++)
{ ptr1=ptr; ptr=ptr-
>next;
if(ptr==NULL)
{
printf("\n can't delete");
return;
}
}
ptr1->next=ptr->next;
free(ptr);
printf("\n deleted nodes%d",loc+1); }
OUTPUT:
EXPERIMENT - 9
AIM: Implementation of Queue as a linked list.
CODE :
#include<stdio.h> #include<stdlib.h>
struct node
{
int data; struct node *next;
}; struct node*head; void
beginsert(); void lastinsert();
void randominsert(); void
begin_delete(); void
last_delete(); void
random_delete(); void display();
void main()
{
int choice=0;
while(choice!=9)
{
printf("\n\n *****Main menu*****\n"); printf("\n choose one of the
option\n");
printf("\n-------------\n"); printf("\n1.Insert in beginning\n2.insert at last\n3.Insert in the
middle\n4.delete in the beginning\n5.delete in the last\n6.delete in the middle\n7.display the list\n");
printf("\n Enter the choice \n"); scanf("\n %d",&choice);
switch(choice)
{ case 1:
beginsert(); break; case
2: lastinsert(); break;
case 3: randominsert();
break; case 4:
begin_delete();
break; case 5:
last_delete(); break;
case 6:
random_delete();
break; case 7:
display(); break;
case 8: exit(0);
break; default:
printf("\n Enter any valid choice\n");
}
}
}
void beginsert()
{
struct node*ptr; int item;
ptr=(struct node*)malloc(sizeof(struct node*));
if(ptr==NULL)
{
printf("OVERFLOW");
} else {
printf("\n Enter the value");
scanf("%d",&item); ptr->data=item; ptr-
>next=head; head=ptr;
printf("\n Node inserted\n");
}
}
void lastinsert() {
struct node*ptr,*temp;
int item;
ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL)
{
printf("\nOVERFLOW\n");
} else {
printf("\n Enter the value?\n"); scanf("%d",&item);
ptr->data=item;
if(head==NULL)
{
ptr->next=NULL; head=ptr;
printf("\n Node inserted");
} else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=ptr; ptr->next=NULL;
printf("\n Node inserted");
}
}
}
void randominsert()
{
int i,loc,item; struct node*ptr,*temp;
ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL)
{
printf("\n OVERFLOW");
} else {
printf("\n Enter the value\n");
scanf("%d",&item); ptr->data=item;
printf("\n Enter the location of the element to be inserted\n");
scanf("\n %d",&loc); temp=head;
for(i=0;i<loc;i++)
{
temp=temp->next;
if(temp==NULL)
{
printf("\n can't insert");
return;
}
}
ptr->next=temp->next; temp->next=ptr;
printf("\n Node inserted");
}
}
void display()
{
struct node*ptr; ptr=head;
if(ptr==NULL)
{
printf("\nNothing to print");
} else {
printf("\n Printing values. . . . .\n");
while(ptr!=NULL)
{
printf("%d ",ptr->data); ptr=ptr->next;
}
}
}
void begin_delete()
{
struct node*ptr;
if(head==NULL)
{
printf("\n list is empty\n");
} else { ptr=head;
head=ptr->next;
free(ptr);
printf("\n Node deleted from the beginning ..\n");
}
}
void last_delete()
{
struct node*ptr,*ptr1;
if(head==NULL)
{
printf("\n the list is empty\n");
}
else if(head->next==NULL)
{
head=NULL; free(head);
printf("\nOnly node of the list deleted ..\n");
} else {
ptr=head;
while(ptr->next!=NULL)
{ ptr1=ptr;
ptr=ptr->next;
}
ptr1->next=NULL;
free(ptr);
printf("\n Deleted node from the last..\n"); }
}
void random_delete()
{
struct node*ptr,*ptr1; int loc,i;
printf("\n Enter the location of the node after which you want to delete\n"); scanf("%d",&loc);
ptr=head; for(i=0;i<loc;i++)
{ ptr1=ptr; ptr=ptr-
>next;
if(ptr==NULL)
{
printf("\n can't delete");
return;
}
}
ptr1->next=ptr->next;
free(ptr);
printf("\n deleted nodes%d",loc+1); }
OUTPUT:
EXPERIMENT 10
AIM: Implementation of circular Queue with stack.
CODE:
#include<stdio.h>
#include<stdlib.h> # define maxsize
5 void insert(); void delete(); void
display(); int front=-1,rear=-1; int
queue[maxsize]; void main()
{
int choice;
while(choice!=4)
{
printf("\n********Main menu********"); printf("\n
=====================\n");
printf("\n1.insert an element\n2.delete an element\n3.display list\n.4 exit");
printf("\n enter your choice?"); scanf("%d",&choice);
switch(choice)
{ case 1:
insert(); break;
case 2: delete();
break; case 3:
display(); break;
case 4: exit(0);
break; default:
printf("\n Enter valid choice\n");
}
}
}
void insert()
{
int item; printf("\n enter the elements\n");
scanf("\n %d",&item);
if(rear==maxsize-1)
{
printf("\n OVERFLOW\n");
return;
}
if(front==-1&&rear==-1)
{ front=0; rear=0;
} else { rear=rear+1;
}
queue[rear]=item;
printf("\n Value inserted\n");
}
void delete()
{
int item; if(front==-1||front>rear)
{
printf("\nUNDERFLOEW\n");
return; } else
{ item=queue[front];
if(front==rear)
{ front=-1;
rear=-1; } else
{
front=front+1;
}
printf("\n value deleted\n");
}
} void display()
{
int i;
if(rear==-1)
{
printf("\n empty queue\n");
} else {
printf("\n printing values...\n");
for(i=front;i<=rear;i++)
{
printf("%d ",queue[i]);
}
}
}
OUTPUT:
EXPERIMENT 11
AIM: Binary search tree insertion deletion and traversal.
CODE:
#include<stdio.h> #include<stdlib.h>
inorder(root->right);
}
}
struct node *find_Minimum(struct node *node)
{
struct node *current=node;
while(current&¤t->left!=NULL)
{
current=current->left;
}
return current;
}
OUTPUT:
EXPERIMENT - 12
AIM: Implementation of heap sort.
CODE:
#include<stdio.h>
void heapify(int a[],int n,int i)
{
int largest=i; int left=2*i+1; int right=2*i+2;
if(left<n&&a[left]>a[largest]) { largest=left;
}
if(right<n&&a[right]>a[largest])
{ largest=right;
} if(largest!=i) { int
temp=a[i]; a[i]=a[largest];
a[largest]=temp; heapify(a,n,largest);
}
}
void heapsort(int a[],int n)
{
for(int i=n/2-1;i>=0;i--)
{
heapify(a,n,i);
}
for(int i=n-1;i>=0;i--)
{ int temp=a[0];
a[0]=a[i]; a[i]=temp;
heapify(a,i,0);
}
}
void printarr(int arr[],int n)
{
for(int i=0;i<n;++i)
{ printf("%d",arr[i]); printf("
");
}
}
int main() {
int a[]={48,10,23,43.8,12,36}; int
n=sizeof(a)/sizeof(a[0]);
printarr(a,n); heapsort(a,n);
printf("\n after sorting elements: "); printarr(a,n);
return 0;
}
OUTPUT:
EXPERIMENT 13
AIM: Implementation of insertion sort.
CODE:
#include<stdio.h> #include<conio.h>
void main()
{
int s,i,j,t,lst[50];
printf("Enter the size of the list: \n"); scanf("%d",&s);
OUTPUT:
EXPERIMENT - 14
AIM: Implementation of quick sort.
CODE:
#include<stdio.h>
void swap(int *a,int *b)
{
int t=*a; *a=*b;
*b=t;
}
int partition(int array[],int low ,int high)
{
int pivot = array[high]; int i=(low-1);
for(int j=low;j<high;j++)
{
if(array[j]<=pivot)
{
i++;
swap(&array[i],&array[j]);
}
}
swap(&array[i+1],&array[high]);
return (i+1);
}
void quicksort(int array[],int low,int high)
{
if(low<high)
{
int pi=partition(array,low,high); quicksort(array,low,pi-1);
quicksort(array,pi+1,high);
}
}
void printarray(int array[],int size)
{
for(int i=0;i<size;++i)
{
printf("%d ",array[i]);
} printf("\n");
}
int main()
{
int data[]={8,7,2,1,0,9,6}; int
n=sizeof(data)/sizeof(data[0]); printf("Unsorted array
"); printarray(data,n); quicksort(data,0,n-1);
OUTPUT:
EXPERIMENT - 15
AIM: Implementation of Selection sort.
CODE:
#include<stdio.h>
OUTPUT:
EXPERIMENT - 16
AIM: Implementation of Bubble sort.
CODE:
int main() {
int data[] = {-2, 45, 0, 11, -9};
bubbleSort(data, size);
OUTPUT:
EXPERIMENT 17
AIM: Implementation of Merge sort.
CODE:
#include<stdio.h> #include<stdlib.h>
void merge(int arr[], int l, int m, int r)
{
int i,j,k;
int n1=m-l+1; int n2=r-m;
int L[n1], R[n2];
for(i=0;i<n1;i++)
{
L[i]=arr[l+i];
}
for(j=0;j<n2;j++)
{
R[j]=arr[m+1+j]; }
i=0; j=0;
k=1;
while(i<n1 && j<n2)
{ if(L[i]<=R[j])
{ arr[k]=L[i];
i++; } else {
arr[k]=R[j]; j++; }
k++;
}
while(i<n1)
{ arr[k]=L[i]; i++;
k++;
}
while(j<n2)
{ arr[k]=R[j]; j++;
k++;
}
}
void mergesort(int arr[],int l, int r)
{
if(l<r) { int m=l+(r-l)/2;
mergesort(arr,l,m);
mergesort(arr,m+1,r); merge(arr,l,m,r);
}
}
void printarrAY(int A[],int size)
{
int i;
for(i=0;i<size;i++)
{
printf("%d ", A[i]);
} printf("\n");
}
int main() {
int arr[]={12,11,13,5,6,7}; int
arr_size=sizeof(arr)/sizeof(arr[0]); printf("Given array is \n
"); printarrAY(arr,arr_size); mergesort(arr,0,arr_size-1);
printf("\n Sorted array is \n"); printarrAY(arr,arr_size);
return 0;
}
OUTPUT:
EXPERIMENT - 18
CODE:
OUTPUT: