Question

In: Computer Science

Could you check/edit my code? Design and implement your own linked list class to hold a...

Could you check/edit my code?

Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if the list is empty, and return the length of the list. Be sure to have a class constructor, a class destructor, and a copy constructor for deep copy. Demonstrate your class with a driver program (be sure to include the following cases: insertion at the beginning, end, and inside the list, deletion of first item, last item, and an item inside, searching for an existing/non-existing item, and modifying a list that was initialized to an existing list).

#ifndef LIST_H
#define LINKEDLIST_H

template <class T>
class ListNode
{
public:
   //Node value
   T value;
   //Pointer to the next node
   ListNode<T> *next;
   //Constructor
   ListNode(T nodeValue)
   {
       value = nodeValue
           next = nullptr;
   }
};
//LinkedList class
template <class T>
class LinkedList
{
private:
   //List head pointer
   ListNode<T> *head;
public:
   //Constructor
   LinkedList()
   {
       head = nullptr;
   }
   //Destructor
   ~LinkedList();

   //Linked list operations
   void appendNode(T);
   void insertNode(T);
   void deleteNode(T);
   void displayList() const;
};
template <class T>
void LinkedList<T>::appendNode(T newValue)
{
   //To point to a new node
   ListNode<T> *newNode;
   //To move through the list
   ListNode<T> *nodePtr;
   //Allocate a new node and store new Value there.
   newNode = new ListNode<T>(newValue);
   if (!head)
       head = newNode;
   else
   {
       //Initialize nodePtr to head of list.
       nodePtr = head;
       //Find the last node in the list
       while (nodePtr->next)
           nodePtr = nodePtr->next;
       //Insert newNode as the last node.
       nodePtr->next = newNode;
   }
}
template <class T>
void LinkedList<T>::displayList() const
{
   //To move through the list
   ListNode<T> *nodePtr;
   //Position nodePtr at the head of the list.
   nodePtr = head;
   //Traverse the list
   while (nodePtr)
   {
       //Value of this node
       cout << nodePtr->value << endl;
       //Move to the next node.
       nodePtr = nodePtr->next;
   }
}
template <class T>
void LinkedList<T>::insertNode(T newValue)
{
   ListNode<T> *newNode;
   //traverse the list
   ListNode<T> *nodePtr;

   ListNode<T> *previousNode = nullptr;
   newNode = new ListNode<T>(newValue);

   if (!head)
   {
       head = newNode;
       newNode->next = nullptr;
   }
   else
   {
       nodePtr = head;
       previousNode = nullptr;

       //Skip all nodes whose value is less than newValue.
       while (nodePtr != nullptr && nodePtr && nodePtr->value < newValue)
       {
           previousNode = nodePtr;
           nodePtr = nodePtr->next;
       }
       //If the new node is to be the 1st in the list, then insert it before all other nodes
       if (previousNode == nullptr)
       {
           head = newNode;
           newNode->next = nodePtr;
       }
       else {
           previousNode->next = newNode;
           newNode->next = nodePtr;
       }
   }
}
template <class T>
void LinkedList<T>::deleteNode(T searchValue)
{
   //To traverse the list
   ListNode<T> *nodePtr;
   //To point to the previous node
   ListNode<T> *previousNode;
   //There is no need to do anything if the list is empty
   if (!head)
       return;

   //Determine if the first node is one
   if (head->value == searchValue)
   {
       nodePtr = head->next;
       delete head;
       head = nodePtr;
   }
   else
   {
       //Initialize nodePtr to head of list
       nodePtr = head;

       //Skip all nodes whose value member is not equal to num
       while (nodePtr != nullptr && nodePtr->value != searchValue)
       {
           previousNode = nodePtr;
           nodePtr = nodePtr->next;
       }
       //If nodePtr is not at the end of the list, link the
       //previous node ot the node after nodePtr, then delete nodePtr.
       if (nodePtr)
       {
           previousNode->next = nodePtr->next;
           delete nodePtr;
       }
   }
}
//Destructor
//This function will delete every node on the list

template <class T>
LinkedList<T>::~LinkedList()
ListNode<T> *nodePtr;
ListNode<T> *nextNode;

//Position nodePtr at the head of the list.
nodePtr = head;
//While nodePtr is not at the end of teh list...
while (nodePtr != nullptr)
{
   //Save a pointer to the next node.
   nextNode = nodePtr->next;

   delete nodePtr;

   nodePtr = nextNode;
}
}
#endif

Solutions

Expert Solution

There were no bugs in your code but they asked to write methods, which could add/delete elements in the beginning and in the end of the linked list. Also you didn't write find method, which should find element in the linked list. I added these methods to your code and tested your methods, which were working properly. The methods, which I added, are bolded.

#ifndef LIST_H
#define LINKEDLIST_H

template <class T>
class ListNode
{
public:
//Node value
T value;
//Pointer to the next node
ListNode<T> *next;
//Constructor
ListNode(T nodeValue)
{
value = nodeValue
next = nullptr;
}
};
//LinkedList class
template <class T>
class LinkedList
{
private:
//List head pointer
ListNode<T> *head;
public:
//Constructor
LinkedList()
{
head = nullptr;
}
//Destructor
~LinkedList();

//Linked list operations
void appendNode(T);
void insertAtBeginning(T);
void insertNode(T);
void deleteNode(T);
void deleteAtBeginning();
void deleteAtEnd();
int findNode(T);
void displayList() const;
};
template <class T>
void LinkedList<T>::appendNode(T newValue)
{
//To point to a new node
ListNode<T> *newNode;
//To move through the list
ListNode<T> *nodePtr;
//Allocate a new node and store new Value there.
newNode = new ListNode<T>(newValue);
if (!head)
head = newNode;
else
{
//Initialize nodePtr to head of list.
nodePtr = head;
//Find the last node in the list
while (nodePtr->next)
nodePtr = nodePtr->next;
//Insert newNode as the last node.
nodePtr->next = newNode;
}
}

template <class T>
void LinkedList<T>::insertAtBeginning(T newValue)
{
//To point to a new node
ListNode<T> *newNode;
//Allocate a new node and store new Value there.
newNode = new ListNode<T>(newValue);
if (!head)
head = newNode;
else
{
//Add at the beginning
newNode->next = head;
head = newNode;
}
}

template <class T>
void LinkedList<T>::displayList() const
{
//To move through the list
ListNode<T> *nodePtr;
//Position nodePtr at the head of the list.
nodePtr = head;
//Traverse the list
while (nodePtr)
{
//Value of this node
cout << nodePtr->value << endl;
//Move to the next node.
nodePtr = nodePtr->next;
}
}
template <class T>
void LinkedList<T>::insertNode(T newValue)
{
ListNode<T> *newNode;
//traverse the list
ListNode<T> *nodePtr;

ListNode<T> *previousNode = nullptr;
newNode = new ListNode<T>(newValue);

if (!head)
{
head = newNode;
newNode->next = nullptr;
}
else
{
nodePtr = head;
previousNode = nullptr;

//Skip all nodes whose value is less than newValue.
while (nodePtr != nullptr && nodePtr && nodePtr->value < newValue)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
//If the new node is to be the 1st in the list, then insert it before all other nodes
if (previousNode == nullptr)
{
head = newNode;
newNode->next = nodePtr;
}
else {
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
template <class T>
void LinkedList<T>::deleteNode(T searchValue)
{
//To traverse the list
ListNode<T> *nodePtr;
//To point to the previous node
ListNode<T> *previousNode;
//There is no need to do anything if the list is empty
if (!head)
return;

//Determine if the first node is one
if (head->value == searchValue)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
//Initialize nodePtr to head of list
nodePtr = head;

//Skip all nodes whose value member is not equal to num
while (nodePtr != nullptr && nodePtr->value != searchValue)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
//If nodePtr is not at the end of the list, link the
//previous node ot the node after nodePtr, then delete nodePtr.
if (nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
}

template <class T>
int LinkedList<T>::findNode(T searchValue)
{
int ans = 0;
//To move through the list
ListNode<T> *nodePtr;
//Position nodePtr at the head of the list.
nodePtr = head;
//Traverse the list
while (nodePtr != nullptr && nodePtr -> value != searchValue) {
nodePtr = nodePtr->next;
ans++;
}
//If we didn't find the value;
if (nodePtr == nullptr) {
return -1;
}
return ans;
}

template <class T>
void LinkedList<T>::deleteAtBeginning()
{
if (!head)
return;
//To point to a new node
ListNode<T> *newNode;
newNode = head->next;
delete head;
head = newNode;
}

template <class T>
void LinkedList<T>::deleteAtEnd()
{
if (!head)
return;
//To point to a new node
ListNode<T> *newNode;
//To move through the list
ListNode<T> *nodePtr;
while (nodePtr -> next != nullptr) {
nodePtr = nodePtr -> next;
}
delete nodePtr -> next;
}

//Destructor
//This function will delete every node on the list

template <class T>
LinkedList<T>::~LinkedList() {
ListNode<T> *nodePtr;
ListNode<T> *nextNode;

//Position nodePtr at the head of the list.
nodePtr = head;
//While nodePtr is not at the end of teh list...
while (nodePtr != nullptr)
{
//Save a pointer to the next node.
nextNode = nodePtr->next;

delete nodePtr;

nodePtr = nextNode;
}
}
#endif

comment down for any queries

please give a thumbs up


Related Solutions

using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
Develop a C++ "doubly" linked list class of your own that can hold a series of...
Develop a C++ "doubly" linked list class of your own that can hold a series of signed shorts Develop the following functionality: Develop a linked list node struct/class You can use it as a subclass like in the book (Class contained inside a class) You can use it as its own separate class Your choice Maintain a private pointer to a node class pointer (head) Constructor Initialize head pointer to null Destructor Make sure to properly delete every node in...
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
How do you implement stack by using linked list? No code just explain it.
How do you implement stack by using linked list? No code just explain it.
Implement the SimpleQueue interface with the MyQueue class. You can use either a linked list or...
Implement the SimpleQueue interface with the MyQueue class. You can use either a linked list or a dynamic array to implement the data structure. A queue is a specialised form of list in which you can only get and remove the first element in the queue. The class should be able to work with the following code: SimpleQueue queue = new MyQueue(); NB: You cannot import anything from the standard library for this task. The data structure must be of...
Write a program where you- 1. Create a class to implement "Double Linked List" of integers....
Write a program where you- 1. Create a class to implement "Double Linked List" of integers. (10) 2. Create the list and print the list in forward and reverse directions. (10)
Purpose Purpose is to implement some single linked list methods. Add methods to the List class...
Purpose Purpose is to implement some single linked list methods. Add methods to the List class In the ‘Implementation of linked lists’ lecture, review the ‘Dynamic implementation of single linked list’ section. You will be adding new methods to the List class. Eight new methods are required: new constructor – creates a new single linked list from an array of integers e.g. int a[] = {1, 2, 3, 4}; List list = new List(a); toString() – returns a string representing...
In this homework, you will implement a single linked list to store a list of employees...
In this homework, you will implement a single linked list to store a list of employees in a company. Every employee has an ID, name, department, and salary. You will create 2 classes: Employee and EmployeeList. Employee class should have all information about an employee and also a “next” pointer. See below: Employee Type Attribute int ID string name string department int salary Employee* next Return Type Function (constructor) Employee(int ID, string name, string department, int salary) EmployeeList class should...
In this assignment, you will implement a Polynomial linked list, the coefficients and exponents of the...
In this assignment, you will implement a Polynomial linked list, the coefficients and exponents of the polynomial are defined as a node. The following 2 classes should be defined.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT