C++ Programing Help needed - Visual Studio is the IDE. ( Follow the Instructions
ID: 3747977 • Letter: C
Question
C++ Programing Help needed - Visual Studio is the IDE. ( Follow the Instructions Properly)
Node.h, LinkedList.h, LLDriver.cpp
Program description:
You will implement and test an ORDERED linked list class that uses nodes to store items.
• Add a second header file to the project named LinkedList.h. Place the standard #ifndef statement (and the associated #define and #endif) in the file.
• This file will contain the definition of the templated class "LinkedList"
• The LinkedList Class will have just one data member that is a pointer to the first node in the list. You may not add any other member variables
• The Node class should have two member variables ( a data and a pointer ). You may not add any other member variables
Node.h (struct)
Default Constructor
Data should be set to 0 and pointer should be set to nullptr
Overloaded Constructor
Set private data members to be the parameters
LinkedList.h
Constructor
Initializes all private data members
Destructor
Deletes all dynamically allocated memory to prevent memory leaks
Insert
Takes a single argument representing the value to be inserted into the list. The item is inserted into the list in the correct order. Returns a bool (success) to indicate if the insertion was successful.
remove
Takes an argument (by reference) which holds the key of the item to be removed from the list. This item will hold the record (data) that has been deleted upon completion. Only the first occurrence of the value should be removed. This function should return a bool (success).
retrieve
Takes an argument (by reference) which holds the key of the item to be retrieved from the list. That same argument will be used to hold the data once it has been found (think about the “retrieve” function we did last week. Return a bool (success). This is an accessor function
viewFront
Takes an argument (by reference) that will be used to hold the data found in the first Node of the list. Return a bool (success). This is an accessor function
viewBack
Takes an argument (by reference) that will be used to hold the data found in the last Node of the list. Return a bool (success). This is an accessor function
isEmpty
Returns a bool to indicate if the list is empty. This is an accessor function
isFull
Returns a bool to indicate if the list is full. This is an accessor function
clearList
Removes all elements in the linked list. Returns a false value if the list is empty
display
Displays all values currently stored in the list, only in a display functin you can use standard output (cout). This is an accessor function. Return false if no value in the list
getSize
Count and returns the current size of the list. This is an accessor function
Requirements:
1. Declare your LinkedList object like this:
LinkedList <int> ll; // you can use different data types
2. Sample call to the member functions, please don’t ignore the returned Boolean value!:
val = 1;
if (ll.insert(val) == false)
{
// or you can say if (!ll.insert(val))
cout << "insert didn't work" << endl;
// you can use a more meaningful message
}
Note: use (nothrow) in the constructor. for example: template <typename TYPE>
bool LinkedList : : LinkedList(int c)
{ ......etc etc ;
numValues = 0;
list = new (no throw)Type[ etc etc ] ;
}
Sample output:
1 1 1 2 3 4 5 6
After inserting 3: 1 1 1 2 3 3 4 5 6 7
After removing 4: 1 1 1 2 3 3 5 6 7
front value is: 1
last value is: 7
Not Empty
Not full
Currently has 9 nodes
attempting to retrieve 10
not found
attempting to retrieve 3
found 3
after calling clearlist 0 elements remaining
press any key to continnue . . .
Node.h (struct)
Default Constructor
Data should be set to 0 and pointer should be set to nullptr
Overloaded Constructor
Set private data members to be the parameters
Explanation / Answer
LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "Node.h"
#include <iostream>
using namespace std;
template <class TYPE>
class LinkedList
{
Node<TYPE> *front;
public:
LinkedList();
~LinkedList();
bool insert(TYPE x);
bool remove(TYPE &x);
bool retrieve(TYPE &x) const;
bool viewFront(TYPE &x) const;
bool viewBack(TYPE &x) const;
bool isEmpty() const;
bool isFull() const;
bool clearList();
bool display() const;
int getSize() const;
};
template <class TYPE>
LinkedList<TYPE>::LinkedList()
{
front = NULL;
}
template <class TYPE>
LinkedList<TYPE>::~LinkedList()
{
clearList();
}
template <class TYPE>
bool LinkedList<TYPE>::insert(TYPE x)
{
if (front == NULL)
front = new Node<TYPE>(x, NULL);
else if (front->data > x)
front = new Node<TYPE>(x, front);
else
{
Node<TYPE> *t = front;
while (t->next != NULL && t->next->data < x)
t = t->next;
t->next = new Node<TYPE>(x, t->next);
}
return true;
}
template <class TYPE>
bool LinkedList<TYPE>::remove(TYPE &x)
{
if (front == NULL)
return false;
else if (front->data > x)
return false;
else
{
Node<TYPE> *t = front;
while (t->next != NULL && t->next->data < x)
t = t->next;
if (t->next == NULL)
return false;
if (t->next->data != x)
return false;
else
{
Node<TYPE> *k = t->next;
t->next = k->next;
delete k;
return true;
}
}
}
template <class TYPE>
bool LinkedList<TYPE>::retrieve(TYPE &x) const
{
if (front == NULL)
return false;
else if (front->data > x)
return false;
else
{
Node<TYPE> *t = front;
while (t->next != NULL && t->next->data < x)
t = t->next;
if (t->next == NULL)
return false;
if (t->next->data != x)
return false;
else
return true;
}
}
template <class TYPE>
bool LinkedList<TYPE>::viewFront(TYPE &x) const
{
if (front == NULL)
return false;
else
{
x = front->data;
return true;
}
}
template <class TYPE>
bool LinkedList<TYPE>::viewBack(TYPE &x) const
{
if (front == NULL)
return false;
else
{
Node<TYPE> *t = front;
while (t->next != NULL)
t = t->next;
x = t->data;
return true;
}
}
template <class TYPE>
bool LinkedList<TYPE>::isEmpty() const
{
return front == NULL;
}
template <class TYPE>
bool LinkedList<TYPE>::isFull() const
{
return front != NULL;
}
template <class TYPE>
bool LinkedList<TYPE>::clearList()
{
if (front == NULL)
return false;
else
{
Node<TYPE> *t = front;
while (front != NULL)
{
front = front->next;
delete t;
t = front;
}
return true;
}
}
template <class TYPE>
bool LinkedList<TYPE>::display() const
{
if (front == NULL)
return false;
else
{
Node<TYPE> *t = front;
while (t != NULL)
{
cout << t->data;
t = t->next;
}
return true;
}
}
template <class TYPE>
int LinkedList<TYPE>::getSize() const
{
int c = 0;
Node<TYPE> *t = front;
while (t != NULL)
{
c++;
t = t->next;
}
return c;
}
#endif
Node.h
#ifndef NODE_H
#define NODE_H
template <class TYPE>
struct Node
{
TYPE data;
Node *next;
Node();
Node(TYPE d);
Node(TYPE d, Node *n);
};
template <class TYPE>
Node<TYPE>::Node()
{
data = 0;
next = NULL;
}
template <class TYPE>
Node<TYPE>::Node(TYPE d)
{
data = d;
next = NULL;
}
template <class TYPE>
Node<TYPE>::Node(TYPE d, Node *n)
{
data = d;
next = n;
}
#endif
LLDriver.cpp
// LinkedList.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "pch.h"
#include <iostream>
#include "LinkedList.h"
using namespace std;
int main()
{
LinkedList<int> ll;
ll.insert(1);
ll.insert(1);
ll.insert(1);
ll.insert(2);
ll.insert(3);
ll.insert(4);
ll.insert(5);
ll.insert(6);
ll.display();
cout << " ";
ll.insert(3);
ll.display();
cout << " ";
int c = 4;
ll.remove(c);
ll.display();
cout << " ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.