Lab – Data Structure Using C Program – 1: Reverse a string using pointers. #include #include int main() { char str[100],rev[100],*sptr=str,*rptr=rev; int i=-1; printf("\n\nEnter a string: "); gets(str); while(*sptr) { sptr++; i++; } while(i >= 0) { sptr--; *rptr = *sptr; rptr++; i--; } *rptr = '\0'; printf("Reverse of the string is: %s ", rev); return 0; } Output: Enter a string: Data Structure Reverse of the string is: erutcurtS ataD Program – 2: College of Applied Science, Thamarassery 1Lab – Data Structure Using C Implement Pattern matching algorithm. #include #include int main(void) { char text[100],pattern[50]; int **0,j=0,temp=0,text_len=0,pat_len=0,flag=0; printf("Enter String:"); gets(text); printf("Enter Pattern string:"); gets(pattern); text_len=strlen(text); pat_len=strlen(pattern); if(pat_len<=text_len) for(**0;i<=text_len-pat_len;i++) { temp=i; for(j=0;j int main(void) { int arr[5][5],r,c,i,j,num,flag=0; printf("Enter how many rows in your 2D:"); scanf("%d",&r); printf("Enter how many columns in your 2D:"); scanf("%d",&c); printf("Enter %d x %d 2D array elements....\n",r,c); for(**0;i int main(void) { int n1,n2,i,arr1[20],arr2[20],j; printf("Enter first array limit:"); scanf("%d",&n1); printf("Enter first array elements.....\n"); for(**0;i int recursiveSearch(int arr[],int l,int r,int x); int main(void) { int arr[10],n,i,result,x; College of Applied Science, Thamarassery 5Lab – Data Structure Using C printf("Enter Array Limit:"); scanf("%d",&n); printf("Enter n elements in sorted order...."); for(**0;i=1) { int mid=l+(r-l)/2; if(arr[mid]==x) { return mid; } if(arr[mid]>x) { return recursiveSearch(arr,l,mid-1,x); } else { return recursiveSearch(arr,mid+1,r,x); } } College of Applied Science, Thamarassery 6Lab – Data Structure Using C return -1; } Output: Enter Array Limit:8 Enter n elements in sorted order.... Enter Element [1]:1 Enter Element [2]:4 Enter Element [3]:3 Enter Element [4]:5 Enter Element [5]:6 Enter Element [6]:5 Enter Element [7]:2 Enter Element [8]:4 Enter Element to Search:5 5 is present at Location 4 Program – 6: Read a sparse matrix and display its triplet representation using array. #include int main(void) { int arr[5][5],i,j,r,c,row[10],col[10],n,val[10],k=0; printf("Enter how many row in 2D:"); scanf("%d",&r); printf("Enter how many columns in 2D:"); College of Applied Science, Thamarassery 7Lab – Data Structure Using C scanf("%d",&c); printf("Enter %d x %d array elements.....\n",r,c); for(**0;i #include struct node { int data; struct node *link; } *start; int main() { int n,**1; struct node *ptr,*c,*cur,*p; printf("Enter how many nodes in your linked list:"); scanf("%d",&n); printf("Input node 1 data:"); ptr=(struct node*)malloc(sizeof(struct node)); scanf("%d",&ptr->data); start=ptr; for(**1;idata); p->link=NULL; cur=start; College of Applied Science, Thamarassery 9Lab – Data Structure Using C while(cur->link!=NULL) { cur=cur->link; } cur->link=p; } printf("Displaying linked list....\n"); c=start; while(c!=NULL) { printf("->%d",c->data); c=c->link; } } Output: Enter how many nodes in your linked list:6 Input node 1 data:2 Enter node 2 data:6 Enter node 3 data:4 Enter node 4 data:5 Enter node 5 data:8 Enter node 6 data:3 Displaying linked list.... ->2->6->4->5->8->3 Program – 8: Delete a given node from a singly linked list. #include #include struct node { int data; struct node *link; } *start; int main() College of Applied Science, Thamarassery 10Lab – Data Structure Using C { int n,**1,no,flag=0,pos=1; struct node *ptr,*c,*cur,*p; printf("Enter how many nodes in your linked list:"); scanf("%d",&n); printf("Input node 1 data:"); ptr=(struct node*)malloc(sizeof(struct node)); scanf("%d",&ptr->data); start=ptr; for(**1;idata); p->link=NULL; cur=start; while(cur->link!=NULL) { cur=cur->link; } cur->link=p; } printf("Enter number to Delete:"); scanf("%d",&no); p=start; while(p!=NULL) { if(p->data==no) { flag=1; break; } p=p->link; pos++; } if(flag==1) { if(pos==1) { p=start; College of Applied Science, Thamarassery 11Lab – Data Structure Using C start=p->link; free(p); } else { **1; p=start; cur=p; while(ilink; i++; } cur->link=p->link; free(p); } printf("\n%d is Deleted..... ",no); printf("\nAfter deleting Displaying linked list....\n"); c=start; while(c!=NULL) { printf("->%d",c->data); c=c->link; } } else { printf("%d is not present in this linked list",no); } } Output: Enter how many nodes in your linked list:6 Input node 1 data:1 Enter node 2 data:2 Enter node 3 data:3 Enter node 4 data:4 College of Applied Science, Thamarassery 12Lab – Data Structure Using C Enter node 5 data:5 Enter node 6 data:6 Enter number to Delete:4 4 is Deleted..... After deleting Displaying linked list.... ->1->2->3->5->6 Program – 9: Create a doubly linked list of integers and display in forward and backward direction. #include #include struct node { struct node *prev; struct node *next; int data; }; struct node *start=NULL; void traverse(); int main(void) { int n,i,item; struct node *ptr,*temp; printf("Enter how many nodes:"); scanf("%d",&n); for(**0;idata=item; if(start==NULL) { ptr->next=NULL; ptr->prev=NULL; start=ptr; } College of Applied Science, Thamarassery 13Lab – Data Structure Using C else { temp=start; while(temp->next!=NULL) { temp=temp->next; } temp->next=ptr; ptr->prev=temp; ptr->next=NULL; } } printf("Printing values forward direction....\n"); ptr=start; while(ptr!=NULL) { printf("%d->",ptr->data); ptr=ptr->next; } ptr=start; while(ptr->next!=NULL) { ptr=ptr->next; } printf("\nPrinting values backward direction....\n"); while(ptr!=NULL) { printf("%d->",ptr->data); ptr=ptr->prev; } return 0; } Output: Enter how many nodes:5 Enter value:1 Enter value:2 Enter value:3 Enter value:4 College of Applied Science, Thamarassery 14Lab – Data Structure Using C Enter value:5 Printing values forward direction.... 1->2->3->4->5-> Printing values backward direction.... 5->4->3->2->1-> Program – 10: Implement Stack using array. #include #define maxsize 50 int stack[maxsize],top=-1; void create(); void display(); void pop(); void push(); int main() { int ch,top=-1; while(1) { printf("\n1.Create\n"); printf("2.Display\n"); printf("3.Push\n"); printf("4.Pop\n"); printf("5.Exit\n"); printf("Enter your choice:"); scanf("%d",&ch); if(ch==5) { return 0; } switch(ch) { case 1: create(); break; case 2: display(); break; College of Applied Science, Thamarassery 15Lab – Data Structure Using C case 3: push(); break; case 4: pop(); break; } } } void create() { int che; do { top++; printf("Input element:"); scanf("%d",&stack[top]); printf("Press 1 if more data other wise 0:"); scanf("%d",&che); }while(che==1); return; } void display() { int i; if(top==-1) { printf("Stack is empty"); } for(**top;i>=0;i--) { printf("%d\t",stack[i]); } return; } void push() { int m; if(top==maxsize-1) { College of Applied Science, Thamarassery 16Lab – Data Structure Using C printf("Overflow"); return; } top++; printf("Enter new element:"); scanf("%d",&m); stack[top]=m; return; } void pop() { if(top==-1) { printf("Underflow"); return; } printf("%d is deleted",stack[top]); stack[top]='\0'; top--; return; } Output: 1.Create 2.Display 3.Push 4.Pop 5.Exit Enter your choice:1 Input element:5 Press 1 if more data other wise 0:1 Input element:4 Press 1 if more data other wise 0:1 Input element:3 Press 1 if more data other wise 0:2 1.Create 2.Display College of Applied Science, Thamarassery 17Lab – Data Structure Using C 3.Push 4.Pop 5.Exit Enter your choice:2 3 4 5 1.Create 2.Display 3.Push 4.Pop 5.Exit Enter your choice:3 Enter new element:7 1.Create 2.Display 3.Push 4.Pop 5.Exit Enter your choice:2 7 3 4 5 1.Create 2.Display 3.Push 4.Pop 5.Exit Enter your choice:4 7 is deleted 1.Create 2.Display 3.Push 4.Pop 5.Exit Enter your choice:2 3 4 5 1.Create 2.Display 3.Push 4.Pop 5.Exit Enter your choice:5 College of Applied Science, Thamarassery 18Lab – Data Structure Using C Program – 11: Implement Stack using linked list. #include #include #define maxsize 50 struct node { int data; struct node *link; }; struct node *top; void create(); void traverse(); void pop(); void push(); int main() { int c; while(1) { printf("\n1.Create\n"); printf("2.Traverse\n"); printf("3.Push\n"); printf("4.Pop\n"); printf("5.Exit\n"); printf("Enter your choice:"); scanf("%d",&c); if(c==5) { return 0; } switch(c) { case 1: create(); break; case 2: traverse(); College of Applied Science, Thamarassery 19Lab – Data Structure Using C break; case 3: push(); break; case 4: pop(); break; } } } void create() { int che; do { struct node *p; p=(struct node*)malloc(sizeof(struct node)); printf("Input element:"); scanf("%d",&p->data); p->link=NULL; p->link=top; top=p; printf("Press 1 if more data other wise 0:"); scanf("%d",&che); }while(che==1); return; } void traverse() { struct node *p; p=top; if(p==NULL) { printf("Stack is empty"); return; } printf("Stack traversing...\n"); while(p!=NULL) { printf("%d->",p->data); College of Applied Science, Thamarassery 20Lab – Data Structure Using C p=p->link; } return; } void push() { struct node *p; p=(struct node*)malloc(sizeof(struct node)); printf("Enter new element:"); scanf("%d",&p->data); p->link=top; top=p; return; } void pop() { if(top==NULL) { printf("Underflow"); return; } struct node *p; printf("%d is deleted",top->data); top=top->link; free(p); return; } Output: 1.Create 2.Traverse 3.Push 4.Pop 5.Exit Enter your choice:1 Input element:6 Press 1 if more data other wise 0:1 Input element:7 College of Applied Science, Thamarassery 21Lab – Data Structure Using C Press 1 if more data other wise 0:1 Input element:5 Press 1 if more data other wise 0:0 1.Create 2.Traverse 3.Push 4.Pop 5.Exit Enter your choice:2 Stack traversing... 5->7->6-> 1.Create 2.Traverse 3.Push 4.Pop 5.Exit Enter your choice:3 Enter new element:9 1.Create 2.Traverse 3.Push 4.Pop 5.Exit Enter your choice:2 Stack traversing... 9->5->7->6-> 1.Create 2.Traverse 3.Push 4.Pop 5.Exit Enter your choice:4 9 is deleted 1.Create 2.Traverse 3.Push 4.Pop 5.Exit College of Applied Science, Thamarassery 22Lab – Data Structure Using C Enter your choice:2 Stack traversing... 5->7->6-> 1.Create 2.Traverse 3.Push 4.Pop 5.Exit Enter your choice:4 5 is deleted 1.Create 2.Traverse 3.Push 4.Pop 5.Exit Enter your choice:2 Stack traversing... 7->6-> 1.Create 2.Traverse 3.Push 4.Pop 5.Exit Enter your choice:5 Program – 12: Evaluation of postfix expression. #include #include int stack[20]; int top=-1; void push(int); int pop(); int main(void) { char exp[20],*e; int n1,n2,n3,num; printf("Enter the postfix expression:"); scanf("%s",exp); College of Applied Science, Thamarassery 23Lab – Data Structure Using C e=exp; while(*e!='\0') { if(isdigit(*e)) { num=*e-48; push(num); } else { n1=pop(); n2=pop(); switch(*e) { case '+': n3=n1+n2; break; case '-': n3=n2-n1; break; case '*': n3=n2*n1; break; case '/': n3=n2/n1; break; } push(n3); } e++; } printf("Result=%d",pop()); return 0; } void push(int x) { stack[++top]=x; } int pop() { College of Applied Science, Thamarassery 24Lab – Data Structure Using C return stack[top--]; } Output: Enter the postfix expression:74+5- Result=6 Program – 13: Implement Queue using array. #include #define maxsize 50 int queue[maxsize],front=-1,rear=-1; void push(); void pop(); void display(); int main(void) { int ch,c=1; while(1) { printf("\n1.Push"); printf("\n2.Pop"); printf("\n3.Display"); printf("\n4.Exit"); printf("\nEnter Your Choice:"); scanf("%d",&ch); if(ch==4) { return 0; } switch(ch) { case 1: push(); break; case 2: pop(); College of Applied Science, Thamarassery 25Lab – Data Structure Using C break; case 3: display(); break; } } return 0; } void push() { int a; printf("Enter value:"); scanf("%d",&a); if(rear==maxsize-1) { printf("Overflow"); return; } rear++; queue[rear]=a; if(front==-1) { front=0; } return; } void pop() { int a; if(front==-1) { printf("Queue Empty"); return; } printf("%d is deleted",queue[front]); a=queue[front]; queue[front]=0; if(front==rear) { front=rear=-1; College of Applied Science, Thamarassery 26Lab – Data Structure Using C } else { front++; } return; } void display() { int t; if(front==-1) { printf("Queue is Empty"); } else { printf("Queue elements are....\n"); for(t=front;t<=rear;t++) { printf("%d\t",queue[t]); } } return; } Output: 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:1 Enter value:2 1.Push 2.Pop 3.Display 4.Exit College of Applied Science, Thamarassery 27Lab – Data Structure Using C Enter Your Choice:1 Enter value:4 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:1 Enter value:6 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:3 Queue elements are.... 2 4 6 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:1 Enter value:7 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:3 Queue elements are.... 2 4 6 7 1.Push 2.Pop 3.Display 4.Exit College of Applied Science, Thamarassery 28Lab – Data Structure Using C Enter Your Choice:2 2 is deleted 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:3 Queue elements are.... 4 6 7 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:1 Enter value:8 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:3 Queue elements are.... 4 6 7 8 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:2 4 is deleted 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:3 Queue elements are.... College of Applied Science, Thamarassery 29Lab – Data Structure Using C 6 7 8 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:4 Program – 14: Implement Queue using linked list. #include #include struct node { int data; struct node *link; } *front,*rear; void push(); void pop(); void display(); int main(void) { int ch; while(1) { printf("\n1.Push"); printf("\n2.Pop"); printf("\n3.Display"); printf("\n4.Exit"); printf("\nEnter Your Choice:"); scanf("%d",&ch); if(ch==4) { return 0; } switch(ch) { case 1: push(); College of Applied Science, Thamarassery 30Lab – Data Structure Using C break; case 2: pop(); break; case 3: display(); break; } } return 0; } void push() { struct node *p; int d; p=(struct node*)malloc(sizeof(struct node)); printf("Enter Data:"); scanf("%d",&d); p->data=d; p->link=NULL; if(front==NULL) { front=p; } else { rear->link=p; } rear=p; return; } void pop() { struct node *p; if(front==NULL) { printf("Queue is Empty"); return; } printf("%d is deleted",front->data); College of Applied Science, Thamarassery 31Lab – Data Structure Using C if(front==rear) { free(front); rear=NULL; front=NULL; } else { p=front; front=p->link; free(p); } return; } void display() { struct node *ptr; ptr=front; if(front==NULL) { printf("Queue is Empty"); return; } printf("Queue elements are....\n"); while(ptr!=NULL) { printf("%d->",ptr->data); ptr=ptr->link; } return; } Output: 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:1 College of Applied Science, Thamarassery 32Lab – Data Structure Using C Enter Data:3 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:1 Enter Data:4 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:1 Enter Data:5 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:3 Queue elements are.... 3->4->5-> 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:1 Enter Data:7 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:3 College of Applied Science, Thamarassery 33Lab – Data Structure Using C Queue elements are.... 3->4->5->7-> 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:2 3 is deleted 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:3 Queue elements are.... 4->5->7-> 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:2 4 is deleted 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:3 Queue elements are.... 5->7-> 1.Push 2.Pop 3.Display 4.Exit Enter Your Choice:4 Program – 15: College of Applied Science, Thamarassery 34Lab – Data Structure Using C Search an element in a binary search tree. #include #include struct bst { int info; struct bst *left; struct bst *right; }; struct bst* search(struct bst *root, int); int main(void) { struct bst *q,*p,*root,*k; int n,i,item,key; printf("Enter how many nodes:"); scanf("%d",&n); for(**0;iinfo=item; p->left=NULL; p->right=NULL; if(i==0) root=p; else { q=root; while(1) { if(p->info>q->info) { if(q->right==NULL) { q->right=p; break; } else { College of Applied Science, Thamarassery 35Lab – Data Structure Using C q=q->right; } } else { if(q->left==NULL) { q->left=p; break; } else { q=q->left; } } } } } printf("\nenter an element for search:"); scanf("%d",&key); k=search(root,key); if(k!=NULL) { printf("%d is present in this bst",key); } else { printf("%d is not present in this bst",key); } } struct bst* search(struct bst *root,int key) { if(root==NULL) { return root; } else if(key==root->info) { return root; } College of Applied Science, Thamarassery 36Lab – Data Structure Using C else if(keyinfo) return search(root->left,key); else return search(root->right,key); } Output: Enter how many nodes:5 enter element [1]:3 enter element [2]:6 enter element [3]:4 enter element [4]:8 enter element [5]:5 enter an element for search:4 4 is present in this bst Program – 16: Implement exchange sort. #include int main(void) { int i,j,temp,n,a[10]; printf("Enter your array limit:"); scanf("%d",&n); printf("Enter %d array elements....\n",n); for(**0;ia[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf("\nArray elements after sort....\n"); for(**0;i int main(void) { int i,j,min,n,a[10],temp; printf("Enter your array limit:"); scanf("%d",&n); printf("Enter %d array elements....\n",n); for(**0;i int main(void) { int i,j,n,a[10],value; printf("Enter your array limit:"); scanf("%d",&n); printf("Enter %d array elements....\n",n); for(**0;i=0)&&(a[j]>value));j--) { a[j+1]=a[j]; } a[j+1]=value; } printf("\nArray elements after sort....\n"); for(**0;i