list reverse function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lovecreatesbea...@gmail.com

    #1

    list reverse function

    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

    #2
    Re: list reverse function

    lovecreatesbea. ..@gmail.com 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@sun .com

    Comment

    • Richard

      #3
      Re: list reverse function

      "lovecreatesbea ...@gmail.com" <lovecreatesbea uty@gmail.comwr ites:
      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?

      Comment

      • lovecreatesbea...@gmail.com

        #4
        Re: list reverse function

        On Oct 17, 4:02 am, Richard <rgr...@gmail.c omwrote:
        "lovecreatesbea ...@gmail.com" <lovecreatesbea ...@gmail.comwr ites:
        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.

        Comment

        Working...