double link list problem

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 2056 Views - Last Post: 12 September 2013 - 04:39 PM Rate Topic: -----

#16 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: double link list problem

Posted 10 September 2013 - 09:30 PM

View Postannie12, on 11 September 2013 - 04:54 AM, said:

i used the link pointer so the tmp pointer knows where it to stop. i remove the link pointer but i dont understand what should i do in the while loop to make it teminate


The next pointer of tail should be (will be) 0/null.

61              tmp=tmp->nxtptr;


So you can check for tmp equal null.


Concepts
When you have an empty list head and tail will equal zero/null.

When you have one item/node in the list head and tail will point to the same node. When you add a node to the end, then you should update tail but head will still point to the same/previous node.

With more than one nodes already in the list, the code will be the same - only changing the tail pointer and leaving the head pointer as is.

The node pointed to by head will have a previous pointer set to null.
The node pointed to by tail will have a next pointer set to null.
Was This Post Helpful? 0
  • +
  • -

#17 annie12   User is offline

  • D.I.C Head

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

Re: double link list problem

Posted 11 September 2013 - 12:35 AM

View Post#define, on 11 September 2013 - 09:30 AM, said:

View Postannie12, on 11 September 2013 - 04:54 AM, said:

i used the link pointer so the tmp pointer knows where it to stop. i remove the link pointer but i dont understand what should i do in the while loop to make it teminate


The next pointer of tail should be (will be) 0/null.

61              tmp=tmp->nxtptr;


So you can check for tmp equal null.


Concepts
When you have an empty list head and tail will equal zero/null.

When you have one item/node in the list head and tail will point to the same node. When you add a node to the end, then you should update tail but head will still point to the same/previous node.

With more than one nodes already in the list, the code will be the same - only changing the tail pointer and leaving the head pointer as is.

The node pointed to by head will have a previous pointer set to null.
The node pointed to by tail will have a next pointer set to null.

i tried as you say but its still the same problem and one question if i didnot change head whats the right way of updating nxtptr
  void addtolist(int val)
      {
           
           if(tail==0)
           {
              node *newptr=new node(val);
              newptr->prvptr=0;
              newptr->nxtptr=0;
              tail=newptr;
              head=newptr;
              
              }       
           else
           {
               node *tmp=new node(val);
               tail->prvptr=tail;
               
               tail->nxtptr=tmp;
               tmp->nxtptr=0;
               tail=tmp;
               }
               
              
              
               
       }  
       void print()
       {
            node *tmp=tail;
            tmp=tail->nxtptr;
            while(tmp!=head)
            {
              cout<<tmp->data<<" ";
              tmp=tmp->nxtptr;
              }
       }


Was This Post Helpful? 0
  • +
  • -

#18 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: double link list problem

Posted 11 September 2013 - 04:43 PM

Here in addtolist it is the tmp previous pointer you wish to change to tail. It might help to draw it on paper.

16             tail->prvptr=tail;



apart from that the rest of the function looks ok (as far as I'm concerned).


What if tmp was called new_tail? :

         else
         {
             node * new_tail = new node(val);

             /* ... */

             tail = new_tail;
         }




In print, I take it, you are printing the items from the head to the tail (or is it the other way around). You can use some comments which specify what you are trying to do and any important points:

// print items from the head to the tail
// use the next pointer to move to the next node
// finish when beyond tail and the current pointer is null
void print()
{
  // current node pointer initialised to start - head?
  // node * tmp = tail;
  // tmp=tail->nxtptr;

  // loop until current pointer is null  
  // while(tmp!=head)
  {
    // print item in node
    cout << tmp->data << " ";
    // set current pointer to next pointer
    tmp = tmp->nxtptr;
  }
}


Was This Post Helpful? 0
  • +
  • -

#19 annie12   User is offline

  • D.I.C Head

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

Re: double link list problem

Posted 12 September 2013 - 01:37 PM

View Post#define, on 12 September 2013 - 04:43 AM, said:

Here in addtolist it is the tmp previous pointer you wish to change to tail. It might help to draw it on paper.

16             tail->prvptr=tail;



apart from that the rest of the function looks ok (as far as I'm concerned).


What if tmp was called new_tail? :

         else
         {
             node * new_tail = new node(val);

             /* ... */

             tail = new_tail;
         }




In print, I take it, you are printing the items from the head to the tail (or is it the other way around). You can use some comments which specify what you are trying to do and any important points:

// print items from the head to the tail
// use the next pointer to move to the next node
// finish when beyond tail and the current pointer is null
void print()
{
  // current node pointer initialised to start - head?
  // node * tmp = tail;
  // tmp=tail->nxtptr;

  // loop until current pointer is null  
  // while(tmp!=head)
  {
    // print item in node
    cout << tmp->data << " ";
    // set current pointer to next pointer
    tmp = tmp->nxtptr;
  }
}


after again changing as follows it there is no error but its not printing 5 and 9 which are added to the list and its printing 8 instead of that
#include<iostream>
#include<stdio.h>
using namespace std;
class node{
      public:
      node *prvptr;
      node *nxtptr;
      int data;
      node()
      {
            prvptr=0;
            nxtptr=0;
            }      
      node(int val,node *p=0,node *n=0)
      {
               prvptr=p;
               nxtptr=n;
               data=val;
               }
      };
     
class dll{//double link list
      public:
      node *head;
      node *tail;
      node *link;
      
      dll()
      {
        head=0;
        tail=0;
        }
    void addtolist(int val)
    {
         
         if(tail==0)
         {
            node *newptr=new node(val);
            newptr->prvptr=0;
            newptr->nxtptr=0;
            tail=newptr;
            head=newptr;
            
            }       
         else
         {
             node *tmp=new node(val);
             tmp->prvptr=tail;
             
             tmp->nxtptr=tmp;
             tmp->nxtptr=0;
             tail=tmp;
             }       
     }  
     void print()
     {
          node *tmp=head;//tmp points to head
       
          while(tmp!=0)
          {
            cout<<tmp->data<<" ";//now tmp points to data in the head 
            tmp=tmp->nxtptr;//tmp points to nextptr in the head
            }
     }

       };
int main()
{
    dll obj;
    obj.addtolist(5);
    obj.addtolist(9);
    obj.print();
    cin.get();
    return 0;
}                                        
  


Was This Post Helpful? 0
  • +
  • -

#20 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 794
  • View blog
  • Posts: 2,880
  • Joined: 31-December 10

Re: double link list problem

Posted 12 September 2013 - 04:39 PM

Your problem is in the addtolist() member function, particulary this code:
node *tmp=new node(val);
tmp->prvptr=tail;
             
tmp->nxtptr=tmp;
tmp->nxtptr=0;
tail=tmp;


You're not connecting the old tail's next pointer to the new node. That's why only the first number is being added. Setting tmp's next pointer to tmp and then setting it to zero is somewhat strange when you just need to set it to zero.

*EDIT*:
I'm assuming the third line with code on it was meant to be:
tail->nxtptr = tmp;


This post has been edited by vividexstance: 12 September 2013 - 04:41 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2