-
Notifications
You must be signed in to change notification settings - Fork 2
/
stack.c
85 lines (74 loc) · 1.13 KB
/
stack.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
#include<stdio.h>
#include<stdlib.h>
struct stack{
int k;
struct stack *st;
};
struct stack *head=NULL;
#define m 10
void push(int n,int c);
void pop();
void display();
void createstack()
{
int choice=1; int n,i,k;
while(choice == 1)
{
printf("Enter your choice \n 1. Push \n 2. Pop \n 3. Display \n ");
scanf("%d",&n);
switch(n)
{
case 1:
printf("Enter the value to push into stack : ");
scanf("%d",&k);
int c=1;
push(k,c);
c++;
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
}
printf("Enter \n1. to continue \n0. to end : ");
scanf("%d",&choice);
}
}
void push(int data,int c)
{
struct stack *new = (struct stack*)malloc(sizeof(struct stack*));
if(c==m){
printf("stack overflow");}
else{
new->k=data;
new->st=head;
head=new;}
}
void pop()
{
struct stack *del= (struct stack*)malloc(sizeof(struct stack*));
del = head;
if(head==NULL)
printf("stack is empty\n");
else{
head=del->st;
del->st=NULL;
printf("%d deleted\n",del->k);
free(del);
}
}
void display()
{
struct stack *temp = (struct stack*)malloc(sizeof(struct stack*));
temp=head;
printf("Stack is : ");
while(temp!=NULL)
{
printf(" %d \n",temp->k);
temp=temp->st;
}
}