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

C++ question: Design and implement your own linked list class to hold a sorted list of...
C++ question: Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member functions 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 have member functions to display the...
Make in c++ this Programming Challenge 1. Design your own linked list class to hold a...
Make in c++ this Programming Challenge 1. Design your own linked list class to hold a series of integers. The class should have member functions for appending, inserting, and deleting nodes. Don’t forget to add a destructor that destroys the list. Demonstrate the class with a driver program. 2. List Print Modify the linked list class you created in Programming Challenge 1 to add a print member function. The function should display all the values in the linked list. Test...
write a java code to implement a linked list, called CupList, to hold a list of...
write a java code to implement a linked list, called CupList, to hold a list of Cups. 1.Define and write a Cup node class, called CupNode, to hold the following information about a cup: •number (cup number) •capacity (cup capacity in ml) •Write a method size() that returns the number of elements in the linkedlist CupList. •Write a method getNodeAt() that returns the reference to cup node object at a specific position given as a parameter of the method. •Write...
C++ What Should This Program Do? Linked List Class Design your own linked list class (List.h)...
C++ What Should This Program Do? Linked List Class Design your own linked list class (List.h) to hold a series of strings. The linked list node should be implemented as a struct. The class should have member functions for appending, inserting, and deleting nodes. You should also have a display function that will traverse the list & display each node’s value. Don’t forget to add a destructor that destroys the list. DRIVER – driver.cpp Write a driver program (driver.cpp) that...
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...
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() {...
In Python, I've created a Node class for implementing a singly linked list. My Code: class...
In Python, I've created a Node class for implementing a singly linked list. My Code: class Node: def __init__(self,initdata): self.data = initdata self.next = None def getData(self): return self.data def getNext(self): return self.next def setData(self,newdata): self.data = newdata def setNext(self,newnext): self.next = newnext class SinglyLinkedList: def __init__(self): self.head = None def add(self,key): addkey = Node(key) addkey.setNext(self.head) self.head = addkey Now the question is: Create an append method that is O(1) by modifying the constructor of the SinglyLinkedList class by adding...
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)
. Implement your own custom linked list array implementation with the following changes: (a) Fill in...
. Implement your own custom linked list array implementation with the following changes: (a) Fill in the public E get(int index) method (b) Also add code to the get method to print out a message for each time an element in the list is checked while searching for the element You may want to study how the toString method goes from element to element in the list Java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT