You are on page 1of 1

1 #include <stdio.

h>
2 #include <stdlib.h>
3 struct node
4 {
5 int info;
6 struct node *ptr;
7 }*top,*temp;
8
9 void push(int data);
10 void pop();
11 int count = 0;
12
13 void main()
14 {
15 int no, ch;
16 printf("1. Push\n");
17 printf("2. Pop\n");
18 printf("3. Exit\n");
19 while (1)
20 {
21 printf("\n Enter choice: ");
22 scanf("%d", &ch);
23 switch (ch)
24 {
25 case 1:
26 printf("Enter data to add to stack ");
27 scanf("%d", &no);
28 push(no);
29 break;
30 case 2:
31 pop();
32 break;
33 case 3:
34 exit(0);
35 default :
36 printf(" Invalid choice");
37 break;
38 }
39 }
40 }
41 void push(int data)
42 {
43 if (top == NULL){
44 top =(struct node *)malloc(1*sizeof(struct node));
45 top->ptr = NULL;
46 top->info = data;
47 }
48 else{
49 temp =(struct node *)malloc(1*sizeof(struct node));
50 temp->ptr = top;
51 temp->info = data;
52 top = temp;
53 }
54 count++;
55 }
56 void pop()
57 {
58 temp = top;
59 if (temp == NULL)
60 {
61 printf("\n stack is empty");
62 return;
63 }
64 else
65 temp = temp->ptr;
66
67 printf("\n Popped value from stack is : %d", top->info);
68 free(top);
69 top = temp;
70 count--;
71 }
72

You might also like