Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm stuck on a C++ problem. I have to build a singly-linked-list from an array and then remove a node from the linked-list. To remove

I'm stuck on a C++ problem. I have to build a singly-linked-list from an array and then remove a node from the linked-list. To remove the node I MUST pass the node one prior to the node I want to delete to a function(ex: If the linked-list holds {1, 2, 3, 4, 5} and I want to delete the node containing 3, I MUST pass the node containing 2 to the function removeAfterSingle). My code will delete from anywhere in the middle without issue, but I can't free the memory of the deleted node and I can't delete the first or last nodes. This is my code:

#include using namespace std; struct singleNum { int num; singleNum *next; }; struct singleNum *head1 = NULL; void buildListSingle(int num); void removeAfterSingle(struct singleNum *ptr1); void printListSingle(); int main() { int data[] = { 1, 10, 15, 50, 2, 11, 100, 33, 22 }; int sizeData = sizeof(data) / sizeof(data[0]); int i; int key; struct singleNum *ptr; for (i = 0; i < sizeData; i++) { buildListSingle(data[i]); } cout << "The singly-linked-list is: "; printListSingle(); cout << "Enter an array element to be deleted: "; cin >> key; ptr = head1; while (ptr->next != NULL) { if (ptr->next->num == key) { removeAfterSingle(ptr); } else { ptr = ptr->next; } } cout << "New singly-linked-list: "; printListSingle(); system("pause"); return 0; } void buildListSingle(int num) { struct singleNum *listSingle = new(nothrow)singleNum; listSingle->num = num; listSingle->next = NULL; struct singleNum *temp; if (head1 == NULL) { head1 = listSingle; } else { temp = head1; while (temp->next != NULL) { temp = temp->next; } temp->next = listSingle; } } void removeAfterSingle(struct singleNum *ptr1) { struct singleNum *temp; temp = ptr1->next; ptr1->next = ptr1->next->next; delete[](temp); } void printListSingle() { struct singleNum *temp; if (head1 != NULL) { temp = head1; while (temp != NULL) { cout << temp->num << " "; temp = temp->next; } } cout << endl; }

If I uncomment delete[](temp); in the removeAfterSingle function then my IDE (Visual Studio) says there's a problem with the line if(ptr.next->num != NULL) in the while loop of main.

I've tried using if/else statements in the removeAfterSingle function to delete the last node but that just leads to nothing being deleted.

I'm haven't even begun to see how to remove the first node.

I'm not sure if I'm passing the node to function in the best way. I tried creating a pointer to a node and just passing the pointer like I would any other pointer but that never seemed to work correctly.

Please help!!!

Update: I think I'm good on the freeing the memory. Where I really need the help is when I need to delete the first and/or last nodes.

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

Big Data Fundamentals Concepts, Drivers & Techniques

Authors: Thomas Erl, Wajid Khattak, Paul Buhler

1st Edition

0134291204, 9780134291208

More Books

Students also viewed these Databases questions