Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My test code for my code is hanging on my remove_last call in my main function via: lab It should remove node than dump the

My test code for my code is hanging on my remove_last call in my main function via: lab

It should remove node than dump the empty list to screen but it gets caught in a infinite loop after running my link.remove_last() function. It does in fact remove the last but is stuck on something perhaps missing a return but as my test file is supposed to continue and make a new list after all the final node is removed but for some reason when i remove last on a list that has only one node it removes it and terminates my program which i don't want here is the outputimage text in transcribed

it is supposed to continue and call the next command addf from my test file but instead it brings me back to the command prompt/ terminal

here is my test fileimage text in transcribed

reml is where my program terminates after removing a one node list with remove_last yet it should run the next command after dump whcih is addf 0 44... it does run dump again before exiting so im confused on that too please help me to fix my code to where my test will run all the commands til the end if you can shed some light as well I want to be the best and i'm new so extra info is greatly appreciated.

*/ #include #include #include

using namespace std;

class node { public: typedef int data_t;

node *next; data_t data;

node(data_t d) { next = NULL; data = d; } };

class linked_list

{ private: node *head;

public:

linked_list() { head = NULL; }

int size() { if (!head) return 0; int c = 0; node *tptr = head; while(tptr){ c++; tptr = tptr->next; } return c; }

void add(int n, node::data_t d) { /* i = index of current node prev - holds the previous node tptr - iterate over the list curr - points to new node s - size of list */ node *tptr = head, *prev = head, *curr; int c = 0; int s = size(); // Invalid index insert if ((n s)) { cout

} if (n==size()) { add_last(d); return; }

}

void add_last(node::data_t d) { if (!head) { head = new node(d); return; }

node *tptr = head; while(tptr) { if(tptr->next==NULL) tptr->next = new node(d); return; } }

void add_first(node::data_t d) {

if (!head)head = new node(d); else{

node *tptr = head; head = new node(d); head->next = tptr; }}

node::data_t get(int n) { node *tptr = head; int c = 0; // If n = 0 "return the data of first node in a list if (n == 0) return get_first(); // If n = size() of list "return the data at last node" if (n==size()) return get_last(); // Iterate and get data from Node while(tptr) { if (n==c) return tptr->data; tptr=tptr->next; c++; } // If node index doesn't exist return -1; }

node::data_t get_first() { // If list is empty return -1 if (!head) return -1; else return head->data; // Return the head->data }

node::data_t get_last() { node *tptr = head; // If list is empty if (!head) {return -1;} // Iterate through to the last node while (tptr->next) { tptr = tptr->next;

} return tptr->data; }

void remove(int n) { /* tptr - points to current node prev - points to previous c - position of current */

node *tptr = head, *prev =head; tptr = head; prev = head; int c = 0; // if node to be removed is the first if (n==0) { remove_first(); return; } // remove last node if (n == size()) { remove_last(); ; } // handle removal at given point while(tptr) { if (c==n) { prev->next = tptr->next; return; } c++; prev = tptr; tptr = tptr->next; }

}

void remove_first() { if(!head) return; // exit if no nodes node *tptr = head; head = head->next; delete tptr;

}

void remove_last() { /* tptr - pointrer to node prev - pointer to previous node */ node *tptr = head, *prev = head; //if no nodes if(!head) cout

} while(tptr->next !=NULL) { prev = tptr; tptr= tptr->next; } // remove last node

prev->next = NULL; } void dump() { node *tptr;

cout

if (head == NULL) { cout

tptr = head;

while (tptr) { cout data next; } cout

}

};

int main(void) { linked_list ll; string cmd; int i, d;

while (cin >> cmd >> i >> d) { cout

if (cmd == "add") ll.add(i, d); else if (cmd == "addf") ll.add_first(d); else if (cmd == "addl") ll.add_last(d); else if (cmd == "rem") ll.remove(i); else if (cmd == "remf") ll.remove_first(); else if (cmd == "reml") ll.remove_last(); else if (cmd == "get") { d = ll.get(i); cout g++ .CPP [. File

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 9 For Linux UNIX And Windows Advanced Database Administration Certification Certification Study Guide

Authors: Roger E. Sanders, Dwaine R Snow

1st Edition

1583470808, 978-1583470800

More Books

Students also viewed these Databases questions