Question

Linked List Implementation Write a console application that keeps track of names (strings) in a list....

Linked List Implementation

Write a console application that keeps track of names (strings) in a list.

-- Include a class called: LinkedList

-- Utilize a struct called: Node

Since it is a class, create separate .h and .cpp files for LinkedList. Node can be embedded inside the LinkedList files (no programs using the LinkedList class would need to know about the Node or see an interface to it). You can implement Node in its own file.

You’ll need to write a main and some additional functions to implement the user interface. All menu/error output should occur in this code; the only output done in LinkedList will be streaming the items in the list to "cout" in the print and/or printReverse methods (see details below).

Notes:

-- Do not use any templates from the Standard Template Library.

-- The user interface (including main()) should be in a separate source (.cpp) file.

Details:

The linked list class should support the following methods (public member functions):

-- insert - inserts the given item in the list (in alphabetical order) if it’s not already there (no duplicates). Returns true if successful.

-- remove - removes the given item from the list. Returns true if successful.

-- print - prints the list in order

-- count - returns a count of the number of items in the list using recursion (no loops allowed)

-- removeAll - removes all items from the list

-- printReverse - prints the list in reverse order using recursion (no loops allowed)

The linked list should store a list of strings in alphabetical order. Duplicates are not allowed.

Do display a menu to the user consisting of the following options:

a - Add an item

r - Remove an item

d - Remove all items from the list

Additional details:

-- Your LinkedList class should have a constructor and destructor (destructor should delete every node)

-- If an item is to be added or removed, prompt for the item name to be added or deleted (spaces should be allowed)

-- After each operation:

-- give the user feedback as to whether the operation was successful or not

-- re-display the list

-- redisplay the menu of user options

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

LinkedList.h

#ifndef LINKEDLIST_H

#define LINKEDLIST_H

#include<iostream>

class LinkedList{

public:

LinkedList();

~LinkedList();

bool insert(std::string);

bool remove(std::string);

void print();

int count();

void removeAll();

void printReverse();

private:

struct Node

{

std::string data;

Node* next;

};

Node* head = nullptr;

bool searchItem(std::string);

int countRecursive(Node*);

void printReverseRecursive(Node*);

};

#endif

LinkedList.cpp

#include<iostream>

#include"LinkedList.h"

LinkedList::LinkedList(){

}

LinkedList::~LinkedList(){

removeAll();

}

bool LinkedList::searchItem(std::string item)

{

Node* curr = this->head;

while(curr != nullptr){

if(curr->data == item)

return true;

else

curr = curr->next;

}

return false;

}

bool LinkedList::insert(std::string item)

{

Node* curr = this->head;

Node* newnode = nullptr;

bool search = searchItem(item);

if(search && count() > 0) return false;

if(curr == nullptr){

this->head = new Node;

this->head->data = item;

this->head->next = nullptr;

return true;

}else{

while(curr->next && (item.compare(curr->next->data) > 0)){

curr = curr->next;

}

newnode = new Node;

newnode->data = item;

if(curr->next && (item.compare(curr->next->data) < 0)){

newnode->next = curr->next;

curr->next = newnode;

}else{

newnode->next = curr->next;

curr->next = newnode;

}

}

return false;

}

bool LinkedList::remove(std::string item)

{

Node* curr = this->head;

Node* prev = nullptr;

Node* next = nullptr;

if(curr == nullptr) return false;

if(curr != nullptr && item.compare(curr->data) == 0){

next = curr->next;

delete curr;

curr = next;

return true;

}

while(curr != nullptr && item.compare(curr->data) != 0){

prev = curr;

curr = curr->next;

}

if(curr == nullptr) return false;

prev->next = curr->next;

delete curr;

return true;

}

void LinkedList::print()

{

Node* curr = this->head;

while(curr != nullptr){

std::cout<<curr->data<<std::endl;

curr=curr->next;

}

}

int LinkedList::countRecursive(Node* head)

{

if(head == nullptr)

return 0;

else

return 1+countRecursive(head->next);

}

int LinkedList::count()

{

Node* newhead = this->head;

return countRecursive(newhead);

}

void LinkedList::removeAll()

{

Node* curr = this->head;

Node* temp = nullptr;

while(curr != nullptr){

temp = curr->next;

delete curr;

curr = temp;

}

this->head = nullptr;

}

void LinkedList::printReverseRecursive(Node* head)

{

if(head == nullptr)

return;

else{

printReverseRecursive(head->next);

std::cout<<head->data<<std::endl;

}

}

void LinkedList::printReverse()

{

Node* newhead = this->head;

printReverseRecursive(newhead);

}

main.cpp

#include<iostream>

#include"LinkedList.h"

using namespace std;

void do_AddItem(LinkedList& list)

{

string item;

cout<<endl<<"Enter item to add : ";

cin>>item;

if(list.insert(item)){

cout<<"Item added successfuly"<<endl;

}else{

cout<<"Failed to add Item"<<endl;

}

cout<<"List Items are :"<<endl;

list.print();

cout<<endl;

}

void do_RemoveItem(LinkedList& list)

{

string item;

cout<<endl<<"Enter item to remove : ";

cin>>item;

if(list.remove(item)){

cout<<"Item removed successfuly"<<endl;

}else{

cout<<"Failed to remove Item"<<endl;

}

cout<<"List Items are :"<<endl;

list.print();

cout<<endl;

}

void do_RemoveAllItems(LinkedList& list)

{

list.removeAll();

cout<<"ALl items removed successfuly"<<endl;

cout<<"List Items are :"<<endl;

list.print();

cout<<endl;

}

int main()

{

LinkedList list;

char choice;

do{

cout<<"-- Menu (Press 'q' to exit)--"<<endl;

cout<<"a - Add an item"<<endl;

cout<<"r - Remove an item"<<endl;

cout<<"d - Remove all items from the list"<<endl;

cout<<"Enter choice : ";

cin>>choice;

switch (choice)

{

case 'a':

do_AddItem(list);

break;

case 'r':

do_RemoveItem(list);

break;

case 'd':

do_RemoveAllItems(list);

break;

case 'q':

break;

default:

cout<<endl<<"invalid choice"<<endl;

break;

}

}while(choice != 'q');

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Linked List Implementation Write a console application that keeps track of names (strings) in a list....
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
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