Introduction to Linked Lists in C++ Course

  • C/C++
  • Thread starter Defennder
  • Start date
  • Tags
    Intro
In summary, the conversation discusses the concept of linked lists and dynamic memory allocation in a C++ course. The code provided contains the header and implementation files for a linked list, with a member variable *head being declared but not initially set to point to the start of the list. It is initialized to null in the constructor and set to the first item added to the list in the insert method when index is equal to 1.
  • #1
Defennder
Homework Helper
2,593
5
I'm taking a C++ course and have just been introduced to the concept of linked lists and dynamic memory allocation. The following is from my notes:

Code:
//ListP.h: List ADT using Linked List
typedef int ListItemType;
class List {

    public:
        List();
        ~List();
        // ... ...No change to other methods
        bool isEmpty() const;
        int getLength() const;

        void insert(int index, const ListItemType& newItem)
            throw (ListException, ListIndexOutOfRangeException);

        void remove(int index)
            throw (ListIndexOutOfRangeException);

        void retrieve(int index, ListItemType& dataItem) const
            throw (ListIndexOutOfRangeException);

    private:
        struct ListNode {
            ListItemType item;
            ListNode *next;
        };
        int size;
        ListNode *head;
        ListNode* find(int index) const;
}; // end List class

List::List():size(0), head(NULL){}
List::~List()
{
    while (!isEmpty())
        remove(1);
} // end destructor
bool List::isEmpty() const
{
    return size == 0;
} // end isEmpty
int List::getLength() const
{
    return size;
} // end getLength

List::ListNode *List::find(int index) const
{
    if ( (index < 1) || (index > getLength()) )
        return NULL;
    else // count from the beginning of the list.
    {
        ListNode *cur = head;
        for (int skip = 1; skip < index; ++skip)
            cur = cur->next;
        return cur;
    } // end if
} // end find

void List::retrieve(int index,
        ListItemType& dataItem) const
throw(ListIndexOutOfRangeException)
{
    if ( (index < 1) || (index > getLength()) )
        throw ListIndexOutOfRangeException(
                "Bad index in retrieve");
    else
    { // get pointer to node, then data in node
        ListNode *cur = find(index);
        dataItem = cur->item;
    } // end if
} // end retrieve

    void List::insert(int index, const ListItemType& newItem)
throw(ListIndexOutOfRangeException, ListException)
{
    int newLength = getLength() + 1;
    if ( (index < 1) || (index > newLength) )
        throw ListIndexOutOfRangeException(
                "Bad index in insert");
    else
    { // try to create new node and place newItem in it
        ListNode *newPtr = new ListNode;
        size = newLength;
        newPtr->item = newItem;
        // attach new node to list
        if (index == 1)
        { // insert new node at beginning of list
            newPtr->next = head;
            head = newPtr;
        }
        else
        { ListNode *prev = find(index-1);
            // insert new node after node
            // to which prev points
            newPtr->next = prev->next;
            prev->next = newPtr;
        } // end if
    } // end insert

    void List::remove(int index)
        throw(ListIndexOutOfRangeException)
        {
            ListNode *cur;
            if ( (index < 1) || (index > getLength()) )
                throw ListIndexOutOfRangeException(
                        "Bad index in remove");
            else
            { --size;
                if (index == 1) {
                    // delete the first node from the list
                    cur = head; // save pointer to node
                    head = head->next;
                }

                else {
                    ListNode *prev = find(index - 1);
                    cur = prev->next; // save pointer to node
                    prev->next = cur->next;
                } // end if
                cur->next = NULL;
                delete cur;
                cur = NULL;
            } // end if
        } // end remove
The above contains both the header file and implementation file for a linked list. The question I have is, given that *head is a struct pointer to the start of the linked list, I don't see where in the above code is it initialised to point to the start of the linked list. I only see it declared under private of class List. The member functions above make use of *head as though it were already made to point to the start of the list, but it doesn't appear to have been made to point to the starting of the linked list. Is this supposed to be done in int main() or has it alread been done above?
 
Last edited:
Technology news on Phys.org
  • #2
It is set to null in the constructor ... head(NULL) ... and is set to the first item added to the list... under the insert method, when index == 1.
 
  • #3
Okay thanks.
 

FAQ: Introduction to Linked Lists in C++ Course

What is a linked list?

A linked list is a data structure in which each element, called a node, contains both data and a pointer to the next node in the list. This allows for dynamic memory allocation and efficient insertion and deletion of elements.

Why use linked lists instead of arrays?

Linked lists have several advantages over arrays, such as the ability to dynamically allocate memory, efficient insertion and deletion operations, and the ability to store data of varying sizes. They also have a lower memory overhead, as they do not require a contiguous block of memory like arrays do.

What are the different types of linked lists?

The three main types of linked lists are singly linked lists, doubly linked lists, and circular linked lists. Singly linked lists have nodes that only point to the next node, doubly linked lists have nodes that point to both the next and previous nodes, and circular linked lists have the last node pointing back to the first node.

How do you insert and delete nodes in a linked list?

To insert a node, you first create a new node and assign its next pointer to the next node in the list. Then, you update the previous node's next pointer to point to the new node, and the new node becomes the "middle" node between the previous and next nodes. To delete a node, you update the previous node's next pointer to point to the next node, and then free the memory of the deleted node.

What are some common applications of linked lists?

Linked lists are commonly used in many programming languages for implementing data structures such as stacks, queues, and hash tables. They are also used in applications where data needs to be stored and retrieved in a specific order, such as music or video playlists, or in systems where dynamic memory allocation is required.

Similar threads

Replies
6
Views
3K
Replies
9
Views
2K
Replies
17
Views
7K
Replies
6
Views
13K
Replies
9
Views
3K
Replies
2
Views
1K
Replies
1
Views
1K
Replies
5
Views
2K
Back
Top