Advertisement
Lucky134Lucky

Untitled

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