1: #include <stdio.h>
2: #include <stdlib.h>
3: int stack[5];
4: void push();
5: int pop();
6: void traverse();
7: int is_empty();
8: int top_element();
9: int top = 0;
10: int main()
11: {
12: int element, choice;
13: for (;;)
14: {
15: printf("Stack Operations.\n");
16: printf("1. Insert into stack (Push operation).\n");
17: printf("2. Delete from stack (Pop operation).\n");
18: printf("3. Print top element of stack.\n");
19: printf("4. Check if stack is empty.\n");
20: printf("5. Traverse stack.\n");
21: printf("6. Exit.\n");
22: printf("Enter your choice.\n");
23: scanf("%d",&choice);
24: switch (choice)
25: {
26: case 1:
27: if (top == 5)
28: printf("Error: Overflow\n\n");
29: else {
30: printf("Enter the value to insert.\n");
31: scanf("%d", &element);
32: push(element);
33: }
34: break;
35: case 2:
36: if (top == 0)
37: printf("Error: Underflow.\n\n");
38: else {
39: element = pop();
40: printf("Element removed from stack is %d.\n", element);
41: }
42: break;
43: case 3:
44: if (!is_empty()) {
45: element = top_element();
46: printf("Element at the top of stack is %d\n\n", element);
47: }
48: else
49: printf("Stack is empty.\n\n");
50: break;
51: case 4:
52: if (is_empty())
53: printf("Stack is empty.\n\n");
54: else
55: printf("Stack is not empty.\n\n");
56: break;
57: case 5:
58: traverse();
59: break;
60: case 6:
61: exit(0);
62: }
63: }
64: }
65: void push(int value) {
66: stack[top] = value;
67: top++;
68: }
69: int pop() {
70: top--;
71: return stack[top];
72: }
73: void traverse() {
74: int d;
75: if (top == 0) {
76: printf("Stack is empty.\n\n");
77: return;
78: }
79: printf("There are %d elements in stack.\n", top);
80: for (d = top - 1; d >= 0; d--)
81: printf("%d\n", stack[d]);
82: printf("\n");
83: }
84: int is_empty() {
85: if (top == 0)
86: return 1;
87: else
88: return 0;
89: }
90: int top_element() {
91: return stack[top-1];
92: }
getsprogramming Tutorials , C Programming, CPP Programming and JAVA Programming , Python Programming , Javascript Programming Welcome to getsprogramming Blog Here You Find Different Programs in Easier way to Explain and gain Knowledge and Try Your Self..Thanks
Friday, 17 July 2015
Write a C Program To Implement Stack - Last In First Out (LIFO) Method
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment