Question: c++ Goal: Learn how to use class and struct to construct a linked list. Learn how to new and delete object. (Dynamic memory allocation) Requirement:

c++

Goal:

Learn how to use class and struct to construct a linked list.

Learn how to new and delete object. (Dynamic memory allocation)

Requirement:

Please use the skeleton code below to implement a class for manipulating linked lists.

#include

using namespace std;

struct ListNode {

int data;

ListNode *next;

ListNode(int x) : data(x), next(nullptr) {}

};

class LinkedList {

private:

ListNode *head = nullptr;

ListNode *tail = nullptr;

public:

void addNode(int x)

{

/// Add code here

/// Add a node to the list

/// 1. If the list is empty, create the head node.

/// 2. If the list exists, add the node to the tail.

}

void addNodeToSortedList(int x)

{

/// Add code here

/// Add a node to the list

/// 1. If the list is empty, create the head node.

/// 2. If the list exists, add the node to the tail.

}

void deleteAll()

{

/// Add code here

/// Delete all nodes and free the memory

}

void display()

{

/// Add code here

/// Display all nodes in the sequence from head to tail

/// Example:

/// 6 -> 2 -> 28 -> 1 -> @

}

};

int main()

{

LinkedList ll;

ll.addNode(6);

ll.addNode(2);

ll.addNode(28);

ll.addNode(1);

/// Display 6 -> 2 -> 28 -> 1 -> @

ll.display();

ll.deleteAll();

ll.addNodeToSortedList(6);

ll.addNodeToSortedList(2);

ll.addNodeToSortedList(28);

ll.addNodeToSortedList(1);

/// Display 1 -> 2 -> 6 ->28 -> @

ll.display();

ll.deleteAll();

return 0;

}

Thanks for your help!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!