Untitled


SUBMITTED BY: becausemon01

DATE: June 26, 2017, 2:09 p.m.

FORMAT: Text only

SIZE: 1.3 kB

HITS: 707

  1. #include <iostream>
  2. using namespace std;
  3. struct Node
  4. {
  5. int data;
  6. Node* pNext;
  7. };
  8. struct list
  9. {
  10. Node* pHead;
  11. Node* pTail;
  12. };
  13. void khoaitao(list& l)
  14. {
  15. l.pHead=NULL;
  16. l.pTail=NULL;
  17. }
  18. Node* getNode(int x)
  19. {
  20. Node* p= new Node;
  21. if(p==NULL)
  22. return NULL;
  23. else {
  24. p->data=x;
  25. p->pNext=NULL;
  26. }
  27. }
  28. void addHead(list &l, Node* p)
  29. {
  30. if(l.pHead==NULL)
  31. l.pHead=l.pTail=p;
  32. else {
  33. p->pNext=l.pHead;
  34. l.pHead=p;
  35. }
  36. }
  37. void addTail(list &l, Node* p)
  38. {
  39. if(l.pHead==NULL)
  40. l.pHead=l.pTail=p;
  41. else {
  42. l.pTail->pNext=p;
  43. l.pTail=p;
  44. }
  45. }
  46. void addAfter(list &l, int x, int y)
  47. {
  48. Node* p;
  49. for(p=l.pHead;p!=NULL;p->pNext)
  50. {
  51. if(p->data==x)
  52. {
  53. Node* t=getNode(y);
  54. p->pNext=t->pNext;
  55. p->pNext=t;
  56. t->data=y;
  57. break;
  58. }
  59. else addHead(l,p);
  60. }
  61. }
  62. void xuat(list l)
  63. {
  64. for(Node* p=l.pHead;p!=NULL;p=p->pNext)
  65. {
  66. cout<<p->data;
  67. }
  68. }
  69. int main()
  70. {
  71. list l;
  72. khoaitao(l);
  73. int x;
  74. do{
  75. cin>>x;
  76. if(x==3)
  77. break;
  78. else if(x==0)
  79. {
  80. int a;
  81. cin>>a;
  82. Node* p;
  83. addHead(l, p =getNode(a));
  84. }
  85. else if(x==1)
  86. {
  87. int b;
  88. cin>>b;
  89. Node* p;
  90. addTail(l,p=getNode(b));
  91. }
  92. else if(x==2)
  93. {
  94. int c,d;
  95. cin>>c;
  96. cin>>d;
  97. addAfter(l,c,d);
  98. }
  99. }while(x!=3);
  100. xuat(l);
  101. return 0;
  102. }

comments powered by Disqus