Linked Lists in C++

Pages 469-473 in Lafore

 

Link to code we wrote in class

 

Linked lists in C++

•      Keeping lists on computers is one of the most common things computers do.

•      So far, we have looked at using arrays to keep lists of data

•      Using arrays to store lists has some definite disadvantages

–    An array has a fixed size

–    If you want to insert in the middle of the list, items behind it must be moved

•      Another option is to use linked lists, where all the items are not stored contiguously in memory

 

Linked lists

•      In this data structure, one of the data members in the class is a pointer to an object of its own class

•      Example:  class intNode
                {   int   info;  // data can be any type
                    intNode  *next;    }

•      Two objects of this class can be instantiated and chained together by having the *next pointer of one node point to the next node

•      The second node’s next pointer can point to a third node, and so on.

•      We can also have external pointers pointing to the type intNode

 

A node in C++

•      This is another part of C++ that is inherited from C, before classes were used

•      Instead of a class, a struct is often used to declare the node.

–   A struct is just like a class except the data members are public by default

•   In a class the data members are private by default.

•    structs often do NOT have member functions, but they may

 

To make a linked list

•             Create a struct to hold the node

•             Create a class that holds:

–          Data member:

•          A pointer to the first item in the list

–          Member functions to manipulate the list

•          AddToFront,   AddToEnd, InsertAt

•          Remove

•          Find

•          Print

•             Create a main function to test the linked list class

 

struct to hold the information and a pointer to itself

struct node
{
        string item; 
// this will be a list of strings
        node *nextPtr;

       
public node(string i)    // constructor—put
                                                             //  data in the node
       {   item = i;
             nextPtr = NULL;  }
}

 

Class to hold reference to first node in the list

class linkList
{   
  private:
        node *headPtr;

    
  public:
        linkList ( )  
// constructor; sets up an empty list
      {    headPtr = NULL;            }

      
      void addToFront(string item)

             {……….}
       //other methods go here
}

 

Driver to use linkList

•      A main( ) method

–   Must declare a variable of type linkList; this will call the linkList constructor

–   Other statements in main( ) will probably call the member functions in the class list to manipulate the list

•   I.e. add to the list, delete from the list etc.

 

Designing a function to add to the list

•      Add to the front of the list

•      What are the preconditions?

–    The list must exist

–    It is either empty, or it has some items in it

•      What do we need to do?

–    Get RAM from the OS for the new node

–    Put the data into the node

•    We could let the constructor do this for us

–    Set the nextPtr inside the new node

–    Link the new node into the front of the list

 

AddToFront member function

void addToFront(string str)
  {
              node *tempPtr = new node(str);            
              tempPtr->nextPtr = headPtr;
              headPtr = tempPtr;
  }
  //end addToFront function

 

Note how a pointer accesses data members of the class

 

Designing a displayList function

•      What are the preconditions?

–   The list must exist

–   It is either empty, or it has some items in it

•      What do we need to do?

–   Declare a pointer to a node to transverse the list

–   Start at the first node in the list, and output the data in that node

–   Go to the second node and output

–   Keep going until the end of the list

 

displayList member function

void displayList( )
  {
              node *currPtr = headPtr;
              while(currPtr != NULL)
              {
                          cout << currPtr->item << endl;
                          currPtr = currPtr->nextPtr;
              }  // end while
  }
  // end displayList function

 

Designing a function to add to the end of the list

•      Same preconditions as before

•      What do we need to do?

–   Get RAM from the OS for the new node

–   Check to see if the list is empty;

•   if it is empty, set head to point to the new node

•      If not empty,

–   find the end of the list ,

•   Will have to declare a new pointer to traverse the list

–   then link in the new node there

•   Set the pointer in the new node first

•   Then link in to the list

 

AddToEnd(string str)

void addToEnd(string str)
  {
              node *tempPtr = new node(str);
              node *currPtr = headPtr;
                         
              if(headPtr == NULL)
              {           headPtr = tempPtr;    }
              else
              {
                          while(currPtr->next != NULL)
                          {           currPtr= currPtr->nextPtr; }
                          currPtr->nextPtr = tempPtr;
              }
 }//
end addToEnd function

 

Designing a function to delete nodes

•      We have several ways we might do this, but let’s delete a node with a known item

•      A precondition is that the list is not empty

•      We have to transverse the list, looking for the item

•      When we find the item, we must delete that node

•      We will be pointing to the node, but to delete the node, we must have a pointer pointing to the previous node

•      Look at diagrams, then look at the code

 

Doubly linked lists

•      The node has three components instead of two.

struct node
{
        string item; 
// this will be a list of strings
        node *nextPtr, *prevPtr;

       
public node(string i)    // constructor—put
                                                             //  data in the node
       {   item = i;
             nextPtr = NULL;  }
}