Stack, Queue, Linked List
Stack, Queue, Linked List
goto steps 4,5,6,7,8 respectively else goto 9 Step4:call the function create Step5:call the function push Step6:call the function pop Step7:call the function display Step8:call the function delete Step9:print invalid choice Step10: print the condition do you want to continue Step11: if c= y or Y goto 2 else goto 12 Step12: stop Algorithm for create: Step1:start Step2:enter the size of array as n Step3:print stack is created with size n Step4:return Algorithm for delete: Step1:start Step2:if n != 0 print stack is deleted top=0; n=0; else print create stack first step3: return Algorithm for push: Step1:start Step2:if n>0 and if (top+1) <=n goto 3 else goto 4 Step3:a[++top] = x, print x is inserted into stack Step4:print stack is Full Step5:else print create stack first Step6:return Algorithm for pop: Step1:start Step2:if n>0 and if top>0 goto 3 else goto 4 Step3:if x = a[top] and top-
print x is deleted from stack Step4:print stack is empty Step5:else print create stack first Step6:return Algorithm for display: Step1:start Step2:if n != 0 and if top>0 goto 3 else goto 4 Step3:for(I=top;I>0; I--) print a[I] Step4:print stack is empty Step5:else print create stack first Step6:return Program: #include<conio.h> #include<stdio.h> int stack[10],top=0,i,ch,size=0; main() { int j; char c; clrscr(); printf("\n\t\t ********Stack operations using arrays*****\n"); do { printf("\n\n 1. Create"); printf("\n 2. Push"); printf("\n 3. Pop"); printf("\n 4. Display"); printf("\n 5. Delete"); printf("\n Enter your choice :\t"); scanf("%d",&ch); switch(ch) { case 1: create(); break; case 2: push(); break; case 3: pop(); break; case 4: display(); break;
case 5: delete(); break; default : printf("Choose correct choice"); } printf("\n\n\n\n Do yant to continue : \t"); c=getche(); }while(c=='Y' || c=='y'); getch(); } create() { printf("\n\nEnter the size of the Stack :\t"); scanf("%d",&size); printf("\n Stack is Created with the size :%d",size); } pop() { int x; if (size!=0) { if(top>0) { x=stack[top-1]; top--; printf("\n\n %d is deleted form stack",x); } else printf("\n\n Stack is empty"); } else printf("\n\n First Create Stack"); } push() { int x; if (size!=0) { if(top<size) { printf("\n\n Enter the push element :\t");
scanf("%d",&x); stack[top]=x; top++; } else printf("\n\n Stack over flow"); } else printf("\n\n First Create Stack"); } display() { int i; if (size!=0) { if(top>0) { printf("\n\t\t -----"); printf("\n\t Top ----->|%3d |" ,stack[top-1]); for(i=top-2;i>=0;--i) { printf("\n\t\t -----"); printf("\n\t\t |%3d |" ,stack[i]); } printf("\n\t\t -----"); } else printf("\n\n Stack is empty"); } else printf("\n\n First Create Stack"); } delete() { if (size!=0) { printf("\n\n Stack is deleted"); size=0; top=0; } else
printf("\n\n First Create Stack"); } Output: 1. Create 2. Push 3. Pop 4. Display 5. Delete Enter your choice : 1 Enter size of stack : 3 Stack is created with size 3 Do you want to continue y 1. Create 2. Push 3. Pop 4. Display 5. Delete Enter your choice 2 Enter the inserted element 45 45 is inserted in to stack Do you want to continue y 1. Create 2. Push 3. Pop 4. Display 5. Delete Enter your choice 4 Top --> 45 Do you want to continue n
Experiment:15 Write a program To create a single linked list Algorithm: Step1:initially list empty Start=NULL Step2:allocate space to newly create node Node = create a node Step3:assign value to information part of node Info[node]=start Step4:[assign null to address part fro signaling end of list Next[node] = start Step5:assign address of first node to start variable Start=node Step6:return the created node Return(start) Program: #include<stdio.h> #include<malloc.h> struct link { int info; struct link *next; }; int I; struct link *start =NULL; void create_link_list(struct link*) void display (struct linke*); void create_link_list(struct link*) { char ch; printf("\nInput choice n for break"); I=0; Ch=getchar(); While(ch!='n') { printf("\nInput the node %d",I+1); scanf("%d",&node->info);
node->next=(struct link *)malloc(sizeof(struct link)); node = node->next; node ->next = start; I++; Printf("\nInput choice n for break"); Ch=getchar(); } } void display(struct link * node) { printf("\nValues of entered node are as follow : "); while(node) { printf("%d",node->info); node = node->next; } } void main() { struct link * node = (struct link*) malloc(sizeof(struct link)); start = node; create_link_list(start); display(start); } Output: Input choice 'n' for break: Input the node 1 : 11 Input choice 'n' for break: Input the node 2 : 22 Input choice 'n' for break: Input the node 3 : 33 Input choice 'n' for break: n Values of entered nodes are as follows : Exercise: Programs in Linked Lists (a). Searching (b). Sorting. (c). Concatenation. (d). Traversing (e). Merging two lists.
11
22
33
Experiment: 16 Write a program to perform double linked list operations Algorithm: To insert the node at beginning Step1:if start != NULL a). rlink[p] =start b). llink[start]=p Step2:start=p To insert the node at the middle Step1:llink[p] = t Step2:rlink[po]= rlink[t] Step3:llink[rlink[t]= p Step4:rlink[t]=p To insert at the end of list Step1:llink[p]=t Step2:rlink[t]=p To delete the first node Step1:start = rlink[start] Step2:if start != NULL Llink[start]= NULL To delete the middle node of the list Step1:if llink[p] != NULL Step2:rlink[p] = null Program: Double Linked List */ #include<stdio.h> #include<conio.h> void create(); void display(); void insert(); void delete(); struct double_list { struct double_list *lptr, *rptr; int n; };
typedef struct double_list node; node *p,*q,*r,*start=NULL; int ch, po, I, num, count=0; void create() { printf("\nEnter the list of no.s and stop with 100 : "); scanf("%d",&num); while(num!=0) { count++; p=(node *)malloc(sizeof(node)); p->n = num; if(start==NULL) { p->lptr=r->rptr=NULL; start=q=p; } else { p->rptr=NULL; p->lptr=q; q->rptr=p; q=p; } printf("\nenter 100 to stop : "); scanf("%d",&num); } printf("\nCount = %d",count); }
void display() { p=start; printf("NULL->"); while(p->rptr!=NULL) { printf("%d->",p->n); p=p->rptr; } printf("%d",p->n); printf("->NULL\n"); printf("\n Count = %d",count);
} void insert() { printf("\nEnter the no you want to insert : "); scanf("%d",&num); printf("\nEnter the position you want to insert : "); scanf("%d",&po); if(po<=count+I) { count++; p=(node *) malloc(sizeof(node *)); p->n = num; q=start; if(po==1) { p->lptr=NULL; p->rptr=q; q->lptr=p; start=p; } else if(po==count) { for(I=0;I<=po-2;I++); q=q->rptr; p->rptr=NULL; p->lptr=q; q->rptr=p; } else { for(I=0;I<=po-2;I++); q=q->rptr; r=q->rptr; q->rptr=p; p->rptr=r; p->lptr=q; r->lptr=p; } } printf("\nCount = %d",count); printf("\nInserted element is : %d",num); }
void delete() { printf("\nEnter position you want to delete : "); scanf("%d",&po); q=start; if(po<=count) { count--; if(po==1) { num=q->n; q->rptr->lptr=NULL; start=q->rptr; free(0); } else if(po==count+1) { for(I=1;I<=po-2;I++); q=q->rptr; num=q->rptr->n; p=q->rptr; q->rptr=NULL; free(0); } } printf("\nCount = %d",count"); printf("\n\t Deleted element is %d",num); } void main() { int ch; do { printf("\n1. Create\n2. Insert\n3. Delete\n4. Display\n5. Exit"); printf("\nEnter Choice : "); scanf("%d",&ch); switch(ch) { case 1: create(); break; case 2: insert(); break; case 3: delete(); break; case 4: display(); break;
case 5: exit(0); } }while(ch<=5); getch(); } Output: 1. Create 2. Insert 3. Delete 4. Display 5. Exit Enter Choice : 1 Enter the List of no.s and stop with 100 25 36 45 69 100 Count = 4 1. Create 2. Insert 3. Delete 4. Display 5. Exit Enter Choice : 2 Enter the no you want to insert : 3 Enter the position you want to insert : 3 Count = 5 The inserted element is : 3 1. Create 2. Insert 3. Delete 4. Display 5. Exit Enter Choice : 3 Enter position do you want to delete : 3 Count = 3 Deleted element is : 3 1. Create 2. Insert 3. Delete 4. Display 5. Exit Enter Choice : 4
Null ->25->36->45->69->NULL 1. Create 2. Insert 3. Delete 4. Display 5. Exit Enter Choice : 5 Exercise: Doubly linked list operations (a). Implement Circular linked list operations (b). Implement Doubly circular linked list operations (c). Implement Stack using single linked list (d). Implement Queue using single linked list (e). Implement Operations of polynomial.
Experiment:17 Write a program to Implement the binary search tree Algorithm: Step1:start Step2:enter the key value to be searched Step3:initialize p=root, found to zero Step4:while(p!=NULL) Step5:if p->info > key then goto 16 Else goto 7 Step6:p=p=>ltree Step7:if p->info<key then goto 8 Else goto 9 Step8:p=p->rtree Step9:if p->info = key then goto 10 Step10: found=1, break Step11: if found = 0 then goto 12 Else goto 13 Step12: print "key is not found" Step13: print "key is found" Step14: stop Program: #include<stdio.h> struct node { struct node * ltree; int info; struct node * rtree; } *root= NULL; void binary search(void) { struct node *p; int key,found; printf("\nEnter the key value to be searched : "); scanf("%d",&key); p=root; found=0; while(p!=NULL)
{ if(p->info>key) p=p->ltree; else if(p->info<key) p=p->rtree; else if(p->info = = key) { found = 1; break; } } if (found == 0) printf("Key is not found"); else printf("key is found"); } void insert(struct node *p, int x) { if((*p) = =NULL) { (*) = (struct node *) malloc(sizeof(struct node)); (*p)->info =x; (*p)->ltree = NULL; (*p) -> rtree = NULL; } else if((*p)->info>x) insert(&(*p)->ltree,x); elseif((*n)->info<x) insert((*p)->rtree,x); else printf("Duplicate element "); } void delete(struct node **p, int x) { struct node t,*a; if((*p) == NULL) printf("\nThe key is not in the list"); elseif((*p)->info->x); delete(&(*p)->ltree,x); elseif((*p)->info<x) delete(&(*p)->rtree,x); else
{ t=*p; if((*p)->ltree == NULL && (*p)->rtree == NULL) *p=NULL; elseif((*p)->ltree == NULL) (*p) = (*p)->rtree; else if((*p)-<rtree == NULL) (*p) = (*p)->ltree; else { a=&(*p)->rtree; while((*a)->ltree!= NULL) a=&(*p)->ltree; t=*a; (*p)->info=(*a)->info; (*a)=(*a)->rtree; } free(t); } } main() { int ch,key; do { clrscr(); printf("\Menu"); printf("\n1- Insert\n2 - Delete\n3- Search\n4 - Exit"); printf("\nEnter your choice : "); scanf("%d"m&ch); switch(ch) { case 1: { printf("\nEnter the value to be inserted : "); scanf("%d",&key); insert(&root,key); break; } case 2: { printf("Enter the value to be deleted : "); scanf("%d",&key);
delete(&root,key); break; } case 3: binary_search(); break; case 4: break; default: printf("\nInvalidc choice : "); } getch(); }while(ch!=4); getch(); } Output: Menu 1- Insert 2- Delete 3- Search 4- Exit Enter your choice : 1 Enter the inserted element : 88 Menu 1- Insert 2- Delete 3- Search 4- Exit Enter your choice : 3 Enter the key value to be searched : 33 Output: The key value is not found Menu 1- Insert 2- Delete 3- Search 4- Exit Enter your choice : 4 Exercise: Programs on Trees (1). Binary Tree creation (2). Tree traversals
a). Inorder b). Preorder c).Postorder Experiment: 18 Write a program to perform the graph traversal Algorithm: Add queue(v) - Add v at rear of the queue Delete queue(v) - delete the front element of the queue and assign it to v Empty queue(v) - specific whether the queue is empty or not Bfs(v) { let visited be an array of n elements n represents no of vertices it is global array all the elements are initialized to false let v, w are the indices of the array 'v' is index of the starting vertex } step1: visited[v] = true step2: add queue(v) step3: while not empty(q) do { a. del queue(q) b. for all adjacent w do if visited[w] = false then i. visited[w] = true ii. add queue[w] step4: return Program: Program for B.F.S */ #include<stdio.h> struct node { char c; struct node *nl, *an; }; typedef struct node node; main() { node *st,*te,*ex,*te1,*te2,*queue[10]; int I,j,n,v,count,front=0,rear=-1;
char ch,cl,visit[10]; clrscr(); printf("\nEnter the number of node : "); scanf("%d",&n); if(n>=1) { st=(node *) malloc(sizeof(node )); fflush(stdin); st->c=ch; st->an=NULL; te=st; for(I=2;I<=n;I++) { fflsuh(stdin); printf("\nEnter %d node : ",I); scanf("%d",&ch); fflsuh(stdin); ex=(node *) malloc(sizeof(node )); ex->c=ch; ex->an =NULL; te->nl = ex; ex->nl =NULL; te=ex; } te->nl=NULL; te=st; } for(I=1;I<=n;I++) { te1=te; printf("\nis tree an adjacent node"); fflush(stdin); scanf("%c",&ch); ex=(node *)malloc(sizeof(node)); ex->c =ch; ex->an=NULL; te1->an=ex; /*te2->st;*/ for(j=1;j<=n;j++) { if(ch==te2->c) { ex->nl=te2;
break; } else te2=te2->nl; } te1=ex; printf("Any more adjacent nodes "); scanf("%c",&cl); } te=te->nl; } te=st; printf("\nBreadth First Search "); rear++; queue[rear]=te; visit[1]= te->c; v=1; while(rear>=front) { count=0; te1=te1->an; for(j=1;j<=v;j++) { if(te1->c==visit[I]) count=1; } if(count==0) { rear++; queue[rear]=te1; v++; visist[v]=te1->c; }} getch(); } Output: Enter No. of Nodes 2 Enter the 1 Node A Enter the 2 Node: B Is there an adjacent node: Y Enter the adjacent node: B
Any more adjacent Node: n A B Experiment: 19 Write a program to Conversion of infix expression to postfix expression Algorithm: Step1:start Step2:push 'c' into stack and add')' at end of infix expression Step3:read infix expression from left to right end repeat step3 4 to 7 for each element of infix expression until the stack is empty Step4:if an operand is encountered add it top(p is post fix expression) Step5:if left parenthesis encountered push it into stack Step6:if an operator encountered then 6.1 Repeatedly pop the stack and add into p each operator (on the top of stack) which has same precedence or higher precedence than the operator which is in infix expression 6.2 Add the operator to stack step7: if a right parenthesis encountered then 7.1 Repeatedly pop from 0 the stack and add to p each operator parenthesis is encountered 7.2 Remove the left parenthesis (does not add the left parenthesis top) step8: stop*/ Program: #include<conio.h> #include<stdio.h> #include<string.h> char stack[30]; int top=-1; main() { char infix[20]; void push(char); char pop(); void in_to_post(char a[]); clrscr();
printf("\n\t ********\n"); printf("\n\n Enter the infix expression \t : "); gets(infix); printf("\n\n Given expression is\t : "); puts(infix); printf("\n\n Converted Postfix expression is\t : "); in_to_post(infix); getch(); } void in_to_post(char infix[]) { int length; static int index=0,pos=0; char symbol,temp; char postfix[40]; length=strlen(infix); push('#'); while(index<length) { symbol=infix[index]; switch(symbol) { case '(': push(symbol); break; case ')': temp=pop(); while(temp!='(') { postfix[pos]=temp; pos++; temp=pop(); } break; case '+': case '-': case '*': case '/': case '^': while(preced(stack[top])>=preced(symbol)) { temp=pop(); postfix[pos]=temp; pos++;
} push(symbol); break; default : postfix[pos++]=symbol; } index++; } while(top>0) { temp=pop(); postfix[pos++]=temp; } postfix[pos++]='\0'; puts(postfix); return; } char pop() { char item; if (top==-1) { printf("\n\n Stack is empty"); getch(); return(0); } else { item=stack[top]; top--; } return(item); } void push(char symb) { if(top>=39) { printf("\n\n Stack over flow"); getch(); return; } else
{ top++; stack[top]=symb; } } int preced(char ch) { if (ch==47) return(5); else if (ch==42) return(4); else if (ch==43) return(3); else return(2); } Output: ENTER AN INFIX EXPRESSION :(5+4)*3/9 EQUIVALENT POSTFIX IS : 54+3*9/
Experiment: 20 Write a program to Evaluation of postfix expression Algorithm: Step1:start Step2:add a right parenthesis ")" attend of postfix expression p Step3:scan p from l to r and repeat step3 to 4 for the each element of p the s symbal ")" is encountered Step4:if an operand is encountered push in to stack Step5:if an operator * encountered a. Remove the two top elements of stack where a is the b. Evaluate and b is next top element c. Place result of a*b back in to stack Step6:set a value equal to top element on stack Step7:stop Program: #include<conio.h> #include<stdio.h> #include<ctype.h> char stack[30]; int top=-1; main() { int i=0; char suffix[20]; float value[20],result; void push(char); float pop(); float eval(char a[],float d[]); clrscr(); printf("\n\t ********Evaluation of Postfix expression*****\n"); printf("\n\n Enter the postfix expression \t : "); gets(suffix);
while(suffix[i]!='\0') { if(isalpha(suffix[i])) { fflush(stdin); printf("\n Enter the value of %c\t", suffix[i]); scanf("%f",&value[i]); } i++; } printf("\n\n Given expression is\t : "); puts(suffix); result=eval(suffix,value); printf("\n\n result of Postfix expression %s = %f",suffix,result); getch(); } float eval(char suffix[],float data[]) { int i=0; float op1,op2,res; char ch; char postfix[40]; while(suffix[i]!='\0') { ch=suffix[i]; if(isalpha(suffix[i])) push(data[i]); else { op2=pop(); op1=pop(); switch(ch) { case '+': push(op1+op2); break; case '-': push(op1-op2); break; case '*': push(op1*op2); break; case '/': push(op1/op2); break;
case '^': push(pow(op1,op2)); break; } } i++; } res=pop(); return(res); } float pop() { float num; num=stack[top]; top=top-1; return(num); }
void push(char num) { top++; stack[top]=num; } Output: Enter the post-fix expression: ABC+-: Enter the value of A : 3 Enter the value of B : 5 Enter the value of C : 4 Result of postfix expression: 4
Experiment: 21 Write a program to implement Queue Operations Algorithm: Step1:start Step2:do Step3:print 1. Insert 2. Delete 0. Exit Step4:enter your choice Step5:if x = 1 call insert and goto 4 Step6:if x = 2 call delete and goto 4 Step7:if x =3 call show and goto 4 Step8:if x = 0 call goto 10 Step9:else print invalid choice and goto 4 Step10: stop Algorithm for insertion: Step1:start Step2:if n>0 goto 2 else goto 5 and if(rear+1) <=n then goto 3 else goto 6 Step3:read inserted element as k Step4:a[++rear]= k Step5:printf queue is full Step6:stop Algorithm for deletion: Step1:start Step2:if n>0 then goto 3 else goto 7 Step3:if (front <=rear) then goto 4 else goto 6 Step4:element = a[front], front=front+1 Step5:print x is deleted from queue Step6:print queue is empty Step7:stop Algorithm for display: Step1:start Step2:if(n>0) then got 3 else got 7 Step3:if(front <= rear) then goto 4 else goto 6
Step4:for(I=front; I<=rear;I++) then goto 5 else goto 6 Step5:print a[I+1]; Step6:print queue is empty Step7: stop
Program: #include<conio.h> #include<stdio.h> int queue[10],front,rear,i,ch,size=0; main() { int j; char c; clrscr(); printf("\n\t\t ********Queue operations using arrays*****\n"); do { printf("\n\n 1. Create"); printf("\n 2. Insertion"); printf("\n 3. Delection"); printf("\n 4. Display"); printf("\n 5. Delete"); printf("\n Enter your choice :\t"); scanf("%d",&ch); switch(ch) { case 1: create(); break; case 2: insert(); break; case 3: deletion(); break; case 4: display(); break; case 5: delete(); break; default : printf("Choose correct choice");
} printf("\n\n\n\n Do yant to continue : \t"); c=getche(); }while(c=='Y' || c=='y'); getch(); } create() { printf("\n\nEnter the size of the Queue :\t"); scanf("%d",&size); printf("\n Queue is Created with the size :%d",size); front=rear=0; } deletion() { int x; if (size!=0) { if(front!=rear) { x=queue[front]; front++; printf("\n\n %d is deleted form Queue",x); } else printf("\n\n Queue is empty"); } else printf("\n\n First Create Queue"); } insert() { int x; if (size!=0) { if(rear<size-1) { printf("\n\n Enter the inserted element :\t"); scanf("%d",&x); queue[rear]=x; rear++;
} else printf("\n\n Queue over flow"); } else printf("\n\n First Create Queue"); } display() { int i; if (size!=0) { if(front!=rear) { printf("\n\n\t"); for(i=front;i<rear;i++) printf(" -----"); printf("\n\t |"); for(i=front;i<rear;i++) printf(" %3d |" ,queue[i]); printf("\n\t"); for(i=front;i<rear;i++) printf(" -----"); printf("\n\t"); printf(" ^"); for(i=front+1;i<rear;i++) printf(" "); printf("^"); printf("\n\t |"); for(i=front+1;i<rear;i++) printf(" "); printf("|"); printf("\n\tFront"); for(i=front+1;i<rear;i++) printf(" "); printf("Rear"); } else printf("\n\n Queue is empty"); } else printf("\n\n First Create Queue");
} delete() { if (size!=0) { printf("\n\n Queue is deleted"); size=0; rear=front=0; } else printf("\n\n First Create Queue"): } Output : 1. Create 2. Insert 3. Delete 4. Display 5. Delete Enter your choice : 1 Enter size of Queue : 3 1. Create 2. Insert 3. Delete 4. Display 5. Delete is created with size 3 Do you want to continue y 1. Create 2. Insert 3. Delete 4. Display 5. Delete Enter your choice 2 Enter the inserted element 45 45 is inserted in to Queue Do you want to continue y 1. Create 2. Insert 3. Delete 4. Display 5. Delete Enter your choice 4