Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct List
  6. {
  7. int info;
  8. List *next;
  9. };
  10. List *First;
  11. List *Last;
  12. List *helper;
  13. void Create()
  14. {
  15. First=NULL;
  16. Last=NULL;
  17. }
  18. void insertFirst(int x)
  19. {
  20. helper=new List;
  21. helper->info=x;
  22. if(First==NULL)
  23. {
  24. First=helper;
  25. Last=helper;
  26. First->next=NULL;
  27. Last->next=NULL;
  28. }
  29. else
  30. {
  31. helper->next=First;
  32. First=helper;
  33. }
  34. }
  35. void insertLast(int x)
  36. {
  37. helper=new List;
  38. helper->info=x;
  39. helper->next=NULL;
  40. if(First==NULL)
  41. {
  42. First=helper;
  43. Last=helper;
  44. First->next=NULL;
  45. Last->next=NULL;
  46. }
  47. else
  48. {
  49. Last->next=helper;
  50. Last=helper;
  51. }
  52. }
  53. void insertAfter(int key, int x)
  54. {
  55. List *looker=new List;
  56. looker=First;
  57. helper=new List;
  58. helper->info=x;
  59. if(First==NULL)
  60. {
  61. cout<<"Error"<<endl;
  62. }
  63. else if(First==Last)
  64. {
  65. if(First->info!=key)
  66. {
  67. cout<<"Error"<<endl;
  68. }
  69. else
  70. {
  71. First->next=helper;
  72. Last=helper;
  73. Last->next=NULL;
  74. }
  75. }
  76. else
  77. {
  78. while(looker!=NULL)
  79. {
  80. if(looker->info==key)
  81. {
  82. helper->next=looker->next;
  83. looker->next=helper;
  84. break;
  85. }
  86. looker=looker->next;
  87. }
  88. }
  89.  
  90. }
  91. void display()
  92. {
  93. List *helper=new List;
  94. helper=First;
  95. while(helper!=NULL)
  96. {
  97. cout<<helper->info<<" ";
  98. helper=helper->next;
  99. }
  100. cout<<"Wcisnij dowolny klawisz, aby kontynuowac"<<endl;
  101. cin.get();
  102. cin.get();
  103.  
  104. }
  105.  
  106. int main()
  107. {
  108. int choice=1000;;
  109. while(choice!=0)
  110. {
  111.  
  112. cout<<"============================================="<<endl;
  113. cout<<"MENU"<<endl;
  114. cout<<"============================================="<<endl;
  115. cout<<"0. Wyjscie z programu"<<endl;
  116. cout<<"1. Tworzenie listy jednokierunkowej"<<endl;
  117. cout<<"2. Wstawianie elementu na poczatek listy"<<endl;
  118. cout<<"3. Wstawianie elementu na koniec listy"<<endl;
  119. cout<<"4. Wstawianie elementu po zadanym na liste"<<endl;
  120. cout<<"5. Wyswietlanie zawartosci listy"<<endl;
  121. cin>>choice;
  122.  
  123. if(choice==1)
  124. {
  125. Create();
  126. }
  127. else if(choice==2)
  128. {
  129. cout<<"Podaj liczbe"<<endl;
  130. int x;
  131. cin>>x;
  132. insertFirst(x);
  133. }
  134. else if(choice==3)
  135. {
  136. cout<<"Podaj liczbe"<<endl;
  137. int x;
  138. cin>>x;
  139. insertLast(x);
  140. }
  141. else if(choice==4)
  142. {
  143. cout<<"Podaj element, po ktorym wstawisz"<<endl;
  144. int key;
  145. cin>>key;
  146. cout<<endl;
  147. cout<<"Podaj liczbe"<<endl;
  148. int x;
  149. cin>>x;
  150.  
  151. insertAfter(key,x);
  152. }
  153. else if(choice==5)
  154. {
  155. display();
  156. }
  157. else if(choice==0)
  158. break;
  159. system("cls");
  160. }
  161.  
  162. return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement