Page 1 of 1

C++ HELP WITH LINKED LISTS Please! Need help rewriting linked lists. Insructions and specifications are below as well as

Posted: Sun Jul 10, 2022 11:27 am
by answerhappygod
C++ HELP WITH LINKED LISTS Please! Need help rewritinglinked lists. Insructions and specifications are below as well asobvious organized code files. I will also include the errormessages I get.
C Help With Linked Lists Please Need Help Rewriting Linked Lists Insructions And Specifications Are Below As Well As 1
C Help With Linked Lists Please Need Help Rewriting Linked Lists Insructions And Specifications Are Below As Well As 1 (268.05 KiB) Viewed 53 times
C Help With Linked Lists Please Need Help Rewriting Linked Lists Insructions And Specifications Are Below As Well As 2
C Help With Linked Lists Please Need Help Rewriting Linked Lists Insructions And Specifications Are Below As Well As 2 (270.92 KiB) Viewed 53 times
Errors I'm getting:
C Help With Linked Lists Please Need Help Rewriting Linked Lists Insructions And Specifications Are Below As Well As 3
C Help With Linked Lists Please Need Help Rewriting Linked Lists Insructions And Specifications Are Below As Well As 3 (40.88 KiB) Viewed 53 times
3 organized code files:
//file data.h/////////////////////////////////////////////#ifndef DATA_H#define DATA_H
#include "string"using std::string;
struct Data { int id; string data;};
struct Node { Data data; Node *next; Node *prev;};
#endif /* DATA_H *////////////////////////////////////////////////////////////////linkedlist.h//////////////////////////////////////////////////// # ifndef LINKED_LIST# define LINKED_LIST
# include "data.h"# include <iomanip># include <string># include <stdlib.h># include <iostream># include <new>
class LinkedList{
public: LinkedList(); ~LinkedList(); bool addNode(int, string*); bool deleteNode(int); bool getNode(int, Data*); void printList(bool = false); int getCount(); bool clearList(); bool exists(int);
private: Node *head;
bool addHead(int, string); bool nodeCouple(bool, int, string, Node*,Node*); bool deleting(Node*); /* You may want to create private helper functions, forexample: ○ addHead() and deleteHead() ○ addTail() and deleteTail() ○ addMiddle() and deleteMiddle() */};
# endif///////////////////////////////////////////////////////////////////////////////////linkedlist.cpp/////////////////////////////////////////////////////////////////////// # include "linkedlist.h"//contructor========================================================= LinkedList::LinkedList(){ Node *head = NULL; // pointer to a Nodebut pointing at "nothing" for now }//destructor========================================================= LinkedList::~LinkedList(){ // destructor this->clearList();}
//addHead()===============================================================bool LinkedList::addHead(int id =- 1, string data = ""){ head = new Node;
head->data.id = id; head->data.data = data; head->next = nullptr; head->prev = nullptr;
return true;}//addNode()===============================================================bool LinkedList::addNode(int id = -1, string data = ""){ // set perameters to default -1 and ""; // will return false if default perameters areset bool success=false; // flag
if(id >= 0 && !head){ // newNode is 1stnode success = this->addHead(id,data); } else if(id >= 0 && head){
Node*current = head; Node*newNode = new Node; newNode->data.id = id; newNode->data.data = data; newNode->next = nullptr; newNode->prev = nullptr;
while(id > current->data.id&& current->next){ current=current->next; } if(id!=current->data.id) { // test if dupe ids arepresent success =nodeCouple(success, id, data, current, newNode); } } return success;}
//nodeCouple()============================================================bool LinkedList::nodeCouple(bool success, int id=-1, stringdata="", // addNode() extension Node*current=nullptr, Node*newNode =nullptr){
if(id>current->data.id &&!current->next) { // newNode==tail current->next = newNode; newNode->prev = current; }
else if(id<current->data.id &&!current->prev) { // newNode==head current->prev = newNode; newNode->next = current; head = newNode; }
else { // newNode==middle (newNode occurs beforecurrent node) newNode->prev =current->prev; newNode->next = current; current->prev->next =newNode; current->prev = newNode; }
return true;}//deleteNode=============================================================bool LinkedList::deleteNode(int id = -1){
bool success = false;
if(id >= 0 && head) { // do not enter method body if id's invalid orlist is empty
Node*current = head; // allocatingcurrent at head
while(!(id == current->data.id)&& current->next) { // increment untilcurrent=tail or current->id=target id current =current->next; }
if(id == current->data.id) { // current==target fordeletion success=deleting(current); }
}
return success;
}//deleting()=================================================bool LinkedList::deleting(Node*current){ // deleteNode() extension
if(!current->next &&!current->prev){ // current==only node in list head = nullptr; }
else if(!current->prev){ // current==head head = current->next; current->next->prev = nullptr; }
else if(!current->next){ // current==tail current->prev->next = nullptr; }
else{ // current==middle current->prev->next =current->next; current->next->prev =current->prev; }
delete current; return true;} //getNode()===========================================================bool LinkedList::getNode(int id=-1, Data*dummyData=nullptr){ bool success = false;
if(id >= 0 && head) { // i.e. do not enter unless conditionsfor successful // "getting" are present. Placing conditionalhere // also prevents segmentation fault (e.g.would seg fault // if trying to access current->nextwhen head=nullptr)
Node*current = head;
while(current->next && id!= current->data.id){ // increment until end of list orid==current->id current =current->next; } if(id==current->data.id){ //current==target dummyData->data =current->data.data; dummyData->id =current->data.id; success = true; } } return success;}//getCount()===========================================int LinkedList::getCount(){ int count=0; if(head){ // check that list isn't empty Node*current = head; while(current){ count++; current =current->next; } }
return count;}//clearList()================================================bool LinkedList::clearList(){ bool success = false; int count=0; int length = this->getCount();
Node*current = head; Node*tmpNode; // for holding and deleting currentnode so that "current" can still be incremented
while(current){ count++; tmpNode = current; // assign currentnode to tmp current = current->next; //increment current delete tmpNode; // delete tmpnode } head=nullptr; if(length==count){ success=true; } return success;}//exits()========================================================bool LinkedList::exists(int id =- 1){ // default perameter is -1 bool success = false; Node*current = head; while(id != current->data.id &&current->next){ current = current->next; } if(id==current->data.id){ success=true; } return success;}void LinkedList::printList(){ }
● Write a complete and proper doubly linked list conforming to the definition given here and in the lecture notes, following all best practices and loose coupling. The linked list itself will be a collection of self-referential "nodes" which are defined for you in data.h Your class will have one-and-only-one class attribute: Node *head; // a pointer to the first node or NULL if the list is empty. Upon creation the linked List object will be "empty." i.e. It will contain no nodes. head will point to NULL or nullpointer. Upon destruction, call your clearList() method. Methods may not be more than about 24 lines long. If they are, break them up into properly modular methods. You can make any private methods you like. Public methods (these are the only public methods allowed): ● O bool addNode(int, string*); pass this method an int id and some non-empty string by reference. The id must be unique and a positive number greater than 0. Return true or false to indicate success or failure. Nodes added must be stored in ascending order of id. Memory for the node must be allocated inside the linked list object. This method must do proper error checking. Ids cannot be negative and duplicate ids are not allowed. The test code will test this. DO NOT use your own exists() method to look for non-unique ids, and do not duplicate the exists() functionality to look for non-unique ids. If you do that, that will increase the processing time of your method by a factor of 2x. You want to first only verify your id is a positive int, and the string is non-empty, then search for the place to add the new node, and during the search look for duplicates, then after you determine there is no duplicate, then allocate your memory and insert the node. This is the algorithm you must follow to ensure efficiency and no memory leaks. If you deviate from that algorithm, your code will not be correct, even if it "works."
o bool deleteNode(int); pass this method an id to delete. Return true or false to indicate success or failure. Delete the memory the node was using. DO NOT use your own exists() method to first look for the id to delete, just search and delete in one loop. o bool getNode(int, Data*); pass this method an id and an empty struct Data passed by reference from main(). ■ If the id is found, fill the empty struct Data passed by reference, and return true. If the id is not found, place a -1 in the id and empty string in the string and return false. ■ DO NOT use your own exists() method to look first for the node, just search and find it in one loop. void printList(bool = false); Will print out the entire list in order either forward or backward based on the bool passed in. Make the bool default to false (i.e. forward). Your prototype will look as given, and your definition should be something like void printList(bool backward){} This is the only method allowed to print. o int getCount(); Get a count of the nodes in the list. You must calculate count on the fly each time this is called. You may not maintain a class attribute count. bool clearList(); Resets the linked list, deletes all allocated memory and sets head = NULL; bool exists(int); Checks to see if an id exists in the list. o Adding and deleting always leaves the link list properly sorted in ascending order. O Remember all good programming practices (including but not limited to the following): ■ Only one return per statement. Do not repeat yourself. If you have repeated code, you will lose points. The most common place people break this rule is in allocating a new node and assigning values to that node. Do not duplicate any code, especially the node allocation/assignment code. This will make your addNode() architecture challenging but it is what you need to do to learn proper architectural practices. ■ Never use break or continue in loops. ■ Keep functions small, no more than about two dozen lines. ■ Do not nest if statements or loops more than 2-3 deep. Do not print from any method except specific printing methods.
linkedlist.cpp:35:6: error: no declaration matches 'bool LinkedList::addNode(int, std::string)' 35 | bool Linked List::addNode(int id = -1, string data = "") ^~~~~~~~~~ In file included from linkedlist.cpp:7: linkedlist.h:25:10: note: candidate is: 'bool LinkedList::addNode(int, std::string*)' bool addNode(int, string*); 25 ^~~~~~~ linkedlist.h:18:7: note: 'class LinkedList' defined here 18 class LinkedList ^~~~~~~~~~ linkedlist.cpp:253:6: error: no declaration matches 'void LinkedList::printList()' 253 void LinkedList::printList() { ^~~~~~~~~~ In file included from linkedlist.cpp:7: linkedlist.h:28:10: note: candidate is: 'void LinkedList::printList(bool)' void printList(bool = false); 28 ^~~~~~~~~ linkedlist.h:18:7: note: 'class LinkedList' defined here 18 class LinkedList ^~~~~~