Transcript
7/31/2019 Stack,Queue,Linked List
1/33
Experiment: 14
Write a program for the Implementation of Stack operations
Algorithm:
Step1: start
Step2: enter your choice as x
Step3: if x is 1 ,2,3,4,5 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 continueStep11: if c= y or Y goto 2 else goto 12
Step12: stop
Algorithm for create:
Step1: startStep2: enter the size of array as n
Step3: print stack is created with size n
Step4: returnAlgorithm for delete:
Step1: start
Step2: if n != 0print 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) 0 and if top>0 goto 3 else goto 4Step3: if x = a[top] and top-
7/31/2019 Stack,Queue,Linked List
2/33
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
#includeint 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;
7/31/2019 Stack,Queue,Linked List
3/33
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
7/31/2019 Stack,Queue,Linked List
4/33
scanf("%d",&x);
stack[top]=x;
top++;
}
elseprintf("\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
7/31/2019 Stack,Queue,Linked List
5/33
7/31/2019 Stack,Queue,Linked List
6/33
7/31/2019 Stack,Queue,Linked List
7/33
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 : 33Input choice 'n' for break: n
Values of entered nodes are as follows : 11 22 33
Exercise:
Programs in Linked Lists
(a). Searching
(b). Sorting.
(c). Concatenation.
(d). Traversing(e). Merging two lists.
7/31/2019 Stack,Queue,Linked List
8/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]= pStep4: rlink[t]=p
To insert at the end of list
Step1: llink[p]=t
Step2: rlink[t]=p
To delete the first nodeStep1: start = rlink[start]
Step2: if start != NULL
Llink[start]= NULLTo delete the middle node of the list
Step1: if llink[p] != NULL
Step2: rlink[p] = null
Program:
Double Linked List */
#include#include
void create();
void display();
void insert();
void delete();struct double_list
{
struct double_list *lptr, *rptr;
int n;};
7/31/2019 Stack,Queue,Linked List
9/33
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);
7/31/2019 Stack,Queue,Linked List
10/33
7/31/2019 Stack,Queue,Linked List
11/33
void delete()
{
printf("\nEnter position you want to delete : ");
scanf("%d",&po);
q=start;if(pon;
q->rptr->lptr=NULL;
start=q->rptr;
free(0);
}
elseif(po==count+1)
{
for(I=1;Irptr;
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;
7/31/2019 Stack,Queue,Linked List
12/33
case 5: exit(0);
}
}while(ch
7/31/2019 Stack,Queue,Linked List
13/33
Null ->25->36->45->69->NULL
1. Create
2. Insert
3. Delete
4. Display5. 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.
7/31/2019 Stack,Queue,Linked List
14/33
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->infortree
Step9: if p->info = key then goto 10
Step10: found=1, break
Step11: if found = 0 then goto 12
Else goto 13Step12: print "key is not found"
Step13: print "key is found"
Step14: stop
Program:
#include
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)
7/31/2019 Stack,Queue,Linked List
15/33
7/31/2019 Stack,Queue,Linked List
16/33
{
t=*p;
if((*p)->ltree == NULL && (*p)->rtree == NULL)
*p=NULL;
elseif((*p)->ltree == NULL)(*p) = (*p)->rtree;
else if((*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);
7/31/2019 Stack,Queue,Linked List
17/33
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- Delete3- Search
4- Exit
Enter your choice : 1
Enter the inserted element : 88
Menu1- Insert
2- Delete
3- Search4- Exit
Enter your choice : 3
Enter the key value to be searched : 33Output: The key value is not found
Menu
1- Insert
2- Delete
3- Search4- Exit
Enter your choice : 4
Exercise:
Programs on Trees
(1). Binary Tree creation
(2). Tree traversals
7/31/2019 Stack,Queue,Linked List
18/33
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 verticesit 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 doif visited[w] = false theni. visited[w] = true
ii. add queue[w]
step4: return
Program:
Program for B.F.S */
#includestruct 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;
7/31/2019 Stack,Queue,Linked List
19/33
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;Ic=ch;
ex->an =NULL;
te->nl = ex;ex->nl =NULL;
te=ex;
}te->nl=NULL;
te=st;
}for(I=1;Ic =ch;
ex->an=NULL;
te1->an=ex;
/*te2->st;*/for(j=1;jc)
{ex->nl=te2;
7/31/2019 Stack,Queue,Linked List
20/33
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;jc==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: YEnter the adjacent node: B
7/31/2019 Stack,Queue,Linked List
21/33
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 then6.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 expression6.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 eachoperator parenthesis is
encountered
7.2 Remove the left parenthesis (does not add the left parenthesistop)
step8: stop*/
Program:
#include
#include#include
char stack[30];
int top=-1;
main()
{char infix[20];
void push(char);
char pop();
void in_to_post(char a[]);clrscr();
7/31/2019 Stack,Queue,Linked List
22/33
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=preced(symbol))
{
temp=pop();
postfix[pos]=temp;pos++;
7/31/2019 Stack,Queue,Linked List
23/33
}
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
7/31/2019 Stack,Queue,Linked List
24/33
{
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/
7/31/2019 Stack,Queue,Linked List
25/33
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 stackStep5: 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 stackStep7: stop
Program:
#include
#include#include
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);
7/31/2019 Stack,Queue,Linked List
26/33
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;
7/31/2019 Stack,Queue,Linked List
27/33
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 : 4Result of postfix expression: 4
7/31/2019 Stack,Queue,Linked List
28/33
Experiment: 21
Write a program to implement Queue Operations
Algorithm:
Step1: start
Step2: do
Step3: print 1. Insert 2. Delete 0. ExitStep4: 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 10Step9: 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) 0 then goto 3 else goto 7
Step3: if (front 0) then got 3 else got 7Step3: if(front
7/31/2019 Stack,Queue,Linked List
29/33
7/31/2019 Stack,Queue,Linked List
30/33
}
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
7/31/2019 Stack,Queue,Linked List
31/33
}
else
printf("\n\n Queue over flow");
}
elseprintf("\n\n First Create Queue");
}
display()
{
int i;
if (size!=0)
{
if(front!=rear)
{
printf("\n\n\t");for(i=front;i
7/31/2019 Stack,Queue,Linked List
32/33
}
delete()
{
if (size!=0){
printf("\n\n Queue is deleted");
size=0;
rear=front=0;
}
else
printf("\n\n First Create Queue"):
}
Output :
1. Create2. Insert
3. Delete
4. Display
5. Delete
Enter your choice : 1Enter size of Queue : 3
1. Create
2. Insert3. Delete
4. Display
5. Delete is created with size 3Do you want to continue y
1. Create
2. Insert
3. Delete
4. Display
5. DeleteEnter your choice 2
Enter the inserted element 45
45 is inserted in to Queue
Do you want to continue y
1. Create2. Insert
3. Delete
4. Display
5. DeleteEnter your choice 4
7/31/2019 Stack,Queue,Linked List
33/33
45
^ ^
| |
| |
Front RearDo you want to continue n
top related