explain the lines

S

simran_hello_000

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
 
D

Daniel T.

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.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Odd choice of algorithms.



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.


Same thing here.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top