Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help with a Linked List Program

Help with a Linked List Program

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelp
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L LilKoopa

    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; }

    R Offline
    R Offline
    Robert C Cartaino
    wrote on last edited by
    #2

    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.

    L 1 Reply Last reply
    0
    • R Robert C Cartaino

      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.

      L Offline
      L Offline
      LilKoopa
      wrote on last edited by
      #3

      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.

      L 1 Reply Last reply
      0
      • L LilKoopa

        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.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        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

        1 Reply Last reply
        0
        • L LilKoopa

          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; }

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #5

          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

          L 1 Reply Last reply
          0
          • D David Crow

            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

            L Offline
            L Offline
            LilKoopa
            wrote on last edited by
            #6

            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

            D 1 Reply Last reply
            0
            • L LilKoopa

              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

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #7

              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

              L 1 Reply Last reply
              0
              • D David Crow

                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

                L Offline
                L Offline
                LilKoopa
                wrote on last edited by
                #8

                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

                D 1 Reply Last reply
                0
                • L LilKoopa

                  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

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #9

                  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

                  L 1 Reply Last reply
                  0
                  • D David Crow

                    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

                    L Offline
                    L Offline
                    LilKoopa
                    wrote on last edited by
                    #10

                    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

                    D 1 Reply Last reply
                    0
                    • L LilKoopa

                      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

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #11

                      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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups