Question

can someone please double check my code here are the requirements please help me fulfill the...

can someone please double check my code

here are the requirements

please help me fulfill the requirements

Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions:

append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges the nodes in the list so that their order is reversed), and search (which returns the position of a specific value in the list. Assume the first node is position 0. If the items is not found, then return a -1 which will tell the calling function that that value does not exist)

Create a menu-driven program to demonstrate your linked list capabilities.

Low level validation

1. Mutator functions which are given a node value that is not a capital letter should have an exit_failure.

2. Mutator functions which are told to access a node that doesn't exist should return a -1 so that the calling function can handle the problem. (No exit_failure used in this case b/c the calling function can't know if the node is there or not.)

Program Level Validation

If a call to a mutator returns a -1 that will indicate that the node is not found. Display an appropriate error message to the user and then cycle back to the menu again. For example:

1. Append

2. Insert

3. Delete

4. Print

5. Reverse

6. Search

Please choose a menu option:

2

What value do you wish to insert?

B

What position to do wish to insert?

3

**I'm sorry but there is no position 3 in the linked list.

1. Append

2. Insert

3. Delete

4. Print

5. Reverse

6. Search

Please choose a menu option:

***********

Make sure that you have proper constructors and destructors (a destructor must delete every node). Make your input and output professional. Break your code down into functions so as to maintain modular design. No global variables.

Demo your program as follows:

append node

append node

append node

append node

append node

print list

insert at position

print list

delete at position

print list

reverse

print list

search list

---------------

Here's a jing from a previous semester showing what the linked list assignment has to do. It uses integers in it's list instead of capital letters, but o/w it's the same.

https://www.screencast.com/t/w4OeB05A

this is my code

characterlist.cpp file

CharacterList::CharacterList()

{

head = nullptr;

}

CharacterList::~CharacterList()

{

head = nullptr;

}

void CharacterList::appendNode(char character)

{

checkValue(character);

//create new node

list *newNode = new list;//Points to a new node

//store data in new node

newNode->variable = character;

newNode->next = nullptr;

//if list is empty(no nodes) make a new node the first node on the list

if (head == nullptr)

head = newNode;

//else traverse the list to find last node

//Add new node at the end of the list

else

{

//initialize node pointer to head

//Moves through the list

list *nodePtr = head;

//find last node in the list

while (nodePtr->next)

nodePtr = nodePtr->next;

//Insert new node as the last node

nodePtr->next = newNode;

}

}

int CharacterList::insertNode(char character, int position)

{

checkValue(character);

list *newNode = new list;

newNode->variable = character;

newNode->next = NULL;

list *current;

current = head;

int num = 0;

if (position == 1)

{

if(!head)

{

newNode = head;

newNode->next = head;

}

else

{

newNode->next = head->next;

newNode = head;

}

return 1;

}

else if (position > 1 && position <= num )

{

list *previous;

current = head;

for (int i = 1; i < position; i++)

{

previous = current;

current = current->next;

}

previous->next = newNode;

newNode->next = current;

}

else

{

return -1;

}

while (current != nullptr)

{

current = current->next;

num++;

}

return -1;

}

int CharacterList::deleteNode(int position)

{

list *nodePtr;

list *previousNode;

if (!head)

{

return -1;

}

if (head->variable == position)

{

nodePtr = head->next;

delete head;

head = nodePtr;

}

else

{

nodePtr = head;

while (nodePtr != nullptr && nodePtr->variable != position)

{

previousNode = nodePtr;

nodePtr = nodePtr->next;

}

if (nodePtr)

{

previousNode->next = nodePtr->next;

delete nodePtr;

}

else

{

return -1;

}

return 1;

}

void CharacterList::printNode()

{

list *nodePtr;

nodePtr = head;

while (nodePtr)

{

cout << nodePtr->variable << endl;

nodePtr = nodePtr->next;

}

}

//*************************************************************************************

//Name:reverseNode

//Task:

//does not return anything

//Called in main

//*************************************************************************************

void CharacterList::reverseNode()

{

list *current;

list *previous;

list *next;

current = head;

previous = NULL;

while (current != NULL)

{

next = current->next;

previous = current->next;

previous = current;

current = next;

}

previous = head;

}

int CharacterList::searchNode(char character)

{

int position = 0;

if (head == nullptr)

return -1;

list *current = head;

while (current != nullptr) {

if (current-> variable == character)

{

return position;

}

position++;

current = current->next;

}

return -1;

}

void CharacterList::checkValue(char character)

{

if (character >= 65 && character <= 90)

{

return;

}

else

{

exit(EXIT_FAILURE);

}

}

//*************************************************************************************

//Name:Destructor

//Task:This function deletes every node in the list

//*************************************************************************************

CharacterList::~CharacterList()

{

list *nodePtr; // To traverse the list.

list *nextNode; // To point to the next node.

nodePtr = head;

// // While nodePtr is not at the end of the list.

while (nodePtr != NULL)

{

// // Save a pointer to the next node.

nextNode = nodePtr->next;

//

// // Delete the current node.

delete nodePtr;

//

// // Position nodePtr at the next node.

nodePtr = nextNode;

}  

}

characterlist.h

#ifndef CHARACTERLIST_H

#define CHARACTERLIST_H

class CharacterList

{

private:

//Declare structure from node list

struct list

{

char variable;

struct list *next;

};

//list head pointer

list *head;

public:

// Constructor

CharacterList();

// Destructor

~CharacterList();

//Linked list operations

void checkValue(char);

void appendNode(char);

int insertNode(char, int);

int deleteNode(int);

void printNode();

void reverseNode();

int searchNode(char);

};

#endif

main.cpp

#include "CharacterList.h"

using namespace std;

void menu();

int main()

{

CharacterList list;

int choice = 0;

char character;

int position;

while (1) {

switch (choice) {

case 1:

cout << "Enter Character to append: " << endl;

cin >> character;

list.appendNode(character);

break;

case 2:

cout << "Enter Character to insert: " << endl;

cin >> character;

cin.ignore();

cout << "In which position do you wish to insert your character?";

cin >> position;

cin.ignore();

list.insertNode(character, position);

break;

case 3:

cout << "Which character do you wish to delete: ";

cin >> character;

cin.ignore();

list.deleteNode(position);

break;

case 4:

list.printNode();

break;

case 5:

list.reverseNode();

break;

case 6:

cout << "Which character do you wish to search for: ";

cin >> character;

cin.ignore();

list.searchNode(character);

break;

default:

cout << endl << endl << endl << endl;

cout << " Invlid Entry" << endl << endl;

cout << " Please enter the appropiate option " << endl << endl;

system("PAUSE");//gives chance to continue program w/o crash

menu();//user gets a chance to insert again

cin >> choice;

}

}

return 0;

}

void menu()

{

cout << "Welcome to the List" << endl;

cout << "Please select one of the following options:";

cout << "1.Append" << endl;

cout << "2.Insert" << endl;

cout << "3.Delete" << endl;

cout << "4.Print" << endl;

cout << "5.Reverse" << endl;

cout << "6.Search" << endl;

cout << "Enter your choice : ";

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

here is your modified CharacterList.cpp : -------->>>>>

#include "CharacterList.h"

CharacterList::CharacterList()
{
head = nullptr;
}
void CharacterList::appendNode(char character)
{
checkValue(character);
//create new node
list *newNode = new list;//Points to a new node
//store data in new node
newNode->variable = character;
newNode->next = nullptr;
//if list is empty(no nodes) make a new node the first node on the list
if (head == nullptr)
  head = newNode;
//else traverse the list to find last node
//Add new node at the end of the list
else{
  //initialize node pointer to head
  //Moves through the list
  list *nodePtr = head;
  //find last node in the list
  while (nodePtr->next)
  nodePtr = nodePtr->next;
  //Insert new node as the last node
  nodePtr->next = newNode;
}
}
int CharacterList::insertNode(char character, int position){
checkValue(character);
list *newNode = new list;
newNode->variable = character;
newNode->next = NULL;
list *current;
current = head;
int num = 0;
while (current != nullptr)
{
  current = current->next;
  num++;
}
num--;
current = head;
if (position == 1)
{
  if(!head)
  {
   newNode = head;
   newNode->next = head;
  }
  else
  {
   newNode->next = head->next;
   newNode = head;
  }
  return 1;
}
else if (position > 1 && position <= num )
{
  list *previous;
  current = head;
  for (int i = 1; i < position; i++)
  {
   previous = current;
   current = current->next;
  }
  previous->next = newNode;
  newNode->next = current;
}
else
{
  return -1;
}
}
int CharacterList::deleteNode(int position)
{
list *nodePtr;
list *previousNode;
if (!head)
{
  return -1;
}
if (position == 0)
{
  nodePtr = head->next;
  delete head;
  head = nodePtr;
}
else
{
  nodePtr = head;
  int i = 0;
  while (nodePtr != nullptr && i != position)
  {
   previousNode = nodePtr;
   nodePtr = nodePtr->next;
   i++;
  }
  if (nodePtr)
  {
   previousNode->next = nodePtr->next;
   delete nodePtr;
  }
  else
   return -1;
}
return 1;
}
void CharacterList::printNode(){
list *nodePtr;
nodePtr = head;
while (nodePtr)
{
  cout << nodePtr->variable << endl;
  nodePtr = nodePtr->next;
}
}
//*************************************************************************************
//Name:reverseNode
//Task:
//does not return anything
//Called in main
//*************************************************************************************
void CharacterList::reverseNode()
{
list *current;
list *previous;
list *next;
current = head;
previous = NULL;
while (current->next != NULL)
{
  next = current->next;
  current->next = previous;
  previous = current;
  current = next;
}
current->next = previous;
head = current;
}
int CharacterList::searchNode(char character)
{
int position = 0;
if (head == nullptr)
  return -1;
list *current = head;
while (current != nullptr) {
  if (current-> variable == character)
  {
   return position;
  }
  position++;
  current = current->next;
}
return -1;
}
void CharacterList::checkValue(char character)
{
if (character >= 65 && character <= 90)
{
  return;
}
else
{
  exit(EXIT_FAILURE);
}
}
//*************************************************************************************
//Name:Destructor
//Task:This function deletes every node in the list
//*************************************************************************************
CharacterList::~CharacterList()
{
list *nodePtr; // To traverse the list.
list *nextNode; // To point to the next node.
nodePtr = head;
// // While nodePtr is not at the end of the list.
while (nodePtr != NULL)
{
  // // Save a pointer to the next node.
  nextNode = nodePtr->next;
  //
  // // Delete the current node.
  delete nodePtr;
  //
  // // Position nodePtr at the next node.
  nodePtr = nextNode;
}
}

Add a comment
Know the answer?
Add Answer to:
can someone please double check my code here are the requirements please help me fulfill the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • What is the specific answer for 1. and 2. Thanks! Add a new method, find, to...

    What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...

  • Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual...

    Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual studio 2013 this is my code so far #include <cstdlib> #include <list> #include <iostream> using namespace std; struct node {    int data;    node* next; }; void initialize(node*p); void insert(int data,int n); void remove(int b,int pos); bool empty(node*); void length(); bool valuein(int c); int reportvalue(int value); void ptintlist(); int menu(); int main() {    struct node*head=NULL;    char commands;    int value;   ...

  • Hi there, I am working on a binary search tree code in c++. The program must...

    Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I...

    I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I have right now. ************city.h**************** #ifndef city_h #define city_h #include <string> using namespace std; class City{ public: City () { name = "N/A"; population = 0; } City (string nm, unsigned int pop){ name = nm; population = pop; } void setName (string name) { this -> name = name; } void setPopulation (unsigned int population){ this -> population = population; } string getName() const...

  • Hello, can someone help me solve the remove function since when I try to remove it...

    Hello, can someone help me solve the remove function since when I try to remove it doesn't work, please and thank you. #include <iostream> #include <cstdlib> #include <string> using namespace std; class node { public:    typedef int data_t;    node *next;    data_t data;    node(data_t d) { next = NULL; data = d; } }; class linked_list { private:    node *head; public:    linked_list()    {        head = NULL;    }    // Get the...

  • I have a C++ code that lets me enter, display and delete a student record. I...

    I have a C++ code that lets me enter, display and delete a student record. I need to implement a function that prints the average grade score of the students I input. Below is my code and a picture of how my code looks right now. #include<iostream> #include<stdlib.h> using namespace std; //Node Declaration struct node {    string name;    string id;    int score;    node *next;   }; //List class class list {        private:        //head...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT