apl-mhd

stack and queue

Apr 21st, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4.  
  5. struct list{
  6. int data;
  7. struct list *next;
  8.  
  9. };
  10. typedef struct list node;
  11. node *head;
  12.  
  13.  
  14. void insert(int x){
  15.  
  16.  
  17.  
  18. node *temp,*temp2;
  19.  
  20. if(head == NULL){
  21. head = new node();
  22. head->data = x;
  23. head->next = NULL;
  24. temp = head;
  25. }
  26. else{
  27.  
  28. temp2 = new node();
  29. temp2->data=x;
  30. temp2->next = NULL;
  31. temp->next = temp2;
  32. temp = temp2;
  33.  
  34. }
  35.  
  36.  
  37. }
  38.  
  39. int len(node *head){
  40. int count=0;
  41. while(head !=NULL){
  42.  
  43. count++;
  44. head = head->next;
  45. }
  46.  
  47. return count;
  48. }
  49.  
  50. void stackQ(char x ){
  51. node *temp = head, *temp2;
  52.  
  53. if(head->next == NULL)
  54. delete head;
  55.  
  56. else if(x== 'D'){
  57. head = head->next;
  58. delete temp;
  59.  
  60. }
  61. else{
  62.  
  63.  
  64. while(temp->next->next!=NULL){
  65. temp=temp->next;
  66. }
  67. temp2 = temp->next;
  68. temp->next = NULL;
  69.  
  70. delete temp2;
  71.  
  72. }
  73.  
  74.  
  75. }
  76.  
  77. void display(){
  78. node *temp = head;
  79. while(temp !=NULL){
  80. printf("%d ", temp->data);
  81. temp = temp->next;
  82.  
  83. }
  84. cout<<endl;
  85. }
  86.  
  87. bool empty(){
  88.  
  89. if( head == NULL)
  90. return true;
  91.  
  92. return false;
  93.  
  94. }
  95.  
  96. int main(int argc, char **argv)
  97. {
  98.  
  99. head = NULL;
  100.  
  101. insert(10);
  102. insert(20);
  103.  
  104. insert(30);
  105. insert(40);
  106.  
  107.  
  108. stackQ('x');
  109. display();
  110.  
  111. stackQ('D');
  112. display();
  113.  
  114.  
  115. return 0;
  116. }
Add Comment
Please, Sign In to add comment