Thread: Simple add to linked list

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    Simple add to linked list

    Hey guys. I have a few years of programming under by belt but I'm new to C++ (coming from mostly Java and C#). I have a linked list that seems to work properly except for the add method. The nodes of the linked list are AdjacencyListNode objects that just have an int (variable name is data) and a pointer (variable name is next) to the next node.

    Adding the first node to the list behaves properly. Adding the second node does add a second node, but the data in the node is that of the first. In general there is just some weird behavior going on. I've posted the add method and the part of the program that calls this. The output is below the code. If anyone sees something that is incorrect, please tell me. Thanks for your time guys

    Code:
    //============================================
    // Adds a new node to the beginning of
    // the list. Parameter node is the pointer to
    // the node that will be added to the list.
    //
    // PRE:  AdjacencyList with size n is defined
    // POST: AdjacencyList with node added to the 
    //       front of the list. Length is n + 1.
    //============================================
    void AdjacencyList::add(AdjacencyListNode * node)
    {
    	// Check if list is empty
    	if (first == NULL)
    		first = node;
    
    	else //List not empty 
    	{
    		//Hook up new node's next
    		node->next = first;
    
    		// Set first to new node
    		first = node;
             }
    		//Increment length
    		length++;
    }
    Here's the part of the main program that calls this:
    Code:
    	AdjacencyList list;
    	list.add(new AdjacencyListNode(2));
    	list.display();
    
    	list.add(new AdjacencyListNode(3));
    	list.display();
    
    	list.add(new AdjacencyListNode(4));
    
    	list.display();
    
    	cout << list.length << endl;
    And finally the output:
    Code:
    Displaying List:
    Node Data: 2
    Displaying List:
    Node Data: 3
    Node Data: 3
    Displaying List:
    Node Data: 4
    Node Data: 4
    Node Data: 3
    3

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I am afraid that I cannot reproduce your problem. You might want to post the smallest and simplest compilable program that demonstrates the problem. (I have a small and simple compilable program that demonstrates that there is no problem.) After all, perhaps the problem lies in code that you did not show.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Okay. I've posted the related classes below. Thanks again!

    Graph.cpp
    Code:
    //================================================
    // Main method
    //================================================
    int main()
    {
    	AdjacencyList list;
    	list.add(new AdjacencyListNode(2));
    	list.display();
    
    	list.add(new AdjacencyListNode(3));
    	list.display();
    
    	list.add(new AdjacencyListNode(4));
    
    	list.display();
    
    	cout << list.length << endl;
    	return 0;
    }
    AdjacencyList.h
    Code:
    #pragma once
    #include "AdjacencyListNode.h"
    
    #ifndef ADJLIST_DEF
    #define ADJLIST_DEF
    
    class AdjacencyList
    {
    public:
    	int length;					// Size of the list
    	AdjacencyListNode * first;		// Pointer to the front of the list
    	AdjacencyList();				// Constructor
    	void add(AdjacencyListNode *);	// Adds a new node to the front of list
    	void display();			        // Displays the nodes of the list 
    };
    #endif
    AdjacencyList.cpp
    Code:
    #include "StdAfx.h"
    #include "AdjacencyList.h"
    #include "AdjacencyListNode.h"
    #include <iostream>
    using namespace std;
    //============================================
    // Default Constructor for an AdjacencyList
    //
    // PRE: AdjacencyList is undefined
    // POST: AdjacencyList is defined with first
    //       set to null and length zero.
    //============================================
    AdjacencyList::AdjacencyList()
    {
    	first = NULL;
    	length = 0;
    }
    
    //=============================================
    // Prints the contents of the list.
    //
    // PRE:  List is defined.
    // POST: Contents of the list are printed.
    //=============================================
    void AdjacencyList::display()
    {
    	//Temproary node to iterate through the list with
    	AdjacencyListNode * tmp;
    	tmp = first;
    
    	cout << "Displaying List: " << endl; 
    	first->display();
    
    	// Iterate through list and display each node
    	while (tmp->next != NULL)
    	{
    		tmp->display();
    		tmp = tmp->next;
    	}
    }
    
    //============================================
    // Adds a new node to the beginning of
    // the list. Parameter node is the pointer to
    // the node that will be added to the list.
    //
    // PRE:  AdjacencyList with size n is defined
    // POST: AdjacencyList with node added to the 
    //       front of the list. Length is n + 1.
    //============================================
    void AdjacencyList::add(AdjacencyListNode * node)
    {
    	// Check if list is empty
    	if (first == NULL)
    		first = node;
    
    	else //List not empty 
    	{
    		//Hook up new node's next
    		node->next = first;
    
    		// Set first to new node
    		first = node;
    	}
    		//Increment length
    		length++;
    }
    AdjacencyListNode.h
    Code:
    #pragma once
    #ifndef ADJNODE_DEF
    #define ADJNODE_DEF
    
    class AdjacencyListNode
    {
    public:
    	AdjacencyListNode();			// Creates a new empty node
    	AdjacencyListNode(int);		// Creates node with int as data 
    	AdjacencyListNode * next;		// Pointer to next item in list
    	void display();				// Prints the node's data
    	int data;					// Number to identify the node in the
                                                            // graph
    };
    #endif
    AdjacencyListNode.cpp
    Code:
    #include "StdAfx.h"
    #include "AdjacencyListNode.h"
    #include <iostream>
    using namespace std;
    //===========================================
    // Default constructor for AdjacencyListNode
    //
    // PRE:  Node is undefined.
    // POST: The node is defined with data set
    //		 to -1 and next set to null.
    //===========================================
    AdjacencyListNode::AdjacencyListNode()
    {
    	data = -1;
    	next = NULL;
    }
    
    //=============================================
    // Alternate Constructor for AdjacencyListNode. 
    // Parameter num is the value for the data to
    // be set to.
    //
    // PRE:  Node is undefined.
    // POST: Node is defined with data = num and
    //       next set to null.
    //=============================================
    AdjacencyListNode::AdjacencyListNode(int num)
    {
    	data = num;
    	next = NULL;
    }
    
    //===================================
    // Displays the node's data
    //
    // PRE:  Node is defined.
    // POST: Node's data is printed.
    //===================================
    void AdjacencyListNode::display()
    {
    	cout << "Node Data: " << data << endl;
    }

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The bug appears to be in your display() function. It prints the first node twice and never prints the last node.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Bingo!! Thanks a lot I figured it was something stupid on my part.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reverse a linked list, probably a simple fix.
    By csharp100 in forum C Programming
    Replies: 5
    Last Post: 11-10-2010, 11:54 PM
  2. Simple Linked List Problem
    By Flamefury in forum C++ Programming
    Replies: 6
    Last Post: 03-20-2010, 07:38 PM
  3. simple problem in a linked list !
    By Meshal in forum C Programming
    Replies: 13
    Last Post: 05-02-2007, 10:55 PM
  4. Need help with simple linked list
    By Mahak in forum C++ Programming
    Replies: 5
    Last Post: 09-14-2006, 09:56 AM
  5. simple linked list
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 12-01-2005, 11:27 AM