Question: Debug the following c++ code: It currently gives me a Segmentation Fault (core dumped) and gives me an incorrect output. Refer to the main for
Debug the following c++ code: It currently gives me a Segmentation Fault (core dumped) and gives me an incorrect output. Refer to the main for what the program is supposed to be doing.
p2.h:
-----------------------------------------
#include
#ifndef P2_H #define P2_H
class intLinkedList { private: class node { public: int data; // Same as cap from the previous code node *next; // Points to the next node node(const int v, node *ptr); }; node *root; // Points to the head of the array node *last; // Points to the tail of the array int count; // Records total entries public: void printIt(node * p, int v); int find(node *p, int v, int i) const; void clear(node * p); intLinkedList(); /* Precondition: No preconditions Postcondition: Initializes the pointers and sets the cap for the list */ ~intLinkedList(); /* Precondition: No preconditions Postcondition: Resets the number of elements. */ int insert(int v); /* Precondition: count < size Postcondition: Returns the value you needed. */ int add(int v); /* Precondition: Assumes v is small enough to store in a space Postcondition: Adds a value to the end of the list */ int insertAt(int v, int index); /* Precondition: Assumes that there is an existing index value Postcondition: Inserts the value at a specific location in the list */ int find(int value); /* Precondition: Assumes that there are contents for the function to sort through Postcondition: Finds a certain value in a list of values. */ void printIt(); /* Precondition: Assumes that there is content to print Postcondition: Prints to the command terminal */ void clear(); /* Precondition: Assumes that there is an array to clear Postcondition: Gives an empty list back */ int length() const; /* Precondition: Assumes there is something keeping track of entries Postcondition: Returns number of values in the list. */ int deleteAt(int index); /* Precondition: Assumes the index is not null Postcondition: Deletes the value at a specific location in the list */ }; #endif
-------------------------------------------------------------------------------------------
p2.cpp
-------------------------------------------------------------------------------------------
#include
using namespace std; // Allows all standard library items to be used
intLinkedList::intLinkedList() { root = last = NULL; count = 0; };
intLinkedList::node::node(const int v, node *ptr) { data = v; ptr = next; };
intLinkedList::~intLinkedList(){ clear(root); };
int intLinkedList::insert(int v){ int rc = 0; node *ptr = new node(v, root); if(root == NULL) { last = ptr; } root = ptr; count++; return rc; }
int intLinkedList::add(int v){ int rc = -1; node *ptr = new node(v, root); if (root = NULL) { root = ptr; }else { last -> next = ptr; rc = count; count++; } return rc; };
int intLinkedList::insertAt(int v, int index){ int rc = 0; node *ptr = new node(v, NULL); node *temp = root;
if ((index >= 0) && (index <= count) && (count != v)) { temp = temp -> next; count++; ptr -> next = temp -> next; temp -> next = ptr; rc = index; }else { rc = -1; } return rc; };
int intLinkedList::find(node *p, int v, int i) const { int rc = -1; if (p) { if (v == p -> data) { rc = i; }else { rc = find(p -> next, v, i+1); } } return rc; }
int intLinkedList::find(int v) { return find(root,v,0); }
void intLinkedList::printIt(node *p, int index) { if (p) { cout << index << "Has a value of " << p -> next << endl; printIt(p -> next, index + 1); } }
void intLinkedList::printIt() { printIt(root,0); };
void intLinkedList::clear(node *p) { return clear(p->next); };
void intLinkedList::clear() { clear(root); root = last = NULL; count = 0; }; int intLinkedList::length() const { return count; };
int intLinkedList::deleteAt(int index) { node *temp1; node *temp2 = root; int rc = -1; if (index >= 0 && index < count) { for (int i = 0; i < index - 1; i++) { temp2 = temp2 -> next; } temp1 = temp2 -> next; temp2 -> next = temp1 -> next; delete temp1; count--; rc = temp2 -> next -> data; } return rc; }
-------------------------------------------------------------------------
p2m.cpp
-------------------------------------------------------------------------
#include
using namespace std;
main(int argc, char *argv[]) { intLinkedList myLa;
int i;
for (i = 0; i < 40; i++) { cout << "insert of " << i << " got " << myLa.insert(i) << endl; }
cout << "List A has count of " << myLa.length() << endl; cout << "insertAt(100,25) got " << myLa.insertAt(100, 25) << endl;
cout << "find(20) returned " << myLa.find(20) << endl; myLa.printIt(); cout << endl; myLa.clear(); cout << "List A has count of " << myLa.length() << endl; }
------------------------------------------------------------------------------
OUTPUT
------------------------------------------------------------------------------
$ ./p2 insert of 0 got 0 insert of 1 got 0 insert of 2 got 0 insert of 3 got 0 insert of 4 got 0 insert of 5 got 0 insert of 6 got 0 insert of 7 got 0 insert of 8 got 0 insert of 9 got 0 insert of 10 got 0 insert of 11 got 0 insert of 12 got 0 insert of 13 got 0 insert of 14 got 0 insert of 15 got 0 insert of 16 got 0 insert of 17 got 0 insert of 18 got 0 insert of 19 got 0 insert of 20 got 0 insert of 21 got 0 insert of 22 got 0 insert of 23 got 0 insert of 24 got 0 insert of 25 got 0 insert of 26 got 0 insert of 27 got 0 insert of 28 got 0 insert of 29 got 0 insert of 30 got 0 insert of 31 got 0 insert of 32 got 0 insert of 33 got 0 insert of 34 got 0 insert of 35 got 0 insert of 36 got 0 insert of 37 got 0 insert of 38 got 0 insert of 39 got 0 List A has count of 40 Segmentation fault (core dumped)
THE ERROR IS LIKELY IN THE IMPLEMENTATION OF THE p2.cpp
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
