Velocity Reviews > list reverse function

# list reverse function

lovecreatesbea...@gmail.com
Guest
Posts: n/a

 10-16-2007
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;
}

Eric Sosman
Guest
Posts: n/a

 10-16-2007
(E-Mail 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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

Richard
Guest
Posts: n/a

 10-16-2007
"(E-Mail Removed)" <(E-Mail Removed)> writes:

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

Peter Pichler
Guest
Posts: n/a

 10-16-2007
(E-Mail Removed) wrote:

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

lovecreatesbea...@gmail.com
Guest
Posts: n/a

 10-17-2007
On Oct 17, 4:02 am, Richard <(E-Mail Removed)> wrote:
> "(E-Mail Removed)" <(E-Mail Removed)> writes:
> > 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?

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.

 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 OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post komal C++ 6 01-25-2005 11:13 AM Daniel C Programming 5 10-27-2004 10:16 PM manuel Python 3 09-11-2004 04:06 PM dogbite C++ 4 10-10-2003 05:06 AM Shannon Yoder ASP .Net 1 08-14-2003 05:14 AM

Advertisments