fork download
  1. #include<stdio.h>
  2.  
  3. struct Node
  4. {
  5. int data;
  6. struct Node *next;
  7. }*top = NULL;
  8.  
  9. void push(int);
  10. void pop();
  11. void display();
  12.  
  13. void main()
  14. {
  15. int choice, value;
  16. printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
  17. while(1){
  18. printf("Enter your choice: ");
  19. scanf("%d",&choice);
  20. switch(choice){
  21. case 1: printf("\nEnter value to insert: ");
  22. scanf("%d", &value);
  23. push(value);
  24. break;
  25. case 2: pop(); break;
  26. case 3: display(); break;
  27. case 4: exit(0);
  28. default: printf("\nWrong selection!!! \n");
  29. }
  30. }
  31. }
  32. void push(int value)
  33. {
  34. struct Node *newNode;
  35. newNode = (struct Node*)malloc(sizeof(struct Node));
  36. newNode->data = value;
  37. if(top == NULL)
  38. newNode->next = NULL;
  39. else
  40. newNode->next = top;
  41. top = newNode;
  42. printf("\nInsertion is Success!!!\n");
  43. }
  44. void pop()
  45. {
  46. if(top == NULL)
  47. printf("\nStack is Empty!!!\n");
  48. else{
  49. struct Node *temp = top;
  50. printf("\nDeleted element: %d", temp->data);
  51. top = temp->next;
  52. free(temp);
  53. }
  54. }
  55. void display()
  56. {
  57. if(top == NULL)
  58. printf("\nStack is Empty!!!\n");
  59. else{
  60. struct Node *temp = top;
  61. while(temp->next != NULL){
  62. printf("%d--->",temp->data);
  63. temp = temp -> next;
  64. }
  65. printf("%d",temp->data);
  66. }
  67. }
Success #stdin #stdout 0s 9432KB
stdin
1
55
3
1
66
3
2
3
4
stdout
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 
Enter value to insert: 
Insertion is Success!!!
Enter your choice: 55Enter your choice: 
Enter value to insert: 
Insertion is Success!!!
Enter your choice: 66--->55Enter your choice: 
Deleted element: 66Enter your choice: 55Enter your choice: