Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 19th, 2004, 8:13 AM   #1
maybesomeday
Newbie
 
Join Date: Dec 2004
Posts: 9
Rep Power: 0 maybesomeday is on a distinguished road
Hi, I'm new to programming (currently doing the introduction unit at university) and for one of my pieces of coursework I need to put words from a txt file into a linked list. I was just wondering if there is any particular function to do this (similar to fgets())? Any help would be really appreciated as I have looked through my textbooks and havent been able to find any. I am programming in c. Any links or advice would be really appreciated, Thanks for any help you may be able to provide,
maybesomeday is offline   Reply With Quote
Old Dec 19th, 2004, 8:27 AM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
If you know how linked lists work, you should be able to write your own. AFAIK, there isn't an in-built one.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Dec 27th, 2004, 2:28 PM   #3
maybesomeday
Newbie
 
Join Date: Dec 2004
Posts: 9
Rep Power: 0 maybesomeday is on a distinguished road
I have actually just gone to start this now and I've realsied i'm at a complete loss on how to go about it. I'm thinking at the moment that I need to go along the string input from the file looking for spaces and once I have found one some how copy the word into the linked list. Then I would need to move along the next word and copy this etc until I was to reach the terminating character. Does this sound like the right approach to this problem? Again any help would be really appreciated
maybesomeday is offline   Reply With Quote
Old Dec 27th, 2004, 5:30 PM   #4
Ravilj
Programmer
 
Ravilj's Avatar
 
Join Date: Sep 2004
Location: JHB , South Africa
Posts: 79
Rep Power: 5 Ravilj is on a distinguished road
yes that sounds write, you can use the string class which will have all the functions you need. Look at this pdf for a reference on the string functions.

You can use the following struct to for each node.

struct node {
node * prev;
string word;
node* next;
};

prev and next are pointers that hold the address of the previous node and the next node in the linked list. The first node in the linked list will have its prev pointer set to null and the last node will have its next pointer set to null. This will allow you to traverse up and down the list. prev is not required if you dont need to traverse back up the list.

I hope that makes sense...
__________________
Ravilj's OpenGL Terrain aka WinTerrain Last Updated: 17/01/2005!
Ravilj is offline   Reply With Quote
Old Dec 28th, 2004, 5:27 PM   #5
Tama
Programmer
 
Join Date: Dec 2004
Posts: 35
Rep Power: 0 Tama is on a distinguished road
>string word;
This is C++, the OP wanted C.

>I need to put words from a txt file into a linked list.
It's surprisingly easy once you get a feel for it, but until then you'll feel lost. If you're anything like me that is. Here's some code that reads strings from stdin and saves them in a linked list:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node {
 char str[BUFSIZ];
 struct node *next;
};

int main(void)
{
 struct node *save;
 struct node *list = NULL;
 char buffer[BUFSIZ];

 /* Build the list */
 while (fgets(buffer, sizeof buffer, stdin) != NULL) {
  save = malloc(sizeof *save);
  if (save == NULL)
   break;
  strcpy(save->str, buffer);
  save->next = list;
  list = save;
 }

 /* Print and destroy the list */
 while (list != NULL) {
  save = list->next;
  printf("%s", list->str);
  free(list);
  list = save;
 }

 return 0;
}
Tama is offline   Reply With Quote
Old Dec 29th, 2004, 10:36 AM   #6
maybesomeday
Newbie
 
Join Date: Dec 2004
Posts: 9
Rep Power: 0 maybesomeday is on a distinguished road
Thanks looking at different code is really helping me with linked lists. I'm just trying ot learn some stuff on linked lists by doing questions at the moment. I'm currently doing a question where I need to write a function that takes an index number (0-2 for a list with 3 elements) then returns the value of that element in the list.

The following code is the code for the question, the only piece of code in there that I myself wrote was the function GetNth().

#include <stdio.h>

/*List structure */
struct node {
  int data;
  struct node *next;
};


/*Given a reference (pointer to pointer) to the head*
 *of a list and an int, push a new node on the front of the list.
 *It creates a new node with the int as data value,struct node *s to the 
 *beginning of the list off the .next of the new node, and finally
 *changes hte head to point to the new node*/
 void Push(struct node **head, int newData)
 {
   struct node *newNode = (struct node *)malloc(sizeof(struct node)); /*Allocate node*/
   newNode -> data = newData;             /*Put in data*/
   newNode -> next = (*head);
   (*head) = newNode;
 }  
 
 
 /*Build and return the list {1,2,3}*/
struct node *BuildOneTwoThree()
{
  struct node *head = NULL; /*start with empty list*/
  Push(&head,3);
  Push(&head,2);
  Push(&head,1);
  
  return (head);
}  


int GetNth (struct node *head,int index)
{  int count = 0;
  
  if (index = 0 ||index = 1||index = 2)
  {  
    if (count < index)
    count++;
    
    else
    count = index;
  }
  return (index);
}  


int main (void)
{
  struct node *myList = BuildOneTwoThree();
  int lastNode = GetNth(myList, 2);
}

for the line
if (index = 0 ||index = 1||index = 2)

it says there a 'invalid Ivalue in assignment', could someone please just tell me what this is as it doesnt say in the compilers help section.
maybesomeday is offline   Reply With Quote
Old Dec 29th, 2004, 10:41 AM   #7
maybesomeday
Newbie
 
Join Date: Dec 2004
Posts: 9
Rep Power: 0 maybesomeday is on a distinguished road
Haha Ive also just realised I've made no attempt to return the value in the list. :wacko: But I can change that easily enough, but I would really appreciate finding out what this problem is as i always seem to get it.
maybesomeday is offline   Reply With Quote
Old Dec 29th, 2004, 12:09 PM   #8
Tama
Programmer
 
Join Date: Dec 2004
Posts: 35
Rep Power: 0 Tama is on a distinguished road
>if (index = 0 ||index = 1||index = 2)
= is assignment, == is comparison. I would be very surprised if you wanted assignment as an if condition. So you should change your code to this:
if (index == 0 ||index == 1||index == 2)
Tama is offline   Reply With Quote
Old Jan 1st, 2005, 2:09 PM   #9
maybesomeday
Newbie
 
Join Date: Dec 2004
Posts: 9
Rep Power: 0 maybesomeday is on a distinguished road
. My current idea for how to go about this involes:

1) Define string.
2)create pointer to FILE.
3) use fopen() to associate file and pointer.
4) use fread to put contents of file into string.
5)put string into fread() to split it by using a space as the token in the other string.
6) thne on each output this gives create a new node on the linked list and put stroks output.

Does this seem like a good approach to the problem? As I think it could waste a lot of memory etc by having to create the large string to hold the contents of the file in. If anyone knows of a better approach I would be very grateful to be put in the right direction.
Also if this is an acceptable approach does anyone know any way of making a string size increase dynmaically, as I need to be able to read a file of any size into it and for it still to work. this will not happen if I leave the size of the string as a constant (atleast not from what I can see).
So if anyone could give me any pointers I would be very grateful, sorry about all the questions! This piece of coursework is just a huge step up and I want to make sure I'm on the right track.
maybesomeday is offline   Reply With Quote
Old Jan 1st, 2005, 2:37 PM   #10
Tama
Programmer
 
Join Date: Dec 2004
Posts: 35
Rep Power: 0 Tama is on a distinguished road
>Does this seem like a good approach to the problem?
Well, it's somewhat of a roundabout solution. Don't you think it would be easier to do this instead?
/* Assuming the largest a word can be is 50 */

struct node {
 char word[51];
 struct node *next;
};

FILE *in;
char buffer[51];

/* Open the file and test for success */

while (fscanf(in, "%50s", buffer) == 1) {
 struct node *nn;

 /* Create a new node */
 strcpy(nn->word, buffer);

 /* Insert nn into the list */
}

/* Make sure end-of-file stopped the loop */
if (!feof(in))
 fprintf(stderr, "Unknown input error\n");
Tama is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:18 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC