Question
C++ format, if you can complete the extra credit functions that would be nice. Below are the starter codes. LList.h #ifndef LLIST_H #define LLIST_H /*
C++ format, if you can complete the extra credit functions that would be nice. Below are the starter codes.
LList.h
#ifndef LLIST_H #define LLIST_H
/* Linked List class that store integers, with [] operator. Uses head pointer. */ #include
#define int int
using namespace std;
struct node_t { int data; node_t* next; };
// This implementation will use a head pointer, // allowing O(1) insertion on the front, // and O(n) on the end. class LList {
public: LList(){ head = NULL; }
~LList(){ clear(); }
LList(const LList& other){ // Do the same as the default constructor head = NULL; // Check if the other LList is empty if(other.head == NULL){ return; } // Not empty? Iterate through the other list // and push_back on myself. node_t* temp = other.head; while(temp){ push_back(temp->data); temp = temp->next; } }
// Similar to copy constructor, but check for self // assignment, if not, clear and copy all data. LList& operator= (const LList& other){ if(this == &other){ return *this; } clear(); if(other.head == NULL){ return *this; } node_t* temp = other.head; while(temp){ push_back(temp->data); temp = temp->next; } return *this; }
bool empty() const { // TODO: Fill me in }
unsigned int size() const { // TODO: Fill me in }
void push_back(int value){ // TODO: Fill me in }
void push_front(int value){ // Empty list? if(head == NULL){ head = new node_t; head->data = value; head->next = NULL; }else{ // Not empty node_t* temp = new node_t; temp->data = value; temp->next = head; head = temp; } }
void pop_front(){ if(head == NULL) return; node_t* temp = head; head = head->next; delete temp; }
void pop_back(){ // TODO: Fill me in }
// Overload [] operator // Return logic error if index out of bounds int& operator[](unsigned pos){ node_t* temp = head; while(temp != NULL && pos > 0){ temp = temp->next; pos--; } // As long as I don't have a null pointer, assign. if(pos == 0 && temp != NULL){ return temp->data; } throw logic_error("Index invalid"); }
LList reverse() const { // TODO: Fill me in return LList(); // Remove me! }
bool operator==(const LList& other) const { }
bool operator!=(const LList& other) const { return !operator==(other); }
void clear(){ node_t* last = head; while(head){ head = head->next; delete last; last = head; } // Normaly you never want to change head or you'll orphan part // of the list, but in this case we are wiping the list, // so it is ok to so and saves us a variable. head = NULL; }
private: node_t* head;
};
// Note this function is O(n^2) because getAt is O(n) and we are // doing it n times.
ostream& operator 0){ out
#endif
#ifndef LLIST_H #define LLIST_H
/* Linked List class that store integers, with [] operator. Uses head pointer. */ #include
#define int int
using namespace std;
struct node_t { int data; node_t* next; };
// This implementation will use a head pointer, // allowing O(1) insertion on the front, // and O(n) on the end. class LList {
public: LList(){ head = NULL; }
~LList(){ clear(); }
LList(const LList& other){ // Do the same as the default constructor head = NULL; // Check if the other LList is empty if(other.head == NULL){ return; } // Not empty? Iterate through the other list // and push_back on myself. node_t* temp = other.head; while(temp){ push_back(temp->data); temp = temp->next; } }
// Similar to copy constructor, but check for self // assignment, if not, clear and copy all data. LList& operator= (const LList& other){ if(this == &other){ return *this; } clear(); if(other.head == NULL){ return *this; } node_t* temp = other.head; while(temp){ push_back(temp->data); temp = temp->next; } return *this; }
bool empty() const { // TODO: Fill me in }
unsigned int size() const { // TODO: Fill me in }
void push_back(int value){ // TODO: Fill me in }
void push_front(int value){ // Empty list? if(head == NULL){ head = new node_t; head->data = value; head->next = NULL; }else{ // Not empty node_t* temp = new node_t; temp->data = value; temp->next = head; head = temp; } }
void pop_front(){ if(head == NULL) return; node_t* temp = head; head = head->next; delete temp; }
void pop_back(){ // TODO: Fill me in }
// Overload [] operator // Return logic error if index out of bounds int& operator[](unsigned pos){ node_t* temp = head; while(temp != NULL && pos > 0){ temp = temp->next; pos--; } // As long as I don't have a null pointer, assign. if(pos == 0 && temp != NULL){ return temp->data; } throw logic_error("Index invalid"); }
LList reverse() const { // TODO: Fill me in return LList(); // Remove me! }
bool operator==(const LList& other) const { }
bool operator!=(const LList& other) const { return !operator==(other); }
void clear(){ node_t* last = head; while(head){ head = head->next; delete last; last = head; } // Normaly you never want to change head or you'll orphan part // of the list, but in this case we are wiping the list, // so it is ok to so and saves us a variable. head = NULL; }
private: node_t* head;
};
// Note this function is O(n^2) because getAt is O(n) and we are // doing it n times.
ostream& operator 0){ out
#endif
list-sandbox.cpp
/* Fill me in */
#include
#include "LList.h"
using namespace std;
int main(){ LList a; for(int i = 0; i
return 0; }
Part B: Linked Lists Task1: Modify LList.h to implement the functions or operator overloading listed below. Most of the functions are stubbed out so you just need to add the proper logic. You will be editing the LList.h file and compiling the sandbox file. Here is a list of the required methods you must fill in: LList.h push back (int) - Place the given value on the end of the linked-list reverse() const - Return a new linked-list which is the current one reversed size ( ) -Returns the size (length) of the linked-list * pop_back () - Removes the last element of the linked-list. If the list is empty operator-= (LL1 st R) -Return true if the current linked-list contains Overload the plus (+) operator to add two lists together emptyO - Return true if the list is empty otherwise return false. do nothing identical values to the passed one Extra Credit: 10-Points Eaclh getAt (unsigned) -Return the value at the given position, or throw a logic error if the index is invalid. See the overloaded [] operator for hints on how to throw an exception . setAt (int, unsigned) Assign the given value to the given position (0-based). Throw a logic error if the index is invalid You can use list-sandbox. cpp to experiment with the linked-list classes. Compile list-sandbox.cpp toa list-sandbox executable. List-sandbox.cpp will not be graded Tip for Testing: Use the list-sandbox.cpp to create linked list objects and also test out your various list modifying functions. LList.h has a definition for overloadedStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started