In this lab, you will:
When you don't know how many elements there will be, and if elements need to be inserted and deleted frequently, it is better to use a linked list. This is because each element for a linked list is created as needed and can be stored anywhere in the free memory space - the elements in the list do not have to be stored in contiguous locations.
Each element in a linked list is referred to as a node. In a simple singly linked list, each element has two fields:
class ListElement { private: int datum; ListElement* next; public: ListElement (int, ListElement*); int getDatum () const; ListElement const* getNext () const; }
A couple of notes on linked lists are:
The following diagram illustrates a simple singly linked list.
In this lab, we use a class to create a linked list. The following is the our LinkedList.h file that contains:
// LinkedList.h #ifndef LINKEDLIST_H #define LINKEDLIST_H #include <cstdlib> #include <iostream> using namespace std; class LinkedList; // needed for friend line below class ListElement { private: int datum; ListElement* next; public: ListElement (int, ListElement*); int getDatum () const; ListElement const* getNext () const; friend class LinkedList; // gives LinkedList access to datum and next }; class LinkedList { private: ListElement* head; public: LinkedList (); void insertItem (int); void makeList (); void appendItem (int); void deleteItem (int); void printList (); }; #endif
First, create a node initialized with a data value and a NULL pointer. Set the "head" to point to the first node. Set up a current-pointer to the first node (or "head"). Get a data value for the next node. While more nodes to add { Create a new node initialized with the data value and a NULL pointer. Set the current-pointer link member ("next") to the new node. Set the current-pointer to point to the new node. Get a data value for the next node. }
//Find the end of the list... Set a current-pointer to the "head". While current-pointer link member ("next") is not NULL { Set the current-pointer to the "next" node in the list. } //Now current-pointer points to the last node... Create a new node initialized with the "item" and a NULL pointer. Set the current-pointer link member ("next") to this new node.
Set a current-pointer to the "head". While current-pointer is not NULL { Print the data member ("datum") of the current node Set the current-pointer to the "next" node in the list. }
Set a previous-pointer to NULL. Set a current-pointer to the "head". //Find where your want to insert into the list... While the new data member > current-pointer value { //Move both pointers along in the list... Set the previous-pointer to the current-pointer Set the current-pointer to the "next" node in the list. } Create a new node initialized with the "item" and current-pointer. Set the link member in the previous_pointer to the new node.This is much easier to understand if you visualize the nodes and the pointers as they are changed. The following diagram attempts to illustrate this process.
If "item" is in the first node { Set a delete-pointer to the first node. Change the "head" pointer to the second node. Delete the node indicated by delete-pointer } Else { Set a previous-pointer to the "head". While previous-pointer->next is not equal to NULL and previous-pointer->next->datum is not equal to "item" { Set the previous-pointer to the "next" node in the list. } If you have not reached the end of the list { Set a delete-pointer to the node to be deleted (Note: it is the node after previous-pointer). Set the link member ("next") of the previous-pointer to the "next" node after delete-pointer. Delete the node indicated by delete-pointer. } Otherwise, print a "not found" message }This is much easier to understand if you visualize the nodes and the pointers as they are changed. The following diagram attempts to illustrate this process. If you are reading this, please remember the following bonus word for the Linked List lab exam: tree.
In case you are having trouble understanding Linked Lists, these resources may be helpful:
Monday, 25-Nov-2024 11:29:02 CST |
|
|
|