Question: PLEASE DO NOT SPAM THE QUESTION I WILL AUTOMATICALLY DISLIKE IT (IF YOU SOLVE IT PLEASE POST A PICTURE OF IT WORKING ON YOUR END,
PLEASE DO NOT SPAM THE QUESTION I WILL AUTOMATICALLY DISLIKE IT (IF YOU SOLVE IT PLEASE POST A PICTURE OF IT WORKING ON YOUR END, THANKS)
In C++ Fix the code. It is not outputting what it is supposed to. It is not appending or inserting properly. Code down bellow

// File : list_demo.cpp
#include
#include "List.h"
// simulated menu choice operations on the list
enum ListOp {
LIST_APPEND,
LIST_INSERT,
LIST_PRINT
};
// --------------------------------------------------------
// Function Declarations (proto-types)
// --------------------------------------------------------
void appendList(List* list);
void insertList(List* list);
void printList(List* list);
/**
* Default constructor - initialize empty list
*/
List::List() {
head = NULL;
size = 0;
} // default constructor
/**
* Destructor - clean up nodes
*/
List::~List() {
Node* curr = head;
while( curr != NULL )
{
Node* temp = curr;
curr = curr->next;
delete temp;
}
} // destructor
/**
* Accessor for the list size property
* @return int size (empty = 0)
*/
int List::getSize() {
return size;
} // getSize
/**
* Signifies if the list is empty or not
* @return bool True if empty (size==0)
*/
bool List::isEmpty() {
return (size==0);
} // isEmpty
/**
* Add a new value into the list at the head, a
* specified position, or at the tail if position
* is -1 or invalid position
* @param value - int value to store
* @param position - position in list to store value
*/
void printList(List* list) {
int size =list->getSize();
if (size) {
std::cout
for (int i = 0; i
std::cout read(i);
if (i
std::cout
}
std::cout
}
else {
std::cout
}
} // printList
void List::insert(int value, int position) {
// if position == -1, means adding node to end
if(position==-1)
{
//creating node
Node *newNode = new Node();
Node *last = head;
newNode->value = value;
newNode->next = NULL;
// is list is empty, then insert at starting
if(head==NULL)
{
head = newNode;
size++;
return;
}
// go to end of linked list
while(last->next!=NULL)
{
last = last->next;
}
// insert at end
last -> next = newNode;
size++; // increase the size
return;
}
else // else if position is not -1
{
Node *temp = head;
int x = 0;
// going to that position first
while(temp)
{
if(x==position)
{
// creating node, and inserting at position
Node *t= new Node();
t->value = value;
t->next = temp;
temp = t;
size++;
return;
}
x++;
temp=temp->next;
}
}
} // insert
void insertList(List* list) {
int value = rand() % 100; // calc random number value
int position = list->getSize(); // init to size in case list is empty
if(list->isEmpty()) {
list->insert(value); // test 1-parameter insert w/ default position
}
else {
position = rand() % list->getSize(); // calc random position (0 (size -1))
list->insert(position, value); // test 2-parameter insert
}
std::cout
} // insertList
int List::read(int position) {
int x = 0;
Node *temp =head;
// going to that position and returning the value
while(temp)
{
if(position==x)
{
return temp->value;
}
x++;
temp = temp->next;
}
// if position is not found, return -1
return -1;
} // read
/**
* Modify a value at a specified position in the list
* @param value - new value
* @param position - position in the list
*/
void List::modify(int value, int position) {
int x = 0;
// goinf to that position and modifying value
Node *temp = head;
while(temp)
{
if(position==x)
{
temp->value = value;
}
x++;
temp = temp->next;
}
} // modify
void appendList(List* list) {
srand(time(NULL)); // refresh random numbers
int value = rand() % 100; // calc random number value
list->insert(value); // test 1-parameter insert w/ default position
std::cout getSize() - 1
std::endl;
}
int main()
{
// sample run of program
// created list
List list; // the one and only List
char again; // user choice to continue
do {
srand(time(NULL)); // refresh random numbers
// random list operations
int op = rand() % 4 ;
switch(op) {
case LIST_APPEND:
appendList(&list);
break;
case LIST_INSERT:
insertList(&list);
break;
}
// print the current list
printList(&list);
// prompt user to continue
std::cout
std::cin.get(again);
std::cout
} while(again != 'N' && again != 'n');
return 0; // return success to OS
}
// File : List.h
#ifndef LINKEDLIST_LIST_H
#define LINKEDLIST_LIST_H
// constants
// --------------------------------------------------------
const int LIST_HEAD = 0; // list position of head node
const int LIST_TAIL = -1; // specify current tail position
class List {
private:
// internal storage structure for a Node
struct Node {
int value;
Node* next;
};
Node* head; // hold reference to head of the list (Node[0])
int size; // current size of the list (empty==0)
public:
List();
~List();
int getSize();
bool isEmpty();
void insert(int value, int position=LIST_TAIL);
int remove(int position=LIST_TAIL);
int read(int position);
void modify(int value, int position);
private:
Node* traverse(int position);
};
#endif //LINKEDLIST_LIST_H
Inserted 48 at List ={48} Again (N)?y Appended 4 at 1 List ={48,40} Again (N)? Appended 4 at 2 List ={48,40,40} Again (N)?y Inserted 63 at 2 List ={48,40,40}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
