Variable-sized lines of text in linked list
- From: Scottman <FonzoCool@xxxxxxxxx>
- Date: Thu, 28 Feb 2008 11:45:01 -0800 (PST)
I am trying to read a text file into memory without any knowledge of
how long each line will be. I am looking to store each line in a
linked list structure, however, I am unsure of how to dynamically
allocate space for each line.
This is how I set up the linked list...
typedef struct node {
char *line;
struct node *next;
} linkedlist;
linkedlist* createlinkedlist(void) {
linkedlist* head;
head = (linkedlist *)malloc(sizeof(linkedlist));
head->line = NULL;
head->next = NULL;
return head;
}
void addnode(linkedlist* list, char *line) {
linkedlist* freespot;
linkedlist* newnode;
freespot = list;
while (freespot->next != NULL)
freespot = freespot->next;
newnode = (linkedlist *)malloc(sizeof(linkedlist));
newnode->line = line;
newnode->next = NULL;
freespot->next = newnode;
}
So with this in place, how can I read in variable length lines,
malloc() the proper storage for each and pass the pointer to
addnode()?
Thanks!
Cheers,
Scott Nelson
.
- Follow-Ups:
- Re: Variable-sized lines of text in linked list
- From: John Bode
- Re: Variable-sized lines of text in linked list
- From: Bill Reid
- Re: Variable-sized lines of text in linked list
- From: CBFalconer
- Re: Variable-sized lines of text in linked list
- From: ediebur
- Re: Variable-sized lines of text in linked list
- From: cr88192
- Re: Variable-sized lines of text in linked list
- From: Morris Dovey
- Re: Variable-sized lines of text in linked list
- From: pete
- Re: Variable-sized lines of text in linked list
- From: Ioan - Ciprian Tandau
- Re: Variable-sized lines of text in linked list
- From: santosh
- Re: Variable-sized lines of text in linked list
- Prev by Date: Re: Cannot optimize 64bit Linux code
- Next by Date: Re: Why not provide a standard non-busy waiting method?
- Previous by thread: Cannot optimize 64bit Linux code
- Next by thread: Re: Variable-sized lines of text in linked list
- Index(es):
Relevant Pages
|