Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > explain the lines

Reply
Thread Tools

explain the lines

 
 
simran_hello_000@yahoo.com
Guest
Posts: n/a
 
      01-28-2007
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

 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      01-28-2007
"" <> 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

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      01-29-2007
On Jan 28, 5:51 pm, "Daniel T." <danie...@earthlink.net> wrote:
> "simran_hello_...@yahoo.com" <simran_hello_...@yahoo.com> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Anyone explain these grid lines? Stone Cold Digital Photography 6 05-11-2006 01:52 AM
Asp.Net Calender, how to display 5 lines if there are only 5 lines in one month? Jack ASP .Net 9 10-12-2005 03:44 AM
can anyone explain the last two lines to me?thanks! biomahui@gmail.com Perl Misc 8 04-18-2005 03:27 AM
Explain the magic? Counting lines in a file Dale Atkin C++ 6 12-15-2004 04:49 PM
Modems, Analog Lines and ... Electrical Lines? Sens Fan Happy In Ohio Computer Support 5 09-02-2004 04:15 AM



Advertisments