0% found this document useful (0 votes)
43 views30 pages

4505 Stack

The document discusses stacks and their implementation. It defines a stack as a linear data structure where items can only be added or removed from one end, called the top. The basic stack operations of push and pop are described, where push adds an item to the top and pop removes the top item. Stacks can be implemented using arrays or linked lists. Array implementation uses a fixed-size array and tracks the top index, while linked list implementation inserts and removes nodes from the front of the list.

Uploaded by

anejaajay1461
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views30 pages

4505 Stack

The document discusses stacks and their implementation. It defines a stack as a linear data structure where items can only be added or removed from one end, called the top. The basic stack operations of push and pop are described, where push adds an item to the top and pop removes the top item. Stacks can be implemented using arrays or linked lists. Array implementation uses a fixed-size array and tracks the top index, while linked list implementation inserts and removes nodes from the front of the list.

Uploaded by

anejaajay1461
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

Stack and Queue

Stack and Queue / Slide 2

Stack Overview
 Stack
 Basic operations of stack
 Pushing, popping etc.
 Implementations of stacks using
 array
 linked list
Stack and Queue / Slide 3

Stack Overview
 Stack : Linear data structure
It’s list of elements in which items may be added or
removed only at 1 end called TOP.

Ex: Stack of pennies.


Stack of dishes.
Stack of folded towels.
 Basic operations of stack
 Pushing, popping etc.
 Implementations of stacks using
 array
 linked list
Stack and Queue / Slide 4
Stack
 A stack is a list with the restriction.
 Sometimes, there are situations when 1 wants to restrict
insertions and deletions.
 that insertions and deletions can only be performed at the top
of the list.(not in the middle and at end)

 The other end is called bottom


 Fundamental operations:
 Push: Equivalent to an insert
 Pop: Deletes the most recently inserted element
 Top: Examines the most recently inserted element
Stack and Queue / Slide 5

Stack
 Stacks are less flexible
 but are more efficient and easy to implement
 Stacks are known as LIFO (Last In, First Out)
lists.
 The last element inserted will be the first to be
retrieved
 Or elements are removed from stack in reverse
order of that in which they were inserted into stack.
Basic operations of stack
Stack and Queue / Slide 6

Push and Pop


 Primary operations: Push and Pop
 Push
 Add an element to the top of the stack
 Pop
Remove the element at the top of the stack
Note: A can’t be deleted before B

empty stack push an element push another pop

top
B
top top A
A A
top
Stack and Queue / Slide 7

Note: Free storage list called AVAIL is


implemented as stack because free nodes
were removed from beginning of AVAIL list
and new available nodes were inserted only at
beginning of AVAIL list .
Note: Stacks are frequently used to indicate
order of processing of data when certain steps
of processing must be postponed until other
conditions are fulfilled.
Stack and Queue / Slide 8

Implementation of Stacks
 Any list implementation could be used to
implement a stack
 Arrays (static: the size of stack is given initially)
 Linked lists (dynamic: never become full)
Stack and Queue / Slide 9

Array Implementation
 Stack can be represented in memory using linear
array and are maintained in memory using
1. STACK (LINEAR ARRAY)
2.TOP (contains loc of top element of stack.)
3. MAXSTK ( which gives max. no. of elements that can be held by stack.
 Need to declare an array size ahead of time

 Associated with each stack is TopOfStack


 for an empty stack, set TopOfStack to -1
Stack and Queue / Slide 10
Stack and Queue / Slide 11

 TOP=0 Or TOP=NULL
indicate that stack is empty.
 TOP=3 means stack has 3 elements
 TOP=MAXSTK means stack already filled.
Stack and Queue / Slide 12

 Push
 (1)   Increment TopOfStack by 1.
 (2)   Set Stack[TopOfStack] = X
 Pop
 (1)   Set return value to Stack[TopOfStack]
 (2)   Decrement TopOfStack by 1
 These operations are performed in very fast
constant time
Stack and Queue / Slide 13

Stack class
 Attributes of Stack
 MAXSTK: the max size of stack
 TOP: the index of the top element of stack
 values: point to an array which stores elements of stack
 Operations of Stack
 IsEmpty: return true if stack is empty, return false otherwise
 IsFull: return true if stack is full, return false otherwise
 Top: return the element at the top of stack
 Push: add an element to the top of stack
 Pop: delete the element at the top of stack
 DisplayStack: print all the data in the stack
Stack and Queue / Slide 14

Push Stack

 Check OVERFLOW condition, whether there is room in Stack for


new item or it's already filled
 void Push(const double x);
 Push an element onto the stack
 If the stack is full, print the error information.
 Note top always represents the index of the top element. After pushing an
element, increment top.

void StackPush(const double x) {


if (IsFull())
printf("Error: the stack is full.“);
else
values[++top] = x;
}
Stack and Queue / Slide 15

PUSH(STACK,TOP,MAXSTK,ITEM)
This Procedure pushes an item onto stack
1. [Stack already filled?]
If TOP=MAXSTK,then print OVERFLOW and
return.
2. Set TOP:=TOP+1 [Increment Top by 1]
3. Set STACK[TOP]:=ITEM.
[Insert ITEM in new top position]
4. Return
Stack and Queue / Slide 16
Pop Stack
Check UNDERFLOW CONDITION,whether Stack has an item to
be removed?
 double Pop()
 Pop and return the element at the top of the stack
 If the stack is empty, print the error information.
(In this case, the return value is useless.)
 Don’t forgot to decrement top

double StackPop() {
if (IsEmpty()) {
printf("Error: the stack is empty.“);
return -1;
}
else {
return values[top--];
}
}
Stack and Queue / Slide 17

Pop(STACK,TOP,ITEM)
This Procedure deletes top element of stack and assigns it to
variable ITEM.
1. [Check whether Stack has an item to be removed?]
If TOP=0,then print UNDERFLOW and return.
2. Set ITEM:=STACK[TOP] [Assigns TOP element to ITEM]
3. Set TOP:=Top-1.
[Decrease TOP by 1]
4. Return
Implementation based on Linked List
Stack and Queue / Slide 18

(Linked Stack)
 Now let us implement a stack based on a linked list.
 Push and pop at the head of the list
 New nodes should be inserted at the front of the list, so that they become the top of the stack
 Nodes are removed from the front (top) of the list
 PUSH operation into stack is accomplished by inserting node into
front or start of list.
 POP operation is undertaken by deleting node pointed to by START
pointer.
Stack and Queue / Slide 19

List Stack Example

push(6)

top

NULL

6

info link

TOP of the stack


Stack and Queue / Slide 20

List Stack Example


push(6)
push(1)

top
TOP of the stack

1

6
Stack and Queue / Slide 21

List Stack Example


push(6);
push(1);
push(7);
7
top

1

6
Stack and Queue / Slide 22

List Stack Example


push(6);
8 push(1);
push(7);
7 push(8);
top
pop();

1

6
Stack and Queue / Slide 23

List Stack Example


push(6);
push(1);
TOP of the stack
push(7);
7 push(8);
top
pop();

1

6
Stack and Queue / Slide 24

Procedure: Push-link-stack(INFO,LINK,TOP,AVAIL,ITEM)
This procedure pushes an ITEM into linked stack.
1. [Available space?]
If AVAIL=NULL, then write: OVERFLOW and exit.
2. [Remove 1st node from AVAIL list]
Set NEW:=AVAIL and
AVAIL:=LINK[AVAIL]
3. Set INFO[NEW]:=ITEM [Copies ITEM into new node]
4. Set LINK[NEW]:=TOP
[New node points to original top node in stack]
5. Set TOP=NEW [reset TOP to point to mew node at top of
stack]
6. Exit
Stack and Queue / Slide 25

Procedure: Pop-link-stack(INFO,LINK,TOP,AVAIL,ITEM)
This procedure deletes top element of linked stack and assigns
it to variable ITEM.
1. [Check stack has an item to be removed?]
If TOP=NULL, then write: UNDERFLOW and exit.
2. [copies top element of stack into item]
Set ITEM:=INFO[TOP]
3. Set TEMP:=TOP and TOP:=LINK[TOP]
[Save old value of TOP ptr. In TEMP and Reset TOP to
point to next node in stack]
4. [Return deleted node to AVAIL LIST]
Set LINK[TEMP]:=AVAIL and
AVAIL=TEMP
5. Exit
Stack and Queue / Slide 26

POLISH NOTATIONS
 Infix, Prefix(Polish), and Postfix(Reverse Polish)
 If the operator is placed in between operand, it is called infix
notation Ex. A+B (A+B)*C A+(B*C)
 prefix notations – operators preceded the operands
Ex. +AB
(A+B)*C = [+AB]*C = *+ABC
A+(B*C) = A+[*BC) = +A*BC
(A+B)/(C-D)= [+AB]/[-CD] = /+AB-CD
 Postfix- Operator symbol is placed after two operands
 Ex. A+B= AB+
(A+B)*C= [AB+]*C= AB+C*
A+(B*C) = A+[BC*]=ABC*+
Stack and Queue / Slide 27

 For instance, the expression that would be written in


conventional infix notation as
(5 − 6) * 7 can be written in prefix as
*(− 5 6) 7 or simply * − 5 6 7
 following infix expression:
5 + ((1 + 2) * 4) − 3 can be written in postfix as
512+4*+3−
Stack and Queue / Slide 28
Algo: This algorithm finds value of an arithmetic
expression P written in postfix notation.
1. Add a right parenthesis”)” at end of P.
2. Scan P from left to right and repeat steps 3
and 4 for each element of P until sentinel is
encountered
3. If an operand is encountered, put it on stack
4. If an operand @ is encountered,then:
a. Remove 2 top elements of stack,where A is
top element and B is next to top element on
stack.
5. Set value equal to top element on stack.
6. Exit
Stack and Queue / Slide 29
Algorithm: Polish(Q,P)
Suppose Q is an arithmetic expression written in infix notation. This algorithm finds
equivalent postfix expression P.
1. Push “(“ onto STACK, and add “)” to end of Q
2. Scan Q from left to right and repeat steps 3 to 6 for each element of Q until STACK is
empty:
3. If an operand is encountered, add it to P.
4. If a left parenthesis is encountered, push it onto STACK.
5. If an operator @ is encountered, then:
(a) Repeatedly pop from STACK and add to P each operator(on top of STACK)
which has same precedence as or higher precedence than @.
(b) Add @ to STACK
6. If a right parenthesis is encountered, then:
(a) Repeatedly pop from STACK and add to P each operator(on top of STACK) until
a left parenthesis is encountered.
(b) Remove left parentheses
7. Exit
Stack and Queue / Slide 30

Postfix Expressions
 Calculate 4.99 * 1.06 + 5.99 + 6.99 * 1.06
 Need to know the precedence rules
 Postfix (reverse Polish) expression
 4.99 1.06 * 5.99 + 6.99 1.06 * +
 Use stack to evaluate postfix expressions
 When a number is seen, it is pushed onto the stack
 When an operator is seen, the operator is applied to the 2
numbers that are popped from the stack. The result is
pushed onto the stack
 Example
 evaluate 6 5 2 3 + 8 * + 3 + *
 The time to evaluate a postfix expression is O(N)
 processing each element in the input consists of stack
operations and thus takes constant time

You might also like