Sie sind auf Seite 1von 9

C Program to Implement a Stack

This is a C program to Implement a stack.

Problem Description
This program implements the stack operation.

Problem Solution
1. Use three functions for three operations like push, pop and display.
2. Use switch statement to access these functions.
3. Exit.

Program/Source Code
Here is source code of the C program to implement a stack. 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. Stack is a LIFO data structure.
3. * Stack operations: PUSH(insert operation), POP(Delete operation)
4. * and Display stack.
5. */
6. #include <stdio.h>
7. #define MAXSIZE 5
8.
9. struct stack
10. {
11. int stk[MAXSIZE];
12. int top;
13. };
14. typedef struct stack STACK;
15. STACK s;
16.
17. void push(void);
18. int pop(void);
19. void display(void);
20.
21. void main ()
22. {
23. int choice;
24. int option = 1;
25. s.top = -1;
26.
27. printf ("STACK OPERATION\n");
28. while (option)
29. {
30. printf ("------------------------------------------\n");
31. printf (" 1 --> PUSH \n");
32. printf (" 2 --> POP \n");
33. printf (" 3 --> DISPLAY \n");
34. printf (" 4 --> EXIT \n");
35. printf ("------------------------------------------\n");
36.
37. printf ("Enter your choice\n");
38. scanf ("%d", &choice);
39. switch (choice)
40. {
41. case 1:
42. push();
43. break;
44. case 2:
45. pop();
46. break;
47. case 3:
48. display();
49. break;
50. case 4:
51. return;
52. }
53. fflush (stdin);
54. printf ("Do you want to continue(Type 0 or 1)?\n");
55. scanf ("%d", &option);
56. }
57. }
58. /* Function to add an element to the stack */
59. void push ()
60. {
61. int num;
62. if (s.top == (MAXSIZE - 1))
63. {
64. printf ("Stack is Full\n");
65. return;
66. }
67. else
68. {
69. printf ("Enter the element to be pushed\n");
70. scanf ("%d", &num);
71. s.top = s.top + 1;
72. s.stk[s.top] = num;
73. }
74. return;
75. }
76. /* Function to delete an element from the stack */
77. int pop ()
78. {
79. int num;
80. if (s.top == - 1)
81. {
82. printf ("Stack is Empty\n");
83. return (s.top);
84. }
85. else
86. {
87. num = s.stk[s.top];
88. printf ("poped element is = %dn", s.stk[s.top]);
89. s.top = s.top - 1;
90. }
91. return(num);
92. }
93. /* Function to display the status of the stack */
94. void display ()
95. {
96. int i;
97. if (s.top == -1)
98. {
99. printf ("Stack is empty\n");
100. return;
101. }
102. else
103. {
104. printf ("\n The status of the stack is \n");
105. for (i = s.top; i >= 0; i--)
106. {
107. printf ("%d\n", s.stk[i]);
108. }
109. }
110. printf ("\n");
111. }

Program Explanation
1. Ask the user for the operation like push, pop, display and exit. Use the variable top to
represent the top of the stack.
2. According to the option entered, access its respective function using switch statement.
3. In the function push(), firstly check if the stack is full. If it is, then print the output as “Stack
is Full”. Otherwise take the number to be inserted as input and store it in the variable num.
Copy the number to the array stk[] and increment the variable top by 1.
4. In the function pop(), firstly check if the stack is empty. If it is, then print the output as “Stack
is Empty”. Otherwise print the top most element of the array stk[] and decrement the variable
top by 1.
5. In the function display(), using for loop print all the elements of the array.
6. Exit.

Runtime Test Cases


STACK OPERATION
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
34
Do you want to continue(Type 0 or 1)?
0
$ a.out
STACK OPERATION
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
34
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
2
poped element is = 34
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
3
Stack is empty
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
50
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
60
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
3

The status of the stack is


60
50
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
4

C Program to Implement a Queue using an Array


This is a C Program to Implement a queue using array.

Problem Description
This C program implements the queue operations using array.

Problem Solution
1. Use three functions for three operations like insert, delete and display.
2. Use switch statement to access these functions.
3. Exit.

Program/Source Code
Here is source code of the C Program to implement a queue using array. 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 a Queue using an Array
3. */
4. #include <stdio.h>
5.
6. #define MAX 50
7.
8. void insert();
9. void delete();
10. void display();
11. int queue_array[MAX];
12. int rear = - 1;
13. int front = - 1;
14. main()
15. {
16. int choice;
17. while (1)
18. {
19. printf("1.Insert element to queue \n");
20. printf("2.Delete element from queue \n");
21. printf("3.Display all elements of queue \n");
22. printf("4.Quit \n");
23. printf("Enter your choice : ");
24. scanf("%d", &choice);
25. switch (choice)
26. {
27. case 1:
28. insert();
29. break;
30. case 2:
31. delete();
32. break;
33. case 3:
34. display();
35. break;
36. case 4:
37. exit(1);
38. default:
39. printf("Wrong choice \n");
40. } /* End of switch */
41. } /* End of while */
42. } /* End of main() */
43.
44. void insert()
45. {
46. int add_item;
47. if (rear == MAX - 1)
48. printf("Queue Overflow \n");
49. else
50. {
51. if (front == - 1)
52. /*If queue is initially empty */
53. front = 0;
54. printf("Inset the element in queue : ");
55. scanf("%d", &add_item);
56. rear = rear + 1;
57. queue_array[rear] = add_item;
58. }
59. } /* End of insert() */
60.
61. void delete()
62. {
63. if (front == - 1 || front > rear)
64. {
65. printf("Queue Underflow \n");
66. return ;
67. }
68. else
69. {
70. printf("Element deleted from queue is : %d\n", queue_array[front]);
71. front = front + 1;
72. }
73. } /* End of delete() */
74.
75. void display()
76. {
77. int i;
78. if (front == - 1)
79. printf("Queue is empty \n");
80. else
81. {
82. printf("Queue is : \n");
83. for (i = front; i <= rear; i++)
84. printf("%d ", queue_array[i]);
85. printf("\n");
86. }
87. } /* End of display() */

Program Explanation
1. Ask the user for the operation like insert, delete, display and exit.
2. According to the option entered, access its respective function using switch statement. Use
the variables front and rear to represent the first and last element of the queue.
3. In the function insert(), firstly check if the queue is full. If it is, then print the output as “Queue
Overflow”. Otherwise take the number to be inserted as input and store it in the variable
add_item. Copy the variable add_item to the array queue_array[] and increment the variable
rear by 1.
4. In the function delete(), firstly check if the queue is empty. If it is, then print the output as
“Queue Underflow”. Otherwise print the first element of the array queue_array[] and
decrement the variable front by 1.
5. In the function display(), using for loop print all the elements of the array starting from front
to rear.
6. Exit.

Runtime Test Cases


1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 10
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 15
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 20
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 30
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 2
Element deleted from queue is : 10
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 3
Queue is :
15 20 30
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 4