Jump to content

Creating a linked list in C

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
I've managed to create a basic linked list by creating the list items and linking them manually, like this (bear in mind that this is not the whole program, only fragments of it):


#undef NIL

#define NIL 0x00000000


struct line {

	int linenumber;

	char *text;

	struct line *nextline = NIL;

}


struct line line1 = { 1, "text1", NIL };

struct line line2 = { 2, "text2", NIL };

struct line line3 = { 3, "text3", NIL };

line1.nextline = &line2;

line2.nextline = &line3;


That works.

But the problem is I want to be able to create an unlimited number of list items. I want them to be generated automatically, rather than having to individually create them. In order for this to happen, they can not all have names, because obviously I can not give a unique name to all of possibly hundreds of structures.

I have tried linking a structure to an unnamed item, using the following methods:


linex.nextline = &{ num, "", NIL};



*linex.nextline = { num, "", NIL };



linex.nextline = { num, "", NIL };



linex.nextline -> { num, "", NIL };


All of them generate a compiler error.

How do you implement a linked list with an unlimited number of automatically generated items?
Life's too short to be cool. Be a nerd.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
malloc is your friend. You will have to create them dynamically using malloc.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
typedef struct _line
{
    int linenumber;
    struct _line* nextline;
    char text[];
} line;

line* add_line(line* prevhead, const char* contents)
{
    int size = strlen(contents) + 1;
    line* retval = malloc(sizeof(line) + (sizeof(char) * size)); // Struct hack, it's beautiful.
    strcpy(retval->text, contents);
    while (prevhead->nextline) prevhead = prevhead->nextline;
    retval->linenumber = prevhead->linenumber + 1;
    retval->nextline = NULL;
    prevhead->nextline = retval;
    return retval;
}
You can use the above function to add each line. See if that fits more along the lines of what you were after. :)
Wow I changed my sig!

#4
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
As an aside, defining NIL is a bit of an obfuscation. Just use NULL or 0 like everybody else in C and C++.

#5
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
@dcs: I use NIL because that's the name used in computer science and programming theory. I've seen pointers referred to as NIL frequently in texts on Pascal and Modula-2. If NULL is the terminology used in C, I'll use that.

@ZekeDragon: I'm going to try that code, once I figure out what it means. Thanks for the suggestion.

@WingedPanther: I don't really understand what malloc() does and I'm afraid to use it because I have a feeling I might end up overwriting valuable memory. Are there any tutorials on how to use it? Thank you for your help.
Life's too short to be cool. Be a nerd.

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
malloc is THE function that will enable you to do what you're trying to do. It just grabs new memory and returns a pointer to it. You'll have to learn it to make any progress with this program.

malloc - C++ Reference
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

DarkLordoftheMonkeys said:

@dcs: I use NIL because that's the name used in computer science and programming theory. I've seen pointers referred to as NIL frequently in texts on Pascal and Modula-2. If NULL is the terminology used in C, I'll use that.
That's exactly what I meant -- it looks like it's from a different language. And yes, NULL or 0 is what is used in C and C++. :)

#8
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
I tried malloc() and it works.

The program:


#include <stdio.h>

#include <stdlib.h>


#undef NULL

#define NULL 0x00000000


struct listItem {

	char *value;

	struct listItem *next;

};


int main(){

	struct listItem firstItem = { "1", NULL };

	firstItem.next = (struct listItem *) malloc( sizeof firstItem );

	printf( "%p\n", firstItem.next );

	(*firstItem.next).value = "2";

	printf( "%s\n", (*firstItem.next).value );

	return 0;

}


The output:


0x100120

2


Life's too short to be cool. Be a nerd.

#9
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
You don't define NULL. It is defined in the standard headers.

Eternally Confuzzled - Linked List Tutorial

[edit]Yikes. You may want to back up to strings a little bit first. You might think you're copying the string "2", but you are instead pointing to a string literal. If you were reading data from a file, you'd have to do things quite differently.