0% found this document useful (0 votes)
0 views

Data Structure Programs

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

Data Structure Programs

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

PROGRAM 1- LINEAR SEARCH AND BINARY SEARCH

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int A[100],n,key;
int Lsearch (int A[],int n,int key)
{
int i=-1;
while(i<n)
{
if(A[++i]==key)
return i;
}
return -1;
}
int binsearch (int A[],int n,int key)
{
int first,last,mid,i;
first=0;
last=n-1;
while (first<=last)
{
mid=(first+last)/2;
if (key==A[mid])
return mid;
else if (key<A[mid])
last=mid-1;
else
first =mid+1;
}
return -1;
}
void acceptinput()
{
int i;
printf("enter the number of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter element %d:",i+1);
scanf("%d",&A[i]);
}
printf("enter an element to be searched:");
scanf("%d",&key);
}
void main ()
{
int ch,flag;
while (1)
{
printf("\nsearching technique");
printf("\n****");
printf("\n1.linear search");
printf("\n2.binary search ");
printf("\n3.exit");
printf("\n enter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1: acceptinput();
flag=Lsearch (A,n,key);
if(flag==-1)
printf ("\n search is un succesful");
else
printf("\n an element %d found at position :%d",key,flag+1);
break;
case 2:printf("\n enter element in ascending order for binary search \n");
acceptinput();
flag =binsearch (A,n,key);
if (flag==-1)
printf("%d not found in array \n",key);
else
printf("\n an element %d found at position %d ",key,flag+1);
break;
default: exit(0);
printf("\n invalid choice\n");
return;
}
getch();
}
}

PROGRAM 2 : BUBBLE SORT

#include<stdio.h>
#include<conio.h>
void bubble_sort(int a[],int n)
{
int pass,temp,j;
for(pass=1;pass<n;pass++)
{
for(j=0;j<=n-pass-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
void main()
{
int i,j, a[20],n,temp;
printf("\n enter the number of element:");
scanf("%d",&n);
printf("\n enter the array element:/n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubble_sort(a,n);
printf("\n The sorted array element are :\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}

PROGRAM 3: INSERTION AND SELECTION SORT

#include<stdio.h>
#include<stdlib.h>
int a[100],n;
int maxin(int a[],int k,int n)
{
int loc,j,max,i;
max=a[k];
loc=k;
for(j=k+1;j<=n-1;j++)
if(max<a[j])
{
max=a[j];
loc=j;
}
return(loc);
}
void insertion_sort(int a[],int n)
{
int pass,k,temp,j;
for(pass=1;pass<n;pass++)
{
k=a[pass];
for(j=pass-1;j>=0&&k>a[j];j--)
a[j+1]=a[j];
a[j+1]=k;
}
}
void acceptinput()
{
int i;
printf("enter the number of elements:");
scanf("%d",&n);
printf("\n enter the array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void display()
{
int i;
printf("\n the sorted array is:");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
void main()
{
int k,temp,loc,ch;
while(1)
{
printf("\n sorting techniques");
printf("\n ******************");
printf("\n 1.insertion sort");
printf("\n 2. selection sort");
printf("\n 3. exit");
printf("\n enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: acceptinput();
insertion_sort(a,n);
display();
break;
case 2: acceptinput();
for(k=0;k<n;k++)
{
loc=maxin(a,k,n);
temp=a[k];
a[k]=a[loc];
a[loc]=temp;
}
display();
break;
case 3: exit(0);
default: printf("invalid choice\n");
}
getch();
}
}

PROGRAM 5 : LINEAR QUEUE

#include<stdio.h>
#include<conio.h>
#define MAX 50
int queue_array[MAX];
int rear= -1;
int front= -1;
void DISPLAY()
{
int i;
if(front== -1)
printf("queue is empty:\n");
else
{
printf("queue is:\n");
for(i=front;i<=rear;i++)
printf("%d\n",queue_array[i]);
}
getch();
}
void insert_Q(int item)
{
if(rear==MAX-1)
printf("queue is overflow\n");
else
{
if(front==-1)
front=0;
rear=rear+1;
queue_array[rear]=item;
}
DISPLAY();
}
void delete_Q()
{
if(front==-1)
{
printf("\n queue is underflow");
return;
}
else
{
printf("\n element deleted from the queue is :%d\n",queue_array[front]);
front=front+1;
}
DISPLAY();
}
void main()
{
int choice;
clrscr();
insert_Q(61);
insert_Q(16);
insert_Q(8);
insert_Q(27);
delete_Q();
delete_Q();
delete_Q();
getch();
}

PROGRAM 6 : CIRCULAR QUEUE

#include<stdio.h>
#define size 4
int front=-1;
int rear=-1;
int queue[size];
void display_CQ()
{
int i;
printf("\n Circular Queue:");
if(front>rear)
{
for(i=front;i<size;i++)
{
printf("%d_>",queue[i]);
}
for(i=0;i<=rear;i++)
printf("%d_>",queue[i]);
}
else
{
for(i=front;i<=rear;i++)
printf("%d_>",queue[i]);
}
printf("[%d]",queue[front]);
getch();
}
void insert_CQ(int item)
{
if(front==0 &&rear==size-1)
{
printf("queue is full");
return;
}
else if(rear==-1)
{
rear++;
front++;
}
else if(rear==size-1 && front>0)
{
rear=0;
}
else
{
rear++;
}
queue[rear]=item;
display_CQ();
}
void delete_CQ()
{
if(front==-1)
{
printf("Queue is empty");
}
else if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front++;
}
display_CQ();
}
void main()
{
clrscr();
printf("\n***Insertion***:");
insert_CQ(61);
insert_CQ(16);
insert_CQ(8);
insert_CQ(27);
printf("\n***Deletion***:");
delete_CQ();
delete_CQ();
delete_CQ();
delete_CQ();
}

PROGRAM 7: SINGLY LINKED LIST

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct node
{
int INFO;
struct node*LINK;
};
typedef struct node NODE;
NODE *start=NULL;
void insertOrdered(int data)
{
NODE *NEWNODE=(NODE*) malloc(sizeof(NODE));
NEWNODE->INFO=data;
if(start==NULL)
{
start= NEWNODE;
start->LINK=NULL;
}
else if (data <start->INFO)
{
NEWNODE->LINK=start;
start=NEWNODE;
}
else
{
NODE*PREVPTR=start;
NODE* CURRPTR=start->LINK;
while(CURRPTR!=NULL&& data>CURRPTR->INFO)
{
PREVPTR= CURRPTR;
CURRPTR= CURRPTR->LINK;
}
PREVPTR->LINK=NEWNODE;
NEWNODE->LINK=CURRPTR;
}
}
void deleteOrdered(int data)
{
NODE *PREVPTR =start;
NODE *CURRPTR= start->LINK;
if(start==NULL)
printf("\n list is empty");
else if(data == start ->INFO)
{
start=CURRPTR;
free(PREVPTR);
}
else
{
while(CURRPTR != NULL&& CURRPTR->INFO !=data)
{
PREVPTR=CURRPTR;
CURRPTR= CURRPTR->LINK;
}
if(CURRPTR!= NULL)
{
PREVPTR->LINK= CURRPTR->LINK;
free(CURRPTR);
}
else
printf("\n data not found in the list");
}
}
void display()
{

NODE *CURRPTR= start;


if(CURRPTR ==NULL)
printf("empty");
else
{
while(CURRPTR !=NULL)
{
printf("%d",CURRPTR-> INFO);
printf("->");
CURRPTR=CURRPTR->LINK;
}
printf("NULL");
}
}
int main()
{
int ch, data;
while(1)
{
printf("\n ordered linked list operations");
printf("\n ***");
printf("\n 1.insert");
printf("\n 2. delete");
printf("\n 3. display");
printf("\n 4. exit");
printf("\n enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n enter data to be inserted:");
scanf("%d", &data);
printf("\n linked list before insertion is:\n");
display();
insertOrdered(data);
printf("\n linked list after insertion is:\n ");
display();
break;

case 2: printf("\n enter data to be deleted :");


scanf("%d",&data);
printf("\n linked list before deletion is :\n");
display();
deleteOrdered(data);
printf("\n linked list after deletion is :\n ");
display();
break;
case 3: display();
break;
case 4: exit (0) ;
}
}
}

PROGRAM 8 : EVALUATE 2 POLYNOMIAL

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct polynomial
{
int coeff;
int power;
struct polynomial *LINK;
};
typedef struct polynomial NODE;
NODE *poly1=NULL,*poly2=NULL,*poly3=NULL;
NODE *create_poly();
NODE *add_poly(NODE *poly1,NODE *poly2);
void display_poly(NODE *ptr);
NODE *create_poly()
{
int flag;
int coeff,pow;
NODE *tmp_node = (NODE *) malloc(sizeof(NODE));
NODE *poly=tmp_node;
do
{
printf("\n enter coeff:");
scanf("%d",&coeff);
tmp_node->coeff = coeff;
printf("\n ENTER pow:");
scanf("%d",&pow);
tmp_node->power=pow;
tmp_node->LINK=NULL;
printf("\n DO you want add more terms?(Y=1/N=0):");
scanf("%d", &flag);
if(flag==1)
{
tmp_node->LINK=(NODE*)malloc(sizeof(NODE));
tmp_node=tmp_node->LINK;
tmp_node->LINK=NULL;
}
}while(flag);
return poly;
}
NODE *add_poly(NODE *poly1,NODE *poly2)
{
NODE *tmp_node,*poly;
tmp_node=(NODE *)malloc(sizeof(NODE));
tmp_node->LINK=NULL;
poly3=tmp_node;
while(poly1 && poly2) {
if(poly1->power>poly2->power) {
tmp_node->power=poly1->power;
tmp_node->coeff=poly1->coeff;
poly1=poly1->LINK;
}
else if(poly1->power<poly2->power){
tmp_node->power=poly2->power;
tmp_node->coeff=poly2->coeff;
poly2=poly2->LINK;
}
else{
tmp_node->power=poly1->power;
tmp_node->coeff=poly1->coeff + poly2->coeff;
poly1=poly1->LINK;
poly2=poly2->LINK;
}
if(poly1 && poly2){
tmp_node->LINK=(NODE *) malloc(sizeof(NODE));
tmp_node=tmp_node->LINK;
tmp_node->LINK=NULL;
}
}
while(poly1||poly2){
tmp_node->LINK=(NODE *)malloc(sizeof(NODE));
tmp_node=tmp_node->LINK;
tmp_node->LINK=NULL;
if(poly1){
tmp_node->power=poly1->power;
tmp_node->coeff=poly1->coeff;
poly1=poly1->LINK;
}
if(poly2){
tmp_node->power=poly2->power;
tmp_node->coeff=poly2->coeff;
poly2=poly2->LINK;
}
}
return 0;
}
void display_poly(NODE *ptr)
{
while(ptr!=NULL)
{
printf("%dx^%d",ptr->coeff,ptr->power);
ptr=ptr->LINK;
if(ptr!=NULL)
printf(" + ");
}
}
void main()
{
clrscr();
printf("\n create 1st polynomial\n");
poly1=create_poly();
printf("\n the first polynomial expression is:\n");
display_poly(poly1);
printf("\n create 2nd polynomial\n");
poly2=create_poly();
printf("\n the second polynomial expression is:\n");
display_poly(poly2);
add_poly(poly1,poly2);
printf("\n the addition of two polynomials is:\n");
display_poly(poly3);
}

PROGRAM 9: STACK

#include<stdio.h>
#include<conio.h>
#define MAX 5
int STACK [MAX];
int TOP=-1;
Void DISPLAY();
Void PUSH(int item)
{
if(TOP==MAX-1)
{
printf("\n Stack Overflow");
}
else
printf("\n**PUSH%d**");
TOP=TOP+1;
STACK[TOP]=item;
DISPLAY();
}
void POP()
{
if(TOP=-1)
{
printf("\n Stack UNdeflow");
getch();
}
else
{
printf("\n**%dPOPED**",STACK[TOP]);
TOP=TOP-1;
DISPLAY();
}
}
void DISPLAY()
{
int i;
for(i=TOP;i>0;i--)
{
printf("\n STACK[%d]=%d",i,STACK[i]);
}
getch();
}
void main()
{
int i;
clrscr();
printf("\n PUSH 5,9,34,17,32\n");
PUSH(5);
PUSH(9);
PUSH(34);
PUSH(17);
PUSH(32):
printf("\n POP 3 elemets\n");
POP();
POP();
POP():
}

PROGRAM 10: GCD

#include<stdio.h>
#include<conio.h>
int GCD(int m,int n)
{
if (n==0)
{
return(m);
}
else if(n>m)
{
return (GCD(n,m));
}
else
{
return(GCD(n,m%n));
}
}
void main()
{
int gcd12,gcd3;
clrscr();
gcd12=GCD(4,6);
printf("\n GCD between 4&6=%d",gcd12);
gcd3=(GCD(gcd12,8));
printf("\n GCD between 4,6&8=%d",gcd3);
getch();
}

PROGRAM 12 : INFIX TO POSTFIX EXPRESSION

#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 20
char s[MAX];
int top=-1;
int precedence(char elem);
void push(char elem);
int pop();
void main()
{
char infix[MAX],postfix[MAX],ch,elem;
int i=0,j=0;
clrscr();
printf("\n\t\t program to convert infix to postfix expression.:");
printf("\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n enter the infix expression:\n");
scanf("%s",&infix);
elem=1;
push('#');
for(i=0;i<strlen(infix);i++)
{
ch=infix[i];
if(isalnum(ch))
postfix[j++]=ch;
else if(ch=='(')
push(ch);
else if(ch==')')
{
while(s[top]!='(')
postfix[j++]=pop();
elem=pop();
}
else
{
while(precedence(s[top])>=precedence(ch))
postfix[j++]=pop();
push(ch);
}
}
while(s[top]!='#')
postfix[j++]=pop();
postfix[j]='\0';
printf("\n postfix expression conversion is:\n%s \n",postfix);
}
int precedence(char elem)
{
switch(elem)
{
case'+':
case'-':return(1);
case'*':
case'/':return(2);
case'^':return(3);
case'(':
case'#':return(0);
}
return 0 ;
}
void push(char elem)
{
++top;
s[top]=elem;
}
int pop()
{
char elem;
elem=s[top];
--top;
return(elem);
}

PROGRAM 13: EVALUATE POSTFIX EXPRESSION

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 20
int s[MAX],top=0;
void main ()
{
char postfix [MAX],ch;
int i,op1,op2,res;
printf("\n\t\t program to evaluate postfix expression.");
printf("\n\t\t ~~~~~~");
printf("\n enter the postfix expression:\n");
scanf("%s",&postfix);
for(i=0;i<strlen(postfix);i++)
{
ch=postfix[i];
if(isdigit(ch))
push(ch-'0');
else
{ op2=pop();
op1=pop();
switch(ch)
{
case '+':res=op1+op2;
break;
case'-':res=op1-op2;
break;
case'*':res= op1*op2;
break;
case'/':res= op1/op2;
break;
case '^':res=pow(op1,op2);
break;
default: printf(" invalid character \n");
}
push (res);
}
}
printf(" reasult of above expression is:%d\n", pop());
}
push( int element )
{
++top;
s[top]=element;
return(s[top]);
}
int pop()
{
int element;
element=s[top];
--top;
return(element);
}

PROGRAM 15: INORDER , PREORDER AND POSTORDER

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node*left;
struct node*right;
};
typedef struct node NODE;
NODE*root=NULL;
disp(struct node *ptr,int level)
{
int i;
if(ptr!=NULL)
{
disp(ptr->right,level+1);
for(i=0;i<level;i++)
printf("\t\t ");
printf("%10d\n",ptr->info);
disp(ptr->left,level+1);
}
return 0;
}
void create(int item)
{
NODE*newnode,*currptr,*ptr;
newnode=(NODE*)malloc(sizeof(NODE));
newnode->info=item;
newnode->left=NULL;
newnode->right=NULL;
if(root==NULL)
root=newnode;
else
{
currptr=root;
while(currptr!=NULL)
{
ptr=currptr;
currptr=(item>currptr->info)?currptr->right:currptr->left;
}
if(item<ptr->info)
ptr->left=newnode;
else
ptr->right=newnode;
}
}
void in_order(NODE*ptr)
{
if(ptr)
{
in_order(ptr->left);
printf("%d\t",ptr->info);
in_order(ptr->right);
}
}
void pre_order(NODE*ptr)
{
if(ptr)
{
printf("%d\t\t",ptr->info);
pre_order(ptr->left);
pre_order(ptr->right);
}
}
void post_order(NODE*ptr)
{
if(ptr)
{
post_order(ptr->left);
post_order(ptr->right);
printf("%d\t\t",ptr->info);
}
}
void main()
{
int item,ch,n,i;
while(1)
{
printf("\n binary search tree menu");
printf("\n-------------------------");
printf("\n 1.create");
printf("\n 2.display");
printf("\n 3.preorder");
printf("\n 4.inorder");
printf("\n 5.postorder");
printf("\n 6. exit");
printf("\n enter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n enter the number of nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter the data for the node:");
scanf("%d",&item);
create(item);
}
break;
case 2:printf("\n the binary tree nodes are:\n\n\n\n");
disp(root,1);
break;
case 3:printf("\n preorder traversal is:");
pre_order(root);
break;
case 4:printf("\n inorder traversal is:");
in_order(root);
break;
case 5:printf("\n postorder traversal is:");
post_order(root);
break;
case 6:exit(0);
}
getch();
}
}

PROGRAM 16: HEAP SORT

#include<stdio.h>
#include<conio.h>

void heapify(int arr[],int n,int i)


{
int largest=i;
int left=2*i+1;
int right=2*i+2;
int temp;
if(left<n&&arr[left]>arr[largest])
largest=left;
if(right<n&&arr[right]>arr[largest])
largest=right;
if(largest!=i)
{
temp=arr[i];
arr[i]=arr[largest];
arr[largest]=temp;
heapify(arr,n,largest);
}
}
void heapSort(int arr[],int n)
{
int i, temp;
for (i=n/2-1;i>=0;i--)
heapify(arr,n,i);
for(i=n-1;i>=0;i--)
{
temp=arr[0];
arr[0]=arr[i];
arr[i]=temp;
heapify(arr,i,0);
}
}
void main()
{
int arr[20];
int n,i;
clrscr();
printf(" enter the size of the array:");
scanf("%d",&n);
printf("\n enter the elements:");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
heapSort(arr,n);
printf("\n sorted array is :");
for(i=0;i<n;++i)
printf("%d\t",arr[i]);
getch();

PROGRAM 17: STRING OPERATIONS

#include<Stdio.h>
#include<conio.h>
#include<stdlib.h>
int LENGTH(char*str)
{
int i=0;
while (str[i]!='\0')
i++;
return i;
}
void CONCAT(char *str1,char *str2)
{
int i=0,j=0;
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
i++;
j++;
}
str1[i]='\0';
}
void SUBSTR(char str [],int pos,int len)
{
char sub [50];
int slen=LENGTH(str);
int p,j,max_ext;
if(pos > slen)
{
printf("\n invalid position");
return;
}
max_ext = slen-pos+1;
if(len > max_ext)
{
printf("\n invalid substring length");
return;
}
p=pos - 1;
for(j=0;j<len;j++)
{
sub[j]=str[p];
p++;
}
sub[j]='\0';
printf("\n substring =%s",sub );
}
void REPLACE(char str[],char substr[],char replacestr[])
{f
char output[50];
int i=0,j=0,flag=0,start=0;
while(str[i]!='\0')
{
if (str[i]==substr[j])
{
if (!flag)
start=i;
j++;
if (substr[j]=='\0')
break;
flag=1;
}
else
{
flag=start=j=0;
}
i++;
}
if (substr[j]=='\0'&& flag)
{
for(i=0;i < start;i++)
output[i]=str[i];
for(j=0;j<LENGTH(replacestr);j++)
{
output[i]=replacestr[j];
i++;
}
for(j = start+LENGTH(substr);j<LENGTH(str);j++)
{
output[i]=str[j];
i++;
}
output[i]='\0';
printf("output :%s\n",output);
}
else
printf("%s is not a substring of %s \n",substr,str);
}
void main()
{
char str1[50],str2[50],substr[50],repstr[50];
int len,pos,ch;
while(1)
{
printf("\n string operations");
printf("\n****");
printf("\n 1.string length");
printf("\n 2. string concatenation");
printf("\n 3.extracting substring");
printf ("\n4.replace a string");
printf("\n 5 .exit");
printf("\n enter your choice:");
scanf("%d",&ch);
fflush(stdin);
switch(ch)
{
case 1:printf("\n enter the string :");
gets(str1);
printf("\n the length of a string :%d",LENGTH(str1));
break;
case 2: printf ("\n enter the first string:");
gets(str1);
printf("\n enter the second string:");
gets(str2);
CONCAT(str1,str2);
printf("\n concatenated string :%s",str1);
break;
case 3: printf("\n enter the string :");
gets(str1);
printf("\n enter the position of a substring :");
scanf("%d",&pos);
printf ("\n enter the length of a substring:");
scanf("%d",&len);
SUBSTR(str1,pos,len);
break;
case 4: printf("enter the string :");
gets(str1);
printf("enter the string to be removed:");
gets(substr);
printf("enter the string to replace:");
gets(repstr);
REPLACE(str1,substr,repstr);
case 5:exit (0);
default:printf("\n invalid option");
break;
}
}
getch();
}

You might also like