|
|
|
#1 (permalink) |
|
Messages: n/a
Hébergeur: |
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; } |
|
|
|
#2 (permalink) |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 (permalink) |
|
Messages: n/a
Hébergeur: |
"lovecreatesbea...@gmail.com" <lovecreatesbeauty@gmail.com> 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? |
|
|
|
#4 (permalink) |
|
Messages: n/a
Hébergeur: |
lovecreatesbea...@gmail.com 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. |
|
|
|
#5 (permalink) |
|
Messages: n/a
Hébergeur: |
On Oct 17, 4:02 am, Richard <rgr...@gmail.com> wrote:
> "lovecreatesbea...@gmail.com" <lovecreatesbea...@gmail.com> 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. |
|
![]() |
| Outils de la discussion | |
|
|