C Program to Implement Stack Operations using Dynamic Memory Allocation

This is a C Program to Implement Stack Operations using Dynamic Memory Allocation.

Problem Description

This program implements stack operations using dynamic memory allocation.

Problem Solution

1. Use malloc function to allocate memory.
2. Define separate functions for the operations like push, pop and display.
3. Use switch statement to access these functions.

Program/Source Code

Here is source code of the C Program to Implement Stack Operations using Dynamic Memory Allocation. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Implement Stack Operations using Dynamic Memory 
  3.  * Allocation
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. struct node
  9. {
  10.     int data;
  11.     struct node *link;
  12. }*top = NULL;
  13.  
  14. #define MAX 5
  15.  
  16. // function prototypes
  17. void push();
  18. void pop();
  19. void empty();
  20. void stack_full();
  21. void stack_count();
  22. void destroy();
  23. void print_top();
  24.  
  25. void main()
  26. {
  27.     int choice;
  28.  
  29.     while (1)
  30.     {
  31.         printf("1. push an element \n");
  32.         printf("2. pop an element \n");
  33.         printf("3. check if stack is empty \n");
  34.         printf("4. check if stack is full \n");
  35.         printf("5. count/display elements present in stack \n");
  36.         printf("6. empty and destroy stack \n");
  37.         printf("7. Print top of the stack \n");
  38.         printf("8. exit \n");
  39.         printf("Enter your choice \n");
  40.         scanf("%d",&choice);
  41.         switch (choice)
  42.         {
  43.         case 1:    
  44.             push();
  45.             break;         
  46.         case 2:    
  47.             pop();
  48.             break;         
  49.         case 3:    
  50.             empty();
  51.             break;         
  52.         case 4:    
  53.             stack_full();
  54.             break;         
  55.         case 5:    
  56.             stack_count();
  57.             break;         
  58.         case 6:    
  59.             destroy();
  60.             break;         
  61.         case 7:    
  62.             print_top();
  63.             break;
  64.         case 8:    
  65.             exit(0);
  66.         default:
  67.             printf("wrong choice\n");         
  68.         }
  69.     }
  70. }
  71.  
  72. // to insert elements in stack
  73. void push()
  74. {
  75.     int val,count;
  76.     struct node *temp;
  77.     temp = (struct node*)malloc(sizeof(struct node));
  78.  
  79.     count = st_count();
  80.     if (count <= MAX - 1)
  81.     {
  82.         printf("\nEnter value which you want to push into the stack :\n");
  83.         scanf("%d",&val);
  84.         temp->data = val;
  85.         temp->link = top;
  86.         top = temp;
  87.     }
  88.     else
  89.         printf("WARNING: STACK FULL\n");
  90. }
  91.  
  92. // to delete elements from stack
  93. void pop()
  94. {
  95.     struct node *temp;
  96.     if (top =  = NULL)
  97.         printf("**Stack is empty**\n");
  98.     else
  99.     {
  100.         temp = top;
  101.         printf("Value popped out is %d \n",temp->data);
  102.         top = top->link;
  103.         free(temp);
  104.     }
  105. }
  106.  
  107. // to check if stack is empty
  108. void empty()
  109. {
  110.     if (top == NULL)
  111.         printf("STACK IS EMPTY\n");
  112.     else
  113.         printf("elements are present, stack is not empty \n");
  114. }
  115.  
  116. // to check if stack is full
  117. void stack_full()
  118. {
  119.     int count;
  120.  
  121.     count = st_count();
  122.     if (count =  = MAX)
  123.     {
  124.         printf("stack is full\n");
  125.     }
  126.     else
  127.         printf("stack is not full \n");
  128. }
  129.  
  130. // to count the number of elements
  131. void stack_count()
  132. {
  133.     int count = 0;
  134.     struct node *temp;
  135.  
  136.     temp = top;
  137.     while (temp! = NULL)
  138.     {
  139.         printf(" %d\n",temp->data);
  140.         temp = temp->link;
  141.         count++;
  142.     }
  143.     printf("size of stack is %d \n",count);
  144. }
  145.  
  146. int st_count()
  147. {
  148.     int count = 0;
  149.     struct node *temp;
  150.     temp = top;
  151.     while (temp! = NULL)
  152.     {
  153.         temp = temp->link;
  154.         count++;
  155.     }
  156.     return count;
  157. }
  158.  
  159. // to empty and destroy the stack
  160. void destroy()
  161. {
  162.     struct node *temp;
  163.     temp = top;
  164.     while (temp! = NULL)
  165.     {
  166.         pop();
  167.         temp = temp->link;
  168.     }
  169.     printf("stack destroyed\n");
  170. }
  171.  
  172. // to print top element of stack
  173. void print_top()
  174. {
  175.     if (top == NULL)
  176.         printf("\n**Top is not available for an EMPTY stack**\n");
  177.     else
  178.         printf("\nTop of the stack is %d \n",top->data);
  179. }
Program Explanation

1. Ask the user for the operations like push, pop, display etc.
2. According to the entered option access the respective functions. Use switch statement to access the functions.
3. Use structure with a data and a pointer as the data module. Use malloc function to assign the memory dynamically.
4. In the push() function ask the user to enter the number to be inserted and store it in the variable val.
5. Copy the value to the new data module’s data.
6. In the pop() function delete the element at the top.
7. In the display() function using for loop display all the data starting from top to the bottom.

advertisement
advertisement
Runtime Test Cases
1. push an element
2. pop an element
3. check if stack is empty
4. check if stack is full
5. count/display elements present in stack
6. empty and destroy stack
7. Print top of the stack
8. exit
Enter your choice
5
size of stack is 0
1. push an element
2. pop an element
3. check if stack is empty
4. check if stack is full
5. count/display elements present in stack
6. empty and destroy stack
7. Print top of the stack
8. exit
Enter your choice
2
**Stack is empty**
1. push an element
2. pop an element
3. check if stack is empty
4. check if stack is full
5. count/display elements present in stack
6. empty and destroy stack
7. Print top of the stack
8. exit
Enter your choice
3
STACK IS EMPTY
1. push an element
2. pop an element
3. check if stack is empty
4. check if stack is full
5. count/display elements present in stack
6. empty and destroy stack
7. Print top of the stack
8. exit
Enter your choice
7
 
**Top is not available for an EMPTY stack**
1. push an element
2. pop an element
3. check if stack is empty
4. check if stack is full
5. count/display elements present in stack
6. empty and destroy stack
7. Print top of the stack
8. exit
Enter your choice
1
 
Enter value which you want to push into the stack :
10
1. push an element
2. pop an element
3. check if stack is empty
4. check if stack is full
5. count/display elements present in stack
6. empty and destroy stack
7. Print top of the stack
8. exit
Enter your choice
1
 
Enter value which you want to push into the stack :
20
1. push an element
2. pop an element
3. check if stack is empty
4. check if stack is full
5. count/display elements present in stack
6. empty and destroy stack
7. Print top of the stack
8. exit
Enter your choice
1
 
Enter value which you want to push into the stack :
30
1. push an element
2. pop an element
3. check if stack is empty
4. check if stack is full
5. count/display elements present in stack
6. empty and destroy stack
7. Print top of the stack
8. exit
Enter your choice
1
 
Enter value which you want to push into the stack :
40
1. push an element
2. pop an element
3. check if stack is empty
4. check if stack is full
5. count/display elements present in stack
6. empty and destroy stack
7. Print top of the stack
8. exit
Enter your choice
1
 
Enter value which you want to push into the stack :
50
1. push an element
2. pop an element
3. check if stack is empty
4. check if stack is full
5. count/display elements present in stack
6. empty and destroy stack
7. Print top of the stack
8. exit
Enter your choice
5
 50
 40
 30
 20
 10
size of stack is 5
1. push an element
2. pop an element
3. check if stack is empty
4. check if stack is full
5. count/display elements present in stack
6. empty and destroy stack
7. Print top of the stack
8. exit
Enter your choice
4
stack is full
1. push an element
2. pop an element
3. check if stack is empty
4. check if stack is full
5. count/display elements present in stack
6. empty and destroy stack
7. Print top of the stack
8. exit
Enter your choice
2
Value popped out is 50
1. push an element
2. pop an element
3. check if stack is empty
4. check if stack is full
5. count/display elements present in stack
6. empty and destroy stack
7. Print top of the stack
8. exit
Enter your choice
2
Value popped out is 40
1. push an element
2. pop an element
3. check if stack is empty
4. check if stack is full
5. count/display elements present in stack
6. empty and destroy stack
7. Print top of the stack
8. exit
Enter your choice
2
Value popped out is 30
1. push an element
2. pop an element
3. check if stack is empty
4. check if stack is full
5. count/display elements present in stack
6. empty and destroy stack
7. Print top of the stack
8. exit
Enter your choice
6
Value popped out is 20
Value popped out is 10
stack destroyed
1. push an element
2. pop an element
3. check if stack is empty
4. check if stack is full
5. count/display elements present in stack
6. empty and destroy stack
7. Print top of the stack
8. exit
Enter your choice
1
 
Enter value which you want to push into the stack :
25
1. push an element
2. pop an element
3. check if stack is empty
4. check if stack is full
5. count/display elements present in stack
6. empty and destroy stack
7. Print top of the stack
8. exit
Enter your choice
5
 25
size of stack is 1
1. push an element
2. pop an element
3. check if stack is empty
4. check if stack is full
5. count/display elements present in stack
6. empty and destroy stack
7. Print top of the stack
8. exit
Enter your choice
8

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.