473,473 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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.

Dec 29 '05 #1
7 1394
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.

Dec 30 '05 #2

ja********@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.

Dec 30 '05 #3

ja********@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.

Dec 30 '05 #4
i just tell the idea not to implement the full version. so.... i
absoultly know what you say.

Dec 30 '05 #5
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?

Dec 30 '05 #6
ja********@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.
Dec 30 '05 #7
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.
Dec 30 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value'...
19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
10
by: Ben | last post by:
Hi, I am a newbie with C and am trying to get a simple linked list working for my program. The structure of each linked list stores the char *data and *next referencing to the next link. The...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
7
by: Indraseena | last post by:
Hi can anybody send me how to create a linked list program in C++. Basically I want to know how a node is handled in c++. I know this in c only. in c we create a structure and use that structure...
10
by: sonu | last post by:
Hi All, here i have a daught how can i find wheather a linked list contains a loop or not.
23
by: Just Another Victim of the Ambient Morality | last post by:
I'm looking for a linked list implementation. Something iterable with constant time insertion anywhere in the list. I was wondering if deque() is the class to use or if there's something else. ...
46
by: junky_fellow | last post by:
Hi, Is there any efficient way of finding the intesection point of two singly linked lists ? I mean to say that, there are two singly linked lists and they meet at some point. I want to find...
11
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.