-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_linklink1.c
119 lines (111 loc) · 1.97 KB
/
stack_linklink1.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
#include <stdio.h>
#include<stdlib.h>
struct stack{
int ptr;
struct stack *top;
};
int is_empty(struct stack *arr) {
if(arr == NULL)
return 1;
return 0;
}
void create(int *ptr){
ptr = (int * )malloc(sizeof(int));
if(ptr == NULL){
printf("no memory\n");
}
}
struct stack* push(struct stack* arr,int data){
struct stack* temp;
temp = (struct stack *)malloc(sizeof(struct stack));
if(temp == NULL){
printf("no memory available\n");
return NULL;
}
else {
temp->ptr = data;
temp->top = arr;
return temp;
}
}
struct stack* pop(struct stack* arr){
struct stack* temp;
if(is_empty(arr)){
printf("no element\n");
return NULL;
}
else {
temp = arr;
printf("deleted top element is : %d\n",(temp->ptr));
if(arr->top == NULL)
return NULL;
arr=arr->top;
free(temp);
return arr;
}
}
void peek(struct stack* arr){
struct stack* temp=arr;
if(is_empty(arr)){
printf("no element\n");
}
else{
temp = arr;
printf("top element is : %d\n",(temp->ptr));
}
}
void length(struct stack *arr){
int c=0;
struct stack* pttr=arr;
if(is_empty(arr)){
printf("no element\n");
}
while(pttr->top != NULL){
c++;
pttr = pttr->top;
}
printf("length is : %d\n",c+1);
}
void display(struct stack *arr){
struct stack* pttr=arr;
if(is_empty(arr)){
printf("no element\n");
}
while(pttr != NULL){
printf("%d ",(pttr->ptr));
pttr = pttr->top;
}
printf("\n");
}
int main(){
struct stack *array;
array = NULL;
//array = (struct stack *)malloc(sizeof(struct stack));
int i,data,item,choice;
printf("enter choice:\t1.push\t2.pop\t3.display\t4.length of stack\t5.top element\t6.exit\n");
while(1) {
printf("enter choice:\t");
scanf("%d",&choice);
switch(choice){
case 1:
scanf("%d",&data);
array = push(array,data);
break;
case 2:
array = pop(array);
break;
case 3:
display(array);
break;
case 4:
length(array);
break;
case 5:
peek(array);
break;
default :
exit(0);
}
}
return 0;
}