Help with a Linked List Program
-
I need help with a linked list program, im probably just not doing something write but if any info is available i would greatly appreicate it, here is my code. its suppossed to append a node, insert a node, and delete a node. The output should look like this. Thanks for all help i can get Here are the initial values: 5 feet, 4 inches 6 feet, 8 inches 8 feet, 9 inches Now inserting the value 7 feel 2 inches. Here are the nodes now. 5 feet, 4 inches 6 feet, 8 inches 7 feet, 2 inches 8 feet, 9 inches Now deleting the last node. Here are the nodes left. 5 feet, 4 inches 6 feet, 8 inches 7 feet, 2inches
// This program demonstrates the linked list template. #include "stdafx.h" #include #include "LinkedList.h" #include "FeetInches.h" using namespace std; int main() { // Define a LinkedList object. LinkedList list; // Define some FeetInches objects. FeetInches distance1(5, 4); // 5 feet 4 inches FeetInches distance2(6, 8); // 6 feet 8 inches FeetInches distance3(8, 9); // 8 feet 9 inches // Store the FeetInches objects in the list. list.appendNode(distance1); // 5 feet 4 inches list.appendNode(distance2); // 6 feet 8 inches list.appendNode(distance3); // 8 feet 9 inches // Display the values in the list. cout << "Here are the initial values:\n"; list.displayList(); cout << endl; // Insert another FeetInches object. cout << "Now inserting the value 7 feet 2 inches.\n"; list.insertNode(distance4); // 7 feet 2 inches FeetInches distance4(7, 2); // Display the values in the list. cout << "Here are the nodes now.\n"; list.displayList(); cout << endl; // Delete the last node. cout << "Now deleting the last node.\n"; list.deleteNode(distance4); // Display the values in the list. cout << "Here are the nodes left.\n"; list.displayList(); return 0; }
-
I need help with a linked list program, im probably just not doing something write but if any info is available i would greatly appreicate it, here is my code. its suppossed to append a node, insert a node, and delete a node. The output should look like this. Thanks for all help i can get Here are the initial values: 5 feet, 4 inches 6 feet, 8 inches 8 feet, 9 inches Now inserting the value 7 feel 2 inches. Here are the nodes now. 5 feet, 4 inches 6 feet, 8 inches 7 feet, 2 inches 8 feet, 9 inches Now deleting the last node. Here are the nodes left. 5 feet, 4 inches 6 feet, 8 inches 7 feet, 2inches
// This program demonstrates the linked list template. #include "stdafx.h" #include #include "LinkedList.h" #include "FeetInches.h" using namespace std; int main() { // Define a LinkedList object. LinkedList list; // Define some FeetInches objects. FeetInches distance1(5, 4); // 5 feet 4 inches FeetInches distance2(6, 8); // 6 feet 8 inches FeetInches distance3(8, 9); // 8 feet 9 inches // Store the FeetInches objects in the list. list.appendNode(distance1); // 5 feet 4 inches list.appendNode(distance2); // 6 feet 8 inches list.appendNode(distance3); // 8 feet 9 inches // Display the values in the list. cout << "Here are the initial values:\n"; list.displayList(); cout << endl; // Insert another FeetInches object. cout << "Now inserting the value 7 feet 2 inches.\n"; list.insertNode(distance4); // 7 feet 2 inches FeetInches distance4(7, 2); // Display the values in the list. cout << "Here are the nodes now.\n"; list.displayList(); cout << endl; // Delete the last node. cout << "Now deleting the last node.\n"; list.deleteNode(distance4); // Display the values in the list. cout << "Here are the nodes left.\n"; list.displayList(); return 0; }
I don't know the specifics of your implementation of "LinkedList.h" but here are a couple of quick things to get you started. You don't ever really "append a node". When you add "7 feet, 2 inches", you are really inserting it (in the middle of your list). You have to find the node you want to add before (just before "8 feet, 9 inches") and then insert your node. You are inserting
distance4
before you create it. You have to create the object first, then insert it. In your code, you say you are "deleting the last node" but that's not what you are doing. Do are deleting a specific node. To delete the last node, you have to go to the end of the list and then delete that node. -
I don't know the specifics of your implementation of "LinkedList.h" but here are a couple of quick things to get you started. You don't ever really "append a node". When you add "7 feet, 2 inches", you are really inserting it (in the middle of your list). You have to find the node you want to add before (just before "8 feet, 9 inches") and then insert your node. You are inserting
distance4
before you create it. You have to create the object first, then insert it. In your code, you say you are "deleting the last node" but that's not what you are doing. Do are deleting a specific node. To delete the last node, you have to go to the end of the list and then delete that node. -
I'm kind of confused I'm not sure if I know what you mean. I looked in my book and it was the same program and the code in the book.
Essentially these two lines need to be swapped because you are attempting to insert a variable before it was created.
list.insertNode(distance4); // 7 feet 2 inches FeetInches distance4(7, 2);
It is hard to say if there are any other bugs because we cannot see the implementation of LinkedList.h Best Wishes, -David Delaune
-
I need help with a linked list program, im probably just not doing something write but if any info is available i would greatly appreicate it, here is my code. its suppossed to append a node, insert a node, and delete a node. The output should look like this. Thanks for all help i can get Here are the initial values: 5 feet, 4 inches 6 feet, 8 inches 8 feet, 9 inches Now inserting the value 7 feel 2 inches. Here are the nodes now. 5 feet, 4 inches 6 feet, 8 inches 7 feet, 2 inches 8 feet, 9 inches Now deleting the last node. Here are the nodes left. 5 feet, 4 inches 6 feet, 8 inches 7 feet, 2inches
// This program demonstrates the linked list template. #include "stdafx.h" #include #include "LinkedList.h" #include "FeetInches.h" using namespace std; int main() { // Define a LinkedList object. LinkedList list; // Define some FeetInches objects. FeetInches distance1(5, 4); // 5 feet 4 inches FeetInches distance2(6, 8); // 6 feet 8 inches FeetInches distance3(8, 9); // 8 feet 9 inches // Store the FeetInches objects in the list. list.appendNode(distance1); // 5 feet 4 inches list.appendNode(distance2); // 6 feet 8 inches list.appendNode(distance3); // 8 feet 9 inches // Display the values in the list. cout << "Here are the initial values:\n"; list.displayList(); cout << endl; // Insert another FeetInches object. cout << "Now inserting the value 7 feet 2 inches.\n"; list.insertNode(distance4); // 7 feet 2 inches FeetInches distance4(7, 2); // Display the values in the list. cout << "Here are the nodes now.\n"; list.displayList(); cout << endl; // Delete the last node. cout << "Now deleting the last node.\n"; list.deleteNode(distance4); // Display the values in the list. cout << "Here are the nodes left.\n"; list.displayList(); return 0; }
What does the implementation of
LinkedList
look like?"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
-
What does the implementation of
LinkedList
look like?"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
Here is linked list.h Thanks for all the help by the way. Do you need FeetInches.h also
// A class template for holding a linked list.
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include // For cout and NULL
using namespace std;template
class LinkedList
{
public:
// Declare a structure for the list
struct ListNode
{
T value; // The value in this node
struct ListNode *next; // To point to the next node
};ListNode *head; // List head pointer
public:
// Constructor
LinkedList()
{ head = NULL; }// Destructor
~LinkedList();// Linked list operations
void appendNode(T);
void insertNode(T);
void deleteNode(T);
void displayList() const;
};//**************************************************
// appendNode appends a node containing the value *
// pased into newValue, to the end of the list. *
//**************************************************template
void LinkedList::appendNode(T newValue)
{
ListNode *nodePtr; // To move through the list// Allocate a new node and store newValue there.
newNode = new ListNode;
newNode->value = newValue;
newNode->next = NULL;// If there are no nodes in the list
// make newNode the first node.
if (!head)
head = newNode;
else // Otherwise, insert newNode at end.
{
// Initialize nodePtr to head of list.
nodePtr = head;// Find the last node in the list. while (nodePtr->next) nodePtr = nodePtr->next; // Insert newNode as the last node. nodePtr->next = newNode;
}
}//**************************************************
// displayList shows the value *
// stored in each node of the linked list *
// pointed to by head. *
//**************************************************template
void LinkedList::displayList() const
{
ListNode *nodePtr; // To move through the list//Position nodePtr at the head of the list.
nodePtr = head;// While nodePtr points to a node, traverse
// the list.
while (nodePtr)
{
// Display the value in this node.
cout << nodePtr->value << endl;// Move to the next node. nodePtr = nodePtr->next;
}
}//**************************************************
// The insertNode function inser -
Here is linked list.h Thanks for all the help by the way. Do you need FeetInches.h also
// A class template for holding a linked list.
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include // For cout and NULL
using namespace std;template
class LinkedList
{
public:
// Declare a structure for the list
struct ListNode
{
T value; // The value in this node
struct ListNode *next; // To point to the next node
};ListNode *head; // List head pointer
public:
// Constructor
LinkedList()
{ head = NULL; }// Destructor
~LinkedList();// Linked list operations
void appendNode(T);
void insertNode(T);
void deleteNode(T);
void displayList() const;
};//**************************************************
// appendNode appends a node containing the value *
// pased into newValue, to the end of the list. *
//**************************************************template
void LinkedList::appendNode(T newValue)
{
ListNode *nodePtr; // To move through the list// Allocate a new node and store newValue there.
newNode = new ListNode;
newNode->value = newValue;
newNode->next = NULL;// If there are no nodes in the list
// make newNode the first node.
if (!head)
head = newNode;
else // Otherwise, insert newNode at end.
{
// Initialize nodePtr to head of list.
nodePtr = head;// Find the last node in the list. while (nodePtr->next) nodePtr = nodePtr->next; // Insert newNode as the last node. nodePtr->next = newNode;
}
}//**************************************************
// displayList shows the value *
// stored in each node of the linked list *
// pointed to by head. *
//**************************************************template
void LinkedList::displayList() const
{
ListNode *nodePtr; // To move through the list//Position nodePtr at the head of the list.
nodePtr = head;// While nodePtr points to a node, traverse
// the list.
while (nodePtr)
{
// Display the value in this node.
cout << nodePtr->value << endl;// Move to the next node. nodePtr = nodePtr->next;
}
}//**************************************************
// The insertNode function inserDid you heed Randor's advice?
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
-
Did you heed Randor's advice?
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
yes, and i get the following error messages. linkedlist.h(47) : error C2065: 'newNode' : undeclared identifier linkedlist.h(43) : while compiling class template member function 'void LinkedList::appendNode(T)' with [ T=FeetInches ] linkedlistexample.cpp(11) : see reference to class template instantiation 'LinkedList' being compiled with [ T=FeetInches ] linkedlist.h(48) : error C2227: left of '->value' must point to class/struct/union/generic type linkedlist.h(49) : error C2227: left of '->next' must point to class/struct/union/generic type
-
yes, and i get the following error messages. linkedlist.h(47) : error C2065: 'newNode' : undeclared identifier linkedlist.h(43) : while compiling class template member function 'void LinkedList::appendNode(T)' with [ T=FeetInches ] linkedlistexample.cpp(11) : see reference to class template instantiation 'LinkedList' being compiled with [ T=FeetInches ] linkedlist.h(48) : error C2227: left of '->value' must point to class/struct/union/generic type linkedlist.h(49) : error C2227: left of '->next' must point to class/struct/union/generic type
LilKoopa wrote:
linkedlist.h(47) : error C2065: 'newNode' : undeclared identifier
The error is self explanatory. On line 47 of linkedlist.h, you are using
newNode
which has not yet been declared."Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
-
LilKoopa wrote:
linkedlist.h(47) : error C2065: 'newNode' : undeclared identifier
The error is self explanatory. On line 47 of linkedlist.h, you are using
newNode
which has not yet been declared."Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
-
ok now I'm down this one error message and i dont know what i am suppossed to do error C2244: 'LinkedList::displayList' : unable to match function definition to an existing declaration
LilKoopa wrote:
error C2244: 'LinkedList::displayList' : unable to match function definition to an existing declaration
So which line is causing this error?
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch