Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

remove all the occurrences of 2's from the linked list recursively //****************************************** list.h************************** struct node { int data; node * next; }; class list {

remove all the occurrences of 2's from the linked list recursively //****************************************** list.h************************** struct node { int data; node * next; }; class list { public: //These functions are already written for you list(); //supplied ~list(); //supplied void build(); //supplied void display(); //supplied /* *****************YOUR TURN! ******************************** */ //Write your function prototype here: int removeTwo(); int removeTwo( node *& head); private: //notice there is both a head and a tail! node * head; node * tail; };

what i have:

int removeTwo()

{

if (head == NULL) return 0;

removeTwo(head);

}

int removeTwo(node *& head)

{ node * t = head;

if(t->data == 2)

{ head = head->next; delete t; removeTwo(head) }

removeTwo(head->next); }

this function works, except if the list has a 2 at the end. then it seg faults. please help!!

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

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions