creating a linked list - trivial :( question

A

alternativa

Hello,
I'm a beginner in C programming and I have a problem that probably will
seem trivial to most of you, however I can't find a solution...
So, I have to write a data base - program should ask the user about the
data and then do some operations like sorting etc. I decided to use a
linked list. The code looks as follows:

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

struct somestruct
{
int field1;
char field2[15];
struct somestruct *nextp;
};

typedef struct somestruct DATA;

DATA enter_data () // function that asks the user for the data
{
DATA temp_somestruct;

printf ("field1: ");
scanf ("%s", &(temp_somestruct.field1));
getchar();

printf ("field2: ");
scanf ("%d", &(temp_somestruct.field2));
getchar();

return temp_somestruct;
}

int main (void)
{
enter_data();
return 0;
}

I want the program to use this data for creating a linked list.. then I
would ask the user for next data and add this to this list..

I guess I should now create a pointer to the structure temp_somestruct
(how?) and assign it to some pointer, let's say frstp (for first
pointer), which at the beginnig should be 'equal' NULL...

best regards,
a.
 
J

jamesonang

DATA * frstp ; // the frstp is the pointer to structure
frstp = NULL ; // the fist pointer is null at the beginning

// the you can create a new node like this
DATA *cur;
cur = (DATA *)malloc(sizeof(DATA)) ;
cur->field1 =< input by user>
cur->field2= < input by user>

frstp->next = cur; // a data is added to list.
 
N

nelu

DATA * frstp ; // the frstp is the pointer to structure
frstp = NULL ; // the fist pointer is null at the beginning

// the you can create a new node like this
DATA *cur;
cur = (DATA *)malloc(sizeof(DATA)) ;
cur->field1 =< input by user>
cur->field2= < input by user>

frstp->next = cur; // a data is added to list.

frstp = NULL ; // the fist pointer is null at the beginning
the first (head) pointer points to NULL
frstp->next = cur; // a data is added to list.
and now frstp's next memeber points to cur. The problem is that frstp
is NULL from the previous call. Can you still do
frstp->next= said:
From what I've seen on the group so far, it seems that casting malloc
is a bad idea as it may hide the fact that you forgot to include
<stdlib.h>.
Also, read http://cfaj.freeshell.org/google/ before posting from google
groups.
 
N

nelu

DATA *cur;
cur = (DATA *)malloc(sizeof(DATA)) ;

Oh yeah, I forgot: after the malloc you should check the value returned
by malloc, just in case you're out of memory or there's no memory
chunk big enough to accomodate your request.
 
J

jamesonang

i just tell the idea not to implement the full version. so.... i
absoultly know what you say.
 
F

Flash Gordon

i just tell the idea not to implement the full version. so.... i
absoultly know what you say.

As nedu said, please read http://cfaj.freeshell.org/google/ which
explains why you need to provide context and how to do it. His/her other
points are also valid criticisms. You should either post correct code
or, where you are deliberately missing bits, point out what should be
added, e.g. adding a comment "don't forget to allow for when frstp is a
null pointer as well."
 
A

alternativa

thanks a lot :)) How can I check that this list is really created? I
need some function for printing... I've wrote something like this:


void printlist ()
{
DATA *listp;

listp=frstp;
while (listp!= NULL)
{
printf ("field1:%d", listp->field1 );
listp = listp->nextp ;
};
};

but it doesn't work...it looks like the list is empty?
 
F

Flash Gordon

alternativa said:
Hello,
I'm a beginner in C programming and I have a problem that probably will
seem trivial to most of you, however I can't find a solution...
So, I have to write a data base - program should ask the user about the
data and then do some operations like sorting etc. I decided to use a
linked list. The code looks as follows:

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

struct somestruct
{
int field1;
char field2[15];
struct somestruct *nextp;
};

typedef struct somestruct DATA;

DATA enter_data () // function that asks the user for the data

// style comments are only valid in C99 (the latest standard that is not
generally fully implemented), not C90 (the standard that actually is
generally implemented). Also, they don't survive line wrapping so it is
better to avoid them when posting even if you are using a C99 compiler.

Also, it is better to be explicit when a function does not take parameters.

DATA enter_data (void) /* function that asks the user for the data */
{
DATA temp_somestruct;

printf ("field1: ");
scanf ("%s", &(temp_somestruct.field1));

field1 is an int, so this is definitely not what you want. Also, when
using the %s specifier in scanf *always* specify a maximum length,
otherwise you have major problems since the user can enter a string that
is too long. It is also often better to use fgets to read a line and
then pass it afterwards.
getchar();

printf ("field2: ");
scanf ("%d", &(temp_somestruct.field2));

field2 is a char array. I somehow think you have muddled up your fields.
A very good reason for using meaningful names, since then it might have
been obvious which was which and prevented this error.
getchar();

return temp_somestruct;
}

int main (void)
{
enter_data();
return 0;
}

I want the program to use this data for creating a linked list.. then I
would ask the user for next data and add this to this list..

If you search, or read your text book, you will find lots of linked list
implementations.
I guess I should now create a pointer to the structure temp_somestruct
(how?)

Look up malloc in your C text book.
> and assign it to some pointer, let's say frstp (for first
pointer), which at the beginnig should be 'equal' NULL...

Yes, you do want to start with your head pointer being null. For a
simple linked list, you obviously need a pointer to the next node, which
you set to null on the last node in the list.
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top