# list reverse function

Discussion in 'C Programming' started by lovecreatesbea...@gmail.com, Oct 16, 2007.

1. ### Guest

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

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

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;
}

, Oct 16, 2007

2. ### Eric SosmanGuest

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

> struct node {int data; struct node next;};
> [...]

'nuff said.

--

Eric Sosman, Oct 16, 2007

3. ### RichardGuest

"" <> writes:

> Reverse a single directional non-circle list. For example:
>
> p | p
> | | |
> +-+-+ +---+ +---+ +---+ | +---+ +---+ +---+ +-+-
> +
> | 1 | -> | 2 | -> | 3 | -> | 4 | | | 1 | <- | 2 | <- | 3 | <- | 4
> |
> +---+ +---+ +---+ +---+ | +---+ +---+ +---+ +---
> +
>
>
>
> 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?

Richard, Oct 16, 2007
4. ### Peter PichlerGuest

wrote:

> Reverse a single directional non-circle list.

Is that an order?

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.

Peter Pichler, Oct 16, 2007
5. ### Guest

On Oct 17, 4:02 am, Richard <> wrote:
> "" <> writes:
> > Reverse a single directional non-circle list. For example:

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

>

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

, Oct 17, 2007