-
Notifications
You must be signed in to change notification settings - Fork 12
/
stack_using_ll.cpp
104 lines (88 loc) · 1.49 KB
/
stack_using_ll.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
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
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node*next;
};
struct node*push(struct node*top,int data){
struct node*ptr=(struct node*)malloc(sizeof(struct node));
ptr->data=data;
ptr->next=top;
return ptr;
}
void transversal(struct node*top){
while(top!=NULL){
printf("%d\n",top->data);
top=top->next;
}
}
struct node*pop(struct node*top){
if(top==NULL){
printf("Stack Is Empty so you can't add node");
return 0;
}
else{
struct node*ptr=top;
top=top->next;
free(ptr);
return(top);
}
}
void is_empty(struct node*ptr){
if(ptr==NULL){
printf("stack is empty\n");
}
else{
printf("stack is not empty");
}
}
int stack_top(struct node*top){
if(top==NULL){
printf("stack is empty\n");
}
else{
return(top->data);
}
}
int stack_bottom(struct node*top){
if(top==NULL){
printf("stack is empty\n");
}
else{
while(top->next!=NULL){
top=top->next;
}
return(top->data);
}
}
int peak(struct node*top,int pos){
for(int i=0;i<pos-1&&top!=NULL;i++){
top=top->next;
}
if(top!=NULL){
return(top->data);
}
else{
return(-1);
}
}
int main(){
struct node* top;
top=push(top,5);
top=push(top,15);
top=push(top,25);
top=push(top,35);
top=push(top,45);
// top=pop(top);
// top=pop(top);
// top=push(top,6);
// transversal(top);
// top=pop(top);
// transversal(top);
// is_empty(top);
// printf("%d\n",stack_top(top));
// printf("%d\n",stack_bottom(top));
for (int i=1;i<=5;i++ ) {
printf("%d\n",peak(top,i));
}
}