Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-09-2004, 11:50 PM   #1 (permalink)
Night_Prawler
Registered User
 
Join Date: Aug 2004
Posts: 2
Night_Prawler is on a distinguished road
A lil help with linked list

Hi i'm currently trying to figure out how to load characters from a text file into a linked list. Anyone out there can help me with this ? a guide? i'm stuck!

thanks first for all the help

Oh forgot to mention, i need it in C, not C++ .
Night_Prawler is offline   Reply With Quote
Old 08-11-2004, 01:31 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Look at dynamical memory allocation malloc() or alloca() ie:
Code:
#include <stdio.h>
#include <stdlib.h>

typedef struct my_array {
      char letter;
      struct my_array* next;
      struct my_array* prev;
} my_array;


int main()
{
  FILE* file;
  my_array* cur;
  my_array* next;
  /* allocate room for atleast one array member */
  cur = (my_array*) alloca(sizeof(my_array));
  cur->prev = NULL;
  cur->next = NULL;
  file = fopen("my_file", "r");
  while(!feof(file))
  {
    if(EOF != cur->letter = fgetc(file))
    {
      /* since we could read something 
       * we need to allocate room for one more letter 
       */
      next = (my_array*) alloca( sizeof(my_array));
      /* alter cur to point at active member in array */
      cur->next = next;
      next->prev = cur;
      next->next = NULL;
      cur = cur->next;
      cur->letter = '\0';
    }
  }
  fclose(file);
  while (cur != NULL)
  { /* NOTE printing in reverse order */
     printf("%c\n", cur->letter);
     cur = cur->prev;
     free (cur->next);
  }
  return 0;
}
It's a very simple run through a dynamicaly allocated array, I use alloca() instead of malloc(), since it is more safe when dealing with memory allocation, only drawback is, it dosn't work on every system.
The program hasn't been tested, and theres virtualy no error checking, but it should give you an idear.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 08-11-2004, 03:02 AM   #3 (permalink)
Night_Prawler
Registered User
 
Join Date: Aug 2004
Posts: 2
Night_Prawler is on a distinguished road
Thanks Man!! Gonna try it out
Night_Prawler is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linker errors on my linked list... Danish Standard C, C++ 1 03-02-2003 08:59 PM


All times are GMT -8. The time now is 06:29 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting