list reverse function

L

lovecreatesbea...

Reverse a single directional non-circle list. For example:

p | p
| | |
+-+-+ +---+ +---+ +---+ | +---+ +---+ +---+ +-+-
+
| 1 | -> | 2 | -> | 3 | -> | 4 | | | 1 | <- | 2 | <- | 3 | <- | 4
|
+---+ +---+ +---+ +---+ | +---+ +---+ +---+ +---
+

Comments are welcome.


struct node {int data; struct node next;};

struct node list_rvs(struct node p)
{
struct node p1 = p, p2 = p->next, p3;

while (p2){
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
}
p->next = 0;
p = p1;
return p;
}
 
E

Eric Sosman

(e-mail address removed) wrote On 10/16/07 15:28,:
Reverse a single directional non-circle list. For example:

p | p
| | |
+-+-+ +---+ +---+ +---+ | +---+ +---+ +---+ +-+-
+
| 1 | -> | 2 | -> | 3 | -> | 4 | | | 1 | <- | 2 | <- | 3 | <- | 4
|
+---+ +---+ +---+ +---+ | +---+ +---+ +---+ +---
+

Comments are welcome.

What comments did your compiler offer when you
asked it (politely) to review your code?
struct node {int data; struct node next;};
[...]

'nuff said.
 
R

Richard

Reverse a single directional non-circle list. For example:

p | p
| | |
+-+-+ +---+ +---+ +---+ | +---+ +---+ +---+ +-+-
+
| 1 | -> | 2 | -> | 3 | -> | 4 | | | 1 | <- | 2 | <- | 3 | <- | 4
|
+---+ +---+ +---+ +---+ | +---+ +---+ +---+ +---
+

Comments are welcome.


struct node {int data; struct node next;};

struct node list_rvs(struct node p)
{
struct node p1 = p, p2 = p->next, p3;

while (p2){
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
}
p->next = 0;
p = p1;
return p;
}

You need to examine the use of structures versus pointers to structures.

Did you compile this?
 
P

Peter Pichler

Reverse a single directional non-circle list.

Is that an order?
Comments are welcome.

Comments on what? Your code does not compile because you miss asterisks
all over the place. As others suggested, you would have known that
yourself had you bothered to compile before posting. Fix the syntax
errors and your homework is done. Where did you copy it from, just out
of interest? The algorithm looks OK, suggesting a level of understanding
that makes the absence of asterisks rather surprising.
 
L

lovecreatesbea...

You need to examine the use of structures versus pointers to structures.

Did you compile this?

Yes, it compiles without syntax error. The parameter and all local
variable are pointers. The asterisks were removed carelessly when I
did replacement. Thank you.
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top