Advertisement
Guest User

Untitled

a guest
May 20th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct node
  6. {
  7. int data;
  8. struct node *link;
  9. };
  10.  
  11. struct node *top=NULL;
  12.  
  13.  
  14. void push(int x)
  15. {
  16. struct node *N = new node;
  17. N->data=x;
  18. N->link=NULL;
  19.  
  20. if(top==NULL)
  21. {
  22. top=N;
  23. }
  24. else
  25. {
  26. N->link=top;
  27. top=N;
  28. }
  29. }
  30.  
  31.  
  32. void pop()
  33. {
  34. if(top==NULL)
  35. {
  36. cout<<"There's no values in the list"<<endl;
  37. }
  38. else
  39. {
  40. top=top->link;
  41.  
  42. }
  43. }
  44.  
  45. void display()
  46. {
  47. struct node *t =top;
  48. while(t->link!=NULL)
  49. {
  50. cout<<t->data<<"->";
  51. t=t->link;
  52. }
  53. cout<<t->data<<endl;
  54. }
  55.  
  56. int main()
  57. {
  58. push(10);
  59. push(20);
  60. push(30);
  61. display();
  62. pop();
  63. display();
  64.  
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement