Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you please help me solve these errors? I don't know why it's not compiling and running successfully. Please help. Thank you greatly. By the

Can you please help me solve these errors? I don't know why it's not compiling and running successfully. Please help. Thank you greatly. By the way, I'm still trying to figure out how to return the largest node in the linked list. Also, I have to make sure the code is running successfully with a recursion.

main.cpp

#include "stdafx.h"

#include

#include "LinkedList.h"

//#include

using namespace std;

//Function prototypes

void linkedListMenu(string);

void displayMenu();

int getValidChoice(int, int, string);

char getValidLetter();

void displayErrorMessage(int);

int main()

{

//Needed to hold sting: username

string userName = "";

//Neded to get usersname

cout << "Please enter a capital letter: ";

getline(cin, userName);

//Needed to start linked list operations

linkedListMenu(userName);

cout << "Thank you" << userName << "for playing; enjoy yourself." << end;

}

//Needed for linked list menu to output choices

void linkedListMenu(string user)

{

const int APPEND_CHOICE = 1;

const int INSERT_CHOICE = 2;

const int DELETE_CHOICE = 3;

const int PRINT_CHOICE = 4;

const int REVERSE_CHOICE = 5;

const int SEARCH_CHOICE = 6;

const int EXIT_CHOICE = 7;

//Defines LinkedList object

LinkedList list;

int menuChoice;

char letterChoice;

int positionChoice;

//Needed to ask user to select a choice

cout << "Please" << user << "select a choice between 1-7. Thank you." << endl;

do

{

//Needed to display menu

displayMenu();

menuChoice = getValidChoice(APPEND_CHOICE, EXIT_CHOICE, "menu choice");

switch (menuChoice)

{

//Needed for append choice & to append node to list

case 1: APPEND_CHOICE:

cout << "Please enter a letter to add it to the end of the linked list: ";

letterChoice = getValidLetter();

list.appendNode(letterChoice);

cout << letterChoice << "was added to end of the linked list.";

break;

case 2: INSERT_CHOICE:

cout << "Please enter a cappital letter and it will be added to the linked list: ";

letterChoice = getValidLetter();

cout << "Select the position you want the letter to be placed in: ";

positionChoice = getValidChoice(1, list.getListLength() + 1, "position");

if (list.insertNode(letterChoice, positionChoice) == -1)

displayErrorMessage(positionChoice);

else

cout << letterChoice << "was insert into position" << positionChoice << "";

break;

case 3: DELETE_CHOICE:

cout << "Which position do you want to delete?";

positionChoice = getValidChoice(1, list.getListLength(), "position");

if (list.deleteNode(positionChoice) == -1)

displayErrorMessage(positionChoice);

else

cout << "The position " << positionChoice << "has been deleted.";

break;

case 4: PRINT_CHOICE:

list.printList();

break;

case 5: REVERSE_CHOICE:

list.reverseList();

cout << "Abra-kadabra, the linked list has been reversed.";

break;

case 6: SEARCH_CHOICE:

cout << "Tell me which letter you want me to seach for?";

letterChoice = getValidLetter();

if (list.searchList(letterChoice) == -1);

cout << "That letter does not exist in the linked list.";

break;

case 7: EXIT_CHOICE;

break;

}

} while (menuChoice != EXIT_CHOICE);

}

void displayMenu()

{

cout << "1. Append node";

cout << "2. Insert node";

cout << "3. Delete position";

cout << "4. Display list";

cout << "5. Reverse linked list";

cout << "6. Search for letter";

cout << "7. Exit choice";

cout << "Your choice: ";

}

int getValidChoice(int minBound, int maxBound, string inputType)

{

int input;

while (!(cin >> input || input < minBound || input > maxBound)

{

cin.clear();

cin.ignore(256, ' ');

cout << "Invalid" << inputType << "please enter a " << inputType << "between " << minBound << "-" << maxBound << ": ";

}

cin.ignore(256, ' ');

return input;

}

char getValidLetter()

{

string input;

cin >> input;

while (!isupper(input.at(0)) || input.length() != 1)

cin.clear();

cin.ignore(256, ' ');

cout << "Invalid letter. Input can only be a capital letter.";

cin >> input;

}

//Needed to return char input

return input.at(0);

}

void displayErrorMessage(int positionNum)

{

cout << "There isn't a position " << positionNum << "in the linked list.";

}

header file

#pragma once

#ifndef LINKEDLIST_H

#define LINKEDLIST_H

class LinkedList

{

private:

struct ListNode

{

double value;

char letter; //capitalLetter; //The Letter of this node

struct ListNode *next; //Pointer to the next node

};

ListNode *head; //List head pointer

int countNodes(ListNode *) const;

int maxNode(ListNode *) const;

void showReverse(ListNode *) const;

public:

LinkedList() //Constructor

{

head = nullptr;

}

~LinkedList(); //{} //Destructor

//Linked list operations

void setLetter(ListNode *, char);

void appendNode(char);

int insertNode(char, int);

int deleteNode(int);

void printList();

void reverseList(); //const

{ showReverse(head); }

int searchList(char); //const;

void displayList() const;

int numNodes() const

{ return countNodes(head); }

int maxNode() const

{ return }

int getListLength();

void findPosition(int);

};

#endif

cpp file

//LinkedList.cpp : Defines the entry point for the application.

//#include "stdafx.h"

#include

#include "LinkedList.h"

//#include

using namespace std;

void LinkedList::setLetter(ListNode *node, char capitalLetter)

{

if (isupper(capitalLetter))

node->letter = capitalLetter;

else

{

cout << "Invalid input. Input needs to be a capital letter." << endl;

exit(EXIT_FAILURE);

}

}

void LinkedList::appendNode(char capitalLetter)

{

ListNode *newNode;

ListNode *nodePtr;

newNode = new ListNode;

setLetter(newNode, capitalLetter);

//newNode->c = letter;

newNode->next = nullptr;

if (!head)

head = newNode;

else

{

nodePtr = head;

while (nodePtr->next)

nodePtr = nodePtr->next;

nodePtr->next = newNode;

}

}

int LinkedList::insertNode(char capitalLetter, int position)

{

ListNode *newNode; //A new node

ListNode *nodePtr; //To traverse the list

ListNode *previousNode = nullptr; //Previous node

int i = 0;

//Allocate a new node ad store num there

newNode = new ListNode;

setLetter(newNode, capitalLetter);

if (!head)

return -1;

if (position < 1)

return -1;

nodePtr = head;

//for (int i = 0; nodePtr != nullptr && i < position; i++)

while (nodePtr != nullptr && i < position)

{

previousNode = nodePtr;

nodePtr = nodePtr->next;

i++;

}

if (position == 1)

{

head = newNode;

newNode->next = nodePtr;

}

else if (position == getListLength() + 1)

appendNode(capitalLetter);

else if (!nodePtr)

return -1;

else

{

previousNode->next = newNode;

newNode->next = nodePtr;

}

return 0;

}

int LinkedList::deleteNode(int position)

{

ListNode *nodePtr; //To traverse linked list

ListNode *previousNode = nullptr; //Points to the previous node

// FIX FIX looks like you use this below without initializing it

int i = 1;

if (!head) //If linked list is empty, do nothing

return -1;

if (position == 1) //Determines if the first node is the one

{

nodePtr = head->next;

delete head;

head = nodePtr;

return 0;

}

else

{

nodePtr = head; //nodePtr is now head of the list

while (nodePtr != nullptr && i < position) //Skip all nodes whose value is not equal to capital letter

{

previousNode = nodePtr;

nodePtr = nodePtr->next;

i++;

}

if (nodePtr)

{

previousNode->next = nodePtr->next;

delete nodePtr;

return 0;

}

else

return -1;

}

}

void LinkedList::printList()

{

ListNode *nodePtr;

int position = 1;

nodePtr = head;

while (nodePtr)

{

cout << "Position " << position << ": " << nodePtr->letter << endl;

position++;

nodePtr = nodePtr->next;

}

}

void LinkedList::reverseList()

{

ListNode *nodePtr;

ListNode *previousNode = nullptr;

ListNode *nextNode = nullptr;

nodePtr = head;

while (nodePtr != nullptr)

{

nextNode = nodePtr->next;

nodePtr->next = previousNode;

previousNode = nodePtr;

nodePtr = nextNode;

}

head = previousNode;

}

int LinkedList::searchList(char searchLetter) //const

{

ListNode *nodePtr;

int *nodePositionArray = nullptr;

int positionIndex = 0;

nodePtr = head;

nodePositionArray = new int[getListLength()];

nodePositionArray[0] = -1;

if (!head)

return -1;

else

{

nodePtr = head;

for (int position = 1; position <= getListLength() && nodePtr != nullptr; position++)

{

if (nodePtr->letter == searchLetter)

{

nodePositionArray[positionIndex] = position;

positionIndex++;

}

nodePtr = nodePtr->next;

}

}

if (nodePositionArray[0] == -1)

return -1;

else

{

cout << "The letter you are searching for was found in position";

if (positionIndex > 1)

cout << "s: ";

else

cout << ": ";

for (int j = 0; j < positionIndex; j++)

{

cout << nodePositionArray[j];

if (j == positionIndex - 1)

cout << "." << endl;

else

cout << ", ";

}

}

delete [] nodePositionArray;

nodePositionArray = nullptr;

return 0;

}

int LinkedList::getListLength()

{

ListNode *nodePtr;

int numberOfNodes = 0;

nodePtr = head;

while (nodePtr)

{

numberOfNodes++;

nodePtr = nodePtr->next;

}

return numberOfNodes;

}

LinkedList::~LinkedList()

{

ListNode *nodePtr;

ListNode *nextNode;

nodePtr = head;

while (nodePtr != nullptr)

{

nextNode = nodePtr->next;

delete nodePtr;

nodePtr = nextNode;

}

}

if (nodePtr)

{

previousNode->next = nodePtr->next;

delete nodePtr;

return 0;

}

else

return -1;

}

}

void LinkedList::printList()

{

ListNode *nodePtr;

int position = 1;

nodePtr = head;

while (nodePtr)

{

cout << "Position " << position << ": " << nodePtr->letter << endl;

position++;

nodePtr = nodePtr->next;

}

}

int LinkedList::countNodes(ListNode *nodePtr) const

{

if (nodePtr != nullptr)

return 1 + countNodes(nodePtr->next);

else

return 0;

}

}

int LinkedList::maxNode(ListNode *nodePtr) const

{

ListNode *nodePtr = nullptr;

}

}

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_2

Step: 3

blur-text-image_3

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

25 Vba Macros For Data Analysis In Microsoft Excel

Authors: Klemens Nguyen

1st Edition

B0CNSXYMTC, 979-8868455629

More Books

Students also viewed these Databases questions