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

    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;
    }
    , Oct 16, 2007
    #1
    1. Advertising

  2. Eric Sosman Guest

    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.

    --
    Eric Sosman, Oct 16, 2007
    #2
    1. Advertising

  3. Richard Guest

    "" <> 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?
    Richard, Oct 16, 2007
    #3
  4. 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.
    Peter Pichler, Oct 16, 2007
    #4
  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
    > > |
    > > +---+ +---+ +---+ +---+ | +---+ +---+ +---+ +---
    > > +

    >
    > > 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.
    , Oct 17, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shannon Yoder

    How do I list records in reverse order?

    Shannon Yoder, Aug 14, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    373
  2. dogbite
    Replies:
    4
    Views:
    673
    osmium
    Oct 10, 2003
  3. Perpetual Snow

    Reverse a linked list

    Perpetual Snow, Nov 26, 2003, in forum: C Programming
    Replies:
    9
    Views:
    3,306
    Derk Gwen
    Nov 27, 2003
  4. manuel
    Replies:
    3
    Views:
    263
    Roy Smith
    Sep 11, 2004
  5. Daniel
    Replies:
    5
    Views:
    374
Loading...

Share This Page