Professional Documents
Culture Documents
STACKS
Introduction Static implementation of stack uses arrays to create a stack.
In static implementation, the size of the stack has to be declared during
Stack is a non-primitive linear data structure.
program design and after that it cannot be resized.
This is an ordered list in which a data item is inserted and deleted from a
Dynamic implementation is also referred to as linked list implementation
common end.
and uses pointers to work upon the stack.
This common end is known as top of stack (TOS).
As all the insertions and deletions in a stack take place from top of stack, Stack implementation using arrays
the last inserted element will be the first to be removed from the stack.
That is why this is called as Last In First Out (LIFO) list. PUSH operation
This is the process of inserting data items into the stack.
Stack terminology
The variable top is used to store the position of the topmost element in the
Push stack.
As the stack is empty initially, top is initialized to -1, i.e. a position out of
This is the process of insertion a data item into the stack.
the stack.
Pop Program : WAP to implement the PUSH operation using an array of five
Deleting a data item from the top of the stack is called pop operation. elements.
1. #include<stdio.h>
TOP 2. int stack[5]; /* Declaring a stack of 5 elements */
3. int top = -1; /* Initially top is out of the stack */
This term refers to the top of stack (TOS). The stack top is used to check
4. void push(); /* Function Prototype */
the position of the last value in the stack. 5. void main()
6. {
Stack underflow 7. push();
This is the situation when the stack is empty or contains no element. 8. }
9.
Hence, no more values can be popped out from the stack. 10. void push() /* Function Definition */
11. {
Stack overflow 12. int n;
This is the situation when the stack becomes full. Hence, no more 13. while(1)/* Infinite loop */
14. {
values can be pushed into the stack. 15. if(top==4)
16. { printf(“Stack overflow...”);
Implementation of stacks 17. break; }
18. printf(“Enter a value : “);
Stack can be implemented in two ways –
19. scanf(“%d”, &n);
(a) Static implementation 20. top++;
21. stack[top] = n;
(b) Dynamic implementation 22. }
23. }
ACC, 103/9, Sec. – 10, Kumbha Marg, Pratap Nagar, Near SBBJ Bank, Sanganer, JAIPUR.
STACKS P a g e | 2.2
Algorithm to implement PUSH operation (using array) Algorithm to implement POP operation (using array)
Note : stack is an array of 5 integers Note : stack is an array of 5 integers
top stores the index of the topmost element in stack top stores the index of the topmost element in the stack
Step 1 : Set top = -1 Step 1 : Repeat Step 2 to Step 6 while (1)
Step 2 : Repeat Step 3 to Step 8 while (1) Step 2 : if (top = -1)
Step 3 : if (top = 4) Then go to Step 3
Then go to Step 4 Else go to Step 5
Else go to Step 6 Step 3 : Print “Stack Underflow….”
Step 4 : Print “Stack Overflow….” Step 4 : Go to Step 7
Step 5 : Go to Step 9 Step 5 : Print top[stack]
Step 6 : Input a value and store it in n Step 6 : Decrement top by 1
Step 7 : Increment top by 1 Step 7 : Stop
Step 8 : top[stack] = n
Step 9 : Stop Stack implementation using linked lists
The logical structure of the node of a stack is similar to that of a singly
POP operation
linked list. it is given below –
The process of processing values from the stack is called pop operation. struct node
While popping a value from the stack, the element where top is pointing {
is extracted and the value of top is decremented by 1. int data;
struct node *next;
Program : Write a function to implement the POP operation.
};
1. void pop()
2. { PUSH operation
3. printf(“\n\n\nThe values in the stack are : \n”);
The process of pushing a data item is almost similar to inserting a data
4. while (1)
item into a singly linked list.
5. {
The only difference is that the top variable always stores the address of
6. if (top==-1)
the latest inserted node into the stack whereas in a singly linked list, head
7. {
8. printf(“\n\nStack underflow...”); always stores the address of the first node in the list.
9. break; Dynamic implementation of stacks using a linked list is similar to a
10. } reverse singly linked list.
11. printf(“%d\t”, stack[top]); This process is illustrated in the following figure –
12. top—; Top
PUSH 10 NULL 10
13. }
Top
14. } PUSH 20 NULL 10 20
Top
PUSH 30 NULL 10 20 30
ACC, 103/9, Sec. – 10, Kumbha Marg, Pratap Nagar, Sanganer, JAIPUR. Contact Nos. : 77370-08028, 98285-48028
STACKS P a g e | 2.3
Program : WAP to implement push operation using linked list Algorithm to implement PUSH operation (using Linked List)
representation. Note : top stores the address of the topmost element in the stack
1. #include<stdio.h> Step 1 : Set top = NULL
2. #include<conio.h>
Step 2 : Input a value in n
3. #include<stdlib.h>
Step 3 : Repeat Step 4 to Step 8 while (n != -1)
4. typedef struct node /* Definition of a node */
Step 4 : Create a node and store the address in temp
5. {
6. int data; Step 5 : data [temp] = n
7. struct node *next; Step 6 : next [temp] = top
8. }STACK; Step 7 : top = temp
9. Step 8 : Input a value in n
10. STACK* push(STACK *); /* Function prototype */ Step 9 : Stop
11. main()
12. { POP operation
13. STACK *top; The procedure to pop a data item from a stack using linked list is shown
14. clrscr(); below –
15. top = NULL; /* Initially, there is no stack */ Top
16. top = push(top); NULL 10 20 30
17. } Top
18. POP 30 NULL 10 20
19. STACK* push(STACK *top) Top
20. { POP 20 NULL 10
21. STACK *temp; POP 10 Top = NULL (stack underflow)
22. int n;
23. printf(“Enter a value : “);
24. scanf(“%d”, &n); Program : Write a function to implement POP operation using linked list
25. while(n!=-1) /* Loop terminates when n = -1 */ representation.
26. { 1. STACK* pop(STACK *); /* Function prototype */
27. temp = (STACK *)malloc(sizeof(STACK)); 2.
28. temp->data = n; 3. STACK* pop(STACK *top)
29. temp -> next = top; 4. {
5. STACK *ptr;
30. top = temp;
6. while(1)
31. printf(“Enter a value : “); 7. {
32. scanf(“%d”, &n); 8. ptr = top;
33. } 9. if(top == NULL)
34. return top; 10. {
35. } 11. printf(“Stack Underflow...”);
12. return top;
13. }
ACC, 103/9, Sec. – 10, Kumbha Marg, Pratap Nagar, Sanganer, JAIPUR. Contact Nos. : 77370-08028, 98285-48028
STACKS P a g e | 2.4
Example 2 : Convert the following infix expression into a postfix expression. Algorithm to convert infix to postfix expression
A *( B +C^D) -E ^F* (G /H ) Step 1 : Input an infix expression
Solution : On adding symbol ‘#’ in the end of the given infix expression – Step 2 : Add symbol ‘#’ into the end of the infix expression
A*(B+C^D)-E^F*(G/H)# Step 3 : Push symbol ‘#’ into the operator stack.
Scanned symbol Operator stack Postfix expression Step 4 : Scan the symbol of infix expression one by one left to right.
A # A Step 5 : Repeat step 6 to step 10 while (scanned symbol ! = ‘#’)
* #* A Step 6 : if scanned symbol is operand then add to postfix expression.
Example : Evaluate the following postfix expression — Scanned Operand stack Calculation
ABC*D/+EF^G *+ Symbol
The values are— A A
A = 3, B = 5, C = 2, D = 2, E = 3, F = 2, G = 3 B A, B
Solution : Appending ‘#’ in the end of the postfix expression – C A, B, C
ABC*D/+EF^G*+# * A, (B*C) B*C
Scanned Symbol Operand stack Calculation D A, (B*C), D
A 3,
/ A, ((B*C))/D) (B*C)/D
B 3, 5 + (A+((B*C))/D)) A+((B*C)/D)
C 3, 5, 2 E (A+((B*C))/D)), E
* 3, 10 5*2=10 F (A+((B*C))/D)), E, F
We need to keep some important facts in mind while working with Program : WAP to print the reverse of an integer number entered by user.
recursion. They are – 1. #include<stdio.h>
o There must be a terminating condition in a recursive function. This 2. #include<conio.h>
3. long int reverse(int);
condition is also known as base case. Base case is a case in which the
4. main()
function does not call itself. 5. {
o In absence of a base case, the function keep on calling itself until the 6. int n;
space in the memory is not fully occupied. 7. long int result;
8. clrscr();
o Every time a new recursive call is made, a new memory space is
9. printf(“Enter the number : “);
allocated to each automatic variable used by the recursive routine. 10. scanf(“%d”, &n);
o A recursive function executes in the reverse order. This means a stack is 11. result = reverse(n);
always used to store values from each calling of the recursive function. 12. printf(“After reversing : %ld”, result);
13. getch();
o Each time recursive call is there, the duplicated values of the local 14. }
variables of a recursive call are pushed onto the stack and all these 15.
values are available to the respective function call when it is popped 16. long int reverse(int num)
out from the stack. 17. {
18. int rem;
Program : WAP to calculate the factorial of given number entered by the 19. static long int sum=0;
user. 20. if(num==0)
21. return(num);
1. #include<stdio.h>
22. else
2. #include<conio.h>
23. {
3. long fact(int);
24. rem = num%10;
4. main()
25. sum = sum * 10 + rem;
5. { 26. reverse(num/10);
6. int n; 27. }
7. long result; 28. return(sum);
8. clrscr(); 29. }
9. printf(“Enter a number : “);
10. scanf(“%d”, &n); Program : WAP to ask a number and calculate the power for that number.
11. result = fact(n); 1. #include<stdio.h>
12. printf(“The factorial of %d is %ld”, n, result); 2. #include<conio.h>
13. getch(); 3. long int power(int, int);
14. } 4. main()
15. long fact(int num) 5. {
16. { 6. int n, p;
17. if(num==1) 7. long int result;
18. return(num); 8. clrscr();
19. else 9. printf(“Enter the number : “);
20. return(num*fact(num-1)); 10. scanf(“%d”, &n);
21. } 11. printf(“Enter the power : “);
12. scanf(“%d”, &p);
ACC, 103/9, Sec. – 10, Kumbha Marg, Pratap Nagar, Sanganer, JAIPUR. Contact Nos. : 77370-08028, 98285-48028
STACKS P a g e | 2.9
ACC, 103/9, Sec. – 10, Kumbha Marg, Pratap Nagar, Sanganer, JAIPUR. Contact Nos. : 77370-08028, 98285-48028