-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstacks.c
157 lines (138 loc) · 2.29 KB
/
stacks.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include<stdio.h>
#include<stdlib.h>
//STACKS USING ARRAY
// int stack[5];
// int top=-1;
// void push()
// {
// printf("push function\n");
// int n;
// scanf("%d",&n);
// if(top==4)
// {
// printf("overfloww\n");
// }
// else
// {
// top++;
// stack[top]=n;
// }
// }
// void display()
// {
// for(int i=top;i>-1;i--)
// {
// printf("%d ",stack[i]);
// }
// }
// void pop()
// {
// printf("pop function \n");
// if(top==-1)
// {
// printf("function underflow \n");
// return;
// }
// else
// {
// int popped;
// popped=stack[top];
// top--;
// printf("%d item deleted \n",popped);
// }
// }
// void peek()
// {
// printf("top most element \n");
// if(top==-1)
// {
// printf("stack is empty\n");
// }
// else
// {
// printf("%d is the top most element \n",stack[top]);
// }
// }
// int main()
// {
// push();
// push();
// push();
// peek();
// push();
// pop();
// display();
// }
//STACKS USING LINKED LIST
struct nodes
{
int input;
int* next;
};
struct nodes *top=0;
void push(int n)
{
struct nodes *newnode;
newnode=(struct nodes*)malloc(sizeof(struct nodes));
newnode->input=n;
newnode->next=top;
top=newnode;
}
void display()
{
struct nodes *temp;
temp=top;
if(top!=0)
{
while(temp!=0)
{
printf("%d ",temp->input);
temp=temp->next;
}
}
else
{
printf("stack is empty");
}
printf("\n");
}
void peek()
{
if(top==0)
{
printf("stack empty ");
}
else
{
printf("%d ",top->input);
}
printf("\n");
}
void pop()
{
struct nodes *temp;
temp=top;
if(top==0)
{
printf("stack is empty \n");
}
else
{
printf("element popped %d \n",top->input);
top=top->next;
free(temp);
}
}
int main()
{
push(6);
push(10);
peek();
push(100);
push(20);
push(7);
display();
pop();
peek();
display();
}