Thread: Linked List Problems

  1. #1
    Registered User
    Join Date
    Dec 2013
    Location
    Trinidad
    Posts
    6

    Question Linked List Problems

    Hey all. I've been playing around with link lists to learn it on my own so I decided to write a small program with it that just takes in data for two nodes and display them.


    Code:
    #include<iostream>
    
    
    using namespace std;
    
    
    struct batsman{
        string name;
        int fours;
        int sixes;
        int innings;
        batsman*ptr;
        batsman();
        int aggregate();
    };
    
    
    batsman::batsman(){
        name = "\0";
        fours = 0;
        sixes = 0;
        innings = 0;
        
    }
    
    
    int batsman::aggregate(){
        return (4*fours)+(6*sixes);
    }
    
    
    void display(batsman*);
    batsman*insert(batsman*,string,int,int);
    
    
    int main(){
        batsman *topptr = NULL,*temp=NULL;
        int x,f,s;
        string name;
        for(x=1;x<=2;x++){
           cout<<"Name:";
           cin>>name;
           cin.get();
           cout<<"Fours:";
           cin>>f;
           cout<<"Sixes:";
           cin>>s;
           cin.get();
           temp = insert(topptr,name,f,s);
           cout<<endl<<endl;
        }
       
            display(temp);
    
    
        
      
        cin.get();
        return 0;  
    }
    
    
    
    
    batsman* insert(batsman*topptr,string name,int fours, int sixes){
        batsman*node;
        node = (batsman*)malloc(sizeof(batsman));
        if(node!=NULL){
            node->name = name;
            node->fours = fours;
            node->sixes = sixes;
            node->innings = node->aggregate();
            node->ptr = topptr;
            topptr = node;
            return topptr;
        }else{
            cout<<"Memory Not Available";
        } 
    
    
    }
    
    
    
    
    void display(batsman*topptr){
    
    
        while(topptr!=NULL){
            cout<<"Name:\t\t"<<topptr->name<<endl;
            cout<<"Fours:\t\t"<<topptr->fours<<endl;
            cout<<"Sixes:\t\t"<<topptr->sixes<<endl;
            cout<<"Aggregate Score:"<<topptr->innings<<endl;
            topptr = topptr->ptr;
        }
        
        
    }
    However as it is...only the lastly entered node displays which makes sense because as it is the link list operates as a stack...but why doesn't it go on to print the first node even though within the while loop of the display function i've incremented topptr by doing topptr=topptr->ptr?

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    It looks like you just need to use topptr everywhere you use temp (and get rid of temp).

    You're missing some headers. At least <string> and something to define NULL (like cstdlib or perhaps cstddef), although in modern C++ nullptr is used instead (this requires at least C++11).

    Also, "\0" is probably not what you want. "" alone adds a nul char after the (empty) string, just like "hello" adds a nul char after the final 'o'. Actually, you don't need that line at all since a string object is automatically initialized to an empty string.

    And in insert the line topptr = node has no useful effect since topptr is a local variable (it doesn't modify the variable in the calling function). Just return node.

    This is C code:
    Code:
    node = (batsman*)malloc(sizeof(batsman));
    In C++ it's:
    Code:
    batsman* node = new batsman;
    Last edited by algorism; 04-02-2016 at 08:22 PM.

  3. #3
    Registered User
    Join Date
    Dec 2013
    Location
    Trinidad
    Posts
    6
    Thanks lol funny thing is the answer came to me yesterday morning when I woke up and I got it working haha. For some reason it didn't occur to me at the time that I wasn't passing the updated value of the head node each time through the function.

    Thanks man

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list problems
    By keisu in forum C Programming
    Replies: 2
    Last Post: 05-12-2012, 01:24 PM
  2. linked list problems
    By masterg in forum C++ Programming
    Replies: 15
    Last Post: 12-04-2008, 01:29 PM
  3. Linked List problems
    By Beowolf in forum C++ Programming
    Replies: 22
    Last Post: 10-15-2007, 12:02 PM
  4. Linked List Problems
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 04-18-2002, 09:10 AM
  5. Linked List problems !
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-03-2002, 11:28 PM