02 Stack
02 Stack
CS 214
Stack
By
Marwa M. A. Elfattah
?Stack – What
?Stack – What
• A stack is Linear - non-primitive data structure.
• Stack is Ordered List of Elements of Same Type.
• In Stack, all Operations are permitted at only one end called
Top.
• Add (20)
33- • Add( 5)
• Add(30)
00
5 • Delete
2 • Delete
0 • Add(0)
• Add( -3)
STACK APPLICATIONS
System Stack
M(){ O(){
... ...
N(); i=2
... Q(i);
O(); ...
... }
... Q(int i){
...
Q(1) ...
...
Q(2) }
If (i>1)
N
)(O N(){
Q(i-1);
...
(M ...
}
) }
Stack
?
Implementation User View
Operations Performed On Stack
• Create the stack, leaving it empty.
item = getchar();
while (!StackFull(stack)&& item!= '\n'){
//Push each item onto the stack
Push(item, &stack);
item = getchar();
}
while (!StackEmpty(stack)){
//Pop an item from the stack
Pop(&item, &stack);
putchar(item);
Exercise
• As a user for the stack ADT, write the StackTop
function which return the top element of the stack
and left the stack unchanged
Return (s->entry[s->top])
Thank you