-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathd39_stack.c
More file actions
101 lines (92 loc) · 1.75 KB
/
d39_stack.c
File metadata and controls
101 lines (92 loc) · 1.75 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Dynamic stack using linked list
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct stack_node
{
int data;
struct stack_node *link;
} stack_node;
typedef struct stack
{
stack_node *tos;
int size;
} stack;
bool check_empty(stack *s)
{
return s->size == 0;
}
// initialize stack data structure
stack* create_stack()
{
stack *s = (stack*)malloc(sizeof(stack));
s->tos = NULL;
s->size = 0;
return s;
}
// primitive to push elements
void push(stack *s,int val)
{
stack_node *sn = (stack_node*)malloc(sizeof(stack_node));
sn->data = val;
sn->link = s->tos;
s->tos = sn;
s->size++;
}
// primitive to pop elements
int pop(stack *s)
{
int val = s->tos->data;
stack_node *temp = s->tos;
s->tos = s->tos->link;
free(temp);
s->size--;
return val;
}
// primitive to search
int search(stack *s,int key)
{
int node=0,n=s->size;
while(n != 0)
{
if(s->tos->data == key)
return s->size-node;
s->tos = s->tos->link;
node++;
n--;
}
return -1;
}
// primitive to reverse dyn stack
void reverse(stack *s)
{
int *temp = (int*)malloc(s->size*sizeof(int)),i=0;
while(!check_empty(s))
{
temp[i] = pop(s);
i++;
}
for(int j=0;j<i;j++)
{
push(s,temp[j]);
}
}
// driver code
int main(void)
{
stack *s = create_stack(); // initializing stack
// pushing elements into created stack
push(s,10);
push(s,20);
push(s,30);
// searching stack for element
printf("%d is located at %d\n",10,search(s,10));
// reversing stack
reverse(s); // turns stack into queue
while(!check_empty(s))
{
printf("%d ",pop(s));
}
printf("\n");
return 0;
}