Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > list reverse function

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      10-16-2007
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.

--


 
Reply With Quote
 
 
 
 
Richard
Guest
Posts: n/a
 
      10-16-2007
"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?
 
Reply With Quote
 
Peter Pichler
Guest
Posts: n/a
 
      10-16-2007
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.
 
Reply With Quote
 
lovecreatesbea...@gmail.com
Guest
Posts: n/a
 
      10-17-2007
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.

 
Reply With Quote
 
 
 
Reply

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
What's wrong in my function of reverse the doubly linked list with dummy node. Daniel C Programming 5 10-27-2004 10:16 PM
little problem with list.reverse() function manuel Python 3 09-11-2004 04:06 PM
Stacks Queues Reverse Reverse Polish dogbite C++ 4 10-10-2003 05:06 AM
How do I list records in reverse order? Shannon Yoder ASP .Net 1 08-14-2003 05:14 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57