Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. using namespace std;
  2. #include <iostream>
  3. //###INSERT CODE HERE -
  4. struct NODE
  5. {
  6. int info;
  7. NODE* pNext;
  8. };
  9. struct LIST
  10. {
  11. NODE* pHead;
  12. NODE* pTail;
  13. };
  14. void CreateList(LIST &L);
  15. NODE* CreateNode(int x)
  16. {
  17. NODE *p;
  18. p = new NODE;
  19. p->info = x;
  20. p->pNext = NULL;
  21. return p;
  22. };
  23. void addTail(LIST &L)
  24. {
  25. int x;
  26. cin>>x;
  27. NODE *p = CreateNode(x);
  28. if(L.pHead == NULL)
  29. {
  30. L.pHead = p;
  31. L.pTail = p;
  32. }
  33. else {
  34. L.pTail->pNext = p;
  35. L.pTail = p;
  36. };
  37. CreateList(L);
  38. };
  39. void addHead(LIST &L)
  40. {
  41. int x;
  42. cin>>x;
  43. NODE *p = CreateNode(x);
  44. if(L.pHead == NULL)
  45. {
  46. L.pHead = p;
  47. L.pTail = p;
  48. }
  49. else {
  50. p->pNext = L.pHead;
  51. L.pHead = p;
  52. };
  53. CreateList(L);
  54. };
  55. void add_afterX_1(LIST &L){
  56. int Y,X;
  57. cin>>X>>Y;
  58. NODE* p = L.pHead;
  59. while(p!=NULL && p->info !=X){
  60. p=p->pNext;// pinfo = 3
  61. }
  62. if(p!=NULL && p->info == X){
  63. NODE* q = CreateNode(Y);
  64. if(L.pTail == p){
  65. L.pTail->pNext = q;
  66. L.pTail = q;// 1 2 3 0 4 5
  67. }
  68. else {
  69. q->pNext = p->pNext;
  70. p->pNext = q;
  71. }
  72. }
  73. CreateList(L);
  74. }
  75. void CreateList(LIST &L) {
  76. int a;
  77. cin>>a;
  78. if(a!=-1){
  79. if(a==0){
  80. addHead(L);
  81. //CreateList(L);
  82. }
  83. if(a==1){
  84. addTail(L);
  85. //CreateList(L);
  86. }
  87. if(a==2){
  88. add_afterX_1(L);
  89. //CreateList(L);
  90. }
  91. }
  92. }
  93. void PrintList(LIST L){
  94. NODE *head=L.pHead;
  95. if(head==NULL)
  96. cout<<"Empty List.";
  97. while(head!=NULL){
  98. cout<<head->info<<" ";
  99. head = head->pNext;
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement