Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   creating a linked list - trivial :( question (http://www.velocityreviews.com/forums/t440694-creating-a-linked-list-trivial-question.html)

alternativa 12-29-2005 10:45 PM

creating a linked list - trivial :( question
 
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.


jamesonang@gmail.com 12-30-2005 01:09 AM

Re: creating a linked list - trivial :( question
 
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.


nelu 12-30-2005 01:25 AM

Re: creating a linked list - trivial :( question
 

jamesonang@gmail.com wrote:
> 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=<something> since frstp is not allocated?

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


nelu 12-30-2005 01:33 AM

Re: creating a linked list - trivial :( question
 

jamesonang@gmail.com wrote:
> 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.


jamesonang@gmail.com 12-30-2005 03:51 AM

Re: creating a linked list - trivial :( question
 
i just tell the idea not to implement the full version. so.... i
absoultly know what you say.


Flash Gordon 12-30-2005 11:29 AM

Re: creating a linked list - trivial :( question
 
jamesonang@gmail.com wrote:
> 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."
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.

alternativa 12-30-2005 11:35 AM

Re: creating a linked list - trivial :( question
 
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?


Flash Gordon 12-30-2005 11:39 AM

Re: creating a linked list - trivial :( question
 
alternativa wrote:
> 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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


All times are GMT. The time now is 04:38 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.