Know this useful things...........

R

Ramu

How do you write a function that can reverse a linked-list?

void reverselist(void)
{
if(head==0)
return;
if(head->next==0)
return;
if(head->next==tail)
{
head->next = 0;
tail->next = head;
}
else
{
node* pre = head;
node* cur = head->next;
node* curnext = cur->next;
head->next = 0;
cur-> next = head;

for(; curnext!=0; )
{
cur->next = pre;
pre = cur;
cur = curnext;
curnext = curnext->next;
}
curnext->next = cur;
}
}

What is public, protected, private?

* Public, protected and private are three access specifiers in C++.
* Public data members and member functions are accessible outside the
class.
* Protected data members and member functions are only available to
derived classes.
* Private data members and member functions can't be accessed outside
the class. However there is an exception can be using friend classes.
 
A

Andrey Tarasevich

Ramu said:
How do you write a function that can reverse a linked-list?

void reverselist(void)
{
> [ugly code skipped]
}

No, that not how you write such a function. Not only it's badly formatted, it is
badly and amateurishly implemented. Useless, not useful.
What is public, protected, private?

My cat's name is Mittens.
 
L

Lutz Altmann

How do you write a function that can reverse a linked-list?

void reverselist(void)
{
if(head==0)
return;
if(head->next==0)
return;
if(head->next==tail)
{
head->next = 0;
tail->next = head;}

else
{
node* pre = head;
node* cur = head->next;
node* curnext = cur->next;
head->next = 0;
cur-> next = head;

for(; curnext!=0; )
{
cur->next = pre;
pre = cur;
cur = curnext;
curnext = curnext->next;}

curnext->next = cur;

}
}

What is public, protected, private?

* Public, protected and private are three access specifiers in C++.
* Public data members and member functions are accessible outside the
class.
* Protected data members and member functions are only available to
derived classes.
* Private data members and member functions can't be accessed outside
the class. However there is an exception can be using friend classes.

:)
There's no need to do that by yourself .. take
a look at the stl reverse algorithm : http://www.sgi.com/tech/stl/reverse.html
!

lutz altmann
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top