Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Why am I getting a Segmentation Fault 11?? If I only insert one node it successfully prints. But once I add a second, it fails

Why am I getting a Segmentation Fault 11?? If I only insert one node it successfully prints. But once I add a second, it fails to print

Sample Input:

"insertEnd hello"

InsertEndBegin

hello

Insert

"insertEnd there"

InsertEndBegin

there

Insert

InsertEndNode

Insert End done

"print"

Segmentation fault: 11

Code:

#include

#include

#include

using namespace std;

class Node{

public:

string line;

Node *next;

};

Node *head = NULL;

void insertNode(string instring, int pos){

//bounds check

cout<< "Insert" << endl;

/* Node *node = new Node;

node->line = instring;

node->next = NULL;

Node *temp = head;

*/

Node *addNode = new Node;

addNode->line = instring;

if(head==NULL){

head=addNode;

}

else{

if(pos==-1){

cout <<"InsertEndNode" <

Node *last = head;

while(last->next !=NULL ) last=last->next;

last->next = addNode;

cout <<"Insert End done"<< endl;

}

else if(pos==0){

node->next = temp;

head = node;

}

}

}

//trying to get this part working, replaces previous else loop

/*

else{

if(pos==-1){

while(temp->next!=NULL){

temp = temp->next;

}

temp->next = node;

temp->line = instring;

cout << "InsertEnd" << endl;

}

else if(pos==0){

node->next = temp;

head = node;

}

else{

int i=0;

while(i

temp = temp->next;

}

node->next = temp->next;

temp->next = node;

}

}

cout << "InsertEndDone" << endl;

*/

void deleteNode(int pos){

Node *temp = head;

Node *prev = NULL;

if(pos==0){

head = head->next;

}

else{

int i = 0;

while(i

prev = temp;

temp = temp->next;

i++;

}

prev->next=temp->next;

}

}

void editNode(int pos, string instring){

Node *temp = head;

if(pos==1){

head->line = instring;

}

else{

while(pos){

temp = temp->next;

pos--;

}

temp->line = instring;

}

}

void printList(){

Node *temp = head;

if(temp ==NULL){

cout<<"The Document is Empty"<

return;

}

int linenum = 1;

while(temp!=NULL){

cout << linenum <<" "<< temp->line <

temp=temp->next;

linenum++;

}

}

vector split(string inputstring){

vector vec;

string s = "";

int state = 0;

for(int i; i

switch(state){

case 0:

if(inputstring[i]==' '){

vec.push_back(s);

s = "";

}

else if(inputstring[i]=='"'){

state = 1;

}

else{

s+=inputstring[i];

}

break;

case 1:

if(inputstring[i]=='"'){

state = 0;

vec.push_back(s);

s = "";

}

else{

s+=inputstring[i];

}

}

//cout<

}

if(s.length()>0){

vec.push_back(s);

}

return vec;

}

int main() {

head=NULL;

string input;

do{

getline(cin, input);

if(input=="quit"){

break;

}

vector vec = split(input);

if(vec[0]=="insertEnd"){

cout << "InsertEndBegin" <

cout << vec[1] << endl;

insertNode(vec[1], -1);

}

else if(vec[0]=="print"){

printList();

}

else if(vec[0]=="delete"){

deleteNode(stoi(vec[1]));

}

else if(vec[0]=="edit"){

editNode(stoi(vec[1]),vec[2]);

}

else if(vec[0]=="insert"){

cout << "InsertPosBegin" << endl;

insertNode(vec[2],stoi(vec[1]));

}

}while(true);

return 0;

}

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

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

More Books

Students also viewed these Databases questions

Question

What really makes you angry?

Answered: 1 week ago