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

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Simple C Linked List

Tim
I can't seem to figure out why this very simple linked list wont
build..

I mean, there is no intelligence, just add to end.

Anyway, please let me know if something i can do will make head (the
root pointer) not be null.

/* LINKED LIST DEFINITIONS */
typedef struct a_fnode {
struct a_packet* data;
int chksum;
struct a_fnode* next;
}fnode,*fnodePTR;
/** ADD NODE TO LIST **/
void addNode (fnodePTR root, fnodePTR node){
fnodePTR temp = root;

int node_counter = 0;

if (root == NULL)
{
printf("HEAD IS NULL: ADDING\n");
root = node;
}
else
{
while (temp != NULL) {
if(DEBUG1)
printf("%d ",node_counter);
temp = temp->next;
}
temp = node;
}
if(DEBUG1)
printf("\nAdding Node %d\n",node->data->seq);
}

/** ALLOCATE NODE **/

fnodePTR allocateNode(packet* data) {
fnodePTR temp ;
/* Allocate memory for our node */
temp = (fnodePTR)malloc(sizeof(fnode));
/* Put in our data */
temp->data = data ;

/*temp->chksum = makeChecksum(data);*/
/* Ground the link pointer */
temp->next = NULL ;
/* Return the allocated node */
return temp ;

}
main {

fnodePTR head = NULL;
fnodePTR tempPtr = NULL;
fnodePTR myNode = NULL;

...

do
{

if( (myNode = allocateNode(myPacket)) == NULL )
printf("couldn't create node\n");
addNode(head, myNode);

} while (some file reading condition

if (head == NULL )
{
if(DEBUG1)
printf("head is null\n");
EOL = 1;
/*no more data left*/
break;
}

}
that last if statement in main, is ALWAYS true. I feel the allocation
of all structs and data is working, because I can extract data all the
way up to point after i "add" to the list, as you can see in the debug
statement in addNode();

thanks a lot for your help

Nov 14 '05 #1
1 2004

On Mon, 11 Apr 2005, Tim wrote:

I can't seem to figure out why this very simple linked list wont
build..
(Side remark: Note that the verb "to build" has a different connotation
in most computing circles than the one you're apparently intending. This
code will "build" in the sense that it will compile; it just won't work.)
I mean, there is no intelligence, just add to end.

Anyway, please let me know if something i can do will make head (the
root pointer) not be null.
Huh? Have you tried 'root = foo;', where 'foo' is any non-null pointer?

[Typography fixed throughout; PLEASE PLEASE PLEASE don't use hard tabs
on Usenet!]
/* LINKED LIST DEFINITIONS */
typedef struct a_fnode {
struct a_packet* data;
int chksum;
struct a_fnode* next;
} fnode, *fnodePTR;
/** ADD NODE TO LIST **/
void addNode(fnodePTR root, fnodePTR node)
{
fnodePTR temp = root;
Here is your first problem. Where is 'temp' used? Not at this scope,
that's for sure! It's only way down inside another level of nested
braces that you use it. So it should be declared there.
int node_counter = 0;
Ditto. In fact, since 'node_counter' is /never/ modified, you could
just as well remove it and use '0' everywhere it's mentioned. This
probably indicates a bug, though not an important or interesting one.
if (root == NULL)
{
printf("HEAD IS NULL: ADDING\n");
root = node;
}
else
{
while (temp != NULL) {
if (DEBUG1)
printf("%d ",node_counter);
temp = temp->next;
}
This loop shows that you haven't learned what a 'for' loop is yet.
In C, 'for' loops contain an initializer, a condition, and an increment.
They don't have to involve integers, or "counting up or down." So we
can rewrite this Pascal-looking loop idiomatically in C as

fnodePTR temp;
for (temp=root; temp != NULL; temp = temp->next)
{
if (DEBUG1)
printf("0 ");
}

(Notice that I've pulled the definition of 'temp' down to where it's
used, and replaced the constant 'node_counter' with its value, 0.)
temp = node;
This line is actually the problem you're concerned about. Think
about what it's doing: It's taking the variable 'temp', which you
defined right in this function, and it's assigning a value to it.
But you don't care about the value of 'temp' --- 'temp' is going to
disappear as soon as you leave the function! What you want to do is
change the values associated with the linked list 'root'! So you
need to get a pointer to the last entry in 'root', and set that
entry's 'next' pointer to point to 'node', like this:

for (temp=root; temp->next != NULL; temp = temp->next)
;
temp->next = node;

So that will fix one of your problems. But what if 'root' is a null
pointer itself? Then you have no list entries to modify! So this
points out a design flaw in your code: You need to pass the linked
list by reference. [UNTESTED CODE]

void addNode(fnodePTR *root, fnodePTR node)
{
if (*root == NULL) {
*root = node;
}
else {
fnodePTR temp;
for (temp = *root; temp->next != NULL; temp = temp->next)
;
temp->next = node;
}
}

And there you go.
/** ALLOCATE NODE **/

fnodePTR allocateNode(packet *data)
{
fnodePTR temp ;

/* Allocate memory for our node */
temp = (fnodePTR)malloc(sizeof(fnode));
This is horrible. Write instead

temp = malloc(sizeof *temp);

You will be glad you did, because this way you don't have to worry about
two different type names in the expression --- you don't write any type
names!
main {
Okay, maybe it /won't/ build. :( The way to define a function in C
is with a type, an identifier, and some parentheses with optional
parameter declarations, like this:

int main(void) {

or (though I don't like this one as much) like this:

int main() {
fnodePTR head = NULL;
fnodePTR tempPtr = NULL;
fnodePTR myNode = NULL;

...

do
{

if( (myNode = allocateNode(myPacket)) == NULL )
printf("couldn't create node\n");
addNode(head, myNode);
This is actually safe, but it looks dubious and kind of silly when
you really analyze what's going on. Why are you bothering to insert
'myNode' into the list even when you know it's a null pointer? Sure,
inserting a null pointer doesn't do anything to the list, but it
wastes time and might be deadly if you decide to put back in the line

/* debugging output */
printf("%d\n", node->data->foo);

(Dereferencing a null pointer invokes undefined behavior.)
} while (some file reading condition

if (head == NULL )
{
if(DEBUG1)
printf("head is null\n");
EOL = 1;
/*no more data left*/
break;
}

}


Don't forget to 'free' all your allocated memory when you're done using
it.

HTH,
-Arthur

PS: "The Elements of Programming Style."
Nov 14 '05 #2

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

Similar topics

4
by: craig delehanty | last post by:
I have a program that creates a linked list. I have added 3 nodes with the following values. head -> 4 -> 13 -> 19 -> NULL Below is a simple recursive function.I want to advance through the...
5
by: John | last post by:
Hi all, Can a linked list be a member of a structure? If so, when I add or remove an element from the linked list, the size of the structure will change. Will it cause any problem? Thanks a...
5
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
5
by: disco | last post by:
I am working on this example from a book "C Primer Plus" by Prata 4th edition - p. 672. There is no erata on this problem at the publisher's website. 1) Is it a violation of copyright laws to...
13
by: na1paj | last post by:
here's a simple linked list program. the DeleteNode function is producing an infinit loop i think, but i can't figure out where.. #include <stdio.h> typedef struct { char *str; //str is a...
6
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
4
by: phe2003 | last post by:
Hi All, I've been testing some extremely simple code about a linked list. What I keep doing is actually writing some parts of a linked list and testing them. Below are my codes:...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
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,...

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.