SilverhandX

list2.cpp

Feb 24th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.01 KB | None | 0 0
  1. #include<iostream>
  2. #include "list.h"
  3. using namespace std;
  4.  
  5. void List::appendNode(int num)
  6. {
  7.     ListNode *newNode; //Points to new node
  8.     ListNode *nodePtr; //Moves through the list
  9.  
  10.     //Allocates new node and stores num there:
  11.     newNode = new ListNode;
  12.     newNode->value = num;
  13.     newNode->next = 0;
  14.  
  15.     //If there are no nodes in the list make newNode the first node:
  16.     if (!head)
  17.         head = newNode;
  18.     else //Else insert newNode at the end:
  19.     {
  20.         nodePtr = head; //Initializes nodePtr to head of the list
  21.  
  22.         while (nodePtr->next) //Finds the last node in the list
  23.             nodePtr = nodePtr->next;
  24.  
  25.         nodePtr->next = newNode; //Inserts newNode as the last node
  26.     }
  27. }
  28.  
  29. void List::insertNode(int num)
  30. {
  31.     ListNode *newNode; //Creates a new node
  32.     ListNode *nodePtr; //To traverse the list
  33.     ListNode *previousNode = 0; //The previous node
  34.  
  35.     //Allocates a new node and stores num there:
  36.     newNode = new ListNode;
  37.     newNode->value = num;
  38.  
  39.     //If there are no nodes in the list make newNode the first node
  40.     if(!head)
  41.     {
  42.         head = newNode;
  43.         newNode->next = 0;
  44.     }
  45.     else
  46.     {
  47.         nodePtr = head; //Position nodePtr at the head of the list
  48.         previousNode = 0;
  49.  
  50.         //Skip all nodes whose value is less than num
  51.         while (nodePtr != 0 && nodePtr->value < num)
  52.         {
  53.             previousNode = nodePtr;
  54.             nodePtr = nodePtr->next;
  55.         }
  56.  
  57.         //If the new node is the 1st in the list insert it before other nodes
  58.         if (previousNode == 0)
  59.         {
  60.             head = newNode;
  61.             newNode->next = nodePtr;
  62.         }
  63.         else
  64.         {
  65.             previousNode->next = newNode;
  66.             newNode->next = nodePtr;
  67.         }
  68.     }
  69. }
  70.  
  71. void List::insertSpecificNode(int num, int place)
  72. {
  73.     ListNode *newNode;
  74.     ListNode *nodePtr;
  75.     ListNode *previousNode = 0;
  76.  
  77.     newNode = new ListNode;
  78.     newNode->value = num;
  79.  
  80.     if(!head)
  81.     {
  82.         head = newNode;
  83.         newNode->next = 0;
  84.  
  85.         return;
  86.     }
  87.     else
  88.     {
  89.         nodePtr = head;
  90.         previousNode = 0;
  91.  
  92.         /*for (int x = 1; x <= place; x++)
  93.         {
  94.             previousNode = nodePtr;
  95.             nodePtr = nodePtr->next;
  96.         }*/
  97.         int x = 1;
  98.  
  99.         while (nodePtr != 0 && x != place)
  100.         {
  101.             x++;
  102.             previousNode = nodePtr;
  103.             nodePtr = nodePtr->next;
  104.         }
  105.  
  106.         if (previousNode == 0)
  107.         {
  108.             head = newNode;
  109.             newNode->next = nodePtr;
  110.         }
  111.         else
  112.         {
  113.             previousNode->next = newNode;
  114.             newNode->next = nodePtr;
  115.         }
  116.     }
  117. }
  118.  
  119. void List::deleteNode(int num)
  120. {
  121.     ListNode *nodePtr;
  122.     ListNode *previousNode;
  123.  
  124.     //If the list is empty, do nothing:
  125.     if (!head)
  126.         return;
  127.     //Determine if the first node is the one:
  128.     if (head->value == num)
  129.     {
  130.         nodePtr = head->next;
  131.         delete head;
  132.         head = nodePtr;
  133.     }
  134.     else
  135.     {
  136.         nodePtr = head;
  137.  
  138.         //Skips all nodes whose value member is not equal to num:
  139.         while (nodePtr != 0 && nodePtr->value != num)
  140.         {
  141.             previousNode = nodePtr;
  142.             nodePtr = nodePtr->next;
  143.         }
  144.         //If nodePtr is not at the end of the list link the previous node to the node after nodePtr, then delete nodePtr:
  145.         if (nodePtr)
  146.         {
  147.             previousNode->next = nodePtr->next;
  148.             delete nodePtr;
  149.         }
  150.     }
  151. }
  152.  
  153. void List::displayList()
  154. {
  155.     ListNode *nodePtr = head;
  156.  
  157.     while(nodePtr != 0)
  158.     {
  159.         cout << nodePtr->value;
  160.         if (nodePtr->next != 0)
  161.             cout << "-->";
  162.         nodePtr = nodePtr->next;
  163.     }
  164. }
  165.  
  166. List::~List()
  167. {
  168.     ListNode *nodePtr;
  169.     ListNode *nextNode;
  170.  
  171.     //Position nodePtr at the head of the list
  172.     nodePtr = head;
  173.  
  174.     //While nodePtr is not at the end of the list
  175.     while (nodePtr != 0)
  176.     {
  177.         //Save a pointer to the next node
  178.         nextNode = nodePtr->next;
  179.  
  180.         //Delete the current node
  181.         delete nodePtr;
  182.  
  183.         //Position nodePtr at the next node
  184.         nodePtr = nextNode;
  185.     }
  186. }
Add Comment
Please, Sign In to add comment