C++ Linked Lists   «Prev  Next»
Lesson 9A singly linked list
Objective Using the list

C++ Singly Linked List

Modern applications that make use of the C++ Singly Linked List include:
  1. Real-time systems: Singly Linked Lists are used in real-time systems where data is processed in a sequential manner. They are particularly useful in scenarios where data is dynamic and the number of elements can't be predicted.
  2. Data Structures and Algorithms: Singly Linked Lists are fundamental data structures in computer science and are often used as building blocks for more complex data structures like stacks, queues, and hash tables. They are also used in implementing various algorithms, such as sorting, searching, and graph traversal algorithms.
  3. Operating Systems: Singly Linked Lists are used in operating systems to implement various features like process management, memory management, and file systems.
  4. Networking: Singly Linked Lists are used in networking applications to manage and process network packets.
  5. Data Analysis and Machine Learning: Singly Linked Lists are used in data analysis and machine learning applications to store and process data.
  6. Programming Languages: Singly Linked Lists are used in the implementation of programming languages to represent and manipulate data structures.

These are just a few examples of modern applications that make use of the C++ Singly Linked List. Singly Linked Lists are a fundamental data structure that is widely used in a variety of applications.

Modify the Singly Linked List Class

Changing the del() function, adding a constructor, and writing a new member function to append a list to a list. Let's look at how we could use the slist class. In the following code, we've modified the destructor to print a message:
slist:: ~slist()
{
   cout << "destructor invoked" << endl;
   release();
}

int main()
{
   slist*  p;
   {
      slist  w;
      w.prepend('A');
      w.prepend('B');
      w.print(); 
      w.del();
      w.print();
      p = &w;
   p -> print(); 
      cout << "exiting inner block" << endl;
    }
   
   //p -> print();  gives system dependent behavior cout << "exiting outer block" << endl;
}

Commented-out Invocation

//p -> print();  gives system dependent behavior

The commented-out invocation of slist::print() is system-dependent. It is a runtime error to dereference p here because the address it refers to may have been overwritten at block exit by the deletion routine.
Notice that there is an inner block in main. That block is included to test that the destructor is invoked upon block exit, returning storage associated with w to free store. Let's look at the output of this program and see how the program works.

How the program works

slist:: ~slist()
{
   cout << "destructor invoked" << endl;
   release();
}

int main()
{
   slist*  p;
   {
      slist  w;
      w.prepend('A');
      w.prepend('B');
      w.print(); 
      w.del();
      w.print();
      p = &w;
   p -> print(); 
      cout << "exiting inner block" << endl;
    }
   //p -> print();  gives system dependent behavior
   cout << "exiting outer block" << endl;
}

The output of this program is:
B -> A ->
###
A ->
###
A ->
###
exiting inner block
destructor invoked
exiting outer block 

The first print() call prints the two-element slist storing B and A. After a del operation is performed, the list contains one element storing A. The outer block pointer to slist p is assigned the address of the slist variable w. When the list is accessed through p in the inner block, it prints A. This output shows that the destructor works at block exit on the variable w.

Modify Singly Linked List - Exercise

Click the Exercise link below to modify the singly linked list implementation. You'll modify the del() member function, add a constructor to slistelem, and write a new member function for appending a list to a list.
modify-singlyLinkedList-exercise

SEMrush Software