Advertisement
Guest User

Linked List

a guest
May 9th, 2016
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class LinkedList
  6. {
  7. private:
  8. public:
  9.     struct Node // declare the node struct
  10.     {
  11.         int NodeData; // data
  12.         int ID;
  13.         struct Node* NextNode; // location of next node
  14.     };
  15.  
  16.     struct Node* Head; /* "Head" of list - basically has nothing
  17.                          except the location of the next item on the list */
  18.     LinkedList() // constructor initializes head
  19.     {
  20.         Head = new Node;
  21.         Head->NextNode = NULL;
  22.     }
  23.  
  24.     ~LinkedList() // deconstructor, nothing unusual here
  25.     {
  26.         DeleteAll();
  27.     }
  28.  
  29.     void AddNodeAtEnd(int NodeData, int ID) /* Adds a new node to the end*/
  30.     {                                       /*of the list with the given input*/
  31.         struct Node* NewNode = new Node; // create new node
  32.         NewNode->NodeData = NodeData; // set data
  33.         NewNode->ID = ID;
  34.         NewNode->NextNode = NULL;
  35.         struct Node* LastNode = NULL;
  36.         if (Head->NextNode == NULL) // check to see if list is empty
  37.         {
  38.             Head->NextNode = NewNode;
  39.             return;
  40.         }
  41.         LastNode = GetLastNode(); // get location of the last node in the list
  42.         LastNode->NextNode = NewNode; // set former last node's address pointer
  43.     }
  44.  
  45.     Node* GetLastNode() // get the last node's memory location as a pointer
  46.     {
  47.         if (Head->NextNode == NULL) // error checking
  48.             return NULL;
  49.         struct Node *TempNode = Head; // create temp node pointer at the head
  50.         while (TempNode->NextNode != NULL) // cycle through all the elements until there are no more
  51.             TempNode = TempNode->NextNode;
  52.         return TempNode;
  53.     }
  54.  
  55.     int GetSize() // same as before, but gives the size of the list instead
  56.     {
  57.         if (Head == NULL)
  58.             return 0;
  59.         int Size = 0;
  60.         struct Node *TempNode = Head;
  61.         while (TempNode->NextNode != NULL)
  62.         {
  63.             TempNode = TempNode->NextNode;
  64.             Size++;
  65.         }
  66.         return Size;
  67.     }
  68.  
  69.     void PrintAll() // same as before, but prints every node's data instead
  70.     {
  71.         if (Head == NULL)
  72.         {
  73.             cout << "Empty!";
  74.             return;
  75.         }
  76.         struct Node* TempNode = Head;
  77.         while (TempNode->NextNode != NULL)
  78.         {
  79.             TempNode = TempNode->NextNode;
  80.             cout << TempNode->NodeData << " " << TempNode->ID << " " /*<< TempNode->NextNode*/ << endl;
  81.         }
  82.     }
  83.  
  84.     void DeleteAll() // same as before, but deletes every node instead
  85.     {
  86.         if (Head == NULL)
  87.             return;
  88.         struct Node* TempNode = Head;
  89.         struct Node* DeletingThisNode = NULL;
  90.         while (TempNode->NextNode != NULL)
  91.         {
  92.             DeletingThisNode = TempNode;
  93.             TempNode = TempNode->NextNode;
  94.             //cout << TempNode->NodeData << " " << TempNode->ID << " " /*<< TempNode->NextNode*/ << endl; // error checking stuff
  95.             delete DeletingThisNode;
  96.         }
  97.         delete Head;
  98.         Head = NULL;
  99.     }
  100. };
  101.  
  102. int main()
  103. {
  104.     LinkedList Testing;
  105.     Testing.AddNodeAtEnd(156, 135);
  106.     Testing.AddNodeAtEnd(12, -15);
  107.     Testing.PrintAll();
  108.     cout << endl;
  109.     Testing.DeleteAll();
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement