CS DataStructure-Lecture 1-Stack Based Array
CS DataStructure-Lecture 1-Stack Based Array
S
Push box Q onto stack: Q Push box S onto stack: Q
D Q
Q Q
MAXSTACK-1 CreateStack(s);
top -2580 0
entry }
s
When we return:
MAXSTACK-1
top -2580 0
entry
© Waleed A. Yousef 2008 s 9
Implementation level But this way User Level
(what really happens) (interface)
void CreateStack(Stack *ps){ void main(){
ps->top=0;
} Stack s;
CreateStack(&s);
CreateStack
&s }
ps
When we return:
The execution time does not
depend on n; therefore the
MAXSTACK-1 complexity is: O(1)
top MAXSTACK 0 e
It could be: StackFull(s) but this
entry wastes memory and time of copying.
© Waleed A. Yousef 2008 s 13
Implementation level User Level
(what really happens) (interface)
void Pop(StackEntry *pe, Stack *ps){
ps->top--;
*pe=ps->entry[--ps->top];
*pe=ps->entry[ps->top];
}
void main(){
Pop StackEntry e;
Stack s;
&e &s
pe ps CreateStack(&s);
Pop(&e, &s);
MAXSTACK-1
}
### top 1
0 0 ###
e entry
top 0 0 e
It could be: StackEmpty(s) but
entry this wastes memory and time of
copying.
© Waleed A. Yousef 2008 s 16
Implementation level User Level
(what really happens) (interface)
//Same preconditions of Pop.
void StackTop(StackEntry *pe, Stack *ps){
*pe=ps->entry[ps->top-1];
} void main(){
StackEntry e;
StackTop Stack s;
&e &s CreateStack(&s);
pe ps
StackTop(&e, &s);
}
MAXSTACK-1
It could be: StackTop(&e,
### top 1 0 ### s)
e entry but this wastes memory and
© Waleed A. Yousef 2008
s time of copying 17
Implementation level User Level
(what really happens) (interface)
/*Pre: Stack is initialized.
Post: returns how many elements exist.
int StackSize(Stack *ps){
return ps->top; void main(){
} StackEntry e;
StackSize Stack s;
int x;
&s
ps CreateStack(&s);
x=StackSize(&s);
MAXSTACK-1 }
It could be: StackSize(s)
top 1 0 ### but this wastes memory and
entry time of copying
© Waleed A. Yousef 2008
s 18
Implementation level User Level
(what really happens) (interface)
/*Pre: Stack is initialized.
Post: destroy all elements; stack looks initialized.
void ClearStack(Stack *ps){
ps->top=0; void main(){
} StackEntry e;
ClearStack Stack s;
&s CreateStack(&s);
ps
ClearStack(&s);
}
MAXSTACK-1 Same code as CreateStack;
why new function then?
top 1
0 0 ### 1- conceptually
entry 2- will see later
© Waleed A. Yousef 2008
s 19
Implementation level
//Precondition: The stack is Initialized
void TraverseStack(Stack *ps, void(*pf)(StackEntry)){
for(int i=ps->top; i>0; i--)
(*pf)(ps->entry[i-1]); User Level:
} how to process each element with a user-
pf
defined function
=
&Display
void Display(StackEntry e){
printf(“e is: %d\n", e);
}
The code of
Display ps=&s void main(){
Stack s;
MAXSTACK-1
CreateStack(&s);
.
.
top 0 TraverseStack(&s, &Display);
entry }
s //&s only for efficiency as said before.
© Waleed A. Yousef 2008 20
Exercise: How to write the function StackTop in the user level? (e.g., if you do not
have the source code of the implementation)
User Level:
void StackTop(StackEntry *pe, Stack *ps){
Pop(pe, ps); /* Why interface, Pre, Post are crucial:
Push(*pe, ps); Pop takes a pointer to the element and a pointer to
} the stack.
###
e
MAXSTACK-1
top 1
0 0 ###
entry
void ReverseRead(void){
StackEntry item;//StackEntry should be defined as char
Stack stack;
CreateStack(&stack);//Initialize the stack to be empty
while (!StackEmpty(&stack)){
Pop(&item, &stack); //Pop an item from the stack.
putchar(item);
}
putchar('\n');
}
So, the main file will compile even if stack.cpp is not compiled
yet. (check the homework; this is detailed in the sheet)