0% found this document useful (0 votes)
18 views3 pages

07 Stack

Uploaded by

Destiny
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)
18 views3 pages

07 Stack

Uploaded by

Destiny
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/ 3

...\source\repos\DynamicDataTypes\DynamicDataTypes\Stack.

c 1
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 typedef struct stackNode { /* self-referential structure */
5 int data;
6 struct stackNode* nextPtr;
7 } StackNode_t;
8
9 void push(StackNode_t**, int);
10 int pop(StackNode_t**);
11 int isEmpty(StackNode_t*);
12 void printStack(StackNode_t*);
13 void instructions(void);
14
15 int main()
16 {
17 StackNode_t* stackPtr = NULL; /* points to stack top */
18 int choice, value;
19
20 instructions();
21 printf("? ");
22 scanf("%d", &choice);
23
24 while (choice != 3) {
25
26 switch (choice) {
27 case 1: /* push value onto stack */
28 printf("Enter an integer: ");
29 scanf("%d", &value);
30 push(&stackPtr, value);
31 printStack(stackPtr);
32 break;
33 case 2: /* pop value off stack */
34 if (!isEmpty(stackPtr))
35 printf("The popped value is %d.\n" ,
36 pop(&stackPtr));
37
38 printStack(stackPtr);
39 break;
40 default:
41 printf("Invalid choice.\n\n");
42 instructions();
43 break;
44 }
45
46 printf("? ");
47 scanf("%d", &choice);
48 }
49
50 printf("End of run.\n");
51 return 0;
52 }
53
...\source\repos\DynamicDataTypes\DynamicDataTypes\Stack.c 2
54 /* Print the instructions */
55 void instructions(void)
56 {
57 printf("Enter choice:\n"
58 "1 to push a value on the stack\n"
59 "2 to pop a value off the stack\n"
60 "3 to end program\n");
61 }
62
63 /* Insert a node at the stack top */
64 void push(StackNode_t** topPtr, int info)
65 {
66 StackNode_t* newPtr;
67
68 newPtr = malloc(sizeof(StackNode_t));
69 if (newPtr != NULL) {
70 newPtr->data = info;
71 newPtr->nextPtr = *topPtr;
72 *topPtr = newPtr;
73 }
74 else
75 printf("%d not inserted. No memory available.\n" ,
76 info);
77 }
78
79 /* Remove a node from the stack top */
80 int pop(StackNode_t** topPtr)
81 {
82 StackNode_t* tempPtr;
83 int popValue;
84
85 tempPtr = *topPtr;
86 popValue = (*topPtr)->data;
87 *topPtr = (*topPtr)->nextPtr;
88 free(tempPtr);
89 return popValue;
90 }
91
92 /* Print the stack */
93 void printStack(StackNode_t* currentPtr)
94 {
95 if (currentPtr == NULL)
96 printf("The stack is empty.\n\n" );
97 else {
98 printf("The stack is:\n");
99
100 while (currentPtr != NULL) {
101 printf("%d --> ", currentPtr->data);
102 currentPtr = currentPtr->nextPtr;
103 }
104
105 printf("NULL\n\n");
106 }
...\source\repos\DynamicDataTypes\DynamicDataTypes\Stack.c 3
107 }
108
109 /* Is the stack empty? */
110 int isEmpty(StackNode_t* topPtr)
111 {
112 return topPtr == NULL;
113 }
114

You might also like