473,796 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ 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_somestru ct.field1));
getchar();

printf ("field2: ");
scanf ("%d", &(temp_somestru ct.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 1418
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********@gmai l.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=<somethin g> 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********@gmai l.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********@gmai l.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_somestru ct.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_somestru ct.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
7958
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' => 'aaa') , array( 'id' => 'B', 'parent_id' => 'A', 'value' => 'bbb') , array( 'id' => 'C', 'parent_id' => 'B', 'value' => 'ccc') , array( 'id' => 'D', 'parent_id' => 'A', 'value' => 'ddd')
19
13577
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., head and tail are not connected. Of course, recursive function aproach to traverse the list is one way. But, depending upon the list size, it could overrun the stack pretty fast.
10
2520
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 problem I get is that I am trying to link a struct that I have defined and its refusing to link. I have tried casting my struct into char * but attempts to cast it back to its original struct to access its contents only seg faults.
4
3605
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; };   struct pcb *pcb_pointer;
7
19171
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 for ctreating nodes. how is this achieved in c++ . my c structure is: struct node
10
16928
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
3867
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. Is there? Thank you...
46
3427
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 out the addres of the node where the two linked intersect. thanks for any help...
11
2554
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 generics instead of System.Object. I want the code in Form1_Load to remain exactly the same, but in the background I want to use generics. I'm trying to get a better understanding of how it works and I'm a little stuck.
0
9527
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10172
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7546
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.