Advertisement
Lucky134Lucky

Untitled

Mar 24th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Node{
  6. public:
  7. friend class LinkedList;
  8. Node(int value){
  9. data = value;
  10. }
  11.  
  12. private:
  13. int data = NULL;
  14. Node* next = 0;
  15.  
  16. };
  17. class LinkedList{
  18. public:
  19.  
  20. void unshift(int value){
  21. current=first;
  22. Node* newPtr = new Node(value);
  23. if (current == 0){
  24. first = newPtr;
  25. last = newPtr;
  26. }
  27. else {
  28. newPtr -> next = first;
  29. first = newPtr;
  30. }
  31. }
  32. int shift(){
  33. current = first;
  34. if (current == 0)
  35. return 0;
  36.  
  37. if (first == last){
  38. int tempData = first->data;
  39. delete first;
  40. first = last = current = 0;
  41. return tempData;
  42. }
  43. else {
  44. Node* tempPtr = first;
  45. first = first->next;
  46. int tempData = tempPtr->data;
  47. delete tempPtr;
  48. return tempData;
  49. }
  50. }
  51. void push(int value){
  52. current = first;
  53. Node* newPtr = new Node(value);
  54. if (current == 0){
  55.  
  56. first = last = newPtr;
  57. }
  58. else{
  59. last->next = newPtr;
  60. last = newPtr;
  61. }
  62. }
  63. int pop(){
  64. current = first;
  65. if (current == 0)
  66. return 0;
  67.  
  68. if (first == last){
  69. int tempData = first->data;
  70. delete first;
  71. first = last = 0;
  72. return tempData;
  73. }
  74. else{
  75.  
  76. while(current->next != last)
  77. current=current->next;
  78.  
  79. current->next=0;
  80.  
  81. Node* tempPtr = last;
  82. last = current;
  83. int tempData = tempPtr->data;
  84. delete tempPtr;
  85. return tempData;
  86. }
  87. }
  88. void insert(int position, int value){
  89.  
  90. Node* newPtr = new Node(value);
  91. current=first;
  92.  
  93. for(int i = 1; i < position; i++) {
  94.  
  95. if (current == 0) {
  96. cout<<"Ne mogu vstavit"<<endl;
  97. return;
  98. }
  99. current=current->next;
  100. }
  101.  
  102. newPtr->next = current->next;
  103. current->next=newPtr;
  104.  
  105. }
  106. void showList(){
  107. current = first;
  108. while (current!=0){
  109. cout<<current->data<<' ';
  110. current = current->next;
  111. }
  112.  
  113. }
  114. ~LinkedList(){
  115. current = first;
  116. Node* tempPtr;
  117.  
  118. while (current!=0){
  119. tempPtr = current;
  120. current = current->next;
  121. delete tempPtr;
  122. }
  123. }
  124. private:
  125. Node* first = 0;
  126. Node* last = 0;
  127. Node* current = 0;
  128. };
  129.  
  130. int main(){
  131. LinkedList d;
  132. int x = -1;
  133. int value;
  134. int position;
  135.  
  136. cout << "1 dlya vstaski v nachalo" << endl
  137. << "2 dlya vzyatiya s nachala" << endl
  138. << "3 dlya vstavki v konec" << endl
  139. << "4 dlya vzatia s konca" << endl
  140. << "5 dlya vstavki v seredinu" << endl
  141. << "0 dlya vihoda" << endl;
  142.  
  143. while (x !=0){
  144. cout<<endl<<"Vvedi nomer ";
  145. cin>>x;
  146. switch(x){
  147. case 1: cout << "vvedi chislo ";
  148. cin >> value;
  149. d.unshift(value); break;
  150. case 2: cout<<d.shift()<<endl;break;
  151.  
  152. case 3: cout << "vvedi chislo ";
  153. cin >> value;
  154. d.push(value); break;
  155.  
  156. case 4: cout<<d.pop()<<endl;break;
  157.  
  158. case 5: cout << "vvedi positsiyu: "; cin >> position;
  159. cout << "vvedi chislo: "; cin >> value; d.insert(position, value);break;
  160. }
  161. cout << "Spisok seychas: "; d.showList();
  162. }
  163. return 0;
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement