explain the lines

Discussion in 'C++' started by simran_hello_000@yahoo.com, Jan 28, 2007.

  1. Guest

    content=// Program to implement Linked List.

    #include <iostream.h>
    #include <conio.h>
    struct node
    {
    node* ptr;
    int data;
    };

    class list
    {
    node* lastptr;

    public:
    list();
    ~list();
    void add(int);
    void remove(int);
    void display();
    node* searchnode(int);
    void releasenode();
    };

    list::list()
    {
    lastptr='\0';
    }

    list::~list()
    {
    while (lastptr)
    {
    node* thisptr=lastptr;
    lastptr=lastptr->ptr;
    delete thisptr;
    }
    }

    void list::add(int value)
    {
    node* thisptr=new(node);
    thisptr->data=value;
    thisptr->ptr=lastptr;
    lastptr=thisptr;
    }

    node* list::searchnode(int value)
    {
    node* thisptr=lastptr;
    while (thisptr)
    {
    if (thisptr->data==value)
    break;
    thisptr=thisptr->ptr;
    }
    return thisptr;
    }

    void list::remove(int value)
    {
    node* thisptr=searchnode(value);

    if (!thisptr) return; //????????????
    while (thisptr->ptr) //???????????
    {
    node* pptr=thisptr->ptr; //????????
    thisptr->data=pptr->data; //????????
    thisptr=thisptr->ptr; //??????????
    }
    releasenode();
    }

    void list::releasenode()
    {
    node* thisptr=lastptr;
    node* previousptr;
    while (thisptr->ptr)
    {
    previousptr=thisptr;
    thisptr=thisptr->ptr;
    }
    if (thisptr==lastptr) lastptr='\0';
    else previousptr->ptr='\0';
    delete thisptr;
    }

    void list::display()
    {
    node* thisptr=lastptr;
    //Loop until this pointer equal to NULL.
    while (thisptr)
    {
    cout<<"Data : "<<thisptr->data<<"\n";
    cout<<"Address : "<<thisptr->ptr<<"\n";
    thisptr=thisptr->ptr;
    }
    }

    void main()
    {
    list linklist;

    //add number(1 until 7) into link list.
    linklist.add(1);
    linklist.add(2);
    linklist.add(3);
    linklist.add(4);
    linklist.add(5);
    linklist.add(6);
    linklist.add(7);
    getch();
    //remove number 4 from the list.
    linklist.remove(4);

    //display the number in descending order.
    linklist.display();
    cout<<endl;
    cout<<"Press any key to continue"<<endl;
    getch();
    }
    hi! this is a prog can anyone help by explaining the lines
    followed by?????????? in function remove
    , Jan 28, 2007
    #1
    1. Advertising

  2. Daniel T. Guest

    "" <> wrote:

    > content=// Program to implement Linked List.
    >


    Odd choice of algorithms.

    > void list::remove(int value)
    > {
    > node* thisptr=searchnode(value);
    >
    > if (!thisptr) return; //????????????


    If searchnode() didn't find a node which has its data equal value, then
    return and do nothing.

    > while (thisptr->ptr) //???????????
    > {
    > node* pptr=thisptr->ptr; //????????
    > thisptr->data=pptr->data; //????????
    > thisptr=thisptr->ptr; //??????????
    > }


    The above loop goes through every node from the one found, to the end of
    the list and copies the value of the next node to this node. This
    algorithm is usually used on vectors only. The strength of a list is
    that a single element can be removed without affecting the values in
    other nodes.

    > releasenode();
    > }
    >
    > hi! this is a prog can anyone help by explaining the lines
    > followed by?????????? in function remove
    Daniel T., Jan 28, 2007
    #2
    1. Advertising

  3. On Jan 28, 5:51 pm, "Daniel T." <> wrote:
    > "" <> wrote:
    > > content=// Program to implement Linked List.

    >
    > Odd choice of algorithms.
    >
    > > void list::remove(int value)
    > > {
    > > node* thisptr=searchnode(value);

    >
    > > if (!thisptr) return; //????????????

    >
    > If searchnode() didn't find a node which has its data equal value, then
    > return and do nothing.


    If searchnode() didn't find a node it returns a 0-pointer and if an
    expression in an if-statement evaluates to 0 it's treated like false,
    so 'if (false)' is the same as 'if (0)'. It's kind of bad style to
    write code like that, it's better to actually write what you mean so
    'if (thisptr == 0)' would be better.


    > > while (thisptr->ptr) //???????????


    Same thing here.

    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Jan 29, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack
    Replies:
    9
    Views:
    2,637
  2. Dale Atkin
    Replies:
    6
    Views:
    416
    Mike Wahler
    Dec 15, 2004
  3. Joe Wright
    Replies:
    0
    Views:
    497
    Joe Wright
    Jul 27, 2003
  4. lovecreatesbeauty

    How to know two lines are a pare parallel lines

    lovecreatesbeauty, Apr 27, 2006, in forum: C Programming
    Replies:
    11
    Views:
    641
    Old Wolf
    Apr 28, 2006
  5. Replies:
    8
    Views:
    123
    Joe Smith
    Apr 18, 2005
Loading...

Share This Page