Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > linked list

Reply
Thread Tools

linked list

 
 
Roy J
Guest
Posts: n/a
 
      04-06-2005
Hi all :
I'm a newbie of c language , I confronted with a problem as below :

#include <stdio.h>
#include <stdlib.h>

typedef struct player {
int number;
struct player *next;
}player;

player *player_init(int n) {
/* Create a loop linked list and initialization */

int i;
player *head,*py;

head = malloc(sizeof *head);
if (! head)
puts("malloc error !");
head->number = n;
head->next = NULL;
for (i = n-1; i > 0; i--) {
py = malloc(sizeof *head);
if (! py)
puts("malloc error !");
py->number = i;
py->next = head->next;
head->next = py;
if (i == 1)
py->next = head;
}
return head;
}
int main(void)
{
int n;
player *head,*tmp;

puts("input the number of player :");
scanf("%d",&n);
head = player_init(n);
for (tmp = head; tmp->next = head; tmp++)
printf("the node #%d\n",(*tmp).number);
return 0;
}
-----------------
I confused the wrong ?

 
Reply With Quote
 
 
 
 
Mark McIntyre
Guest
Posts: n/a
 
      04-06-2005
On 6 Apr 2005 04:46:13 -0700, in comp.lang.c , "Roy J"
<> wrote:

>Hi all :
>I'm a newbie of c language , I confronted with a problem as below :


you posted some code.

You should also post what the problem is. Not everyone will be
prepared to copy your code to a file, compile it and debug it for you.
This is something you should do yourself.


--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
 
Reply With Quote
 
 
 
 
Mark McIntyre
Guest
Posts: n/a
 
      04-06-2005
On 6 Apr 2005 04:46:13 -0700, in comp.lang.c , "Roy J"
<> wrote:

>Hi all :
>I'm a newbie of c language , I confronted with a problem as below :


I think your logic for setting next is wrong. If you debug this,
you'll see it. Alternatively, draw a diagram of what each object
contains/points to, as you iterate from say n = 4 down to n=1.

Also, you need to set next to NULL for your last element, otherwise
theres no way to know you're at the end of the list.

> for (tmp = head; tmp->next = head; tmp++)


this is wrong. You need to move forward to the next node, and you
should keep going till you have a NULL next.

for(tmp=head; tmp!=NULL; tmp = tmp->next)

> printf("the node #%d\n",(*tmp).number);


the usual idiom for this is tmp->number

Also, note that you didn't free any of the malloc'ed pointers. You
should always do this as your OS may not otherwise recover the used
memory.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
Gregory Pietsch
Guest
Posts: n/a
 
      04-06-2005
The comp.lang.c Oracle has pondered your question deeply.

Your question was:

> I confused the wrong ?


And thus spake the Oracle:

} The wrong was not confused. The programmer may be both confused and
wrong,
} nevertheless.
}
} To help you, O supplicant, I suggest implementing two functions:
push() and
} pop(), that take care of putting items onto a singly-linked list such
as this
} and taking the items off again. Think of the SLL as a stack.
}
} Once you master linked lists, you can transcend them. Get the source
code to
} FreeDOS Edlin 2.4 and marvel at how cool dynamic array of dynamic
string code
} can be.

Gregory Pietsch

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      04-07-2005
On 6 Apr 2005 04:46:13 -0700, "Roy J" <> wrote:

>Hi all :
>I'm a newbie of c language , I confronted with a problem as below :
>
>#include <stdio.h>
>#include <stdlib.h>
>
>typedef struct player {
> int number;
> struct player *next;
>}player;
>
>player *player_init(int n) {
> /* Create a loop linked list and initialization */
>
> int i;
> player *head,*py;
>
> head = malloc(sizeof *head);
> if (! head)
> puts("malloc error !");
> head->number = n;


I have no idea what your real problem is but if malloc returns NULL,
then you print an error message. That is good. Unfortunately, you
then proceed to use head as if it pointed to a struct. It does not.
Once you have determined that head is useless, you must not attempt to
use it.


<<Remove the del for email>>
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      04-07-2005
Roy J wrote:
>
> I'm a newbie of c language , I confronted with a problem as below :

.... snip ...

Don't make things over complicated. Study (and run) the following
simple demonstration. Modify to taste. The input mechanism is
especially weak, since it doesn't detect overflows.

------------- cut here, linklist.c --------------
/* A simplified demo of creating a linked list */
/* EOF or a non-numeric entry ends entry phase */

#include <stdio.h>
#include <stdlib.h>

typedef int datatype;

struct node {
struct node *next;
datatype datum;
};

/* ----------------- */

/* add value to head of list */
struct node *addtolist(struct node *root, datatype value)
{
struct node *newnode;

if ((newnode = malloc(sizeof *newnode))) {
newnode->next = root;
newnode->datum = value;
}
return newnode;
} /* addtolist */

/* ----------------- */

void showlist(struct node *root)
{
while (root) {
printf("%d\n", root->datum);
root = root->next;
}
} /* showlist */

/* ----------------- */

int main(void)
{
struct node *root, *tmp;
datatype num;

root = NULL;
while (1 == scanf("%d", &num)) {
if ((tmp = addtolist(root, num)) root = tmp;
else break;
}
showlist(root);
return 0;
} /* main linklist.c */

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

 
Reply With Quote
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      04-08-2005
Groovy hepcat Roy J was jivin' on 6 Apr 2005 04:46:13 -0700 in
comp.lang.c.
linked list's a cool scene! Dig it!

>Hi all :
>I'm a newbie of c language , I confronted with a problem as below :
>
>#include <stdio.h>
>#include <stdlib.h>
>
>typedef struct player {
> int number;
> struct player *next;
>}player;
>
>player *player_init(int n) {
> /* Create a loop linked list and initialization */
>
> int i;
> player *head,*py;
>
> head = malloc(sizeof *head);
> if (! head)
> puts("malloc error !");


You must quit here. If head is null, you print "malloc error !" and
then go on using head. That's not good.

> head->number = n;
> head->next = NULL;
> for (i = n-1; i > 0; i--) {
> py = malloc(sizeof *head);
> if (! py)
> puts("malloc error !");


You should quit here too, after cleaning up all the memory so far
allocated.

> py->number = i;
> py->next = head->next;
> head->next = py;
> if (i == 1)
> py->next = head;
> }
> return head;
>}
>int main(void)
>{
> int n;
> player *head,*tmp;
>
> puts("input the number of player :");
> scanf("%d",&n);
> head = player_init(n);
> for (tmp = head; tmp->next = head; tmp++)

^^^^^^^^^^^^^^^^
And here you're changing the next pointer of the first node of the
list. No doubt what you meant to do was this:

for (tmp = head; tmp = head->next; tmp++)

> printf("the node #%d\n",(*tmp).number);
> return 0;
>}
>-----------------
>I confused the wrong ?


<SARCASM>Well, don't tell us what the problem is, will you. We love
guessing games.</SARCASM> If the problem is not fixed by doing as I
suggest above, then you'll have to tell us what you expect to happen
and what is actually happening, including verbatim all diagnostic
output from the compiler, if any.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      04-08-2005
(Peter "Shaggy" Haywood) wrote:

> Groovy hepcat Roy J was jivin' on 6 Apr 2005 04:46:13 -0700 in
> > head = malloc(sizeof *head);
> > if (! head)
> > puts("malloc error !");

>
> You must quit here. If head is null, you print "malloc error !" and
> then go on using head. That's not good.


No, you must deal with the error here. Quitting is one solution, but
frequently not the right one. Continuing with a less efficient
algorithm, or aborting this one function, reporting this to the user,
and keeping the rest of the program running are often good alternatives.

Richard
 
Reply With Quote
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      04-10-2005
Groovy hepcat Richard Bos was jivin' on Fri, 08 Apr 2005 06:37:13 GMT
in comp.lang.c.
Re: linked list's a cool scene! Dig it!

> (Peter "Shaggy" Haywood) wrote:
>
>> Groovy hepcat Roy J was jivin' on 6 Apr 2005 04:46:13 -0700 in
>> > head = malloc(sizeof *head);
>> > if (! head)
>> > puts("malloc error !");

>>
>> You must quit here. If head is null, you print "malloc error !" and
>> then go on using head. That's not good.

>
>No, you must deal with the error here. Quitting is one solution, but
>frequently not the right one. Continuing with a less efficient
>algorithm, or aborting this one function, reporting this to the user,
>and keeping the rest of the program running are often good alternatives.


Indeed. Good advice.
But the point is that one shouldn't go on using a pointer after a
malloc(), calloc() or realloc() failure. Printing a diagnostic message
isn't enough. Skipping the code that uses the pointer (by quitting or
some other course of action) is a must.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
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
Airplane Program with Linked Lists. The linked list portion is veryconfusing to me. jawdoc C++ 9 03-10-2008 03:38 AM
Linked list within a linked list joshd C++ 12 10-02-2006 08:57 AM
Linked list, New try (was:Linked list, no out put,help) fool C Programming 14 07-03-2006 12:29 AM
Generating a char* from a linked list of linked lists Chris Ritchey C++ 7 07-10-2003 10:12 PM
Generating a char* from a linked list of linked lists Chris Ritchey C Programming 7 07-10-2003 10:12 PM



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