-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhteva.c
95 lines (86 loc) · 2 KB
/
whteva.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
//stacks-all operations
#include<stdio.h>
#include<stdlib.h>
int size,top=-1;
int *a;
void push(){
int data;
printf("Enter value to insert: ");
scanf("%d",&data);
if(top==size-1)
printf("Stack is full\n");
else
a[++top]=data;
}
void pop(){//O(1)
if(top==-1)
printf("Stack is empty, cannot pop");
else{
printf("%d is popped",a[top]);
top--;
}
}
void peek(){
if(top==-1)
printf("Stack is empty\n");
else{
printf("%d element is the top element\n", a[top]);
}
}
void isEmpty(){
if(top==-1)
printf("Stack is empty\n");
else
printf("Stack is not empty\n");
}
void isFull(){
if(top==size-1)
printf("Stack is full\n");
else
printf("Stack is not full\n");
}
void display(){//O(n)
if(top==-1)
printf("Stack is empty");
else{
for(int i=0;i<=top;i++)
printf("%d\n",a[i]);
}
}
void count(){
printf("Number of elements in stack : %d\n",top+1);
}
void main(){
int choice;
printf("Enter size of stack: ");
scanf("%d",&size);
a=(int*)malloc(sizeof(int)*size);
while(1){
printf("\nOption 1- push\n");
printf("Option 2- pop\n");
printf("Option 3- peek\n");
printf("Option 4- isEmpty\n");
printf("Option 5- isFull\n");
printf("Option 6- Display\n");
printf("Option 7- count\n");
printf("\nEnter your choice[1-7]: ");
scanf("%d",&choice);
switch(choice){
case 1: push();
break;
case 2: pop();
break;
case 3: peek();
break;
case 4: isEmpty();
break;
case 5: isFull();
break;
case 6: display();
break;
case 7: count();
break;
default: exit(0);
}
}
}