-
Notifications
You must be signed in to change notification settings - Fork 20
/
RUBYSTAC.C
85 lines (84 loc) · 1.73 KB
/
RUBYSTAC.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
//stack
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct stack
{ int bno;
char bnm[25];
struct stack *next;
};
struct stack *top,*newptr,*ptr;
void display()
{ if(top==NULL)
printf("\n Stack is empty ");
else
{ printf("\n Stack is ");
ptr=top;
printf("\n -> ");
while(ptr)
{ printf("%d",ptr->bno);
printf("%s ",ptr->bnm);
printf("\n ");
ptr=ptr->next;
}
}
}
void push()
{ newptr = (struct stack*)malloc(sizeof(struct stack));
if(newptr==NULL)
{ printf("\n Memory overflow ");
}
else
{ printf("\n Enter book number = ");
scanf("%d",&newptr->bno);
printf("\n Enter book name = ");
scanf("%s",&newptr->bnm);
newptr->next=NULL;
if(top==NULL)
top=newptr;
else
{ newptr->next=top;
top=newptr;
}
printf("\n Stack after push : \n ");
display();
}
}
void pop()
{ if(top==NULL)
printf("\n Memory underflow stack is empty ");
else
{ ptr=top;
top=top->next;
free(ptr);
printf("\n Stack after pop : \n ");
display();
}
}
void main()
{ int ch;
top=NULL;
do
{ clrscr();
printf("\n MENU ");
printf("\n 1.Push in linked list stack ");
printf("\n 2.Pop in linked list stack ");
printf("\n 3. Traverse the stack ");
printf("\n 4. Exit ");
printf("\n Enter choice (1-4) = ");
scanf("%d",&ch);
switch(ch)
{ case 1 : push();
break;
case 2 : pop();
break;
case 3 : display();
break;
case 4 : printf("\n Terminating ");
break;
default:printf("\n Wrong Choice ");
break;
}
getch();
}while(ch!=4);
}