0% found this document useful (0 votes)
92 views29 pages

Data Structures

The document contains code for implementing various data structures and sorting algorithms using C programming language. It includes code for implementing stack, queue, binary search, quicksort, mergesort, and heapsort. For each algorithm, the code includes functions to perform operations like push, pop, enqueue, dequeue, sorting elements, searching elements etc. It also contains sample input/output to demonstrate the working of the algorithms.

Uploaded by

Sarath Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views29 pages

Data Structures

The document contains code for implementing various data structures and sorting algorithms using C programming language. It includes code for implementing stack, queue, binary search, quicksort, mergesort, and heapsort. For each algorithm, the code includes functions to perform operations like push, pop, enqueue, dequeue, sorting elements, searching elements etc. It also contains sample input/output to demonstrate the working of the algorithms.

Uploaded by

Sarath Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 29

1.

STACK USING ARRAY


#include<stdio.h>
#define MAX 4
int a[MAX],TOP=-1;
int push(int item)
{
if(TOP>(MAX-1))
{
printf("\nStack full");
return;
}
TOP++;
a[TOP]=item;
printf("\nPushed item %d",item);
}
int pop()
{
if(TOP<0)
{
printf("\nStack empty popping not possible");
return;
}
TOP--;
printf("\nPoped one item");
}
void main()
{
int choice,cont=1,i,item;
do
{
printf("\n1:PUSH\n2:POP\n3:DISPLAY\nEnter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the item to be pushed onto stack");
scanf("%d",&item);
push(item);
break;
case 2:
pop();
break;
case 3:
if(TOP==-1)
{
printf("\nStack empty");
break;
}

for(i=TOP;i>=0;i--)
printf("\n%d",a[i]);
break;
default :
printf("\nInvalid choice");
}
printf("\nDo you want to continue (1/2) ?");
scanf("%d",&cont);
}while(cont==1);
}

INPUT/OUTPUT
Pushed item 10
Do you want to continue (1/2) ?1
1:PUSH
2:POP
3:DISPLAY
Enter your choice3
10
5
Do you want to continue (1/2) ?1
1:PUSH
2:POP
3:DISPLAY
Enter your choice2
Poped one item
Do you want to continue (1/2) ?1
1:PUSH
2:POP
3:DISPLAY
Enter your choice2
Poped one item
Do you want to continue (1/2) ?1
1:PUSH
2:POP
3:DISPLAY
Enter your choice2
Stack empty popping not possible
Do you want to continue (1/2) ?2

2.QUEUE USING ARRAY


#include<stdio.h>
#define MAX 4
int q[MAX],FRONT=-1,REAR=-1;
int enq(int item)
{
if(REAR==MAX-1)
{
printf("\nQueue full");
return;
}
if(FRONT==-1)
FRONT=0;
REAR++;
q[REAR]=item;
printf("\nEnqueue operation successfull");
}
int deq()
{
if((FRONT==-1)||(FRONT>REAR))
{
printf("\nQueue is empty");
return;
}
else if(FRONT==REAR)
{
FRONT=-1;
REAR=-1;
}
else
FRONT++;
printf("\nDequeue operation succesfull");
}
void main()
{
int choice,item,i;
char cntn='y';
do
{
printf("\n1:ENQUEUE\n2:DEQUEUE\n3:DISPLAY\nPlease enter
your choice");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("\nEnter the item to be entered into queue");
scanf("%d",&item);
enq(item);
break;
case 2:
deq();
break;
case 3:
if((FRONT==-1)||(FRONT>REAR))
{
printf("\nQueue empty");
break;
}
for(i=FRONT;i<=REAR;i++)
printf("\t%d",q[i]);
break;
default:
printf("\nInvalid choice");
}
printf("\nDo you want to continue (y/n) ?");
scanf(" %c",&cntn);
}while(cntn=='y');
}

INPUT/OUTPUT
1:ENQUEUE
2:DEQUEUE
3:DISPLAY
Please enter your choice1
Enter the item to be entered into queue 4
Enqueue operation successfull
Do you want to continue (y/n) ?y
1:ENQUEUE
2:DEQUEUE
3:DISPLAY
Please enter your choice 1
Enter the item to be entered into queue 3

Enqueue operation successfull


Do you want to continue (y/n) ?y
1:ENQUEUE
2:DEQUEUE
3:DISPLAY
Please enter your choice3
4
3
Do you want to continue (y/n) ?y
1:ENQUEUE
2:DEQUEUE
3:DISPLAY
Please enter your choice2
Dequeue operation succesfull
Do you want to continue (y/n) ?y
1:ENQUEUE
2:DEQUEUE
3:DISPLAY
Please enter your choice2
Dequeue operation succesfull
Do you want to continue (y/n) ?y
1:ENQUEUE
2:DEQUEUE
3:DISPLAY
Please enter your choice3
Queue empty
Do you want to continue (y/n) ?n

3. STACK USING LINKEDLIST


#include<stdio.h>
#include<stdlib.h>
struct stack
{
int item;
struct stack *link;
} *TOP=NULL;
typedef struct stack s;
int push()
{
s *temp;
temp=(s *)malloc(sizeof(s));
printf("\nEnter the data");
scanf("%d",&temp->item);
temp->link=TOP;
TOP=temp;
printf("\nPush operation successfull");
return;
}
int pop()
{
s *temp;
if(TOP==NULL)
{
printf("\nStack empty");
return;
}
temp=TOP;
TOP=TOP->link;
free(temp);
}
int display()
{
s *temp;
if(TOP==NULL)
{
printf("\nStack empty");
return;
}
temp=TOP;
while(temp!=NULL)
{
printf("\n%d",temp->item);
temp=temp->link;
}
}

void main()
{
int choice;
char ch='y';
do
{
printf("\n1:PUSH\n2:POP\n3:DISPLAY\nPlease enter a choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
default :
printf("\nPlease enter a valid choice");
}
printf("\nDo you want to continue(y/n)?");
scanf(" %c",&ch);
}while(ch=='y');
}

INPUT/OUTPUT
1:PUSH
2:POP
3:DISPLAY
Please enter a choice1
Enter the data 12
Push operation successfull
Do you want to continue(y/n)?y
1:PUSH
2:POP
3:DISPLAY
Please enter a choice1
Enter the data 22

Push operation successfull


Do you want to continue(y/n)?y
1:PUSH
2:POP
3:DISPLAY
Please enter a choice3
22
12
Do you want to continue(y/n)?y
1:PUSH
2:POP
3:DISPLAY
Please enter a choice2
Do you want to continue(y/n)?y
1:PUSH
2:POP
3:DISPLAY
Please enter a choice 3
12
Do you want to continue(y/n)?y
1:PUSH
2:POP
3:DISPLAY
Please enter a choice2
Do you want to continue(y/n)?y
1:PUSH
2:POP
3:DISPLAY
Please enter a choice3
Stack empty
Do you want to continue(y/n)?n

4. QUEUE USING LINKEDLIST


#include<stdio.h>
#include<stdlib.h>
struct queue
{
int data;
struct queue *next;
} *FRONT=NULL,*REAR=NULL;
typedef struct queue q;
int enq()
{
q *temp;
temp=(q *)malloc(sizeof(q));
printf("\nEnter the item to be enqueued");
scanf("%d",&temp->data);
if((FRONT==NULL)&&(REAR==NULL))
{
FRONT=temp;
REAR=temp;
}
else
{
REAR->next=temp;
REAR=temp;
}
printf("\nEnqueue operation successfull");
return;
}
int deq()
{
q *temp;
if(FRONT==NULL)
{
printf("\nQueue empty dequeueing not possible");
return;
}
temp=FRONT;
if(FRONT==REAR)
{
FRONT=NULL;
REAR=NULL;
}
else
{
FRONT=FRONT->next;
}

printf("\nDequeue operation successfull");


free(temp);
}
int display()
{
q *temp;
if(FRONT==NULL)
{
printf("\nQueue empty");
return;
}
temp=FRONT;
while(temp!=NULL)
{
printf("\t%d",temp->data);
temp=temp->next;
}
return;
}
void main()
{
int choice;
char ch='y';
do
{
printf("\n1:ENQUEUE\n2:DEQUEUE\n3:DISPLAY\nPlease enter a
choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
enq();
break;
case 2:
deq();
break;
case 3:
display();
break;
default :
printf("\nPlease enter a valid choice");
}
printf("\nDo you want to continue(y/n)?");
scanf(" %c",&ch);
}while(ch=='y');
}

INPUT/OUTPUT
1:ENQUEUE
2:DEQUEUE
3:DISPLAY
Please enter a choice1
Enter the item to be enqueued 20
Enqueue operation successfull
Do you want to continue(y/n)?y
1:ENQUEUE
2:DEQUEUE
3:DISPLAY
Please enter a choice1
Enter the item to be enqueued 10
Enqueue operation successfull
Do you want to continue(y/n)?y
1:ENQUEUE
2:DEQUEUE
3:DISPLAY
Please enter a choice 3
20
10
Do you want to continue(y/n)?y
1:ENQUEUE
2:DEQUEUE
3:DISPLAY
Please enter a choice 2
Dequeue operation successfull
Do you want to continue(y/n)?y
1:ENQUEUE
2:DEQUEUE
3:DISPLAY
Please enter a choice2
Dequeue operation successfull
Do you want to continue(y/n)?y
1:ENQUEUE
2:DEQUEUE
3:DISPLAY
Please enter a choice3
Queue empty
Do you want to continue(y/n)?n

5. BINARY SEARCH
#include<stdio.h>
int a[100],cnt=0,n;
void binsrch(int a[],int beg,int end,int key)
{
int flag=0,mid,i;
while(beg<=end)
{
mid=(beg+end)/2;
if(key<a[mid])
end=mid-1;
else if(key>a[mid])
beg=mid+1;
else if(key==a[mid])
{
flag=1;
break;
}
}
if(flag==1)
printf("\n%d is found at %d in the sorted version of the entered
array",key,mid+1);
else
printf("\n%d is not found",key);
}
void main()
{
int i,j,a[100],key,temp;
printf("\nEnter the number of elements in the array");
scanf("%d",&n);
printf("\nEnter the elements of the array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf("\nThe sorted array is ");
for(i=0;i<n;i++)
printf(" %d ",a[i]);
printf("\nEnter the element to be searched");
scanf("%d",&key);
binsrch(a,0,n-1,key);
}

INPUT/OUTPUT
Enter the number of elements in the array 4
Enter the elements of the array
2143
The sorted array is 1 2 3 4
Enter the element to be searched 3
3 is found at 3 in the sorted version of the entered array

6. QUICKSORT
#include<stdio.h>
void qsrt(int a[],int left,int right)
{
int q;
if(right>left)
{
q=partin(a,left,right);
qsrt(a,left,q-1);
qsrt(a,q+1,right);
}
}
int partin(int a[],int left,int right)
{
int p,j,i,temp;
p=a[left];
i=left;
j=right+1;
for(;;)
{
while(a[++i]<p)
if(i>=right)
break;
while(a[--j]>p)
if(j<=left)
break;
if(i>=j)
break;
else
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
if(i==left)
return j;
temp=a[left];
a[left]=a[j];
a[j]=temp;
return j;
}

int main()
{
int a[100],n,i;
printf("\nEnter the number of elements in the array");
scanf("%d",&n);
printf("\nEnter the array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
qsrt(a,0,n-1);
printf("\nThe sorted array is");
for(i=0;i<n;i++)
printf(" %d ",a[i]);
return;
}

INPUT/OUTPUT
Enter the number of elements in the array 5
Enter the array 21 14 2 56 1
The sorted array is 1 2 14 21 56

7. MERGESORT
#include<stdio.h>
void merge(int [],int,int,int);
void mergsrt(int [],int,int);
void mergsrt(int a[],int low,int up)
{
int q;
if(low<up)
{
q=(up+low)/2;
mergsrt(a,low,q);
mergsrt(a,q+1,up);
merge(a,low,up,q);
}
}
void merge(int a[],int low,int up,int mid)
{
//printf("proc once");
int i,j,k,b[10];
i=low;
k=low;
j=mid+1;
while((i<=mid)&&(j<=up))
{
if(a[i]<=a[j])
b[k++]=a[i++];
else
b[k++]=a[j++];
}
while(i<=mid)
b[k++]=a[i++];
while(j<=up)
b[k++]=a[j++];
for(i=low;i<=up;i++)
{
a[i]=b[i];
//printf(" %d ",b[i]);
}
}

void main()
{
int i,n,a[10];
printf("\nEnter the number of elements in the array ");
scanf("%d",&n);
printf("\nEnter the array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergsrt(a,0,n-1);
printf("\nThe sorted array is ");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
}

INPUT/OUTPUT
Enter the number of elements in the array 5
Enter the array 4 5 6 1 3
The sorted array is

8. HEAPSORT

#include<stdio.h>
void heapify(int a[],int i,int m)
{
int l,r,max,temp;
if(i==0)
{
l=1;
r=2;
}
else
{
l=2*i;
r=2*i+1;
}
max=i;
if((l<=m)&&(a[l]>a[max]))
max=l;
if((r<=m)&&(a[r]>a[max]))
max=r;
if(max!=i)
{
temp=a[i];
a[i]=a[max];
a[max]=temp;
heapify(a,max,m);
}
}
int main()
{
int a[10],i,j,n,temp,new;
printf("\nEnter the number of elements in the array");
scanf("%d",&n);
new=n;
printf("\nEnter the elements to be sorted");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
while(n>0)
{
for(i=n/2;i>=0;i--)
heapify(a,i,n);
temp=a[0];
a[0]=a[n];
a[n]=temp;

n=n-1;
}
printf("\nThe sorted array is");
for(i=0;i<new;i++)
printf("\t%d",a[i]);
}

INPUT/OUTPUT
Enter the number of elements in the array 4
Enter the elements to be sorted 22 343 2 1
The sorted array is

22

343

9. INFIX TO POSTFIX

#include<stdio.h>
#include<string.h>
char stac[100];
int TOP=0;
int isp(char x)
{
if((x=='+')||(x=='-'))
return 2;
else if((x=='*')||(x=='/')||(x=='%'))
return 4;
else if(x=='^')
return 5;
else if(x=='(')
return 0;
else if(x==')')
return -1;
else
return 8;
}
int icp(char x)
{
if((x=='+')||(x=='-'))
return 1;
else if((x=='*')||(x=='/')||(x=='%'))
return 3;
else if(x=='^')
return 6;
else if(x=='(')
return 9;
else if(x==')')
return 0;
else
return 7;
}
void push(char x)
{
if(TOP>=99)
printf("\nStack overflow");
else
{
TOP++;
stac[TOP]=x;
}
}

void output(char x)
{
printf("%c",x);
}
char pop()
{
if(TOP==-1)
{
printf("\nError");
return;
}
else
return stac[TOP--];
}
void conv(char *inf)
{
int i=0;
char x,item;
while(inf[i]!='\0')
{
item=inf[i];
x=pop();
if(icp(item)==7)
{
push(x);
output(item);
}
else if(item==')')
{
while(x!='(')
{
output(x);
x=pop();
}
}
else if(isp(x)>=icp(item))
{
while(isp(x)>=icp(item))
{
output(x);
x=pop();
}
push(x);
push(item);
}

else if(isp(x)<icp(item))
{
push(x);
push(item);
}
else
printf("\nInvalid expression");
i++;
item=inf[i];
}
}
void main()
{
int i,b;
char inf[100],temp;
stac[0]='(';
printf("\nEnter the infix expression");
scanf("%s",inf);
b=strlen(inf);
for(i=0;inf[i]!='\0';i++);
i--;
inf[i+1]=')';
inf[i+2]='\0';
printf("\nThe Postfix expression is ");
conv(inf);
}

INPUT/OUTPUT
Enter the infix expression (A+B)^C-(D*E)/F
The Postfix expression is AB+C^DE*F/-

10. HASHING
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
struct bucket
{
int data;
struct bucket *next;
}*t=NULL,*arr[MAX]={NULL},*p=NULL;
int hash(int x)
{
return x%7;
}
void insert()
{
int s,item;
printf("\nEnter the item to be inserted");
scanf("%d",&item);
if(search(item)==1)
printf("\nThe element has already been inserted");
else
{
s=hash(item);
t=(struct bucket *)malloc(sizeof(struct bucket));
arr[s];
t->data=item;
t->next=arr[s];
arr[s]=t;
}
}
void delete()
{
int s,item;
printf("\nEnter the element");
scanf("%d",&item);
s=search(item);
if(s==0)
printf("\nThe element id not found");
else
{
s=hash(item);
p=arr[s];

if(p->data==item)
{
arr[s]=p->next;
printf("\nElement found and deletion succesfull");
free(p);
}
else
{
while(p!=NULL)
{
t=p->next;
if(t->data==item)
{
p->next=t->next;
printf("\nElement found and deletion succesfull");
free(t);
break;
}
p=t;
t=t->next;
}
}
}
}
void display()
{
int i;
for(i=0;i<10;i++)
{
printf("\n%d->",i);
p=arr[i];
if(p==NULL)
printf("NULL");
else
{
t=arr[i];
while(t!=NULL)
{
printf("%d->",t->data);
t=t->next;
}
printf("NULL");
}
}
}

int search(int item)


{
int s;
s=hash(item);
p=arr[s];
if(p==NULL)
{
}
else
{
while(p!=NULL)
{
if(p->data==item)
return 1;
p=p->next;
}
}
return 0;
}
void main()
{
int s,choice,item;
char ch='y';
do
{
printf("\n1:INSERT\n2:DELETE\n3:SEARCH\n4:DISPLAY\n5:EXIT\nPlease enter
your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
printf("\n enter the element to be searched");
scanf("%d",&item);
s=search(item);
if(s==1)
printf("\nFound");
else
printf("\nNot found");
break;
case 4:

display();
break;
case 5:
exit(0);
break;
default:
printf("\nPlease enter a valid choice");
}
printf("\nDo you want to continue(y/n)");
scanf(" %c",&ch);
}while(3);
}

INPUT/OUTPUT

1:INSERT
2:DELETE
3:SEARCH
4:DISPLAY
5:EXIT
Please enter your choice 1
Enter the item to be inserted 2
Do you want to continue(y/n) y
1:INSERT
2:DELETE
3:SEARCH
4:DISPLAY
5:EXIT
Please enter your choice 1
Enter the item to be inserted 9
Do you want to continue(y/n)y
1:INSERT
2:DELETE
3:SEARCH
4:DISPLAY
5:EXIT
Please enter your choice 4

0->NULL
1->NULL
2->9->2->NULL
3->NULL
4->NULL
5->NULL
6->NULL
7->NULL
8->NULL
9->NULL
Do you want to continue(y/n) y
1:INSERT
2:DELETE
3:SEARCH
4:DISPLAY
5:EXIT
Please enter your choice 3
enter the element to be searched 4
Not found
Do you want to continue(y/n)y
1:INSERT
2:DELETE
3:SEARCH
4:DISPLAY
5:EXIT
Please enter your choice 3
enter the element to be searched 2
Found
Do you want to continue(y/n) y
1:INSERT
2:DELETE
3:SEARCH
4:DISPLAY
5:EXIT
Please enter your choice 2
Enter the element 9
Element found and deletion succesfull

Do you want to continue(y/n)y


1:INSERT
2:DELETE
3:SEARCH
4:DISPLAY
5:EXIT
Please enter your choice 4
0->NULL
1->NULL
2->2->NULL
3->NULL
4->NULL
5->NULL
6->NULL
7->NULL
8->NULL
9->NULL
Do you want to continue(y/n)y
1:INSERT
2:DELETE
3:SEARCH
4:DISPLAY
5:EXIT
Please enter your choice2
Enter the element 2
Element found and deletion succesfull
Do you want to continue(y/n)y
1:INSERT
2:DELETE
3:SEARCH
4:DISPLAY
5:EXIT
Please enter your choice 4
0->NULL
1->NULL
2->NULL
3->NULL
4->NULL
5->NULL
6->NULL
7->NULL
8->NULL
9->NULL

Do you want to continue(y/n)y


1:INSERT
2:DELETE
3:SEARCH
4:DISPLAY
5:EXIT
Please enter your choice3
enter the element to be searched 9
Not found
Do you want to continue(y/n) y
1:INSERT
2:DELETE
3:SEARCH
4:DISPLAY
5:EXIT
Please enter your choice 5

You might also like