Linked list only outputs the root node and the last node entered

This is a discussion on Linked list only outputs the root node and the last node entered within the C++ Programming forums, part of the General Programming Boards category; I have no idea why this program doesn't output the list, just the head of the list and the tail. ...

  1. #1
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74

    Linked list only outputs the root node and the last node entered

    I have no idea why this program doesn't output the list, just the head of the list and the tail.
    Code:
    #include <iostream.h>
    
    class node
    {
      public:
        char *v;
        node *next;
    };
    
    int main()
    {
      node *root;
      node *x;
      root = new node;
      root->next = NULL;
      root->v = "Start";
      x = root;
    
      for ( int i = 0; i < 4; i++ ) {
        x->next = new node;
        x = x->next;
        x->next = NULL;
        cout << "Enter your name: ";
        cin.getline( x->v, 80 );
      }
    
      x = root;
      if ( x != NULL )
      {
        int q = 1;
        while ( x->next != NULL ) {
          cout << x->v << " " << q++ << endl;
          x = x->next;
        }
      }
    
      return 0;
    }
    pointer = NULL

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Your char pointer in your class doesn't point to anything. Unless you want to dynamically allocate memory within your class to store your names, you want something like this -

    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    class node
    {
      public:
        char v[80];
        node *next;
    };
    
    int main()
    {
      node *root;
      node *x;
      root = new node;
      root->next = NULL;
      strcpy(root->v,"Start");
      x = root;
    
      for ( int i = 0; i < 4; i++ ) {
    	  char temp[80];
        x->next = new node;
        x = x->next;
    	node;cout << "Enter your name: ";
        cin.getline(temp, 80 );
    	strcpy(x->v,temp);
        x->next = NULL;
        
      }
    
      x = root;
      while ( x != NULL )
      {
        int q = 1;
              cout << x->v << " " << q++ << endl;
          x = x->next;
         }
    
      return 0;
    }
    Also dynamically allocated memory should be deleted after use.
    zen

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    2
    You need to allocate some memory for the char* in the node before trying to read into it:

    x->v = new char[80];
    cout << "Enter your name: ";
    cin.getline(x->v, 80);

    An alternative would be to make 'v' a character array:

    class node {
    public:
    char v[80];
    node *next;
    };

    Hope this helps.

  4. #4
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    Same effect, here's the program I wrote that template for, it's a semi large menu so I broke it down into something smaller for working with the list. My class is just a public char array and a pointer to next.
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <conio.h>
    #include "cust.hpp"
    
    void new_cust(Customer *iter);
    void test_func(Customer *iter, Customer *root);
    int main(){
      //Declare and initialize the customer root node
      Customer *root;
      Customer *iter;
      root = new Customer;
      root->next = NULL;
      strcpy( root->name, "Start");
      iter = root;
      for ( ;; ) {
      int selection;
      cout<<"1. Customers"<<endl
          <<"2. Employees"<<endl
          <<"3. Locations"<<endl
          <<"4. Inventory"<<endl
          <<"5. Exit"<<endl
          <<"Select an option: ";
      cin>>selection;
      cin.ignore();
      switch(selection){
        case 1:
          clrscr();
          int sel1;
          cout<<"1. Create New Customer"<<endl
    	  <<"2. View Customer Account"<<endl
    	  <<"3. Lease Movie to Customer"<<endl
    	  <<"4. View Customer List"<<endl
    	  <<"Select an option: ";
          cin>>sel1;
          cin.ignore();
          switch(sel1){
    	case 1:
              new_cust(iter);
              break;
    	case 2:
              test_func(iter, root);
              break;
    	case 3:
              //lease movie function();
              break;
    	case 4:
              //view customer list function();
              break;
    	default:
    	  cout<<"Invalid selection"<<endl;
    	  break;
          }
          break;
        case 2:
          clrscr();
          int sel2;
          cout<<"1. Create New Employee"<<endl
    	  <<"2. View Employee Data Sheet"<<endl
    	  <<"3. Enter Payroll"<<endl
    	  <<"4. View Payroll Report"<<endl
              <<"5. View Employee List"<<endl
    	  <<"Select an option: ";
          cin>>sel2;
          cin.ignore();
          switch(sel2){
    	case 1:
              //new employee function();
              break;
    	case 2:
              //view employee data sheet function();
              break;
    	case 3:
              //enter payroll function();
              break;
    	case 4:
              //view payroll report function();
    	  break;
    	case 5:
              //view employee list function();
              break;
    	default:
    	  cout<<"Invalid selection"<<endl;
    	  break;
          }
          break;
        case 3:
          clrscr();
          int sel3;
          cout<<"1. View Customer List per Location"<<endl
    	  <<"2. View Employee List per Location"<<endl
    	  <<"3. View Inventory List per Location"<<endl
    	  <<"Select an option: ";
          cin>>sel3;
          cin.ignore();
          switch(sel1){
    	case 1:
              //view customer list per location function();
              break;
    	case 2:
              //view employee list per location function();
              break;
    	case 3:
              //view inventory list per location function();
              break;
    	default:
    	  cout<<"Invalid selection"<<endl;
    	  break;
          }
          break;
        case 4:
          clrscr();
          int sel4;
          cout<<"1. Check-In Inventory"<<endl
    	  <<"2. View Inventory List"<<endl
    	  <<"3. Remove Movie"<<endl
    	  <<"Select an option: ";
          cin>>sel4;
          cin.ignore();
          switch(sel1){
    	case 1:
              //check-in inventory function();
              break;
    	case 2:
              //view inventory function();
              break;
    	case 3:
              //remove movie function();
              break;
    	default:
    	  cout<<"Invalid selection"<<endl;
    	  break;
          }
          break;
        case 5:
          return 0;
        default:
          cout<<"Invalid selection"<<endl;
          break;
      }
      }
    }
    
    // Function to add a new customer
    void new_cust(Customer *iter) {
      char temp[80];
      iter->next = new Customer;
      iter = iter->next;
      cout << "Enter your name: ";
      cin.getline( temp, 80 );
      strcpy( iter->name, temp );
      iter->next = NULL;
    }
    
    //test print of list
    void test_func(Customer *iter, Customer *root) {
      iter = root;
      while ( iter != NULL )
      {
        int q = 1;
              cout << iter->name << " " << q++ << endl;
          iter = iter->next;
         }
    
    }
    pointer = NULL

  5. #5
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Changing the value contained in a pointer is local to a function. If you want to change what iter points to then you'll have to pass a reference or pointer to it -

    Code:
    // Function to add a new customer
    void new_cust(Customer* &iter) {
      char temp[80];
      iter->next = new Customer;
      iter = iter->next;
      cout << "Enter your name: ";
      cin.getline( temp, 80 );
      strcpy( iter->name, temp );
      iter->next = NULL;
    }
    You'll have to alter the declaration also.
    zen

  6. #6
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    That was the problem, thanks zen.
    /* goes to read up on pointers and references */
    pointer = NULL

Popular pages Recent additions subscribe to a feed

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