Data StructuresData Structures
The Stack: DefinitionThe Stack: Definition
stack is an ordered collection of items intowhich new items may be inserted and fromwhich items may be deleted  calledthe TOP of the stackstack is an ordered collection of items intowhich new items may be inserted and fromwhich items may be deleted at one end calledthe TOP of the stack
What is StackWhat is Stack
common data structure incomputing.common data structure incomputing.
Data items are "popped" and"pushed" (retrieved and stored)from the top of the stack.Data items are "popped" and"pushed" (retrieved and stored)from the top of the stack.
Stacks normally have maximumsize. It is an error to push itemsonto full stack, or pop items offan empty stack.Stacks normally have maximumsize. It is an error to push itemsonto full stack, or pop items offan empty stack.
LIFO: Last In First OutLIFO: Last In First Out
stack
Stack featuresStack features
ORDERINGmaintains order when elements added(new elements are added to the end by default)ORDERINGmaintains order when elements added(new elements are added to the end by default)
DUPLICATESyes (allowed)DUPLICATESyes (allowed)
OPERATIONS:OPERATIONS:
add element to end of list ('push')add element to end of list ('push')
remove element from end of list ('pop')remove element from end of list ('pop')
isEmpty()isEmpty()
isFull()isFull()
Top()Top()
Stacks in computer scienceStacks in computer science
the stack is one of the most important datastructures in all of computer sciencethe stack is one of the most important datastructures in all of computer science
compilers use stacks to evaluate expressionscompilers use stacks to evaluate expressions
stacks are great for reversing things, matching uprelated pairs of things, and backtrackingalgorithmsstacks are great for reversing things, matching uprelated pairs of things, and backtrackingalgorithms
stack programming problems:stack programming problems:
reverse letters in string, reverse words in line, orreverse list of numbersreverse letters in string, reverse words in line, orreverse list of numbers
examine file to see if its braces { } and otheroperators matchexamine file to see if its braces { } and otheroperators match
convert infix expressions to postfix or prefixconvert infix expressions to postfix or prefix
Another implementation of StackAnother implementation of Stack
Stacks can be declared by using structurescontaining two objectsStacks can be declared by using structurescontaining two objects
#define STACKSIZE 100#define STACKSIZE 100
struct stack {struct stack {
int top;int top;
int items[STACKSIZE];int items[STACKSIZE];
};};
struct stack s;struct stack s;
empty()empty()
If (s.top==-1)If (s.top==-1)
Stack is emptyStack is empty
ElseElse
stack is not emptystack is not empty
int empty(struct stack *ps)int empty(struct stack *ps)
{{
if (ps->top == -1)if (ps->top == -1)
return(TRUE);return(TRUE);
elseelse
return(FALSE);return(FALSE);
}}
    if (empty(&s))    if (empty(&s))
     stack is empty     stack is empty
elseelse
       stack is not empty       stack is not empty
Push()Push()
void push(struct stack *ps, int x)void push(struct stack *ps, int x)
    { ps->items[++(ps->top)] x;    { ps->items[++(ps->top)] x;
}}
void push(struct stack *ps, int x)void push(struct stack *ps, int x)
    {if (ps->top==STACKSIZE-1)    {if (ps->top==STACKSIZE-1)
{{
cout<<stack overflow;cout<<stack overflow;
exit();exit();
}}
 ps->items[++(ps->top)] x; ps->items[++(ps->top)] x;
return;return;
}}
POP( )POP( )
int pop(struct stack *ps)int pop(struct stack *ps)
{{
if (empty(ps))if (empty(ps))
{{
cout<<stack underflow;cout<<stack underflow;
exit(1);exit(1);
} return (ps->items[ps->top--]);} return (ps->items[ps->top--]);
}}
To use the pop function, the programmer can declare int xand writeTo use the pop function, the programmer can declare int xand write
x=pop(&s);x=pop(&s);