-
Notifications
You must be signed in to change notification settings - Fork 12
/
P52LLStackReverseNum.c
60 lines (51 loc) · 1 KB
/
P52LLStackReverseNum.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Program P5.2
#include <stdio.h>
#include <stdlib.h>
#define RogueValue -9999
typedef struct node {
int num;
struct node *next;
} Node, *NodePtr;
typedef struct stackType {
NodePtr top;
} StackType, *Stack;
int main() {
Stack initStack();
int empty(Stack);
void push(Stack, int);
int pop(Stack);
int n;
Stack S = initStack();
printf("Enter some integers, ending with 0\n");
scanf("%d", &n);
while (n != 0) {
push(S, n);
scanf("%d", &n);
}
printf("Numbers in reverse order\n");
while (!empty(S))
printf("%d ", pop(S));
printf("\n");
} //end main
Stack initStack() {
Stack sp = (Stack) malloc(sizeof(StackType));
sp -> top = NULL;
return sp;
}
int empty(Stack S) {
return (S -> top == NULL);
}
void push(Stack S, int n) {
NodePtr np = (NodePtr) malloc(sizeof(Node));
np -> num = n;
np -> next = S -> top;
S -> top = np;
}
int pop(Stack S) {
if (empty(S)) return RogueValue;
int hold = S -> top -> num;
NodePtr temp = S -> top;
S -> top = S -> top -> next;
free(temp);
return hold;
}