-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
75 lines (57 loc) · 1.28 KB
/
stack.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <stdlib.h>
struct Stack{
int count;
struct Node *top;
};
struct Node{
int value;
struct Node *next;
};
typedef struct Node *NODE_POINTER;
typedef struct Stack *STACK_POINTER;
NODE_POINTER createNode(){
NODE_POINTER newNode = (NODE_POINTER) malloc(sizeof(struct Node));
return newNode;
}
STACK_POINTER createStack(){
STACK_POINTER newStack = (STACK_POINTER) malloc(sizeof(struct Stack));
newStack->count = 0;
return newStack;
}
void push(STACK_POINTER stack,int value){
NODE_POINTER newNode = createNode();
newNode->value = value;
newNode->next = stack->top;
stack->top = newNode;
stack->count++;
}
void printStack(STACK_POINTER stack){
NODE_POINTER top = stack->top;
for(int i = 0;i < stack->count;i++){
printf(" %d ",top->value);
top = top->next;
}
}
int pop(STACK_POINTER stack){
NODE_POINTER top = stack->top;
int value = top->value;
if(top == NULL){
return -1;
}
stack->count--;
stack->top = top->next;
return value;
}
int main(int argc, char const *argv[])
{
STACK_POINTER A = createStack();
push(A,1);
push(A,3);
push(A,5);
//printf("%d",A->top->value);
printStack(A);
pop(A);
printStack(A);
return 0;
}