Can someone help me with the implementation file which is the last part of the c
ID: 3590651 • Letter: C
Question
Can someone help me with the implementation file which is the last part of the code I wrote, we are to only work on that section, the implementation cpp. we can only work on that section, the header and main cpp are for reference, to use their variables, functions, classes etc. please! I still cant get linked lists! //\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\ // Program: Lab 05 // Author: // Course: CSCI/CMPE 2380 // Description: This program builds a doubly linked list from entered numbers // and sorts them as they are added. //\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\ #include "SortedLL.h" int main() { //initialize our pointer to NULL SortedLL LL; int input; cout<<"Please input some numbers."<>input; while( !cin ) { cin.clear(); cin.ignore(2000, ' '); cout<<"Not a legal integer"<> input; } //if they're positive, add them to the linked list if(input > 0) LL.SortedAdd(input); } while( input > 0 ); cout<<"The numbers you entered are:"< //\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\ // Program: Lab 05 // Author: // Course: CSCI/CMPE 2380 // Description: This is the header file for the linked list class //\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\ #include using namespace std; //a basic node for a doubly linked list with data struct Node { int data; Node *next; Node *prev; }; //a linked list class we will use class SortedLL { public: SortedLL(); ~SortedLL(); void Add(int); void SortedAdd(int); void PrintList(); private: void DeleteList(); Node* Head; }; //\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\ // Program: Lab 05 // Author: // Course: CSCI/CMPE 2380 // Description: This is the implementation file for the linked list class //\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\ #include "SortedLL.h" //constructor SortedLL::SortedLL() { //should set head pointer } //destructor SortedLL::~SortedLL() { //should call delete list } /* This function adds a new node to the end of the doubly linked list. If it is the first node, then head will still be NULL. Here we are using head directly, so that when we create a new node it stores it in the variable being used in main. head <=> [2|] <=> [8|] <=> [6|X] */ void SortedLL::Add(int newdata) { //create a new node to hold the data with a terminal (NULL) next pointer //check whether head has been initialized (is NULL) //if not, make the new node head and set prev //if head has been initialized //find the end of the chain with a pointer //add the new node on to the last node in the list //set pointers both forward and backwards } /* This function adds a new node in the list where the data is sorted from low to high. */ void SortedLL::SortedAdd(int newdata) { //create a new node to hold the data with a terminal (NULL) next pointer //check whether head has been initialized (is NULL) //if it is null, make the new node head and set prev //else head is not null //find the spot to insert it //check for special cases } //This function walks through the list and prints the integer for each of the nodes //We are using a copy of head, so changing it's value does not alter the //variable in main void SortedLL::PrintList() { //walk through the list starting at head //print the data for the current node } //This function walks through the list and deletes each node void SortedLL::DeleteList() { //make sure the list has data if(Head != NULL) { //delete first node Node *next = Head->next; delete Head; //loop through list deleting each node while (next != NULL) { Head = next; next = Head->next; delete Head; } //set pointer to null Head = NULL; } }
Explanation / Answer
/*suppose 1 2 3 are the numbers where head points to 1 . Now copy 1 to a pointer p
Now before deleting that location we need to find the next number
Use another pointer temp for that purpose by (temp=p->next) which redirects you to next value
now delete the value which p is holding and set the p value to temp
Repeat the above cycle until p value becomes null */
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.