Question: Please use c++ code! For this final assignment, you will use Qt to create a GUI version of the templated version of LinkedList you made

Please use c++ code!

For this final assignment, you will use Qt to create a GUI version of the templated version of LinkedList you made for assignment 10.

You may design your window as you wish, but it needs to have the minimum functionality:

Radio buttons to select between the different types of data your linked list will store(int, double, string)

A button to display the contents of the linked list

The ability to display only a portion of the linked list

Buttons to push_back, push_front, pop_front, insert_sorted

Button to perform select_sort

Button to remove_duplicates

Should have the ability to make a copy of a list and be able to retain and modify both copies.

Be able to modify any of the lists without losing any information going from one list to the other.

This is my Assignment10 code:

linkedlist.h

#ifndef LINKEDLIST_H_INCLUDED #define LINKEDLIST_H_INCLUDED #include using namespace std;

#include "listEmpty.h"

template struct Node { E data; //data Node *next; Node( E data ) : data(data), next(0) {} };

template class LinkedList { private: Node *head; //head pointer Node *tail; //tail pointer public:

/* ***************************** ** Iterator class *** *******************************/ class Iterator { private: Node *current; public: Iterator(); Iterator(Node *ptr);

int operator*(); Iterator operator++(); bool operator==(const Iterator& right) const; bool operator!=(const Iterator& right) const;

}; /* ***************************** ** CONSTRUCTOR & DESTRUCTOR *** *******************************/ LinkedList(); LinkedList(const LinkedList& source); ~LinkedList();

/* ************* ** Mutators *** ***************/ LinkedList & operator=( const LinkedList& source ); void display() const; void push_front( const E& value ); void push_back( const E& value ); void pop_front() throw(ListEmpty); const E& front() const throw(ListEmpty); const E& back() const throw(ListEmpty); void clear(); void select_sort(); void insert_sorted( const E& value ); void remove_duplicates(); Iterator begin(); Iterator end();

/* ************* ** Accessors *** ***************/ int length() const; int sum() const; bool isEmpty() const;

private: int getLength(Node *ptr) const; int getSum(Node *ptr) const; };

/* ********************************************************* * * Method Iterator: Class Iterator *_________________________________________________________ * Initialize the iterator. *_________________________________________________________ ***********************************************************/ template LinkedList::Iterator::Iterator():current(0){}

/* ********************************************************* * * Method Iterator: Class Iterator *_________________________________________________________ * Initialize the iterator with parameter passed int. *_________________________________________________________ ***********************************************************/ template LinkedList::Iterator::Iterator(Node *ptr):current(ptr){}

/* ********************************************************* * * Method operator*: Class Iterator *_________________________________________________________ * This function overloads the dereferencing operator* *_________________________________________________________ ***********************************************************/ template int LinkedList::Iterator::operator*() { return this->current->data; }

/* ********************************************************* * * Method operator++: Class Iterator *_________________________________________________________ * This function overloads the pre-increment operator++. *_________________________________________________________ ***********************************************************/ template typename LinkedList::Iterator LinkedList::Iterator::operator++() { current = current->next;

return *this; }

/* ********************************************************* * * Method operator==: Class Iterator *_________________________________________________________ * This function overloads the equality operator. *_________________________________________________________ ***********************************************************/ template bool LinkedList::Iterator::operator==(const Iterator& right) const { return (this->current == right.current); } /* ********************************************************* * * Method operator!=: Class Iterator *_________________________________________________________ * This function overloads the not equal to operator. *_________________________________________________________ ***********************************************************/ template bool LinkedList::Iterator::operator!=(const Iterator& right) const { return (this->current != right.current); }

/* ********************************************************* * * Method LinkedList: Class LinkedList *_________________________________________________________ * This function Initializes an empty list. *_________________________________________________________ ***********************************************************/ template LinkedList::LinkedList():head(0),tail(0) {}

/* ********************************************************* * * Method LinkedList: Class LinkedList *_________________________________________________________ * This function Initialize a new list with the contents of an existing list. * PRE-CONDITIONS * source: new list * POST-CONDITIONS * newNode: newNode pointer * current: current pointer *_________________________________________________________ ***********************************************************/ template LinkedList::LinkedList(const LinkedList& source) { Node *newNode; //newNode pointer Node *current; //current pointer

//set head to NULL head = NULL;

if(this != &source) { //if the new list is NULL if(source.head == NULL) { head = NULL; tail = NULL; } else { //current points to the list to be copied current = source.head;

//copy the head node head = new Node(0); head->data = current->data; head->next = NULL;

tail = head;

current = current->next;

//while loop to copy the remaining list while(current != NULL) { newNode = new Node(0); newNode->data = current->data; newNode->next = NULL;

tail->next = newNode; tail = newNode;

current = current->next; } } } }

/* ********************************************************* * * Method ~LinkedList: Class LinkedList *_________________________________________________________ * This function should deallocate all remaining dynamically * allocated memory *_________________________________________________________ ***********************************************************/ template LinkedList::~LinkedList() { while (head != 0) { pop_front(); } }

/* ********************************************************* * * Method :operator=: Class LinkedList *_________________________________________________________ * This function copies over all of the nodes in an existing * list to another already existing list. * PRE-CONDITIONS * source: existing list * POST-CONDITIONS * temp: intNode pointer *_________________________________________________________ ***********************************************************/ template LinkedList & LinkedList::operator=( const LinkedList& source ) { Node *newNode; //new Node Node *current; //current Node

if(this != &source) { //if the new list is NULL if(source.head == NULL) { head = NULL; tail = NULL; } else { //current points to the list to be copied current = source.head;

//copy the head node head = new Node(0); head->data = current->data; head->next = NULL;

tail = head;

current = current->next;

//while loop to copy the remaining list while(current != NULL) { newNode = new Node(0); newNode->data = current->data; newNode->next = NULL;

tail->next = newNode; tail = newNode;

current = current->next; } } }

return *this; }

/* ********************************************************* * * Method display: Class LinkedList *_________________________________________________________ * This function displays to a single line all of the int * values stored in the list *_________________________________________________________ ***********************************************************/ template void LinkedList::display() const { if (head != 0) { for (Node *curr = head; curr != 0; curr = curr->next) { cout << curr->data; //only output a space if its not the last node if (curr != tail) { cout << ' '; } } } }

/* ********************************************************* * * Method push_front: Class LinkedList *_________________________________________________________ * This function inserts a data value (within a new node) * at the front end of the list. * PRE-CONDITIONS * value: integer value * POST-CONDITIONS * temp: the temporary pointer *_________________________________________________________ ***********************************************************/ template void LinkedList::push_front( const E& value ) { Node* temp; //the temporary pointer

//initializing temp = new Node(value); temp->next = head; head = temp;

//check if there is only 1 node, if so point tail to node if (tail == 0) { tail = temp; } }

/* ********************************************************* * * Method push_back: Class LinkedList *_________________________________________________________ * This function inserts a data value (within a new node) * at the back end of the list. * PRE-CONDITIONS * value: integer value * POST-CONDITIONS * temp: the temporary pointer *_________________________________________________________ ***********************************************************/ template void LinkedList::push_back( const E& value ) { Node* temp; //the temporary pointer

//initializing temp = new Node(value);

if(head != 0) { tail->next = temp; tail = temp; } else { head = temp; tail = temp; } }

/* ********************************************************* * * Method pop_front: Class LinkedList *_________________________________________________________ * This function removes the value at the front end of the list. * POST-CONDITIONS * temp: the temporary pointer *_________________________________________________________ ***********************************************************/ template void LinkedList::pop_front() throw(ListEmpty) {

try { if(head == 0) throw ListEmpty(); else { Node* temp; //temp pointer

temp = head; //set head to the temp head = head->next; //head equal to next pointer tail = NULL;

//delete temporary pointer delete temp; } } catch(ListEmpty) { throw; }

}

/* ********************************************************* * * Method front: Class LinkedList *_________________________________________________________ * This function returns the data in the head of the linked list. *_________________________________________________________ ***********************************************************/ template const E& LinkedList::front() const throw(ListEmpty) { try { if(head == 0) throw ListEmpty(); else return head->data; } catch(ListEmpty) { throw; }

}

/* ********************************************************* * * Method back: Class LinkedList *_________________________________________________________ * This function returns the data in the tail of the linked list. *_________________________________________________________ ***********************************************************/ template const E& LinkedList::back() const throw(ListEmpty) { try { if(head == 0) throw ListEmpty(); else return tail->data; } catch(ListEmpty) { throw; } }

/* ********************************************************* * * Method clear: Class LinkedList *_________________________________________________________ * This function makes the List Empty by deallocating all of the nodes in the List. *_________________________________________________________ ***********************************************************/ template void LinkedList::clear() { Node *temp; while(head != 0) { temp = head;

head = head->next; delete temp; } }

/* ********************************************************* * * Method select_sort: Class LinkedList *_________________________________________________________ * This function sorts the list into ascending order using * the selection sort algorithm. * PRE-CONDITIONS * value: integer value * POST-CONDITIONS * int size size of the current list * IntNode* large pointer to the largest node * IntNode* prevLarge; pointer to the previous node before the largest node * IntNode* end; pointer to correct position in list for appending * int pos; count position * int count; controls entire seperator *_________________________________________________________ ***********************************************************/ template void LinkedList::select_sort() { int size; //size of the current list Node* large; //pointer to the largest node Node* prevLarge; //pointer to the previous node before the largest node Node* end; //pointer to correct position in list for appending int pos; //count position int count; //controls entire seperator

//initializing size = 0; large = 0; prevLarge = 0; end = 0; pos = 0; count = 0;

//get the size of the current list for (Node *curr = head; curr != 0; curr = curr->next) { size++; }

while (count < (size - 1)) { //set largest node to default (head) large = head; //set end to default end = head; //node before largest one prevLarge = head;

//reset pos pos = 0;

//finds largest node for (Node *curr = head;pos < (size - count);curr = curr->next) { if (curr->data > large->data) { large = curr; } pos++; }

pos = 1;

//finds node before largest node for (Node *curr = head;pos < (size - count);curr = curr->next) { if (curr->next == large) { prevLarge = curr; } pos++; }

pos = 0;

//chooses correct position in list for appending for (Node * curr = head;pos < (size - count);curr = curr->next) { end = curr; pos++; }

if (large != end) { //if appending to the end if (count == 0) { Node* newNode = new Node(large->data); tail->next = newNode; tail = newNode;

//delete old node if deleting first node if (large == head) { pop_front(); } //deleting a middle node else { Node* temp = large; prevLarge->next = large->next; large = 0; delete temp; } } //if appending to anything else other than the end else { //create a new node with the data Node* newNode = new Node(large->data);

//appends new node newNode->next = end->next; end->next = newNode;

//delete old node if deleting first node if (large == head) { pop_front(); } //deleting a middle node else { Node* temp = large; prevLarge->next = large->next; large = 0; delete temp; } } } count++; } }

/* ********************************************************* * * Method insert_sorted: Class LinkedList *_________________________________________________________ * This function assumes the values in the list are in sorted * (ascending) order and inserts the data into the appropriate * position in the list * PRE-CONDITIONS * value: integer value * POST-CONDITIONS * notFound: return true or false * newNode: the new node with the value *_________________________________________________________ ***********************************************************/ template void LinkedList::insert_sorted( const E& value ) { bool notFound = true; //return true or false; Node* newNode = new Node(value); //the new node with the value

//if list is empty, insert new value if (head == 0) { head = newNode; tail = newNode; } //if not, insert value in right position else { for (Node *curr = head;curr != 0 && curr != tail && notFound; curr = curr->next) { //check if value is the smallest if (value < curr->data) { push_front(value); notFound = false; } //if value is found, insert node else if (curr->next->data >= value) { newNode->next = curr->next; curr->next= newNode; notFound = false; } } //if pos still not found, append value at the end if (notFound) { tail->next = newNode; tail = newNode; } } }

/* ********************************************************* * * Method remove_duplicates: Class LinkedList *_________________________________________________________ * This function removes all values that are duplicates of a * value that already exists in the list. Always remove the * later duplicate. * PRE-CONDITIONS * value: integer value * POST-CONDITIONS * dupe: holds possible deplicate of current node * prev: points to the previous node before the dupe node *_________________________________________________________ ***********************************************************/ template void LinkedList::remove_duplicates() { //terminte if list is empty if (head == 0) return;

Node* dupe = 0; //holds possible deplicate of current node Node* prev = head; //points to the previous node before the dupe node

for (Node *curr = head; curr != 0; curr = curr->next) { dupe = curr->next; prev = curr; while (dupe != 0) { if (curr->data == dupe->data) { //holds temporary node Node* temp = dupe;

//if the duplicate is the last element, fix tail if (dupe == tail) { dupe = 0; prev->next = 0; tail = prev; delete temp; } else { prev->next = dupe->next; dupe = dupe->next; delete temp; } } else { dupe = dupe->next; prev = prev->next; } } } }

/* ********************************************************* * * Method begin: Class LinkedList *_________________________________________________________ * This function returns an iterator at the beginning of the linked list. * POST-CONDITIONS * temp: iterator pointer *_________________________________________________________ ***********************************************************/ template typename LinkedList::Iterator LinkedList::begin() { Iterator temp(head); //iterator pointer

return temp; }

/* ********************************************************* * * Method begin: Class LinkedList *_________________________________________________________ * This function returns an iterator at the end of the linked list. * POST-CONDITIONS * temp: iterator pointer *_________________________________________________________ ***********************************************************/ template typename LinkedList::Iterator LinkedList::end() { Iterator temp(NULL); //iterator pointer

return temp; }

/* ********************************************************* * * Method length: Class IntList *_________________________________________________________ * This function returns the length of the list *_________________________________________________________ ***********************************************************/ template int LinkedList::length() const { return getLength(head); }

/* ********************************************************* * * Method getLength: Class IntList *_________________________________________________________ * This function recursively determines the length of the list. * PRE-CONDITIONS * ptr: Node pointer * POST-CONDITIONS * temp: Node pointer *_________________________________________________________ ***********************************************************/ template int LinkedList::getLength(Node *ptr) const {

if(ptr!= NULL) { //get next node ptr = ptr->next;

//return length of the list return 1+getLength(ptr); } else return 0; }

/* ********************************************************* * * Method sum: Class IntList *_________________________________________________________ * This function returns the sum of the list *_________________________________________________________ ***********************************************************/ template int LinkedList::sum() const { return getSum(head); }

/* ********************************************************* * * Method getSum: Class IntList *_________________________________________________________ * This function recursively determines the sum of all of the * elements in the list. * PRE-CONDITIONS * ptr: Node pointer * POST-CONDITIONS * temp: Node pointer *_________________________________________________________ ***********************************************************/ template int LinkedList::getSum(Node *ptr) const {

if(ptr!= NULL) {

//return the sum of all of the elements return ptr->data + getSum(ptr->next); } else return 0; }

template bool LinkedList::isEmpty() const { if(head == 0) return true; else return false; }

#endif // LINKEDLIST_H_INCLUDED

listEmpty.h

#ifndef LISTEMPTY_H_INCLUDED #define LISTEMPTY_H_INCLUDED #include #include using namespace std;

class ListEmpty { public: ListEmpty() { message = "The list is empty."; }

string what() { return message; } private: string message; };

#endif // LISTEMPTY_H_INCLUDED

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!