Answered step by step
Verified Expert Solution
Question
1 Approved Answer
/ / Linked Lists struct node { int data; node * next; } void add _ to _ end ( node * & head, int
Linked Lists
struct node
int data;
node next;
void addtoendnode& head, int item;
void removenode& head, int target;
int main
node head;
mark the list as empty
head nullptr; NULL
add a node
head new node;
mark this as end of the list
headnext nullptr;
headdata ;
add more nodes
headnext new node;
headnextnext nullptr;
same as line above
head next next nullptr;
Start the cursor at the first node
node cursor head;
Move cursor until it points to the last node NOT NULL
whilecursor next nullptr
cursor cursor next;
Add a new node to the end of the list
cursor next new node;
cursor next next nullptr;
next do the same thing
cursor cursor next;
cursor next nullptr;
output everything in the list
cursor head;
whilecursor nullptr
cout cursor data endl;
cursor cursor next;
This line would cause a seg fault because cursor is pointing to NULL after the loop
cursor next new node;
addtoendhead;
removehead;
void addtoendnode& head, int item
ifhead nullptr
list is empty
head new node;
head next nullptr;
head data item;
return;
node cursor head;
move cursor to last node
whilecursor next nullptr
cursor cursor next;
add new node
cursor next new node;
cursor next next nullptr;
cursor next data item;
What will this function do with an empty list?
void removenode& head, int target
node cursor head;
node prev head;
ifhead data target
head cursor next;
delete cursor;
return;
move cursor to the target node
whilecursor nullptr && cursor data target
prev cursor;
cursor cursor next;
ifcursor nullptr
prev next cursor next;
delete cursor;
else
cout "Target not found
;
or this lab you are to implement two functions, and I have given you the prototypes for the functions.
The first function passes through the linked list and removes all duplicate items. You will need at least two, most likely three, additional pointers to pull this off. Remember that to remove an item you will need to hold onto the node before the one you are removing. When you are done, the total size of the linked list should be I have given you a size function so you can check that. You should have the main call the showlist function to print out this new smaller list.
The second function takes the cleanedup list, and a split value, which is a number between and entered by the user. The function then creates two new lists, one having only numbers greater than the split value and one having only numbers less than the split value. The split value should not appear in either list. The sum of the size of these two lists should be equal to or one less than the cleanedup list you built with the first function. The first function alters the original list, and this function creates two new lists.
#include
#include
#include "headerlabh
using namespace std;
These are the three I have implemented for you which are
available in the headerlabh file
void buildlistnode& head; note that we are passing by reference
void showlistconst node head;
int sizeconst node head;
These are the two that you are to write, as described in the instructions page.
Please write them as a part of the headerlabh file.
void removerepeatsnode& head;
void splitlistconst node original, node& lesser, node& greater, int splitvalue;
Feel free to edit the main as much as you like for debugging.
Please run the command 'make runtests' to test your code before submitting.
int main
int start, stop;
int split;
node head NULL;
node lesser;
node greater;
start timeNULL;
buildlisthead;
stop timeNULL;
cout "Time to build list stop start "seconds.
;
start timeNULL;
showlisthead;
stop timeNULL;
cout "Time to print list stop start "seconds.
;
cout "Size of the list sizehead endl;
return ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started