6 Replies - 429 Views - Last Post: 02 September 2013 - 04:40 AM Rate Topic: -----

#1 annie12  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 27-July 13

link list function not working

Posted 01 September 2013 - 07:13 AM

i make a link list but its display function is not working
#include<iostream>
using namespace std;
//node for single link list
class node1{
public:
      int data;
      node1 *nextptr;
      node1(){}
      node1(node1 *ptr=NULL)
      {
           nextptr=ptr;
           }
      node1(int val,node1 *pt=NULL)
      {       
              data=val;  
              nextptr=pt;
              }
      };
//class single link list      
class sll{
public:
       node1 *lastptr,*firstptr;
       sll()
       {
            lastptr=NULL;
            firstptr=NULL;
            }
       void addfront()
       { 
            int vl;
            cout<<"Enter a value : ";
            cin>>vl;
            node1 *newptr=new node1(vl);
            newptr->nextptr=newptr;
            firstptr=newptr;
            
       } 
       void addback()
       {
            int vl;
            cout<<"Enter a value : ";
            cin>>vl;
            node1 *newptr=new node1(vl);
            newptr->nextptr=newptr;
            lastptr=newptr;
            }
       void display()
       {
          node1 *tmp;
          cout<<tmp->data<<" ";
          }
       };      
int main()
{
    sll obj;
    obj.addback();
    obj.display();
    system("PAUSE");
    return 0;
}         
               




Is This A Good Question/Topic? 0
  • +

Replies To: link list function not working

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: link list function not working

Posted 01 September 2013 - 07:41 AM

void display()
{
    node1 *tmp;
    cout<<tmp->data<<" ";
}



First of all you're dereferencing an uninitialized pointer, so your code invokes undefined behavior and will likely crash.

Secondly you only have a single print statement in your function and its not inside a loop, nor is your function recursive. Ergo only one item will be printed by this function (or it would if it didn't crash before then). Since, I assume, the display method is supposed to print every element of the list, that can't be right. To print all of the elements of the list, your function should contain a loop.
Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3001
  • View blog
  • Posts: 10,393
  • Joined: 08-August 08

Re: link list function not working

Posted 01 September 2013 - 07:49 AM

  • You aren't keeping track of the first element. You're assigning the address of the current object to firstptr.
  • Your display function creates a pointer to nothing, then tries to use it as if it were an object.

Was This Post Helpful? 2
  • +
  • -

#4 annie12  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 27-July 13

Re: link list function not working

Posted 01 September 2013 - 11:35 AM

View PostCTphpnwb, on 01 September 2013 - 07:49 PM, said:

  • You aren't keeping track of the first element. You're assigning the address of the current object to firstptr.
  • Your display function creates a pointer to nothing, then tries to use it as if it were an object.

i change the code its running but not printing
#include<iostream>
using namespace std;
//node for single link list
class node1{
public:
      int data;
      node1 *nextptr;
      node1(){}
      node1(node1 *ptr=NULL)
      {
           nextptr=ptr;
           }
      node1(int val,node1 *pt=NULL)
      {       
              data=val;  
              nextptr=pt;
              }
      };
//class single link list      
class sll{
public:
       node1 *lastptr,*firstptr;
       sll()
       {
            lastptr=NULL;
            firstptr=NULL;
            }
       void addfront()
       { 
            int vl;
            cout<<"Enter a value : ";
            cin>>vl;
            node1 *newptr=new node1(vl);
            newptr->nextptr=newptr;
            firstptr=newptr;
            
       } 
       void addback()
       {
            int vl;
            cout<<"Enter a value : ";
            cin>>vl;
            node1 *newptr=new node1(vl);
            newptr->nextptr=newptr;
            lastptr=newptr;
            }
       void display()
       {
          node1 *tmp=firstptr;
          
          while(tmp!=0){
          cout<<tmp->data<<" ";
          tmp=tmp->nextptr;
          }
       }
       };      
int main()
{
    sll obj;
    obj.addback();
    obj.display();
    system("PAUSE");
    return 0;
}         


Was This Post Helpful? 0
  • +
  • -

#5 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 665
  • View blog
  • Posts: 2,296
  • Joined: 31-December 10

Re: link list function not working

Posted 01 September 2013 - 12:01 PM

This is just my opinion, but I'm sure others would agree. The addFront() and addBack() member functions should take as an argument the value you want to add to the linked list. You should try to write your functions so that they have one task to perform and that's all they do. So you could ask for the values from standard input while in main() and then pass those values to the add*() functions as an argument. This will simplify the add*() member functions so you can jusy focus on what they need to do.

A side note, you really should be writing comments to help yourself and others be able to understand the code. If you came back to this code a year from now, are you going to remember everything the code does? Probably not, but why make it so you need to remember?
Was This Post Helpful? 0
  • +
  • -

#6 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 481
  • Joined: 05-February 10

Re: link list function not working

Posted 01 September 2013 - 12:29 PM

Your display() function is correct. This should signal to you a problem with addback().

This post has been edited by Java Student: 01 September 2013 - 12:30 PM

Was This Post Helpful? 1
  • +
  • -

#7 annie12  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 27-July 13

Re: link list function not working

Posted 02 September 2013 - 04:40 AM

View Postvividexstance, on 02 September 2013 - 12:01 AM, said:

A side note, you really should be writing comments to help yourself and others be able to understand the code. If you came back to this code a year from now, are you going to remember everything the code does? Probably not, but why make it so you need to remember?

because codes are good memories :surrender:

This post has been edited by annie12: 02 September 2013 - 04:51 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1