0% found this document useful (0 votes)
19 views

STACK WITH ARR1.c

This document implements a stack data structure using an array in C. It defines functions to push and pop elements onto the stack. The main function displays a menu to allow the user to push, pop, check the size of the stack, or exit. It uses a global array and top index variable to track the stack. The push function checks for overflow and increments top to add an element. The pop function checks for underflow, returns the top element and decrements top to remove an element, returning a constant if empty.

Uploaded by

Mohd Tausif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

STACK WITH ARR1.c

This document implements a stack data structure using an array in C. It defines functions to push and pop elements onto the stack. The main function displays a menu to allow the user to push, pop, check the size of the stack, or exit. It uses a global array and top index variable to track the stack. The push function checks for overflow and increments top to add an element. The pop function checks for underflow, returns the top element and decrements top to remove an element, returning a constant if empty.

Uploaded by

Mohd Tausif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1 /**

2 * Stack implementation using array in C language.


3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <limits.h> // For INT_MIN
8
9 #define SIZE 100
10
11 // Create a stack with capacity of 100 elements
12 int stack[SIZE];
13
14 // Initially stack is empty
15 int top = -1;
16
17
18 /* Function declaration to perform push and pop on stack */
19 void push(int element);
20 int pop();
21
22
23 void main()
24 {
25 int choice, data;
26
27 while(1)
28 {
29 /* Menu */
30 printf("------------------------------------\n");
31 printf(" STACK IMPLEMENTATION PROGRAM \n");
32 printf("------------------------------------\n");
33 printf("1. Push\n");
34 printf("2. Pop\n");
35 printf("3. Size\n");
36 printf("4. Exit\n");
37 printf("------------------------------------\n");
38 printf("Enter your choice: ");
39
40 scanf("%d", &choice);
41
42 switch(choice)
43 {
44 case 1:
45 printf("Enter data to push into stack: ");
46 scanf("%d", &data);
47
48 // Push element to stack
49 push(data);
50 break;
51
52 case 2:
53 data = pop();
54
55 // If stack is not empty
56 if (data != INT_MIN)
57 printf("Data => %d\n", data);
58 break;
59
60 case 3:
61 printf("Stack size: %d\n", top + 1);
62 break;
63
64 case 4:
65 printf("Exiting from app.\n");
66 exit(0);
67 break;
68
69 default:
70 printf("Invalid choice, please try again.\n");
71 }
72
73 printf("\n\n");
74 }
75
76
77 getch();
78 }
79
80
81
82 /**
83 * Functiont to push a new element in stack.
84 */
85 void push(int element)
86 {
87 // Check stack overflow
88 if (top >= SIZE)
89 {
90 printf("Stack Overflow, can't add more element element to stack.\n");
91 return;
92 }
93
94 // Increase element count in stack
95 top++;
96
97 // Push element in stack
98 stack[top] = element;
99
100 printf("Data pushed to stack.\n");
101 }
102
103
104 /**
105 * Function to pop element from top of stack.
106 */
107 int pop()
108 {
109 // Check stack underflow
110 if (top < 0)
111 {
112 printf("Stack is empty.\n");
113
114 // Throw empty stack error/exception
115 // Since C does not have concept of exception
116 // Hence return minimum integer value as error value
117 // Later in code check if return value is INT_MIN, then
118 // stack is empty
119 return INT_MIN;
120 }
121
122
123 // Return stack top and decrease element count in stack
124 return stack[top--];
125 }

You might also like