01. Stack
-
Upload
akif-vohra -
Category
Documents
-
view
213 -
download
0
Transcript of 01. Stack
-
8/8/2019 01. Stack
1/21
(C) GOYANI MAHESH(C) GOYANI MAHESH 11
DATASTRUCTURE
SMAHESH GOYANI
MAHATMA GANDHI INSTITUE OF TECHNICAL EDUCATION & RESEARCH [email protected]
-
8/8/2019 01. Stack
2/21
-
8/8/2019 01. Stack
3/21
(C) GOYANI MAHESH(C) GOYANI MAHESH 33
Stacks in real life: stack of books, stack ofStacks in real life: stack of books, stack of
platesplates
Non Primitive, Linear Data Structure.
Operation can be performed at one end only
Last In Firs Out (LIFO | FILO | FCLS | LCFS)
Most Frequently accessible element is TOP
Insertion -> PUSH : TOP++
Deletion -> POP : TOP--
Stack Full + Push() = Overflow
Stack Empty + Pop() = Underflow
TERMINOLOGY
-
8/8/2019 01. Stack
4/21
(C) GOYANI MAHESH(C) GOYANI MAHESH 44
20
33
20
47
33
20
3320
77
3320
3320 20
Stack EmptyStack Empty
TOP=-1TOP=-1
TOP=0TOP=0
TOP=TOP=
11
TOP=2TOP=2
TOP=1TOP=1
TOP=2TOP=2
TOP=1TOP=1
TOP=TOP=
00
PushPush(20)(20)
PushPush(33)(33)
PushPush(47)(47)
Pop ()Pop () Push (77)Push (77) PopPop()()
Pop ()Pop ()
-
8/8/2019 01. Stack
5/21
(C) GOYANI MAHESH(C) GOYANI MAHESH 55
STACK OPERATION
int pop()
{
return STACK[TOP--];
}
void push(int x){
STACK[++TOP] = x;}
int top(){
return STACK[TOP];}
int IsFull(){
return ( TOP == STACK_SIZE-1);}
int IsEmpty()
{ return ( TOP == -1 );}
-
8/8/2019 01. Stack
6/21
(C) GOYANI MAHESH(C) GOYANI MAHESH 66
STACK implementation can be done in TWO ways;
(I) Static Implementation (Using Array)
(II) Dynamic Implementation (Using Pointers / LinkedList)
IMPLEMENTATION
-
8/8/2019 01. Stack
7/21
(C) GOYANI MAHESH(C) GOYANI MAHESH 77
TOP
2
5
7
1
2 5 7 1
0 1 32 4
TOP = 3
Using ARRAY
-
8/8/2019 01. Stack
8/21
(C) GOYANI MAHESH(C) GOYANI MAHESH 88
PUSH (STACK, TOP,DATA)
1. [Check for Stack Overflow]
If TOP == STACK_SIZE - 1,then
(a). Stack OverFlow
(b). Return.
2. [Increment TOP]
TOP = TOP + 1
3. [Insert Item On to Stack]
STACK[TOP] = DATA
4. Exit
ALGORITHM
-
8/8/2019 01. Stack
9/21
(C) GOYANI MAHESH(C) GOYANI MAHESH 99
POP(STACK, TOP,DATA)
1. [Check for Stack Underflow]If TOP < 0, then
(a). StackUnderflow
(b). Return.
2. [Read The Top Most Element]
DATA = STACK[TOP]
3. [Decrement TOP]TOP = TOP - 1
4. Exit
ALGORITHM
-
8/8/2019 01. Stack
10/21
(C) GOYANI MAHESH(C) GOYANI MAHESH 1010
PEEP(STACK, TOP, I,DATA)
1. [Check for Stack Underflow]
If TOP - I < 0, then
(a). StackUnderflow
(b). Return.
2. [Read Ith Element]
DATA = STACK[I]
3. Exit
ALGORITHM
-
8/8/2019 01. Stack
11/21
-
8/8/2019 01. Stack
12/21
(C) GOYANI MAHESH(C) GOYANI MAHESH 1212
TOP
2
5
7
1
1 7 5 2
HEAD
We can avoid the size limitation of a stack implemented withan array by using a linked list to hold the stack elements
No need for the current pointer, head is enough.
Using LINKED LIST
-
8/8/2019 01. Stack
13/21
(C) GOYANI MAHESH(C) GOYANI MAHESH 1313
top
2
5
71 7 5 2
head
pop(1)
OPERATION
top
2
5
7
9
7 5 2
head
push(9)
9
newNode
-
8/8/2019 01. Stack
14/21
(C) GOYANI MAHESH(C) GOYANI MAHESH 1414
Allocating and deallocating memory for list nodes doestake more time than preallocated array.
List uses only as much memory as required by the nodes;array requires allocation ahead of time.
List pointers (head, next) require extra memory.
Array has an upper limit, List is limited by dynamicmemory allocation.
ARRAY v/s LINKED LIST
-
8/8/2019 01. Stack
15/21
(C) GOYANI MAHESH(C) GOYANI MAHESH 1515
START
Initialization
Decision
FinalComputation
PartialComputation
UPDATE
RecursiveCall
Return
STOP
Prologue
Body
Epilogue
Recursion
Infix to Postfix
Tower of Hanoi
APPLICATION
int fact (int no)
{
int f=1;
if(no==1)
return no;
else{
f = no * fact (no - 1);
}
}
-
8/8/2019 01. Stack
16/21
(C) GOYANI MAHESH(C) GOYANI MAHESH 1616
Recursion Iteration It is an process of executing astatement of set of statementsrepeatedly, until some specified
condition is met.
Recursion is a technique todefine anything in terms of it self.
Clear cut four steps. Initialization,Condition, Execution & Updating
There must be exclusive ifstatement for stopping condition
Any recursive problem can besolved iteratively
Not all problem have recursivesolution
More efficient in terms of memoryutilization and execution speed
Worse option to go for simpleproblems, or problems not recursivein nature.
-
8/8/2019 01. Stack
17/21
-
8/8/2019 01. Stack
18/21
(C) GOYANI MAHESH(C) GOYANI MAHESH 1818
INFIX TO POSTFIX
1. Push ( on toTEMP_STACK, and add ) to the end ofP.
2. Scan P from left to right and repeat step 3 to 6 for each element
of P untilTEMP_STACKthe stack is empty.
3. If an operand is encountered, add it to OP_STACK
4. If left parenthesis encountered than add it toTEMP_STACK.
5. If an operator is encountered, then:
(a).Repeatedly pop fromTEMP_STACKand add each operator toOP_STACKwhich has same or higher precedence then
(b). AddtoTEMP_STACK
6. If right parenthesis encountered, then:
(a). Repeatedly pop fromTEMP_STACKand add to OP_STACKuntila left parenthesis encountered.
(b). Remove the left parenthesis (Dont add it to OP_STACK)
7. Exit.
E l ti
-
8/8/2019 01. Stack
19/21
(C) GOYANI MAHESH(C) GOYANI MAHESH 1919
CharacterScanned
TEMP_STACK Postfix_STACK
A ( A
+ (+ A
( (+( A
B (+( A B
/ (+( / A B
C (+( / A B C- (+( - A B C /
( (+( - ( A B C /
D (+( - ( A B C / D
P = A +( B / C - ( D * E ^ F ) + G )* H
Evaluation
E l ti
-
8/8/2019 01. Stack
20/21
(C) GOYANI MAHESH(C) GOYANI MAHESH 2020
* (+( - (* A B C / D
E (+( - (* A B C / D E
^ (+( - (* A B C / D E
F (+( - (* A B C / D E F
) (+( - A B C / D E F ^ *
+ (+(+ A B C / D E F * -
G (+(+ A B C / D E F * - G
) (+ A B C / D E F * - G +
* (+ * A B C / D E F * - G +H ( + * A B C / D E F * - G + H
) A B C / D E F * - G + H * +
P = A +( B / C - ( D * E ^ F ) + G )* H
Evaluation
-
8/8/2019 01. Stack
21/21
(C) GOYANI MAHESH(C) GOYANI MAHESH 2121
Tower of Hanoi
A
B
C
D
A
B
C
D
X Y Z
ZX Y
INPUT
OUTPUT