linked list

R

Roy J

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

Mark McIntyre

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.
 
M

Mark McIntyre

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.
 
G

Gregory Pietsch

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
 
B

Barry Schwarz

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

CBFalconer

Roy said:
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 */
 
P

Peter Shaggy Haywood

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

<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"?
 
R

Richard Bos

Groovy hepcat Roy J was jivin' on 6 Apr 2005 04:46:13 -0700 in

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
 
P

Peter Shaggy Haywood

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!
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"?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top