You are on page 1of 60

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

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

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

DATA STRUCTURE Lab Manual for 3rd Sem. /* OUTPUT Menu 1.Add record 2.Search 3.Display 4.Exit Enter your choice.. 1 Enter the USN of the student.. 101 Enter the name of the student.. Abc Enter the marks in 3 subjects.. 24 23 25 Record added successfully.. Menu 1.Add record 2.Search 3.Display 4.Exit Enter your choice.. 1 Enter the USN of the student.. 102 Enter the name of the student.. Def Enter the marks in 3 subjects.. 21 22 23 Record added successfully.. Menu 1.Add record 2.Search 3.Display 4.Exit Enter your choice.. 1 4

DATA STRUCTURE Lab Manual for 3rd Sem. Enter the USN of the student.. 103 Enter the name of the student.. Ghi Enter the marks in 3 subjects.. 20 21 22 Record added successfully.. Menu 1.Add record 2.Search 3.Display 4.Exit Enter your choice.. 3 Display All USN 101 102 103 Name Abc Def Ghi Marks1 2423 2122 2021 Marks2 25 23 22 Marks3 Menu 1.Add record 2.Search 3.Display 4.Exit Enter your choice.. 2 Search Enter the USN to be searched.. 106 Record with given USN not found.. Menu 1.Add record 2.Search 3.Display 5

DATA STRUCTURE Lab Manual for 3rd Sem. 4.Exit Enter your choice.. 2 Search Enter the USN to be searched.. 102 USN: 102 Name: Def Marks: 21 22 23 Menu 1.Add record 2.Search 3.Display 4.Exit Enter your choice.. 4 */ 6

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

DATA STRUCTURE Lab Manual for 3rd Sem. /* OUTPUT Enter two strings.. Comp Science Copy Function a= Comp b= Science cpy1= Comp cpy2= Science Concatenation Function a= Comp Science b= Science cpy1= Comp cpy2= Science */ 8

DATA STRUCTURE Lab Manual for 3rd Sem. /* 3.Write a C Program, which accepts the Internet Protocol (IP) address in decimal dot format(ex.153.18.8.105) and converts it into 32-bit long integer (ex.2568095849) using strtok 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

DATA STRUCTURE Lab Manual for 3rd Sem. /* OUTPUT Enter the ip address in dot format 172.32.5.121 32 bit long integer is 2887779705 */ 10

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: display The program should print appropriate messages for stack overflow, stack underflow and stack empty. PROGRAM */ #include<stdio.h> #include<stdlib.h> #include<conio.h> #define STACK 5 int 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

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

DATA STRUCTURE Lab Manual for 3rd Sem. /* OUTPUT Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 3 Stack is empty Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 1 Enter the element to be inserted 11 11 Inserted Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 1 Enter the element to be inserted 22 22 Inserted Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 1 13

DATA STRUCTURE Lab Manual for 3rd Sem. Enter the element to be inserted 33 33 Inserted Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 1 Enter the element to be inserted 44 44 Inserted Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 1 Enter the element to be inserted 55 55 Inserted Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 1 Stack Overflow 14

DATA STRUCTURE Lab Manual for 3rd Sem. Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 3 Stack elements are: 11 22 33 44 55 Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 2 Element to be popped is 55 Element popped is 55 Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 2 Element to be popped is 44 Element popped is 44 Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 2 15

DATA STRUCTURE Lab Manual for 3rd Sem. Element to be popped is 33 Element popped is 33 Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 2 Element to be popped is 22 Element popped is 22 Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 2 Element to be popped is 11 Element popped is 11 Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 2 Stack Underflow Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 4 */ 16

DATA STRUCTURE Lab Manual for 3rd Sem. /* 5.Write a C Program to convert and print a given valid parenthesized infix arithmetic expression to postfix expression. The expression consists of single character operands and the binary operators +(plus), -(minus), *(multiply) and /(divide). PROGRAM */ #include<stdio.h> #include<conio.h> #include<string.h> #define MAX 5 int 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

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; else top--; } 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(); } /* OUTPUT Enter the infix statement. ((1+(2-3)*4)^5+6) The postfix expression for ((1+(2-3)*4)^5+6) is 123-4*+5^6+ */ 18

DATA STRUCTURE Lab Manual for 3rd Sem. /* 6.Write a C Program to evaluate a valid suffix/postfix expression using stack. Assume that the suffix/postfix expression is read as a single line consisting of non-negative single digit 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

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

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: Display PROGRAM */ #include<stdio.h> #include<conio.h> #include<process.h> #define size 5 int 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

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

DATA STRUCTURE Lab Manual for 3rd Sem. /* OUTPUT enter the choice 1:insert 2:delete 3:display 4:exit 1 enter item to be inserted 11 enter the choice 1:insert 2:delete 3:display 4:exit 1 enter item to be inserted 22 enter the choice 1:insert 2:delete 3:display 4:exit 3 22 11 enter the choice 1:insert 2:delete 3:display 4:exit 2 Item deleted is 22 enter the choice 1:insert 2:delete 3:display 4:exit 2 Item deleted is 11 2 queue empty enter the choice 1:insert 2:delete 3:display 4:exit 1 enter item to be inserted 44 enter the choice 1:insert 2:delete 3:display 4:exit 1 enter item to be inserted 55 enter the choice 1:insert 2:delete 3:display 4:exit 1 enter item to be inserted 66 enter the choice 1:insert 2:delete 3:display 4:exit 1 enter item to be inserted 23

DATA STRUCTURE Lab Manual for 3rd Sem. 77 enter the choice 1:insert 2:delete 3:display 4:exit 1 enter item to be inserted 88 enter the choice 1:insert 2:delete 3:display 4:exit 1 enter item to be inserted 99 queue full enter the choice 1:insert 2:delete 3:display 4:exit 4 */ 24

DATA STRUCTURE Lab Manual for 3rd Sem. /* 8.Write a C Program to simulate the working of a circular queue of integers using an array. Provide the following operations: a.Insert b.Delete c.Display. PROGRAM */ #include<stdio.h> #include<conio.h> #include<process.h> #define size 5 int 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

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

DATA STRUCTURE Lab Manual for 3rd Sem. OUTPUT enter choice 1.Insert 2.Delete 3.Display 4.Exit 1 Enter the element10 enter choice 1.Insert 2.Delete 3.Display 4.Exit 1 Enter the element20 enter choice 1.Insert 2.Delete 3.Display 4.Exit 31020 enter choice 1.Insert 2.Delete 3.Display 4.Exit 2 The element deleted is 10 enter choice 1.Insert 2.Delete 3.Display 4.Exit 2 The element deleted is 20 enter choice 1.Insert 2.Delete 3.Display 4.Exit 2 Queue empty enter choice 1.Insert 2.Delete 3.Display 4.Exit 1 Enter the element 11 enter choice 1.Insert 2.Delete 3.Display 4.Exit 1 Enter the element 22 enter choice 1.Insert 2.Delete 3.Display 4.Exit 1 Enter the element 33 enter choice 1.Insert 2.Delete 3.Display 4.Exit 1 Enter the element 44 enter choice 1.Insert 2.Delete 3.Display 4.Exit 1 Enter the element 55 enter choice 1.Insert 2.Delete 3.Display 4.Exit 1 Enter the element 66 Queue full enter choice 1.Insert 2.Delete 3.Display 4.Exit 2 The element deleted is 11 27

DATA STRUCTURE Lab Manual for 3rd Sem. enter choice 1.Insert 2.Delete 3.Display 4.Exit 1 Enter the element 66 enter choice 1.Insert 2.Delete 3.Display 4.Exit 3 66 22 33 44 55 enter choice 1.Insert 2.Delete 3.Display 4.Exit 4 */ 28

DATA STRUCTURE Lab Manual for 3rd Sem. /* 9.Write a C Program using dynamic variables and pointers, to construct a singly linked list 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 list b. 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

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

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

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

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; } } /* OUTPUT 1:ins_front 2:ins_end 3:ins_pos enter ur choice 1 enter student id,name,sem 11 DUVAN 3 do u want to continue y 1:ins_front 2:ins_end 3:ins_pos enter ur choice 1 enter student id,name,sem 22 PRAJWAL 4 do u want to continue y 1:ins_front 2:ins_end 3:ins_pos enter ur choice 6 STUDENTS INFORMATION -------------------ID NAME SEM --------------------22PRAJWAL 4 11DUVAN 3 do u want to continue y 1:ins_front 2:ins_end 3:ins_pos enter ur choice 3 enter student id,name,sem 33 SACHIN 3 enter the position to be insert 2 do u want to continue y 4:delete 5:search 6:display 4:delete 5:search 6:display 4:delete 5:search 6:display 4:delete 5:search 6:display 33

DATA STRUCTURE Lab Manual for 3rd Sem. 1:ins_front 2:ins_end 3:ins_pos 4:delete 5:search 6:display enter ur choice 6 STUDENTS INFORMATION ID NAME SEM 22PRAJWAL 4 33SACHIN 3 11DUVAN 3 do u want to continue n */ 34

DATA STRUCTURE Lab Manual for 3rd Sem. /* 10.Write a C Program using dynamic variables and pointers to construct a stack of integers using singly linked list and to perform the following operations: a: Push b: Pop c: Display The 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

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

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(); } } } /* OUTPUT Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 3 Stack is empty Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 1 Enter the element to be inserted 11 11 Inserted Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 1 37

DATA STRUCTURE Lab Manual for 3rd Sem. Enter the element to be inserted 22 22 Inserted Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 1 Enter the element to be inserted 33 33 Inserted Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 1 Enter the element to be inserted 44 44 Inserted Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 1 Enter the element to be inserted 55 55 Inserted Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 3 38

DATA STRUCTURE Lab Manual for 3rd Sem. Stack elements are: 11 22 33 44 55 Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 2 Element to be popped is 55 Element popped is 55 Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 2 Element to be popped is 44 Element popped is 44 Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 2 Element to be popped is 33 Element popped is 33 Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 2 39

DATA STRUCTURE Lab Manual for 3rd Sem. Element to be popped is 22 Element popped is 22 Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 2 Element to be popped is 11 Element popped is 11 Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 2 Stack Underflow Menu 1.Push 2.pop 3.display 4.exit Enter your choice.. 4 */ 40

DATA STRUCTURE Lab Manual for 3rd Sem. /* 11.Write a C Program using dynamic variables and pointers to construct a queue of integers using linked list and to perform the following operations: a: Insert b: Delete c: Display The 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

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

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(); } } } /* OUTPUT Menu 1.Insert 2.Delete 3.Display 4.exit Enter your choice.. 3 Queue is empty Menu 1.Insert 2.Delete 3.Display 4.exit Enter your choice.. 1 Enter the element to be inserted 11 11 Inserted Menu 1.Insert 2.Delete 3.Display 4.exit Enter your choice.. 1 43

DATA STRUCTURE Lab Manual for 3rd Sem. Enter the element to be inserted 22 22 Inserted Menu 1.Insert 2.Delete 3.Display 4.exit Enter your choice.. 1 Enter the element to be inserted 33 33 Inserted Menu 1.Insert 2.Delete 3.Display 4.exit Enter your choice.. 1 Enter the element to be inserted 44 44 Inserted Menu 1.Insert 2.Delete 3.Display 4.exit Enter your choice.. 1 Enter the element to be inserted 55 55 Inserted Menu 1.Insert 2.Delete 3.Display 4.exit Enter your choice.. 3 44

DATA STRUCTURE Lab Manual for 3rd Sem. Queue elements are: 11 22 33 44 55 Menu 1.Insert 2.Delete 3.Display 4.exit Enter your choice.. 2 Element to be Deleteped is 55 Element Deleteped is 55 Menu 1.Insert 2.Delete 3.Display 4.exit Enter your choice.. 2 Element to be Deleteped is 44 Element Deleteped is 44 Menu 1.Insert 2.Delete 3.Display 4.exit Enter your choice.. 2 Element to be Deleteped is 33 Element Deleteped is 33 Menu 1.Insert 2.Delete 3.Display 4.exit 45

DATA STRUCTURE Lab Manual for 3rd Sem. Enter your choice.. 2 Element to be Deleted is 22 Element Deleted is 22 Menu 1.Insert 2.Delete 3.Display 4.exit Enter your choice.. 2 Element to be Deleted is 11 Element Deleted is 11 Menu 1.Insert 2.Delete 3.Display 4.exit Enter your choice.. 2 Queue Underflow Menu 1.Insert 2.Delete 3.Display 4.exit Enter your choice.. 4 */ 46

DATA STRUCTURE Lab Manual for 3rd Sem. /* 12. Write a C Program to support the following operations on a doubly linked list where each 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

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

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

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(); } /* OUTPUT enter your choice 1:insert at front 2:insert to left of given data 3:delete 4:display 5:exit inserting at the front of the list enter the element 10 enter your choice 1:insert at front 2:insert to left of given data 3:delete 4:display 5:exit 2 enter element before which node is to be inserted 20 enter the element 30 element not found enter your choice 1:insert at front 2:insert to left of given data 3:delete 4:display 5:exit 2 enter element before which node is to be inserted 10 enter the element 20 list 20 50

DATA STRUCTURE Lab Manual for 3rd Sem. 10 enter your choice 1:insert at front 2:insert to left of given data 3:delete 4:display 5:exit 4 list 20 10 enter your choice 1:insert at front 2:insert to left of given data 3:delete 4:display 5:exit 3 enter element to be deleted 10 list 20 enter your choice 1:insert at front 2:insert to left of given data 3:delete 4:display 5:exit 3 enter element to be deleted 10 element not found enter your choice 1:insert at front 2:insert to left of given data 3:delete 4:display 5:exit 5 */ 51

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

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

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.Traverse inorder\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"); } } } /* OUTPUT MENU 1.Insert 2.Traverse preorder 3.Traverse postorder postorder\n4.Traverse 54

DATA STRUCTURE Lab Manual for 3rd Sem. 4.Traverse inorder 5.Display 6.Exit 1 Enter the value 20 20 inserted 1.Insert 2.Traverse preorder 3.Traverse postorder 4.Traverse inorder 5.Display 6.Exit 1 Enter the value 20 Duplicate elements not allowed 1.Insert 2.Traverse preorder 3.Traverse postorder 4.Traverse inorder 5.Display 6.Exit 1 Enter the value 30 30 inserted 1.Insert 2.Traverse preorder 3.Traverse postorder 4.Traverse inorder 5.Display 6.Exit 1 Enter the value 10 10 inserted 55

DATA STRUCTURE Lab Manual for 3rd Sem. 1.Insert 2.Traverse preorder 3.Traverse postorder 4.Traverse inorder 5.Display 6.Exit 1 Enter the value 40 40 inserted 1.Insert 2.Traverse preorder 3.Traverse postorder 4.Traverse inorder 5.Display 6.Exit 5 40 30 20 10 1.Insert 2.Traverse preorder 3.Traverse postorder 4.Traverse inorder 5.Display 6.Exit 2 Preorder 20 10 30 40 1.Insert 2.Traverse preorder 3.Traverse postorder 4.Traverse inorder 5.Display 6.Exit 56

DATA STRUCTURE Lab Manual for 3rd Sem. 3 Postorder 10 40 30 20 1.Insert 2.Traverse preorder 3.Traverse postorder 4.Traverse inorder 5.Display 6.Exit 4 Inorder 10 20 30 40 1.Insert 2.Traverse preorder 3.Traverse postorder 4.Traverse inorder 5.Display 6.Exit 6 */ 57

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 problem PROGRAM*/ #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

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); } } /* OUTPUT Enter the choice:1.binary search 2.tower of hanoi 1 Enter the array size: 4 10 20 59

DATA STRUCTURE Lab Manual for 3rd Sem. 30 40 Enter the key to be searched: 25 Key not found Enter the choice:1.binary search 2.tower of hanoi 1 Enter the array size: 4 10 20 30 40 Enter the key to be searched: 20 key found Enter the choice:1.binary search 2.tower of hanoi 2 enter n: 4 step1:move1 from A to B step2:move2 from A to C step3:move1 from B to C step4:move3 from A to B step5:move1 from C to A step6:move2 from C to B step7:move1 from A to B step8:move4 from A to C step9:move1 from B to C step10:move2 from B to A step11:move1 from C to A step12:move3 from B to C step13:move1 from A to B step14:move2 from A to C step15:move1 from B to C */ 60

You might also like