You are on page 1of 21

Stacks

Introduction
• Non primitive linear
data structure. Data Element n TOP
• Allows operations at …
one end only. …
• The top element can …
only be accessed at Data Element 3
any time.
Data Element 2
– LIFO (Last-in-first-
Data Element 1
out) data structure.
Thapar University UCS406 - Data Structures and Algorithms 2
Operations
• Two primary operations:
– push() − Pushing (storing) an element on the stack.
– pop() − Removing (accessing) an element from the
stack.
• Other operations for effective functionality:
– peek() − Get the top data element of the stack,
without removing it.
– isFull() − Check if stack is full. OVERFLOW
– isEmpty() − Check if stack is empty. UNDERFLOW
Thapar University UCS406 - Data Structures and Algorithms 3
Stack – Push
push(9) push(1) push(3) push(6) push(4) push(2)

9 1 3 6 4 2
top = 4
4 4 4 4 4 top 4 4 4 4

3 3 3 3 top 6 3 6 3 6 3

2 2 2 top 3 2 3 2 3 2 3 2
top 1 1 1 1 1
1 1 1 1 1 1 1
top 9 9 9 9 9 9
0 0 0 0 0 0 0
top = -1 OVERFLOW

The stack is full, no more elements


can be added. OVERFLOW
Thapar University UTA007 - Computer Programming I 4
Stack – Pop
pop() pop() pop() pop() pop() pop()

4 6 3 1 9 Nothing
top = 4
4 4 4 4 4 4 4 4
6 3 top 6 3 3 3 3 3 3
3 2 3 2 top 3 2 2 2 2 2
1 1 1 1 1 1 top 1 1 1 1 1
9 0 9 0 9 0 9 0 top 9 0 0 0

top = -1 top = -1
UNDERFLOW

The stack is empty, no element


can be removed. UNDERFLOW
Thapar University UTA007 - Computer Programming I 5
Stack as an ADT
• A stack is an ordered list of elements of same data
type.
• Elements are always inserted and deleted at one
end.
• Following are its basic operations:
– S = init() – Initialize an empty stack.
– isEmpty(S) – Returns "true" if and only if the
stack S is empty, i.e., contains no elements.
Thapar University UCS406 - Data Structures and Algorithms 6
Stack as an ADT
– isFull(S) – Returns "true" if and only if the
stack S has a bounded size and holds the maximum
number of elements it can.
– top(S) – Returns the element at the top of the
stack S, or error if the stack is empty.
– S = push(S,x) – Push an element x at the top of the
stack S.
– S = pop(S) – Pop an element from the top of the
stack S.
– print(S) – Prints the elements of the stack S from
top to bottom.
Thapar University UCS406 - Data Structures and Algorithms 7
Implementation
• Using static arrays
– Realizes stacks of a maximum possible size.
– Top is taken as the maximum index of an
element in the array.
• Using dynamic linked lists
– Choose beginning of the list as the top of the
stack.
Thapar University UCS406 - Data Structures and Algorithms 8
Using Static Arrays
Algorithm for Push
• Let,
– STACK[SIZE] is a one dimensional array that will hold
the stack elements.
– TOP is the pointer that points to the top most element
of the stack.
– DATA is the data item to be pushed.
1. If TOP == SIZE – 1
2. Display "Overflow condition"
3. Else
4. TOP = TOP + 1
5. STACK [TOP] = DATA
Thapar University UCS406 - Data Structures and Algorithms 10
Algorithm for Pop
• Let,
– STACK[SIZE] is a one dimensional array that will hold the
stack elements.
– TOP is the pointer that points to the top most element of
the stack.
– DATA is the element popped from the top of the stack.
1. If TOP < 0 (or TOP == -1)
2. Display "Underflow condition."
3. Else
4. DATA = STACK[TOP]
5. TOP = TOP – 1
6. Return DATA
Thapar University UCS406 - Data Structures and Algorithms 11
Static Array Implementation
1. #define MAXLEN 100 9. int isEmpty ( stack S )
10. { return (S.top == -1); }
2. typedef struct
3. { int element[MAXLEN]; 11. int isFull ( stack S )
4. int top; } stack; 12. { return (S.top == MAXLEN - 1);
}
5. stack init ()
6. { stack S; 13. int top ( stack S )
7. S.top = -1; 14. { if (isEmpty(S))
8. return S; } 15. printf("Empty stack\n");
16. else
17. return S.element[S.top]; }

Thapar University UCS406 - Data Structures and Algorithms 12


Contd…
18. stack push ( stack S , int x )
19. { if (isFull(S))
20. printf("OVERFLOW\n");
21. else
22. { ++S.top;
23. S.element[S.top] = x; 25. stack pop ( stack S )
24. } 26. { if (isEmpty(S))
25. return S; } 27. printf("UNDERFLOW\n");
28. else
29. { --S.top; }
30. return S; }

Thapar University UCS406 - Data Structures and Algorithms 13


Contd…
31. void print ( stack S )
32. { int i;
33. for (i = S.top; i >= 0; --i)
34. printf("%d",S.element[i]); }

35. int main () 42. printf("Current stack : ");


36. { stack S; 43. print(S);
37. S = init(); 44. printf(" with top = %d.\n", top(S));
38. S = push(S,10); 45. S = pop(S);
39. S = push(S,45); 46. S = pop(S);
40. S = push(S,1); 47. printf("Current stack : ");
41. S = push(S,50); 48. print(S);
49. printf(" with top = %d.\n", top(S));
50. return 0; }
Thapar University UCS406 - Data Structures and Algorithms 14
Using Dynamic Linked Lists
Algorithm for Push
• Let,
– TOP is the pointer that points to the top most
element of the stack.
– DATA is the data item to be pushed.
1. Create a node pointer (newNode).
2. newNode[data] = DATA.
3. newNode[next] = top.
4. top = newNode.
Thapar University UCS406 - Data Structures and Algorithms 16
Algorithm for Pop
• Let,
– TOP is the pointer that points to the top most element
of the stack.
– temp points to the element popped from the top of the
stack.
1. If (TOP == NULL)
2. Print [Underflow condition].
3. Else
4. initialize a node pointer (temp) with TOP.
5. TOP = TOP[next]
6. Release the memory location pointed by temp.
7. end if
Thapar University UCS406 - Data Structures and Algorithms 17
Dynamic Linked List Implementation
1. struct Node
2. { 8. void push(int value)
3. int data; 9. {
4. struct Node *next; 10. struct Node *newNode;
5. }*top; 11. newNode = (struct
Node*)malloc(sizeof(struct Node));
6. void init() 12. newNode->data = value;
7. { top = NULL;} 13. if(top == NULL)
14. newNode->next = NULL;
15. else
16. newNode->next = top;
17. top = newNode;
18. }
Thapar University UCS406 - Data Structures and Algorithms 18
Contd…
19. void pop()
20. {
21. if(top == NULL)
22. printf("\nUnderflow\n");
23. else{
24. struct Node *temp = top;
25. printf("\nDeleted element: %d", temp->data);
26. top = temp->next;
27. free(temp);
28. }
29. }

Thapar University UCS406 - Data Structures and Algorithms 19


Contd…
30. void display()
31. {
32. if(top == NULL)
33. printf("\nStack is Empty!!!\n");
34. else{
35. struct Node *temp = top;
36. while(temp->next != NULL){
37. printf("%d--->",temp->data);
38. temp = temp -> next;
39. }
40. printf("%d--->NULL",temp->data);
41. }
42. }
Thapar University UCS406 - Data Structures and Algorithms 20
Applications
• Reverse a word.
– Push a word letter by letter and then pop letters from
the stack.
• "UNDO" mechanism in text editors.
• Backtracking.
– Game playing, finding paths, exhaustive searching.
• Parsing.
• Recursive function calls.
• Calling a function.
• Expression Evaluation.
• Expression Conversion.
Thapar University UCS406 - Data Structures and Algorithms 21

You might also like