-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_ll.c
96 lines (95 loc) · 1.38 KB
/
stack_ll.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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *top=NULL;
void push(int x)
{
struct node *t=(struct node*)malloc(sizeof(struct node));
if(t==NULL)
printf("Stack overflow\n");
else
{
t->data=x;
t->next=top;
top=t;
}
}
int pop()
{
struct node *p;
int x=-1;
if(top==NULL)
printf("Underflow\n");
else
{
p=top;
top=top->next;
x=p->data;
free(p);
}
return x;
}
int peek(int pos)
{
struct node *p=top;
int i;
for(i=1;i<pos&&p!=NULL;i++)
{
p=p->next;
}
if(p)
return p->data;
else
return -1;
}
int stacktop()
{
if(top)
return top->data;
else
return -1;
}
int isfull()
{
struct node *t=(struct node*)malloc(sizeof(struct node));
int r;
if(t==NULL)
r= 1;
else
r= 0;
free(t);
return r;
}
int isempty()
{
if(top==NULL)
return 1;
else
return 0;// return top?0:1
}
void display()
{
struct node *p=top;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
void main()
{
int x,y,z;
push(3);
push(2);
push(1);
push(9);
display();
printf("\n%d",peek(2));
printf("\n%d",pop());
printf("\n%d",peek(2));
}