SilverhandX

list.cpp

Feb 22nd, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 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::deleteNode(int num)
  72. {
  73.     ListNode *nodePtr;
  74.     ListNode *previousNode;
  75.  
  76.     //If the list is empty, do nothing:
  77.     if (!head)
  78.         return;
  79.     //Determine if the first node is the one:
  80.     if (head->value == num)
  81.     {
  82.         nodePtr = head->next;
  83.         delete head;
  84.         head = nodePtr;
  85.     }
  86.     else
  87.     {
  88.         nodePtr = head;
  89.  
  90.         //Skips all nodes whose value member is not equal to num:
  91.         while (nodePtr != 0 && nodePtr->value != num)
  92.         {
  93.             previousNode = nodePtr;
  94.             nodePtr = nodePtr->next;
  95.         }
  96.         //If nodePtr is not at the end of the list link the previous node to the node after nodePtr, then delete nodePtr:
  97.         if (nodePtr)
  98.         {
  99.             previousNode->next = nodePtr->next;
  100.             delete nodePtr;
  101.         }
  102.     }
  103. }
  104.  
  105. void List::displayList()
  106. {
  107.     ListNode *nodePtr = head;
  108.  
  109.     while(nodePtr != 0)
  110.     {
  111.         cout << nodePtr->value;
  112.         if (nodePtr->next != 0)
  113.             cout << "-->";
  114.         nodePtr = nodePtr->next;
  115.     }
  116. }
  117.  
  118. List::~List()
  119. {
  120.     ListNode *nodePtr;
  121.     ListNode *nextNode;
  122.  
  123.     //Position nodePtr at the head of the list
  124.     nodePtr = head;
  125.  
  126.     //While nodePtr is not at the end of the list
  127.     while (nodePtr != 0)
  128.     {
  129.         //Save a pointer to the next node
  130.         nextNode = nodePtr->next;
  131.  
  132.         //Delete the current node
  133.         delete nodePtr;
  134.  
  135.         //Position nodePtr at the next node
  136.         nodePtr = nextNode;
  137.     }
  138. }
Add Comment
Please, Sign In to add comment