Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// My code won't run im nearly complete been at it for 48hours or so and i just really need some fresh eyes im getting

// My code won't run im nearly complete been at it for 48hours or so and i just really need some fresh eyes im getting disoriented... I believe that my //remove last function and perhaps my generic remove function is giving me trouble also ive attached my test file its getting hung on the reml function //somehow ....I need some help getting it running and putting it through some rigorous testing like my test file provided.

#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 < 0) || (n>s)) { cout<< "Invalid index. Try a number between 0 and "<

} 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 */ cout<<4<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 */ cout<<1<next !=NULL) {cout<<3<next; } // remove last node

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

cout << " DUMP: (size = " << linked_list::size() << ", first = " << get_first() << ", last = " << get_last() << ") ";

if (head == NULL) { cout << " DUMP: head = NULL "; return; }

tptr = head;

while (tptr) { cout << " DUMP: data = : " << tptr->data << endl; tptr = tptr->next; } cout << endl;

}

};

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

while (cin >> cmd >> i >> d) { cout << "MAIN: cmd = " << cmd << ", i = " << i << ", d = " << d << endl;

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 << "get returns: " << d << endl; } else if (cmd == "getf") { d = ll.get_first(); cout << "getf returns: " << d << endl; } else if (cmd == "getl") { d = ll.get_last(); cout << "getl returns: " << d << endl; } else if (cmd == "dump") ll.dump(); else if (cmd == "quit") exit(0); } } /* test_FIle:

#addNodeToEmptyList 9 9 add 0 3 dump 9 9 #addNOdenumber9ToFrontOfLIst 9 9 addf 9 9 dump 9 9 #removeNumber9atindex0 9 9 rem 0 0 dump 9 9 #deletethelastnode 9 9 reml 9 9 #add3 nodes in different orders addf 0 44 dump 9 9

add 1 22 dump 9 9 add 1 11 dump 9 9 #addtofindex4 9 9 add 4 44 */

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

Step: 3

blur-text-image

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

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

Students also viewed these Databases questions

Question

Why do people behave unethically?

Answered: 1 week ago

Question

2. Record these without judgments, criticisms, or comments.

Answered: 1 week ago

Question

=+ Are there closed-shop requirements or practices?

Answered: 1 week ago