Top Banner
DATA STRUCTURE Lab Manual for 3rd Sem. DATA STRUCTURE LABORATORY (Common to CSE & ISE) /* 1. Write a C Program to create a sequential file with atleast 5 records, each record having the structure shown below: Name: character string of length 25 marks1, marks2, marks3: positive integer USN: non zero positive integer PROGRAM */ #include<stdio.h> #include<stdlib.h> #include<conio.h> void add(); void disp(); void search(); struct { int usn,m1,m2,m3; char name[26]; }std; FILE *fp; void main() { int ch; for(;;) { clrscr(); printf("\nMenu\n\n1.Add record\n2.Search\n3.Display\n4.Exit\nEnter your choice..\n"); scanf("%d",&ch); switch(ch) { case 1: add();break; case 2: search();break; case 3: disp();break; case 4: exit(0); default:printf("\nWrong Choice");getch();break; } } } 1
60
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.DATA STRUCTURE LABORATORY (Common to CSE & ISE)/* 1. Write a C Program to create a sequential file with atleast 5 records, each recordhaving the structure shown below: Name: character string of length 25 marks1, marks2, marks3: positive integer USN: non zero positive integerPROGRAM */#include<stdio.h>#include<stdlib.h>#include<conio.h>void add();void disp();void search();struct{int usn,m1,m2,m3;char name[26];}std;FILE *fp;void main(){int ch;for(;;){clrscr();printf("\nMenu\n\n1.Add record\n2.Search\n3.Display\n4.Exit\nEnter your choice..\n");scanf("%d",&ch);switch(ch){case 1: add();break;case 2: search();break;case 3: disp();break;case 4: exit(0);default:printf("\nWrong Choice");getch();break;}}}

1

Page 2: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.void add(){clrscr();printf("Add Record\n\n");fflush(stdin);printf("\nEnter the USN of the student..\n");scanf("%d",&std.usn);printf("\nEnter the name of the student..\n");fflush(stdin);gets(std.name);printf("\nEnter the marks in 3 subjects..\n");scanf("%d%d%d",&std.m1,&std.m2,&std.m3);fp=fopen("std.dat","a+");if(fp==NULL){printf("\nError in accessing the file");getch();return;}fprintf(fp,"%d\t%s\t%d\t%d\t%d\n",std.usn,std.name,std.m1,std.m2,std.m3);fclose(fp);printf("\nRecord added successfully..\n");getch();return;}void search(){int flag=0,usn;clrscr();printf("Search\n");printf("\nEnter the USN to be searched..\n");scanf("%d",&usn);fp=fopen("std.dat","r");if(fp==NULL){printf("\nError in accessing the file");getch();return;}

2

Page 3: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.while((fscanf(fp,"%d%s%d%d%d",&std.usn,std.name,&std.m1,&std.m2,&std.m3)!=EOF)){if(usn==std.usn){flag=1;printf("\nUSN:%d\nName:%s\nMarks:%d\t%d\t%d\n",std.usn,std.name,std.m1,std.m2,std.m3);getch();}}if(flag==0){printf("\nRecord with given USN not found..\n");getch();}fclose(fp);return;}void disp(){clrscr();printf("\nDisplay All\n");fp=fopen("std.dat","r");if(fp==NULL){printf("\nError in accessing the file");getch();return;}printf("\nUSN\tName\tMarks\tM1\tM2\tM3\n\n");while(fscanf(fp,"%d%s%d%d%d",&std.usn,std.name,&std.m1,&std.m2,&std.m3)!=EOF){printf("%d\t%s\t\t%d\t%d\t%d\n",std.usn,std.name,std.m1,std.m2,std.m3);}getch();fclose(fp);return;}

3

Page 4: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem./* OUTPUTMenu1.Add record2.Search3.Display4.ExitEnter your choice..1Enter the USN of the student..101Enter the name of the student..AbcEnter the marks in 3 subjects..242325Record added successfully..Menu1.Add record2.Search3.Display4.ExitEnter your choice..1Enter the USN of the student..102Enter the name of the student..DefEnter the marks in 3 subjects..212223Record added successfully..Menu1.Add record2.Search3.Display4.ExitEnter your choice..1

4

Page 5: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.Enter the USN of the student..103Enter the name of the student..GhiEnter the marks in 3 subjects..202122Record added successfully..Menu1.Add record2.Search3.Display4.ExitEnter your choice..3Display AllUSN101102103NameAbcDefGhiMarks1242321222021Marks2252322Marks3Menu1.Add record2.Search3.Display4.ExitEnter your choice..2SearchEnter the USN to be searched..106Record with given USN not found..Menu1.Add record2.Search3.Display

5

Page 6: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.4.ExitEnter your choice..2SearchEnter the USN to be searched..102USN: 102Name: DefMarks:212223Menu1.Add record2.Search3.Display4.ExitEnter your choice..4*/

6

Page 7: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem./* 2.write and demonstrate the following C functions: a. newstrcpy that does the same job as strcpy b. newstrcat that does the same job as strcat without using any library functions. PROGRAM */#include<stdio.h>#include<conio.h>void newcopy(char des[],char sor[]){int i;for(i=0;sor[i]!='\0';i++)des[i]=sor[i];des[i]='\0';}void newcat(char des[],char sor[]){int i,j;for(i=0;des[i]!=0;i++);for(j=0;sor[j]!=0;i++,j++)des[i]=sor[j];des[i]='\0';}void main(){char a[1000],b[1000],cpy1[1000],cpy2[1000];clrscr();printf("\nEnter two strings..\n");gets(a);gets(b);clrscr();printf("\nCopy Function\n");newcopy(cpy1,a);newcopy(cpy2,b);printf("\n\na=%s\nb=%s\ncpy1=%s\ncpy2=%s",a,b,cpy1,cpy2");printf("\nConcatenation Function\n");newcat(a,b);getch();printf("\n\na=%s\nb=%s\ncpy1=%s\ncpy2=%s",a,b,cpy1,cpy2);getch();}

7

Page 8: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem./* OUTPUTEnter two strings..CompScienceCopy Functiona= Compb= Sciencecpy1= Compcpy2= ScienceConcatenation Functiona= Comp Scienceb= Sciencecpy1= Compcpy2= Science*/

8

Page 9: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem./* 3.Write a C Program, which accepts the Internet Protocol (IP) address in decimal dotformat(ex.153.18.8.105) and converts it into 32-bit long integer (ex.2568095849) usingstrtok library function and unions.PROGRAM */#include<stdio.h>#include<conio.h>#include<string.h>#include<stdlib.h>typedef struct{ unsigned f4:8; unsigned f3:8; unsigned f2:8; unsigned f1:8;}ip_dot_format;typedef union{ ip_dot_format ip; long int ip32bit;}ip_address;void main(){ char a[13]; ip_address i; char *p; clrscr();printf("\nEnter the ip address in dot format\n");scanf("%s", a);p = strtok(a, ".");i.ip.f1 = atoi(p);p = strtok(NULL, ".");i.ip.f2 = atoi(p);p = strtok(NULL, ".");i.ip.f3 = atoi(p);p = strtok(NULL, ".");i.ip.f4 = atoi(p);printf("32 bit long integer is %lu", i.ip32bit);getch();}

9

Page 10: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem./* OUTPUTEnter the ip address in dot format172.32.5.12132 bit long integer is 2887779705*/

10

Page 11: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem./* 4.Write a C Program to construct a stack of integers and to perform the following operations on it: a: push b: pop c: displayThe program should print appropriate messages for stack overflow, stack underflow andstack empty.PROGRAM */#include<stdio.h>#include<stdlib.h>#include<conio.h>#define STACK 5int st[STACK],top=-1;void push(){int item;if(top==STACK-1){printf("\nStack Overflow..\n");getch();return;}printf("\nEnter the element to be inserted\n");scanf("%d",&item);top++;st[top]=item;printf("\n%d Inserted..\n",st[top]);getch();}void pop(){int item;if(top==-1){printf("\nStack Underflow..\n");getch();return;}printf("\nElement to be popped is %d",st[top]);item=st[top];top--;printf("\nElement popped is %d",item);

11

Page 12: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.getch();}void display(){int i;if(top==-1){printf("\nStack is empty..\n");getch();return;}printf("\nStack elements are:\n");for(i=0;i<=top;i++){printf("\n%d",st[i]);}getch();}void main(){int ch;for(;;){clrscr();printf("\nMenu\n\n1.Push\n2.pop\n3.display\n4.exit\nEnter your choice..\n");scanf("%d",&ch);switch(ch){case 1:push();break;case 2:pop();break;case 3:display();break;case 4: exit(0);default:printf("\nWrong menu choice\n"); getch();}}}

12

Page 13: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem./* OUTPUTMenu1.Push2.pop3.display4.exitEnter your choice..3Stack is emptyMenu1.Push2.pop3.display4.exitEnter your choice..1Enter the element to be inserted1111 InsertedMenu1.Push2.pop3.display4.exitEnter your choice..1Enter the element to be inserted2222 InsertedMenu1.Push2.pop3.display4.exitEnter your choice..1

13

Page 14: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.Enter the element to be inserted3333 InsertedMenu1.Push2.pop3.display4.exitEnter your choice..1Enter the element to be inserted4444 InsertedMenu1.Push2.pop3.display4.exitEnter your choice..1Enter the element to be inserted5555 InsertedMenu1.Push2.pop3.display4.exitEnter your choice..1Stack Overflow

14

Page 15: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.Menu1.Push2.pop3.display4.exitEnter your choice..3Stack elements are:1122334455Menu1.Push2.pop3.display4.exitEnter your choice..2Element to be popped is 55Element popped is 55Menu1.Push2.pop3.display4.exitEnter your choice..2Element to be popped is 44Element popped is 44Menu1.Push2.pop3.display4.exitEnter your choice..2

15

Page 16: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.Element to be popped is 33Element popped is 33Menu1.Push2.pop3.display4.exitEnter your choice..2Element to be popped is 22Element popped is 22Menu1.Push2.pop3.display4.exitEnter your choice..2Element to be popped is 11Element popped is 11Menu1.Push2.pop3.display4.exitEnter your choice..2Stack UnderflowMenu1.Push2.pop3.display4.exitEnter your choice..4*/

16

Page 17: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem./* 5.Write a C Program to convert and print a given valid parenthesized infix arithmeticexpression to postfix expression. The expression consists of single character operands andthe binary operators +(plus), -(minus), *(multiply) and /(divide).PROGRAM */#include<stdio.h>#include<conio.h>#include<string.h>#define MAX 5int F(char ch){switch(ch){case '+':case '-':return 2;case '*':case '/':return 4;case '^':return 5;case '(':return 0;case '#':return -1;default: return 8;}}int G(char ch){switch(ch){case '+':case '-':return 1;case '*':case '/':return 3;case '^':return 6;case '(':return 9;case ')':return 0;default: return 7;}}void in2pos(char infix[],char postfix[]){int top=-1,i,j=0;char symbol,s[100];s[++top]='#';

17

Page 18: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.for(i=0;i<strlen(infix);i++){symbol=infix[i];while(F(s[top])>G(symbol)){postfix[j]=s[top--];j++;}if (F(s[top])!=G(symbol))s[++top]=symbol;elsetop--;}while(s[top]!='#'){postfix[j++]=s[top--];}postfix[j]='\0';}void main(){char infix[100],postfix[100];clrscr();printf("\nEnter the infix statement..\n");scanf("%s",infix);in2pos(infix,postfix);printf("\nThe postfix expression for\n%s is\n%s",infix,postfix);getch();}/* OUTPUTEnter the infix statement.((1+(2-3)*4)^5+6)The postfix expression for ((1+(2-3)*4)^5+6) is123-4*+5^6+*/

18

Page 19: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem./* 6.Write a C Program to evaluate a valid suffix/postfix expression using stack. Assumethat the suffix/postfix expression is read as a single line consisting of non-negative singledigit operands and binary arithmetic operators.The arithmetic operators are +(add), -(subtract), *(multiply) and /(divide).PROGRAM */#include<stdio.h>#include<string.h>#include<conio.h>#include<math.h>#include<ctype.h>float cal(float op1,char ch,float op2){switch(ch){case '+': return (op1+op2);case '-': return (op1-op2);case '*': return (op1*op2);case '/': return (op1/op2);case '^': return (pow(op1,op2));}}void main(){float s[100],res,op1,op2;int i,top=-1;char pos[100],sym;clrscr();printf("\nEnter the postfix expression..\n");scanf("%s",pos);for(i=0;i<strlen(pos);i++){sym=pos[i];if(isdigit(sym)){s[++top]=sym-'0';}else{op2=s[top--];op1=s[top--];res=cal(op1,sym,op2);s[++top]=res;}}

19

Page 20: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.res=s[top--];printf("\n%s = %f",pos,res);getch();}/* OUTPUTEnter the postfix expression..23^6*9-45/78+/+23^6*9-45/78+/+ = 39.053333*/

20

Page 21: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem./* 7.Write a C Program to simulate the working of a queue of integers using an array.Provide the following operations: a: Insert b: Delete c: DisplayPROGRAM */#include<stdio.h>#include<conio.h>#include<process.h>#define size 5int queue[size];int front,rear;void insert(int);int delete1();void display();void main(){ int item,choice; clrscr(); front=0; rear=-1; while(1) { printf("enter the choice\n"); printf("1:insert 2:delete 3:display 4:exit\n"); scanf("%d",&choice); switch(choice) { case 1:printf("Enter item to be inserted\n"); scanf("%d",&item); insert(item); break; case 2:item=delete1(); if(item==-1) { printf("Queue empty\n"); } else { printf("Element deleted is %d\n",item); } break; case 3:display(); break; case 4:exit(0);

21

Page 22: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.default:printf("Invalid choice\n"); break;}}}void insert(int ele){ if(rear==(size-1)) { printf("Queue full\n"); } else { rear=rear+1; queue[rear]=ele; }}int delete1(){ int temp; if(rear<front) { temp=-1; } else { temp=queue[front]; front++; } return(temp);}void display(){ int i; if(rear<front) { printf("Queue empty\n"); } else { for(i=front;i<=rear;i++) { printf("%d\n",queue[i]); } }}

22

Page 23: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem./* OUTPUTenter the choice1:insert 2:delete 3:display 4:exit1enter item to be inserted11enter the choice1:insert 2:delete 3:display 4:exit1enter item to be inserted22enter the choice1:insert 2:delete 3:display 4:exit32211enter the choice1:insert 2:delete 3:display 4:exit2Item deleted is 22enter the choice1:insert 2:delete 3:display 4:exit2Item deleted is 112queue emptyenter the choice1:insert 2:delete 3:display 4:exit1enter item to be inserted44enter the choice1:insert 2:delete 3:display 4:exit1enter item to be inserted55enter the choice1:insert 2:delete 3:display 4:exit1enter item to be inserted66enter the choice1:insert 2:delete 3:display 4:exit1enter item to be inserted

23

Page 24: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.77enter the choice1:insert 2:delete 3:display 4:exit1enter item to be inserted88enter the choice1:insert 2:delete 3:display 4:exit1enter item to be inserted99queue fullenter the choice1:insert 2:delete 3:display 4:exit4*/

24

Page 25: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem./* 8.Write a C Program to simulate the working of a circular queue of integers using anarray. Provide the following operations: a.Insert b.Delete c.Display.PROGRAM */#include<stdio.h>#include<conio.h>#include<process.h>#define size 5int cqueue[size];int front=0,rear=-1,count;void cinsert(int);int cdelete1();void cdisplay();void main(){ int ele; int choice; clrscr(); while(1) { printf("enter choice 1.Insert 2.Delete 3.Display 4.Exit\n"); scanf("%d",&choice); switch(choice) { case 1:printf("Enter the element\n"); scanf("%d",&ele); cinsert(ele); break; case 2:ele=cdelete(); if(ele==-999) { printf("Queue empty\n"); } else { printf("The element deleted is %d\n",ele); } break; case 3:cdisplay(); break; case 4:exit(0); default:printf("Invalid choice\n");

25

Page 26: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.break;}}}void cinsert(int ele){ if(count==size) { printf("Queue full\n"); } else { count=count+1; rear=(rear+1)%size; cqueue[rear]=ele; }}int cdelete(){ int temp; if(count==0) { temp=-999; } else { count=count-1; temp=cqueue[front]; front=(front+1)%size; } return(temp);}void cdisplay(){ int i,j; j=front; for(i=0;i<count;i++) { printf("%d\t\n",cqueue[j]); j=(j+1)%size; }}/*

26

Page 27: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.OUTPUTenter choice 1.Insert 2.Delete 3.Display 4.Exit1Enter the element10enter choice 1.Insert 2.Delete 3.Display 4.Exit1Enter the element20enter choice 1.Insert 2.Delete 3.Display 4.Exit31020enter choice 1.Insert 2.Delete 3.Display 4.Exit2The element deleted is 10enter choice 1.Insert 2.Delete 3.Display 4.Exit2The element deleted is 20enter choice 1.Insert 2.Delete 3.Display 4.Exit2Queue emptyenter choice 1.Insert 2.Delete 3.Display 4.Exit1Enter the element11enter choice 1.Insert 2.Delete 3.Display 4.Exit1Enter the element22enter choice 1.Insert 2.Delete 3.Display 4.Exit1Enter the element33enter choice 1.Insert 2.Delete 3.Display 4.Exit1Enter the element44enter choice 1.Insert 2.Delete 3.Display 4.Exit1Enter the element55enter choice 1.Insert 2.Delete 3.Display 4.Exit1Enter the element66Queue fullenter choice 1.Insert 2.Delete 3.Display 4.Exit2The element deleted is 11

27

Page 28: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.enter choice 1.Insert 2.Delete 3.Display 4.Exit1Enter the element66enter choice 1.Insert 2.Delete 3.Display 4.Exit36622334455enter choice 1.Insert 2.Delete 3.Display 4.Exit4*/

28

Page 29: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem./* 9.Write a C Program using dynamic variables and pointers, to construct a singly linkedlist consisting of the following information in each node: student id (integer), student name(character string) and semester (integer). The operations to be supported are: a. The insertion operation 1. At the front of a list 2. At the back of the list 3. At any position in the listb. Deleting a node based on student id. If the specified node is not present in the list an error message should be displayed. Both the options should be demonstrated.c. Searching a node based on student id and update the info. content. If the specified node is not present in the list an error message should be displayed. Both situations should be displayed.d. Displaying all the nodes in the list.PROGRAM */#include<stdio.h>#include<conio.h>#include<process.h>#include<string.h>void ins_front();void ins_end();void ins_pos();void del();void search();void display();struct NODE{ int id,sem; char name[10]; struct NODE *link;};typedef struct NODE node;node *start=NULL,*temp,*new1,*cptr,*prev;int i,choice;char ch;void main(){ clrscr(); do { printf(" 1:ins_front \n 2:ins_end \n 3:ins_pos \n 4:delete \n 5:search \n 6:display\n"); printf("enter ur choice\n"); scanf("%d",&choice); switch(choice) {

29

Page 30: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.case 1:ins_front(); break;case 2:ins_end(); break;case 3:ins_pos(); break;case 4:del(); break;case 5:search(); break;case 6:display(); break;}printf("do u want to continue\n");fflush(stdin);scanf("%c",&ch);}while(ch=='y');getch();}void ins_front(){ new1=(node*) malloc(sizeof(node)); printf("enter student id, name, sem\n"); scanf("%d%s%d",&new1->id,new1->name,&new1->sem); if(start==NULL) { new1->link=NULL; start=new1; } else { new1->link=start; start=new1; } //return(0);}void ins_end(){ new1=(node*) malloc(sizeof(node)); printf("enter student id,name,sem\n"); scanf("%d%s%d",&new1->id,new1->name,&new1->sem); if(start==NULL) { new1->link=NULL;

30

Page 31: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.start=new1;}temp=start;while(temp->link!=NULL){ temp=temp->link;}temp->link=new1;new1->link=NULL;//return(0);}void ins_pos(){ int pos,count; new1=(node*) malloc(sizeof(node)); printf("enter student id,name,sem\n"); scanf("%d%s%d",&new1->id,new1->name,&new1->sem); printf("enter the position to be insert\n"); scanf("%d",&pos); if(pos==1) { new1->link=start; start=new1; } count=2; prev=start; temp=start->link; while(count<pos && temp!=NULL) { prev=temp; temp=temp->link; count++; } new1->link=temp; prev->link=new1; //return(0);}void del(){ int key; printf("enter the student id to be delete\n"); scanf("%d",&key); if(start->id==key) { temp=start;

31

Page 32: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.start=start->link;free(temp);}prev=start;temp=start->link;while(temp!=NULL && temp->id!=key){ prev=temp; temp=temp->link;}if(temp==NULL){ printf("student with that id does not exist\n"); //return 0;}prev->link=temp->link;}void search(){ int key; printf("enter the student id to be search\n"); scanf("%d",&key); temp=start; while(temp!=NULL) { if(temp->id==key) { printf("student with that id exist & his detals are\n"); printf("%d%s%d",temp->id,temp->name,temp->sem); printf("do u want to update(y/n)\n"); ch=getche(); if(ch=='y') { printf("enter new id,name,sem\n"); scanf("%d%s%d",&temp->id,temp->name,& temp->sem); }//return; } temp=temp->link; }}void display(){ printf("STUDENTS INFORMATION\n");

32

Page 33: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.printf("ID NAME SEM\n");temp=start;while(temp!=NULL){ printf("%d\t%s\t%d\n",temp->id,temp->name,temp->sem); temp=temp->link;}}/* OUTPUT1:ins_front 2:ins_end 3:ins_posenter ur choice1enter student id,name,sem11 DUVAN 3do u want to continuey1:ins_front 2:ins_end 3:ins_posenter ur choice1enter student id,name,sem22 PRAJWAL 4do u want to continuey1:ins_front 2:ins_end 3:ins_posenter ur choice6STUDENTS INFORMATION--------------------ID NAME SEM---------------------22PRAJWAL 411DUVAN 3do u want to continuey1:ins_front 2:ins_end 3:ins_posenter ur choice3enter student id,name,sem33 SACHIN 3enter the position to be insert2do u want to continuey4:delete 5:search 6:display4:delete 5:search 6:display4:delete 5:search 6:display4:delete 5:search 6:display

33

Page 34: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.1:ins_front 2:ins_end 3:ins_pos 4:delete 5:search 6:displayenter ur choice6STUDENTS INFORMATIONID NAME SEM22PRAJWAL 433SACHIN 311DUVAN 3do u want to continuen*/

34

Page 35: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem./* 10.Write a C Program using dynamic variables and pointers to construct a stack ofintegers using singly linked list and to perform the following operations: a: Push b: Pop c: DisplayThe program should print appropriate messages for stack overflow and stack empty.PROGRAM */#include<stdio.h>#include<stdlib.h>#include<conio.h>struct node{int info;struct node *link;};typedef struct node NODE;NODE *start=NULL;void push(){NODE *tmp;int item;tmp=(NODE*)malloc(sizeof(NODE));if(tmp==NULL){printf("\nStack Overflow..\n");getch();return;}printf("\nEnter the element to be inserted\n");scanf("%d",&item);tmp->info=item;if(start==NULL){start=tmp;tmp->link=NULL;}else{tmp->link=start;start=tmp;

35

Page 36: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.}printf("\n%d Inserted..\n",tmp->info);getch();}void pop(){NODE *tmp;int item;if(start==NULL){printf("\nStack Underflow..\n");getch();return;}printf("\nElement to be popped is %d",start->info);item=start->info;tmp=start;start=start->link;free(tmp);printf("\nElement popped is %d",item);getch();}void display(){NODE *tmp;if(start==NULL){printf("\nStack is empty..\n");getch();return;}printf("\nStack elements are:\n");for(tmp=start;tmp!=NULL;tmp=tmp->link){printf("\n%d",tmp->info);}getch();}void main(){int ch;for(;;){

36

Page 37: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.clrscr();printf("\nMenu\n\n1.Push\n2.pop\n3.display\n4.exit\nEnter your choice..\n");scanf("%d",&ch);switch(ch){case 1:push();break;case 2:pop();break;case 3:display();break;case 4: exit(0);default:printf("\nWrong menu choice\n"); getch();}}}/* OUTPUTMenu1.Push2.pop3.display4.exitEnter your choice..3Stack is emptyMenu1.Push2.pop3.display4.exitEnter your choice..1Enter the element to be inserted1111 InsertedMenu1.Push2.pop3.display4.exitEnter your choice..1

37

Page 38: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.Enter the element to be inserted2222 InsertedMenu1.Push2.pop3.display4.exitEnter your choice..1Enter the element to be inserted3333 InsertedMenu1.Push2.pop3.display4.exitEnter your choice..1Enter the element to be inserted4444 InsertedMenu1.Push2.pop3.display4.exitEnter your choice..1Enter the element to be inserted5555 InsertedMenu1.Push2.pop3.display4.exitEnter your choice..3

38

Page 39: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.Stack elements are:1122334455Menu1.Push2.pop3.display4.exitEnter your choice..2Element to be popped is 55Element popped is 55Menu1.Push2.pop3.display4.exitEnter your choice..2Element to be popped is 44Element popped is 44Menu1.Push2.pop3.display4.exitEnter your choice..2Element to be popped is 33Element popped is 33Menu1.Push2.pop3.display4.exitEnter your choice..2

39

Page 40: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.Element to be popped is 22Element popped is 22Menu1.Push2.pop3.display4.exitEnter your choice..2Element to be popped is 11Element popped is 11Menu1.Push2.pop3.display4.exitEnter your choice..2Stack UnderflowMenu1.Push2.pop3.display4.exitEnter your choice..4*/

40

Page 41: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem./* 11.Write a C Program using dynamic variables and pointers to construct a queue ofintegers using linked list and to perform the following operations: a: Insert b: Delete c: DisplayThe program should print appropriate messages for queue full and queue empty.PROGRAM*/#include<stdio.h>#include<stdlib.h>#include<conio.h>struct node{int info;struct node *link;};typedef struct node NODE;NODE *start=NULL;void ins(){NODE *tmp,*curptr;int item;tmp=(NODE*)malloc(sizeof(NODE));if(tmp==NULL){printf("\nQueue Overflow..\n");getch();return;}printf("\nEnter the element to be inserted\n");scanf("%d",&item);tmp->info=item;tmp->link=NULL;if(start==NULL){start=tmp;}else{for(curptr=start;curptr->link!=NULL;curptr=curptr->link){

41

Page 42: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.}curptr->link=tmp;}printf("\n%d Inserted..\n",tmp->info);getch();}void del(){NODE *tmp;int item;if(start==NULL){printf("\nQueue Underflow..\n");getch();return;}printf("\nElement to be Deleted is %d",start->info);item=start->info;tmp=start;start=start->link;free(tmp);printf("\nElement Deleted is %d",item);getch();}void display(){NODE *tmp;if(start==NULL){printf("\nQueue is empty..\n");getch();return;}printf("\nQueue elements are:\n");for(tmp=start;tmp!=NULL;tmp=tmp->link){printf("\n%d",tmp->info);}getch();}void main(){int ch;

42

Page 43: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.for(;;){clrscr();printf("\nMenu\n\n1.Insert\n2.Delete\n3.Display\n4.exit\nEnter your choice..\n");scanf("%d",&ch);switch(ch){case 1:ins();break;case 2:del();break;case 3:display();break;case 4: exit(0);default:printf("\nWrong menu choice\n"); getch();}}}/*OUTPUTMenu1.Insert2.Delete3.Display4.exitEnter your choice..3Queue is emptyMenu1.Insert2.Delete3.Display4.exitEnter your choice..1Enter the element to be inserted1111 InsertedMenu1.Insert2.Delete3.Display4.exitEnter your choice..1

43

Page 44: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.Enter the element to be inserted2222 InsertedMenu1.Insert2.Delete3.Display4.exitEnter your choice..1Enter the element to be inserted3333 InsertedMenu1.Insert2.Delete3.Display4.exitEnter your choice..1Enter the element to be inserted4444 InsertedMenu1.Insert2.Delete3.Display4.exitEnter your choice..1Enter the element to be inserted5555 InsertedMenu1.Insert2.Delete3.Display4.exitEnter your choice..3

44

Page 45: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.Queue elements are:1122334455Menu1.Insert2.Delete3.Display4.exitEnter your choice..2Element to be Deleteped is 55Element Deleteped is 55Menu1.Insert2.Delete3.Display4.exitEnter your choice..2Element to be Deleteped is 44Element Deleteped is 44Menu1.Insert2.Delete3.Display4.exitEnter your choice..2Element to be Deleteped is 33Element Deleteped is 33Menu1.Insert2.Delete3.Display4.exit

45

Page 46: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.Enter your choice..2Element to be Deleted is 22Element Deleted is 22Menu1.Insert2.Delete3.Display4.exitEnter your choice..2Element to be Deleted is 11Element Deleted is 11Menu1.Insert2.Delete3.Display4.exitEnter your choice..2Queue UnderflowMenu1.Insert2.Delete3.Display4.exitEnter your choice..4*/

46

Page 47: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem./* 12. Write a C Program to support the following operations on a doubly linked list whereeach node consists of integers: a. Create a doubly linked list by adding each node at the front. b. Insert a new node to the left of the node whose key value is read as an input. c. Delete the node of a given data, if it is found, otherwise display appropriate message/ d. Display the contents of the list.PROGRAM */#include<stdio.h>#include<conio.h>#include<stdlib.h>void display();struct node{ int info; struct node*next; struct node*prev;};struct node*f=NULL,*temp,*cur;void insert(){ temp=(struct node *)malloc(sizeof(struct node)); if(temp==NULL) printf("\n no memory\n"); else { printf("\n enter the element\n"); scanf("%d",&temp->info); temp->next=temp->prev=NULL; }}void in_front(){ printf("\n inserting at the front of the list\n"); insert(); if(f==NULL) { f=temp; return; } temp->next=f; f->prev=temp; f=temp; display(); return;

47

Page 48: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.}void ins_key(){ int n; printf("\n enter element before which node is to be inserted\n"); scanf("%d",&n); insert(); cur=f; if(cur->info==n) { temp->next=cur; cur->prev=temp; f=temp; display(); return; } while(cur->info!=n&&cur!=NULL) cur=cur->next; if(cur==NULL) { printf("\n element not found\n"); free(cur); return; } cur->prev->next=temp; temp->prev=cur->prev; temp->next=cur; cur->prev=temp; display(); return;}void del_id(){ int n; printf("\n enter the element to be deleted\n"); scanf("%d",&n); cur=f; if(cur->info==n) { printf("\n node containing element %d is deleted\n",cur->info); if(f->next==NULL) { free(cur); f=NULL; } else

48

Page 49: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.{f=f->next;f->prev=NULL;free(cur);}display();return;}while(cur->info!=n&&cur!=NULL) cur=cur->next;if(cur==NULL){ printf("\n element not found\n"); free(cur); return;}cur->prev->next=cur->next;cur->next->prev=cur->prev;display();return;}void display(){ struct node *p; if(f==NULL) { printf("\n list is empty\n"); return; } p=f; printf("\n list\n"); while(p!=NULL) { printf(" %d-",p->info); p=p->next; } return;}void main(){ int ch=1; clrscr(); while(ch) { printf("\n enter your choice\n"); printf("\n 1:insert at front \n 2:insert to left of given data");

49

Page 50: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.printf("\n 3:delete \n 4:display \n 5:exit\n");scanf("%d",&ch);switch(ch){ case 1:in_front(); break; case 2:ins_key(); break; case 3:del_id(); break; case 4:display(); break; case 5:exit(0); default:printf("invalid choice\n"); break;}}getch();}/* OUTPUTenter your choice1:insert at front 2:insert to left of given data3:delete 4:display 5:exitinserting at the front of the listenter the element10enter your choice1:insert at front 2:insert to left of given data3:delete 4:display 5:exit2enter element before which node is to be inserted20enter the element30element not foundenter your choice1:insert at front 2:insert to left of given data3:delete 4:display 5:exit2enter element before which node is to be inserted10enter the element20list20

50

Page 51: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.10enter your choice1:insert at front 2:insert to left of given data3:delete 4:display 5:exit4list2010enter your choice1:insert at front 2:insert to left of given data3:delete 4:display 5:exit3enter element to be deleted10list20enter your choice1:insert at front 2:insert to left of given data3:delete 4:display 5:exit3enter element to be deleted10element not foundenter your choice1:insert at front 2:insert to left of given data3:delete 4:display 5:exit5*/

51

Page 52: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem./* 13. Write a C Program a. To construct a binary search tree of integers. b. To traverse the tree using all the methods i.e,inorder, preorder and postorder. c. To display the elements in the tree.PROGRAM*/#include<stdio.h>#include<conio.h>#include<alloc.h>#include<stdlib.h>struct node{int info;struct node *left,*right;};typedef struct node NODE;NODE *root=NULL;void create(){ NODE *tmp,*curptr,*prev; int item; tmp=(NODE*)malloc(sizeof(NODE)); if(tmp==NULL) { printf("\nNo memory..\n"); return; } printf("\nEnter the value\n"); scanf("%d",&item); tmp->info=item; tmp->left=tmp->right=NULL; if(root==NULL) root=tmp; else { curptr=root; prev=NULL; while(curptr!=NULL) { if(curptr->info==item) {

52

Page 53: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.printf("\nDuplicate elements not allowed\n");free(tmp);return;}if(curptr->info<item){prev=curptr; curptr=curptr->right;}else{prev=curptr;curptr=curptr->left;} } if(prev->info<item) prev->right=tmp; else prev->left=tmp; }printf("%d inserted",item);}void inord(NODE *tmp){if(tmp==NULL) return;inord(tmp->left);printf("%d\n",tmp->info);inord(tmp->right);}void preord(NODE *tmp){if(tmp==NULL) return;printf("%d\n",tmp->info);preord(tmp->left);preord(tmp->right);}void postord(NODE *tmp){if(tmp==NULL) return;postord(tmp->left);

53

Page 54: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.postord(tmp->right);printf("%d\n",tmp->info);}void disp(NODE *tmp,int lev){ int i; if (tmp!=NULL) { disp(tmp->right,lev+1); for(i=0;i<lev;i++) printf("");printf("%d\n",tmp->info);disp(tmp->left,lev+1);}}void main(){int ch;while(1){clrscr();printf("\nMENU\n\n1.Insert\n2.Traversepreorder\n3.Traverseinorder\n5.Display\n6.Exit\n");scanf("%d",&ch);switch(ch){case 1: create();getch(); break;case 2: printf("\nPreorder\n");preord(root);getch();break;case 3: printf("\nPostorder\n");postord(root);getch();break;case 4: printf("\nInorder\n");inord(root);getch();break;case 5: disp(root,0);getch();break;case 6: exit(0);default: printf("\nIncorrect choice..\n");}}}/*OUTPUTMENU1.Insert2.Traverse preorder3.Traverse postorderpostorder\n4.Traverse

54

Page 55: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.4.Traverse inorder5.Display6.Exit1Enter the value2020 inserted1.Insert2.Traverse preorder3.Traverse postorder4.Traverse inorder5.Display6.Exit1Enter the value20Duplicate elements not allowed1.Insert2.Traverse preorder3.Traverse postorder4.Traverse inorder5.Display6.Exit1Enter the value3030 inserted1.Insert2.Traverse preorder3.Traverse postorder4.Traverse inorder5.Display6.Exit1Enter the value1010 inserted

55

Page 56: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.1.Insert2.Traverse preorder3.Traverse postorder4.Traverse inorder5.Display6.Exit1Enter the value4040 inserted1.Insert2.Traverse preorder3.Traverse postorder4.Traverse inorder5.Display6.Exit5403020101.Insert2.Traverse preorder3.Traverse postorder4.Traverse inorder5.Display6.Exit2Preorder201030401.Insert2.Traverse preorder3.Traverse postorder4.Traverse inorder5.Display6.Exit

56

Page 57: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.3Postorder104030201.Insert2.Traverse preorder3.Traverse postorder4.Traverse inorder5.Display6.Exit4Inorder102030401.Insert2.Traverse preorder3.Traverse postorder4.Traverse inorder5.Display6.Exit6*/

57

Page 58: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem./* 14.Write recursive C Program for a. Searching an element on a given list of integers using the Binary Search method. b. Solving the Tower of Hanoi problemPROGRAM*/#include<stdio.h>#include<conio.h>#include<process.h>int search(int,int [],int,int);void towers(int,char,char,char);static int count=0;int n1;void main(){ int choice; int a[25],n,key,low,high,i,ans; clrscr(); printf("\n Enter the choice: \n1.binary search \n2.tower of hanoi\n"); scanf("%d",&choice); switch(choice) { case 1:printf("Enter the array size:\n"); scanf("%d",&n); printf("Enter the array elements:\n"); for(i=0;i<=(n-1);i++) { scanf("%d",&a[i]); } printf("Enter the key to be searched:\n"); scanf("%d",&key); ans=search(key,a,0,n-1); if(ans==1) { printf("Key found\n"); } else { printf("Key not found\n"); } break; case 2:printf("enter n:\n"); scanf("%d",&n1); towers(n1,'A','C','B'); break; default:printf("Invalid choice\n");

58

Page 59: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.break; }getch();}int search(int key,int a[],int low,int high){ int mid; if(low<=high) { mid=(low+high)/2; if(key==a[mid]) { return(1); } if(key<a[mid]) { return(search(key,a,low,mid-1)); } else { return(search(key,a,mid+1,high)); } } return(-1);}void towers(int n1,char source,char dest,char aux){ if(n1==1) { printf("step%d:move%d from %c to %c\n",++count,n1,source,dest); } else { towers(n1-1,source,aux,dest); printf("step%d:move%d from %c to %c\n",++count,n1,source,dest); towers(n1-1,aux,dest,source); }}/* OUTPUTEnter the choice:1.binary search 2.tower of hanoi1Enter the array size:41020

59

Page 60: DATA STRUCTURE Lab Manual for 3rd Sem

DATA STRUCTURE Lab Manual for 3rd Sem.3040Enter the key to be searched:25Key not foundEnter the choice:1.binary search 2.tower of hanoi1Enter the array size:410203040Enter the key to be searched:20key foundEnter the choice:1.binary search 2.tower of hanoi2enter n:4step1:move1 from A to Bstep2:move2 from A to Cstep3:move1 from B to Cstep4:move3 from A to Bstep5:move1 from C to Astep6:move2 from C to Bstep7:move1 from A to Bstep8:move4 from A to Cstep9:move1 from B to Cstep10:move2 from B to Astep11:move1 from C to Astep12:move3 from B to Cstep13:move1 from A to Bstep14:move2 from A to Cstep15:move1 from B to C*/

60