Linked list problem

This is a discussion on Linked list problem within the C++ Programming forums, part of the General Programming Boards category; I don't see what the problem is, when I try to print out the list it just prints the last ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    46

    Linked list problem

    I don't see what the problem is, when I try to print out the list it just prints the last string entered equal to how many nodes there are. I know it's something simple, but I just can't see it.
    Code:
    int main(void)
    {
    	char yn = 'y';
    	node *iter;
    	node *root;
    
    	root = new node;
    	strcpy(root->customer->name, "Start");
    	root->next = NULL;
    
    	iter = root;
    	while(tolower(yn) != 'n'){
    		iter->next = new node;
    		cout<<"Enter customer's name: ";
    		cin.getline(iter->next->customer->name, 256, '\n');
    		iter->next->next = NULL;
    		iter = iter->next;
    		cout<<iter->customer->name<<endl;
    		cout<<"Enter another record? ";
    		cin>>yn; cin.ignore();
    	}
    
    	iter = root;
    	while(iter->next != NULL){
    		cout<<iter->customer->name<<endl;
    		iter = iter->next;
    	}
    
    	return 0;
    }
    C code. C code run. Run code, run...please!

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    root->customer->name would indicate that each node stores a pointer to a customer. Are you creating a new customer every time you're adding a node? Make sure that you're not overwriting the same customer everytime you create a node. You could store a customer for every node instead of a pointer if there is a known number of customers per node -

    Code:
    struct node
    {
    	struct cus
    	{
    		char name[256];
    	}customer;
    	node* next;
    };
    Then you'd access each name using iter->customer.name.
    zen

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    That worked great, but when I transferred the list to my program I get an access violation error when I run it. I went through the program step by step with the debugger and everything seems fine.
    Code:
    #include <iostream.h>
    #include <string.h>
    #include <time.h>
    
    enum {ATLANTA = 1, NEWYORK = 2, BOSTON = 3};
    enum {MASTERCARD = 1, VISA = 2, AMEX = 3};
    
    class Customer
    {
      //Everything is public until I get it to work right
      public:
    	char name[256];
    	int cardType;
    	char cardNum[50];
    	time_t active;
    	int location;
    	static long int acctNumber;
    };
    
    //Linked list
    struct node
    {
    	Customer customer;
    	node *next;
    };
    
    	/*Temprary prototypes*/
    	void fillNode(node *newNode);
    	void create_new_customer(node *iter);
    	void insertNode(node *iter, node *newNode);
    
    int main(void)
    {
    	char yn = 'y';
    	node *iter;
    	node *root;
    
    	root = new node;
    	strncpy(root->customer.name, "No Name", 255);
    	root->next = NULL;
    
    	iter = root;
    	while(tolower(yn) != 'n'){
    		create_new_customer(iter);
    		cout<<"Enter another record? ";
    		cin>>yn; cin.ignore();
    		iter = root->next;
    	}
    
    	iter = root->next;
    	while(iter->next != NULL){
    		cout<<iter->customer.name<<endl;
    		iter = iter->next;
    	}
    	cout<<iter->customer.name<<endl;
    
    	//Cleanliness is godliness
    	delete root, iter;
    
    	return 0;
    }
    
    //Customer options
    void create_new_customer(node *iter)
    {
    	node *newNode = new node;
    	fillNode(newNode);
    
    	//Find the end of the list
    	while(iter->next != NULL)
    		iter = iter->next;
    	insertNode(iter, newNode);
    
    	//Clean up after yourself stupid!
    	delete newNode;
    }
    
    void fillNode(node *newNode)
    {
      //Fill the name field
    	cout<<"Enter the customer's name: ";
    	cin.getline(newNode->customer.name, 256, '\n');
    }
    
    void insertNode(node *iter, node *newNode)
    {
    	iter->next = newNode;
    	newNode->next = NULL;
    	iter = newNode;
    }
    C code. C code run. Run code, run...please!

  4. #4
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You're deleting things you shouldn't be, and not deleting things that you should.

    You don't want to delete the node in create_new_customer(), otherwise your deleting this new customer. You shouldn't call delete on this until you're finished with it (at the end of your program).

    >>//Cleanliness is godliness
    >> delete root, iter;

    Will only delete two nodes. You should walk through your list deleting each node.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 07:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 09:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21