Advertisement
Naimul_X

Untitled

Mar 3rd, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. struct list{
  4.  
  5. int data;
  6. struct list *next;
  7. };
  8. typedef struct list node;
  9.  
  10. node *top=NULL;
  11.  
  12. int peek()
  13. {
  14. if(top==NULL)
  15. {
  16. printf("STACK IS EMPTY\n");
  17. return -1;
  18. }
  19. return top->data;
  20.  
  21. }
  22. void display()
  23. {
  24. node *temp;
  25. temp=top;
  26. if(top==NULL)
  27. {
  28. printf("STACK IS EMPTY\n");
  29. }
  30. else{
  31. while(temp!=NULL)
  32. {
  33. printf("ELEMENTS ARE:%d ",temp->data);
  34. temp=temp->next;
  35. }
  36. }
  37.  
  38. }
  39.  
  40. void push(int item)
  41. {
  42. node *new_node;
  43. new_node = new node();
  44.  
  45. new_node->data=item;
  46. new_node->next=top;
  47. top=new_node;
  48. }
  49. void pop()
  50. {
  51. if(top==NULL)
  52. {
  53. printf("STACK IS EMPTY\n");
  54. }
  55. else{
  56. top=top->next;
  57. }
  58. }
  59.  
  60.  
  61. int main()
  62. {
  63. push(5);
  64. push(10);
  65. push(20);
  66. int x=peek();
  67. printf("TOP IS : %d ",x);
  68. printf("\n");
  69. display();
  70. pop();
  71. pop();
  72. printf("\n");
  73. display();
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement