-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack3.cpp
54 lines (54 loc) · 956 Bytes
/
stack3.cpp
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
#include<bits/stdc++.h>
using namespace std;
struct stack_node
{
int value;
struct stack_node *next;
};
typedef struct stack_node stack_node;
struct stack1
{
stack_node *top;
int size;
};
typedef struct stack1 stack1;
stack1 *create_stack()
{
stack1 *s=(stack1 *)malloc(sizeof(stack1));
s->top=NULL;
s->size=0;
return s;
}
bool empty_stack(stack *s)
{
return s->size==0;
}
void push_stack(stack1 *s, int value)
{
stack_node *n=(stack_node *)malloc(sizeof(stack_node));
n->value=value;
n->next=s->top;
s->top=n;
s->size++;
}
int pop_stack(stack1* s)
{
int value=s->top->value;
stack_node *sn=s->top;
s->top=s->top->next;
s->size--;
free(sn);
return value;
}
int main()
{
stack1 *s=create_stack();
push_stack(s,3);
push_stack(s,7);
push_stack(s,1);
push_stack(s,9);
push_stack(s,6);
while(!empty_stack(s))
cout<<pop_stack(s);
return 0;
}