Advertisement
stsharin

Untitled

Apr 9th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct Node
  4. {
  5. int data;
  6. struct Node *next;
  7. } node;
  8. node* stack=NULL;
  9. void push(int data)
  10. {
  11. node*temp=(node*)malloc(sizeof(node));
  12. temp->data=data;
  13. temp->next=NULL;
  14. if(stack==NULL)
  15. {
  16. stack=temp;
  17. }
  18. else
  19. {
  20. node* current=stack;
  21. while(current->next!=NULL)
  22. {
  23. current=current->next;
  24. }
  25. current->next=temp;
  26. }
  27. }
  28. void pop()
  29. {
  30. node* current=stack;
  31. node* prev;
  32. if(stack==NULL)
  33. {
  34. printf("stack is empty!\n");
  35. return;
  36. }
  37. else
  38. {
  39. while(current->next!=NULL)
  40. {
  41. prev=current;
  42. current=current->next;
  43. }
  44. prev->next=current->next;
  45. free(current);
  46. }
  47. }
  48. void print()
  49. {
  50. node* current=stack;
  51. while(current!=NULL)
  52. {
  53. printf("%d ",current->data);
  54. current=current->next;
  55. }
  56. printf("\n");
  57.  
  58. }
  59.  
  60. int main()
  61. {
  62. pop();print();
  63. push(1);print();
  64. push(2);print();
  65. push(3);print();
  66. pop();print();
  67. push(5);print();
  68. pop();print();
  69. push(8);print();
  70. pop();print();
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement