Question

Using C++ 11, Create the following function based on the criteria mentioned below: node.hpp             ○This...

Using C++ 11, Create the following function based on the criteria mentioned below:

node.hpp

            ○This will contain your ​node struct○value ​is type int

            ○next​ is type Node*

            ○(optional) ​prev​ is type Node*

nodeFunctions.hpp

          ○This will contain the headers for all the functions you need to display, add, and remove                nodes from your list

            ■displayList ­ displays the entire list from beginning to end. If no elements let the user       know the list is empty

            ■addValue ­ adds a node with the past value to the end of the list

        ■removeLast ­ removes last node of list. If no elements let the user know the list is            empty   

            ■destroyList ­ frees all dynamic memory

            ■Any helper functions you so desire

nodeFunctions.cpp

            ○This will have the implementation for the above functions

Each header must have include guards

All functions are voids

//The main function

#include <iostream>

#include "node.hpp"

#include "nodeFunctions.hpp"

void displayMenu();

int getInput();

int main()

{

Node *head = NULL;

Node *tail = NULL;

bool done = false;

int choice = 0;

int value;

while(done != true) {

displayMenu();

choice = getInput();

switch(choice) {

case 1:

displayList(head);

break;

case 2:

std::cout << "Enter value to add to list: ";

std::cin >> value;

addValue(head, value);

break;

case 3:

removeLast(head);

break;

case 4:

done = true;

break;

default:

std::cout << "Not a valid choice" << std::endl;

break;

}

}

destroyList(head);

}

void displayMenu() {

std::cout << "What would you like to do?" << std::endl;

std::cout << "1) Display list" << std::endl;

std::cout << "2) Add value" << std::endl;

std::cout << "3) Remove value" << std::endl;

std::cout << "4) Quit" << std::endl;

}

int getInput() {

int choice;

std::cout << "Choice: ";

std::cin >> choice;

return choice;

}

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

//node.h

typedef struct node{
   int data;
   struct node*next;
}Node;

//nodefunctions.cpp

#include"node.h"
#include<iostream>
using namespace std;

void displayList(Node*head){
   while(head){
       cout<<head->data<<" ";
       head = head->next;
   }
   cout<<"\n\n";
}

void addValue(Node*&head, int value){
   Node*newNode = new Node;
   newNode->data = value;
   newNode->next = NULL;
  
   if(head==NULL){
   head = newNode;
       return;
   }
   Node*current = head;
   while(current->next)current = current->next;
  
   current->next = newNode;
  
}

void removeLast(Node*&head){
   if(head==NULL)return;
  
   Node*current=head,*prev=head;
   if(head->next==NULL){
       delete current;
       head=NULL;
       return;
   }
  
   while(current->next){
       prev = current;
       current = current->next;
   }
   prev->next = current->next;
   delete(current);
}

void destroyList(Node*&head){
   Node* current = head;
   Node* next;
  
while (current != NULL)
{
next = current->next;
delete(current);
current = next;
}
head= NULL;
}

//main.cpp

#include <iostream>
#include "nodefunctions.cpp"

void displayMenu();

int getInput();

int main()

{

Node *head = NULL;

Node *tail = NULL;

bool done = false;

int choice = 0;

int value;

while(done != true) {

displayMenu();

choice = getInput();

switch(choice) {

case 1:

displayList(head);

break;

case 2:

std::cout << "Enter value to add to list: ";

std::cin >> value;

addValue(head, value);

break;

case 3:

removeLast(head);

break;

case 4:

done = true;

break;

default:

std::cout << "Not a valid choice" << std::endl;

break;

}

}

destroyList(head);

}

void displayMenu() {

std::cout << "What would you like to do?" << std::endl;

std::cout << "1) Display list" << std::endl;

std::cout << "2) Add value" << std::endl;

std::cout << "3) Remove value" << std::endl;

std::cout << "4) Quit" << std::endl;

}

int getInput() {

int choice;

std::cout << "Choice: ";

std::cin >> choice;

return choice;

}

//sample output

Add a comment
Know the answer?
Add Answer to:
Using C++ 11, Create the following function based on the criteria mentioned below: node.hpp             ○This...
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
  • This is just a partial C++ code. I am having a problem with the getline(cin, variable)...

    This is just a partial C++ code. I am having a problem with the getline(cin, variable) statement of my code. Please run the code for yourself and see that it doesn't let you enter the full name. It skips the prompt to enter the name and goes straight to the next cout statement. I can't use cin alone because I need to capture and store both the first name followed by space followed by last name. I was trying to...

  • #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...

  • Hi I've a problem with this code. When I add more than 1 song to the...

    Hi I've a problem with this code. When I add more than 1 song to the list, it doesn't show the first one, only shows the latest one, when I called the function displaysong, let's say when you add 2 songs ID 1 then ID 2, it shows only ID 1. Can you fix it.  Everything else is fine.. #include <iostream> #include<string> using namespace std; //class Song class Song{ private: int songID; string title; string artist; string album; int year; public:...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • I need help and have to get this done asap: Using the following source codes provided below, create recursive functions...

    I need help and have to get this done asap: Using the following source codes provided below, create recursive functions and methods in C++ to complete the following exercises: Print the list in forward order Print the list in reverse order Print the following three lines (in this order): "The first node contains <value in first node>" "The last node contains <value in last node>" "The first node contains <value in first node>" Print the sum of all the values...

  • 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;   ...

  • CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C...

    CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C2010Lab11A Add the following to the project. //LinkedList.cpp #include <cstdlib> #include "LinkedList.h" using namespace std; //--------------------------------------------------- //List Element Members //--------------------------------------------------- ListElement::ListElement(int d, ListElement * n) {    datum=d;    next=n; } int ListElement::getDatum () const {    return datum; } ListElement const* ListElement::getNext () const {    return next; } //--------------------------------------------------- //LinkedList Members //--------------------------------------------------- LinkedList::LinkedList () {    head=NULL; } void LinkedList::insertItem(int item) {    ListElement *currPtr = head;    ListElement *prevPtr =...

  • C++ - I have a doubly linked list, but I haven't been able to get the...

    C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...

  • I'm having trouble getting this program to compile and run. It is in C++ Language and...

    I'm having trouble getting this program to compile and run. It is in C++ Language and being run with CodeBlocks. Problem Statement: create and manage a linked list. Program will loop displaying a menu of user operations concerning the management of a linked list. Included will be:       H create link at head       R remove link at head       T create link at tail       K remove link at tail       I remove link at ID          S search...

  • 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();...

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