Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. #include "LinkedList.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. LinkedList::LinkedList() //constructor
  7. {
  8. head = NULL;
  9. tail = NULL;
  10. }
  11. LinkedList::~LinkedList() //deconstructor
  12. {
  13. node_t* temp = tail;
  14. if(temp != NULL){
  15. while(temp->next != NULL){
  16. temp = temp->next;
  17. delete[] temp->prev;
  18. }
  19. }
  20. delete[] temp;
  21. head = NULL;
  22. tail = NULL;
  23.  
  24. }
  25. LinkedList::LinkedList(const LinkedList& src) //constructor som tar en länkad lista
  26. {
  27. head = NULL;
  28. tail = NULL;
  29. node_t* temp = src.tail;
  30. while(temp != NULL){
  31. push_front(temp->value);
  32. temp = temp->next;
  33. }
  34. }
  35. LinkedList& LinkedList::operator=(const LinkedList& rhs) //operatoröverlagring som kopierar länkad lista
  36. {
  37. delete this;
  38. node_t* temp = rhs.tail;
  39. if(temp != NULL){
  40. while(temp != NULL){
  41. push_front(temp->value);
  42. temp = temp->next;
  43. }
  44. }
  45.  
  46. }
  47. LinkedList& LinkedList::operator+=(const LinkedList& rhs) //operatoröverlagring som adderar en länkad lista
  48. {
  49. node_t* temp = rhs.tail;
  50. if(temp != NULL){
  51. while(temp != NULL){
  52. push_back(temp->value);
  53. temp = temp->next;
  54. }
  55. }
  56. }
  57.  
  58. void LinkedList::insert(double value, size_t pos) //linkedlist funktion som stoppar in value i position
  59. {
  60. if(is_empty() || pos >= size()){
  61. push_front(value);
  62. }
  63. else if(pos == 0 && !is_empty()){
  64. push_back(value);
  65. }
  66. else if(pos < size()){
  67. node_t* insertNode = new node_t;
  68. insertNode->value = value;
  69. node_t* temp = tail;
  70. for(int i = 0; i < pos; i++){
  71. temp = temp->next;
  72. }
  73. insertNode->prev = temp->prev;
  74. insertNode->next = temp;
  75. temp->prev->next = insertNode;
  76. }
  77. }
  78. void LinkedList::push_front(double value) //linkedlist funktion som pushar value i början av listan
  79. {
  80. node_t* insertNode = new node_t;
  81. insertNode->value = value;
  82. if(head != NULL){
  83. insertNode->prev = head;
  84. head->next = insertNode;
  85. head = insertNode;
  86. }
  87. else{
  88. head = insertNode;
  89. tail = insertNode;
  90. }
  91. }
  92. void LinkedList::push_back(double value) //linkedlist funktion som pushbackar ett value
  93. {
  94. node_t* insertNode = new node_t;
  95. insertNode->value = value;
  96. if(tail != NULL){
  97. insertNode->next = tail;
  98. tail->prev = insertNode;
  99. tail = insertNode;
  100. }
  101. else{
  102. head = insertNode;
  103. tail = insertNode;
  104. }
  105. }
  106. //Access
  107. double LinkedList::front() const //linkedlistfunktion som kollar vad som ligger längst fram i head
  108. {
  109. if(head != NULL){
  110. return head->value;
  111. }
  112. }
  113. double LinkedList::back() const //linkedlistfunktion som kollar vad som ligger längst bak i tail
  114. {
  115. if(tail != NULL){
  116. return tail->value;
  117. }
  118. }
  119. double LinkedList::at(size_t pos) const //linkedlistfunktion som kollar värdet på en position i listan
  120. {
  121. if(!is_empty() && pos < size()){
  122. node_t* temp = tail;
  123. for(int i = 0; i < pos; i++){
  124. temp = temp->next;
  125. }
  126. return temp->value;
  127. }
  128. }
  129. //remove
  130. void LinkedList::remove(size_t pos) //linkedlistfunktion som tar bort en angiven position i listan
  131. {
  132. if(pos == size()-1){
  133. pop_front();
  134. }
  135. else if(!is_empty() && pos < size()){
  136. node_t* temp = tail;
  137. for(int i = 0; i < pos; i++){
  138. temp = temp->next;
  139. }
  140. temp->prev->next = temp->next;
  141. temp->next->prev = temp->prev;
  142. delete temp;
  143. }
  144. }
  145. //Remove and access
  146. double LinkedList::pop_back() //linkedlist funktion som poppar värdet som ligger i tail
  147. {
  148. //If tail is null the list is empty
  149. if(tail != NULL){
  150. node_t* temp = tail; //Temp copy of tail node to keep for deletion
  151. double returnValue = tail->value; //Value of the node to return when popped
  152. if(tail->next == NULL){ //If tail next is NULL this is the only element in the list
  153. tail = NULL; //Set the head and tail pointers to NULL
  154. head = NULL;
  155. return returnValue; //Return the value
  156. }
  157. tail = tail->next; //If there is more elements after the tail node make the next one the new tail
  158. tail->prev = NULL; //Make sure to set this new tail nodes next to NULL
  159. delete[] temp; //Delete the old tail node
  160. return returnValue; //Return the value
  161. }
  162. }
  163. double LinkedList::pop_front() //linkedlist funktion som poppar värdet som ligger i head
  164. {
  165. if(head != NULL){
  166. node_t* temp = head;
  167. double returnValue = head->value;
  168. if(head->prev == NULL){
  169. delete[] head;
  170. tail = NULL;
  171. head = NULL;
  172. return returnValue;
  173. }
  174. head = head->prev;
  175. head->next = NULL;
  176. delete[] temp;
  177. return returnValue;
  178. }
  179. }
  180. //Informational
  181. size_t LinkedList::size() const
  182. {
  183. if(!is_empty()){
  184. size_t counter = 1;
  185. node_t* temp = tail;
  186. while(temp->next != NULL){
  187. temp = temp->next;
  188. counter++;
  189. }
  190. return counter;
  191. }
  192. return 0;
  193.  
  194. }
  195. bool LinkedList::is_empty() const
  196. {
  197. if(head == NULL){
  198. return true;
  199. }
  200. else{
  201. return false;
  202. }
  203. }
  204. //Output
  205. void LinkedList::print() const
  206. {
  207. if(!is_empty()){
  208. node_t* temp = tail;
  209. while(temp->next != NULL){
  210. cout << temp->value << ", ";
  211. temp = temp->next;
  212. }
  213. cout << temp->value << endl;
  214. }
  215. }
  216. void LinkedList::printReverse() const
  217. {
  218. if(!is_empty()){
  219. node_t* temp = head;
  220. while(temp->prev != NULL){
  221. cout << temp->value << ", ";
  222. temp = temp->prev;
  223. }
  224. cout << temp->value << endl;
  225. }
  226. }
  227. bool LinkedList::sort_check(){
  228. node_t* temp = tail;
  229. while(temp->next != NULL){
  230. if(temp->value > temp->next->value){
  231. return false;
  232. }
  233. temp = temp->next;
  234. }
  235. return true;
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement