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 for data value
L get length of list
D display list
Q quit program
Upon termination of the program, each remaining link will be removed.
Each link will contain a data element of type ‘double’ and an integer ID. The data elements need not be unique but the ID values will be automatically assigned by the program, starting at 1 and incrementing whenever a link is created. Once set, the ID value will not change during the running of the program. It will be used as an identifier when identifying and deleting a link which is not either at the front or the back (head or tail) of the list.
main.cpp_______________________________________________________________________________________________
#include
#include "linkedList.h"
#include "linkedList.cpp"
using namespace std;
char menu(void);
int main()
{
linkedList links;
int linkCount = 0;
int value;
double dataValue;
while(true)
{
switch(tolower(menu()))
{
case 'h': // create new link at head
cout << "Please enter a double dataValue: ";
cin >> dataValue;
links.addAtHead(dataValue);
linkCount += 1;
break;
case 'r': // remove link at head
links.removeAtHead();
linkCount -= 1;
break;
case 't': // create new link at tail
cout << "Please enter a double dataValue: ";
cin >> dataValue;
links.addAtTail(dataValue);
linkCount += 1;
break;
case 'k': // remove link at tail
links.removeAtTail();
linkCount -= 1;
break;
case 'i': // remove link at ID match
cout << "Please enter an integer ID value: ";
cin >> value;
links.removeAtID(value);
linkCount -= 1;
break;
case 'd': // display entire list
cout << links.showFirstValue() << endl;
for (int i = 2; i <= linkCount; ++i)
{
cout << links.showNextValue() << endl;
}
break;
case 'l':
cout << links.getLength() << " links in list." <<
endl;
break;
case 'q': // end program
return 0;
break;
default:
cout << "Unknown request code." << endl <<
endl;
break;
case 's': // search list for value
cout << "Please enter a double data value: ";
cin >> dataValue;
int ID = links.findValue(dataValue);
if (ID == 0)
{
cout << "Data value not found in list." << endl;
}
else
{
cout << "Data value found in list at ID " << ID
<< endl;
}
break;
}
}
}
char menu(void)
{
char reqCode;
cout << endl
<< " **************************" << endl
<< " * H create link at head *" << endl
<< " * R remove link at head *" << endl
<< " * T create link at tail *" << endl
<< " * K remove link at tail *" << endl
<< " * I remove link at ID *" << endl
<< " * S search for data value*" << endl
<< " * L get length of list *" << endl
<< " * D display list *" << endl
<< " * Q quit program *" << endl
<< " **************************" << endl
<< "Please enter request code ";
cin >> reqCode;
return reqCode;
}
linkedList.h_____________________________________________________________________________________________
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include
#include "node.h"
// This class manages a linked list of doubles. It is
// specific to doubles and otherwise would need changes.
class linkedList
{
public:
linkedList(void);
virtual ~linkedList(void);
void addAtHead(double n);
void addAtTail(double n);
int getLength(void);
void removeAtHead(void);
void removeAtTail(void);
void removeAtID(int n);
double showFirstValue(void);
double showNextValue(void);
int findValue(double n);
private:
node *head; // anchor at front of list
node *tail; // anchor at back of list
node *next; // controls "showNextValue()"
int nodeID; // source of node ID in node
};
#endif // LINKEDLIST_H
linkedList.cpp__________________________________________________________________________________________
#include "linkedList.h"
#include
#include "node.h"
linkedList::linkedList(void)
{
head = new node(0, NULL, 0);
tail = new node(0, NULL, 0);
head->setNext(tail);
tail->setNext(tail);
head->setValue(0.0);
tail->setValue(0.0);
nodeID = 0;
}
linkedList::~linkedList(void)
{
node *backup = head->getNext();
node *nextNode = backup->getNext();
while (backup != tail)
{
delete backup;
backup = nextNode;
nextNode = backup->getNext();
}
delete tail;
delete head;
}
void linkedList::addAtHead(double n)
{
head->setNext(new node(n, head->getNext(), ++nodeID));
}
void linkedList::addAtTail(double n)
{
node *temp = head->getNext();
while(temp->getNext() != tail)
{
temp = temp->getNext();
}
temp->setNext(new node(n, tail, ++nodeID));
}
int linkedList::getLength(void)
{
int nodeCounter = 0;
node *nextNode = head;
while (nextNode->getNext() != tail)
{
nodeCounter += 1;
nextNode = nextNode->getNext();
}
return nodeCounter;
}
void linkedList::removeAtHead(void)
{
node *temp = head->getNext();
head->setNext(temp->getNext());
delete temp;
}
void linkedList::removeAtTail(void)
{
node *backup = head;
node *nextNode = backup->getNext();
while (nextNode->getNext() != tail)
{
backup = nextNode;
nextNode = backup->getNext();
}
backup->setNext(nextNode->getNext());
delete nextNode;
}
void linkedList::removeAtID(int n)
{
}
double linkedList::showFirstValue(void)
{
next = head->getNext();
return next->getValue();
}
double linkedList::showNextValue(void)
{
return next->getValue();
}
int linkedList::findValue(double n)
{
node *nextNode = head->getNext();
while (nextNode != tail)
{
if (n == nextNode->getValue())
{
return nextNode->getID();
}
nextNode = nextNode->getNext();
}
return 0;
}
node.cpp_________________________________________________________________________________________________
#include "node.h"
#include
node::node(double d, node *n, int ID)
{
data = d;
nodeID = ID;
next = n;
}
node::~node()
{
//dtor
}
void node::setNext(node *n)
{
next = n;
}
node *node::getNext(void)
{
return next;
}
void node::setValue(double v)
{
data = v;
}
double node::getValue(void)
{
return data;
}
int node::getID(void)
{
return nodeID;
}
node.h_______________________________________________________________________________________________
#ifndef NODE_H
#define NODE_H
#include
class node
{
node(double d, node *n, int ID);
~node(void);
void setNext(node *n);
node *getNext(void);
void setValue(double v);
double getValue(void);
int getID(void);
double data;
int nodeID;
node *next;
};
#endif // NODE_H
//==== main.cpp
===========================================================//
#include <iostream>
#include "linkedList.h"
using namespace std;
char menu(void);
int main()
{
/** -------------- Error Resolved -------------------- **/
// linkedList<int> links;
// Class linkedList is not template class
// Use normal class object
linkedList links;
int linkCount = 0;
int value;
double dataValue;
while(true)
{
switch(tolower(menu()))
{
case 'h': // create new link at head
cout << "Please enter a double dataValue: ";
cin >> dataValue;
links.addAtHead(dataValue);
linkCount += 1;
break;
case 'r': // remove link at head
links.removeAtHead();
linkCount -= 1;
break;
case 't': // create new link at tail
cout << "Please enter a double dataValue: ";
cin >> dataValue;
links.addAtTail(dataValue);
linkCount += 1;
break;
case 'k': // remove link at tail
links.removeAtTail();
linkCount -= 1;
break;
case 'i': // remove link at ID match
cout << "Please enter an integer ID value: ";
cin >> value;
links.removeAtID(value);
linkCount -= 1;
break;
case 'd': // display entire list
cout << links.showFirstValue() << endl;
for (int i = 2; i <= linkCount; ++i)
{
cout << links.showNextValue() << endl;
}
break;
case 'l':
cout << links.getLength() << " links in list." <<
endl;
break;
case 'q': // end program
return 0;
break;
default:
cout << "Unknown request code." << endl <<
endl;
break;
case 's': // search list for value
cout << "Please enter a double data value: ";
cin >> dataValue;
int ID = links.findValue(dataValue);
if (ID == 0)
{
cout << "Data value not found in list." << endl;
}
else
{
cout << "Data value found in list at ID " << ID
<< endl;
}
break;
}
}
}
char menu(void)
{
char reqCode;
cout << endl
<< " **************************" << endl
<< " * H create link at head *" << endl
<< " * R remove link at head *" << endl
<< " * T create link at tail *" << endl
<< " * K remove link at tail *" << endl
<< " * I remove link at ID *" << endl
<< " * S search for data value*" << endl
<< " * L get length of list *" << endl
<< " * D display list *" << endl
<< " * Q quit program *" << endl
<< " **************************" << endl
<< "Please enter request code ";
cin >> reqCode;
return reqCode;
}
//==== linkedList.h ===========================================================//
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <string>
#include "node.h"
// This class manages a linked list of doubles. It is
// specific to doubles and otherwise would need changes.
class linkedList
{
public:
linkedList(void);
virtual ~linkedList(void);
void addAtHead(double n);
void addAtTail(double n);
int getLength(void);
void removeAtHead(void);
void removeAtTail(void);
void removeAtID(int n);
double showFirstValue(void);
double showNextValue(void);
int findValue(double n);
private:
node *head; // anchor at front of list
node *tail; // anchor at back of list
node *next; // controls "showNextValue()"
int nodeID; // source of node ID in node
};
#endif // LINKEDLIST_H
//==== linkedList.cpp
===========================================================//
#include <cstdlib>
#include "node.h"
#include "linkedList.h"
linkedList::linkedList(void)
{
head = new node(0, NULL, 0);
tail = new node(0, NULL, 0);
head->setNext(tail);
tail->setNext(tail);
head->setValue(0.0);
tail->setValue(0.0);
nodeID = 0;
}
linkedList::~linkedList(void)
{
node *backup = head->getNext();
node *nextNode = backup->getNext();
while (backup != tail)
{
delete backup;
backup = nextNode;
nextNode = backup->getNext();
}
delete tail;
delete head;
}
void linkedList::addAtHead(double n)
{
head->setNext(new node(n, head->getNext(), ++nodeID));
}
void linkedList::addAtTail(double n)
{
node *temp = head->getNext();
while(temp->getNext() != tail)
{
temp = temp->getNext();
}
temp->setNext(new node(n, tail, ++nodeID));
}
int linkedList::getLength(void)
{
int nodeCounter = 0;
node *nextNode = head;
while (nextNode->getNext() != tail)
{
nodeCounter += 1;
nextNode = nextNode->getNext();
}
return nodeCounter;
}
void linkedList::removeAtHead(void)
{
node *temp = head->getNext();
head->setNext(temp->getNext());
delete temp;
}
void linkedList::removeAtTail(void)
{
node *backup = head;
node *nextNode = backup->getNext();
while (nextNode->getNext() != tail)
{
backup = nextNode;
nextNode = backup->getNext();
}
backup->setNext(nextNode->getNext());
delete nextNode;
}
void linkedList::removeAtID(int n)
{
}
double linkedList::showFirstValue(void)
{
next = head->getNext();
return next->getValue();
}
double linkedList::showNextValue(void)
{
return next->getValue();
}
int linkedList::findValue(double n)
{
node *nextNode = head->getNext();
while (nextNode != tail)
{
if (n == nextNode->getValue())
{
return nextNode->getID();
}
nextNode = nextNode->getNext();
}
return 0;
}
//==== node.h ===========================================================//
#ifndef NODE_H
#define NODE_H
#include <string>
class node
{
/** -------------- Error Resolved -------------------- **/
// Use public,private,protected
// Acess specifier according to uses
// Member function as public
// Member variable as private
public :
node(double d, node *n, int ID);
~node(void);
void setNext(node *n);
node *getNext(void);
void setValue(double v);
double getValue(void);
int getID(void);
private :
double data;
int nodeID;
node *next;
};
#endif // NODE_H
//==== node.cpp
===========================================================//
#include "node.h"
#include <cstdlib>
node::node(double d, node *n, int ID)
{
data = d;
nodeID = ID;
next = n;
}
node::~node()
{
//dtor
}
void node::setNext(node *n)
{
next = n;
}
node *node::getNext(void)
{
return next;
}
void node::setValue(double v)
{
data = v;
}
double node::getValue(void)
{
return data;
}
int node::getID(void)
{
return nodeID;
}
I'm having trouble getting this program to compile and run. It is in C++ Language and...
Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list). /* definition of the list node class */ class Node { friend class LinkedList; private: int value; Node *pNext; public: /* Constructors with No Arguments...
C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int data) { this->data=data; this->next = 0; } int getData(){return data;} Node* getNext(){return next;} void setNext(Node* next){this->next=next;} }; class LinkedList { private: Node* head = 0; public: int isEmpty() {return head == 0;} void print() { Node* currNode = head; while(currNode!=0) { cout << currNode->getData() << endl; currNode = currNode->getNext(); } } void append(int data) {...
C++ Error. I'm having trouble with a pointer. I keep getting the
error "Thread 1: EXC_BAD_ACCESS (code=1, address=0x18)"
The objective of the assignment is to revise the public method
add in class template LinkedBag so that the new
node is inserted at the end of the linked chain instead of at the
beginning.
This is the code I have:
linkedBag-driver.cpp
BagInterface.hpp
LinkedBag.hpp
Node.hpp
int main) LinkedBag<string> bag; cout << "Testing array-based Set:" << endl; cout << "The initial bag is...
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...
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...
Fill the code in list.cpp app.cpp: #include "list.hpp" int main() { LinkedList aList; bool success = false; aList.evensFrontOddsEnd(3); aList.evensFrontOddsEnd(10); aList.evensFrontOddsEnd(6); aList.evensFrontOddsEnd(9); aList.evensFrontOddsEnd(17); aList.evensFrontOddsEnd(12); aList.printForward(); // output should be: 12 6 10 3 9 17 aList.printBackward(); // output should be: 17 9 3 10 6 12 success = aList.remove(17); if(success) cout << "17 Successfully Removed." << endl; success = aList.remove(10); if(success) cout << "10 Successfully Removed." << endl; success = aList.remove(7); // 7 is not in the list, so success should...
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...
C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...
C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...
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...