Advertisement
erfanul007

Stack Linked list

Nov 30th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct std
  4. {
  5. int id;
  6. struct std *next;
  7. struct std *prev;
  8. }*start=NULL,*end=NULL,*top=NULL;
  9.  
  10. void f_insert()
  11. {
  12. struct std *newnode,*current;
  13. newnode=(struct std*)malloc(sizeof(struct std));
  14. printf("Enter the ID: ");
  15. scanf("%d",&newnode->id);
  16. newnode->next=NULL;
  17. newnode->prev=NULL;
  18. current=start;
  19. if(start==NULL){
  20. start=newnode;
  21. end=newnode;
  22. }
  23. else{
  24. newnode->next=current;
  25. current->prev=newnode;
  26. current=current->prev;
  27. start=current;
  28. }
  29. top=start;
  30. printf("POP successful\n");
  31. }
  32.  
  33. void count()
  34. {
  35. struct std *current;
  36. int count=0;
  37. current=start;
  38. if(start==NULL)
  39. printf("stack is empty\n");
  40. else{
  41. while(current!=NULL)
  42. {
  43. count++;
  44. current=current->next;
  45. }
  46. printf("\nThere are %d number of nodes.\n\n",count);
  47. }
  48. }
  49.  
  50. void l_delete()
  51. {
  52. struct std *current,*temp,*temp1,*temp2;
  53. current=start;
  54. if(start==NULL)
  55. printf("stack is empty\n");
  56. else if(start->next==NULL){
  57. start=NULL;
  58. end=NULL;
  59. top=start;
  60. }
  61. else{
  62. start=current->next;
  63. (current->next)->prev=NULL;
  64. top=start;
  65. }
  66. printf("\ndeleted successfully\nThe element is %d\n",current->id);
  67. }
  68.  
  69. void display()
  70. {
  71. struct std *current;
  72. current=start;
  73. if(start==NULL)
  74. printf("stack is empty\n");
  75. else{
  76. count();
  77. printf("\nThe Top value is : %d\n",top->id);
  78. printf("\nThe link list by forward display:\n");
  79. while(current!=NULL)
  80. {
  81. printf("%d",current->id);
  82. if(current->next!=NULL)
  83. printf("-->");
  84. current=current->next;
  85. }
  86. printf("\n");
  87. }
  88. }
  89.  
  90. int main()
  91. {
  92. while(1){
  93. int n;
  94. printf("\n1. push\n2. Pop\n3. display\nchose: ");
  95. scanf("%d",&n);
  96. if(n==1)
  97. f_insert();
  98. else if(n==2)
  99. l_delete();
  100. else if(n==3)
  101. display();
  102. }
  103. return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement