Stacks unit 4 stacks structure Page Nos


Download 420.74 Kb.
Pdf ko'rish
bet6/10
Sana15.11.2023
Hajmi420.74 Kb.
#1776973
1   2   3   4   5   6   7   8   9   10
Bog'liq
Unit-4

Stacks, Queues 
and Trees 
printf("\n\t\t\t1. To PUSH an element"); 
printf("\n\t\t\t2. To POP an element"); 
printf("\n\t\t\t3. To DISPLAY the elements of stack"); 
printf("\n\t\t\t4. 
Exit"); 
printf("\n\n\n\t\t\tEnter 
your 
choice:-"); 
scanf("%d",&ch); 
switch(ch) 

case 
1: 
printf("\n Enter an element which you want to push "); 
scanf("%d",&item); 
push(&p,item); 
break; 
case 
2: 
item=pop(&p); 
if(item!=NULL); 
printf("\n 
Detected 
item 
is%d",item); 
break; 
case 
3: 
printf(“\nThe 
elements 
of 
stack 
are”); 
display(p); 
break; 
case 
4: 
exit(0); 
} /*switch 
closed 
*/ 
printf("\n\n\t\t Do you want to run it again y/n”);
scanf(“%c”,&choice);
} while(choice==’y’); 

/*end of function main*/ 
Program 4.2: Implementation of Stack using Linked Lists 
Similarly, as we did in the implementation of stack using arrays, to know the working 
of this program, we executed it thrice and pushed 3 elements (10, 20, 30). Then we 
call the function display in the next run to see the elements in the stack.
 
Explanation 
 
Initially, we defined a structure called node. Each node contains two portions, data 
and a pointer that keeps the address of the next node in the list. The Push function will
insert a node at the front of the linked list, whereas pop function will delete the node 
from the front of the linked list. There is no need to declare the size of the stack in 
advance as we have done in the program where in we implemented the stack using 
arrays since we create nodes dynamically as well as delete them dynamically. The 
function display will print the elements of the stack. 


Download 420.74 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   10




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling