0% found this document useful (0 votes)
2 views

02 Stack

Uploaded by

madm85369
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

02 Stack

Uploaded by

madm85369
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Data Structures

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.

So, the stack is called Last-in-First-out (LIFO)


?Stack – What

• 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.

• Determine whether the stack is empty or not.

• Determine whether the stack is full or not.

• Push a new entry onto the top of the stack

• Pop the entry off the top of the stack.


Stack Contiguous Implementation
• Each stack item is adjacent in memory to the next
stack item, and so stack items are kept in an array.
• The top position is kept in an integer field.
• The top and the data array are grouped in a struct.
#define MAX 10
typedef char EntryType;
typedef struct{
int top;
char 10
EntryType entry[ MAX ];
} StackType;
Stack Contiguous Implementation
• Initialization:
Pre: None.
Post: The stack is initialized to be empty.
void CreateStack(StackType *s){
s->top = -1;
}
Stack Contiguous Implementation
Stack empty operation:
Pre: The stack is initialized.
Post: If the stack is empty (1) is returned. Otherwise (0)
is returned.
int StackEmpty(StackType s){
return (s.top==-1);
}
Stack Contiguous Implementation
Stack full operation:
Pre: The stack is initialized.
Post: If the stack is full (1) is returned. Otherwise (0) is
returned.
int StackFull(StackType s){
return (s.top==MAX-1);
}
Stack Contiguous Implementation
• Push operation:
Pre: The stack is initialized and is not full.
Post: Item is added to the top of the stack.
void Push( EntryType item, StackType *s ){
s->entry[++s->top] = item;
}
S->top++;
S->entry[s->top]=item;
Stack Contiguous Implementation
• Push operation with another specification:
Pre: The stack is initialized.
Post: If the stack is not full, item is added to the top of the
stack. Otherwise, an error message is displayed and the
stack is left unchanged
void Push( EntryType item, StackType *s ){
if (s->top == MAX-1)
printf(“Error: Stack Overflow”)
else
s->entry[++s->top] = item;
}
NOW: Which specification is better ???
Stack Contiguous Implementation
• Pop operation:
Pre: The stack is initialized and is not empty.
Post: The top element of the stack is removed from it and is
assigned to item.
void Pop( EntryType *item, StackType*s ){
*item = s->entry[s->top--];
}
Stack Contiguous Implementation
• Pop operation with another specification:
Pre: The stack is initialized.
Post: If the stack is not empty The top element of the stack is
removed from it and is assigned to item. Otherwise, an error
message is displayed and the stack is left unchanged
void Pop(Entry Type*item, Stack type*s){
if (s->top == -1)
printf(“Error: Stack underflow”)
else
*item = s->entry[s->top--];
}
Using Of The Stack
Exercise
Assume that we need to read a line of text and
write it back in a reverse order.
StackType stack;
//Initialize the stack to be empty
CreateStack(&stack);

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

EntryType StackTop(StackType * s){


EntryType item;
pop(&item, s);
push(item, s);
return (item);
}
Exercise
• Rewrite the previouse function as a part of stack
ADT

EntryType StackTop(StackType * s){


EntryType item;
item = s->entry[s->top];
return (item);
}

Return (s->entry[s->top])
Thank you

You might also like