-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackll.c
83 lines (73 loc) · 1.56 KB
/
stackll.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
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node *next;
}*top=NULL; //top is global variable
void lltrav(struct Node *ptr){
while(ptr!=NULL){
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
int isEmpty(struct Node *top){
if(top==NULL){
return 1;
}else{
return 0;
}
}
int isFull(struct Node *top){
struct Node *p=(struct Node *)malloc(sizeof(struct Node)); //if i cant allocate dynamic memory then it is full
if(p==NULL){
return 1;
}else{
return 0;
}
}
struct Node *push(struct Node *top,int x){
if(isFull(top)){
printf("stack overflow\n");
}else{
struct Node *n=(struct Node *)malloc(sizeof(struct Node));
n->data=x; //put the value x in n's data
n->next=top; //n's next becomes top
top=n; //top becomes n
return top;
}
};
int pop(){
struct Node *temp;
temp=top;
if(isEmpty(top)){
printf("stack underflow");
}
int val=temp->data;
top=top->next;
free(temp);
temp=NULL;
return val;
}
int peek(int pos){
struct Node *ptr = top;
for(int i = 0;i<pos-1 && ptr!=NULL;i++){
ptr=ptr->next;
}
if(ptr!=NULL){
return ptr->data;
}else{
return -1;
}
}
int main(){
top=push(top,78);
top=push(top,488);
top=push(top,45);
top=push(top,69);
lltrav(top);
int ele=pop();
printf("element popped is %d\n",ele);
lltrav(top);
printf("%d",peek(2));
return 0;
}