GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-May-2006, 09:48
jaro's Avatar
jaro jaro is offline
Junior Member
 
Join Date: Nov 2005
Location: somewhere in souteast asia
Posts: 63
jaro will become famous soon enough
Question

controlling contents of a linked list ?


greeting!

Just found a sample linked list program and I've decided to play around with it (hoping that I can incorporate it with the project I'm currently working on).

here is the code: (this is only a quick hack/modification on the sample program that I've found, I haven't fix the warning message yet)
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
  char name[1000];
} data;

typedef struct {
  data  d;
  struct NODE *next;
} NODE;

#define CFG_FILE "Read.txt" // name of file to be open

int main(void)
{
  
int num = 0;
FILE *cfgfile;
char data[80];
int count = 1;		//display purposes

NODE* head = NULL;  // this should always point to the 1st node
NODE* pin = NULL;   // used to iterate through nodes. 


	cfgfile = fopen ( CFG_FILE, "r" ); // open file, simulate the data retrieve from the database
		if ( cfgfile == NULL )
		{
			perror ( "Error in reading text file containing cfg file location!!!" );
		}
		else
		{
			while ( fgets ( data, 80, cfgfile ) != NULL )  //delinmeter is newLine
			{
				printf("%d Line Read: %s",count++,data); // prints out the contents of Read.txt
					if( head == NULL )
					{
						head = (NODE*) malloc(sizeof(NODE));
						head->next = NULL;
						pin = head;
					}
					else {
						pin->next = (NODE*) malloc(sizeof(NODE));
						pin = pin->next;
						pin->next = NULL;
					}
				strcpy(pin->d.name,data);
			}
		fclose ( cfgfile );
		}


	//Printing the data. Goes through the linked list node by node.
	printf("\n\n");
	pin = head;
	while( pin != NULL )
	{
		printf("%s", &pin->d.name);
		num = num + strlen(pin->d.name);
		pin = pin->next;
	}
	printf("\ntotal size of string is %d\n",num);

	// cleanup
	while( head != NULL )
	{
		pin = head->next;
		free( head );
		head = pin;
	}

	return 0;
}

the question is, what would I have to do in order for the contents of a linked list to be place in one string (character array).
to elaborate more.
let say the content of the Read.text (which can vary )are as follows:
Quote:
<DATA> 1
<DATA> 2
<DATA> 3
<DATA> 4
after reading the file and inserting the values to the linked list, how would I arranged the contents of the linked list into one string,
into this format
Quote:
<DATA> 1<DATA> 2<DATA> 3<DATA> 4

any kind of help or comment will be much appreciated.

regards,
jaro
__________________
Action.....reAction
  #2  
Old 02-May-2006, 18:14
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: controlling contents of a linked list ?


Quote:
Originally Posted by jaro
Just found a sample linked list program
What? Did it just "fall off of a truck" or what? (Just kidding.)

Quote:
Originally Posted by jaro
I haven't fix the warning message yet

I'm not sure what warning messages you are getting (you might tell us if you still have problems after what I am about to show you), but the following is the way that I usually handle typedefs for self-referential structs. There are other ways, but for me, this lets the thing compile with no messages:

CPP / C++ / C Code:
typedef struct sNODE *nodePtr;

typedef struct sNODE {
    data d;
    nodePtr next;
} NODE;
Quote:
Originally Posted by jaro
the question is, what would I have to do in order for the contents of a linked list to be place in one string (character array).

Well, you already have a loop that goes through and adds up the total number of chars in all of the lines, so you could just allocate some storage and use strcat() or some such thing to copy everything into that. Maybe something like this:
CPP / C++ / C Code:
    pin = head;
    while (pin != NULL) {
        printf("%s", &pin->d.name);
        num = num + strlen(pin->d.name);
        pin = pin->next;
    }
    printf("\ntotal size of string is %d\n", num);
    { /* new block to string them together. Get it? String them ... Oh, well */
        char *newstr;
        newstr = malloc(num + 1);
        newstr[0] = 0;
        pin = head;
        while (pin != NULL) {
             strcat(newstr, pin->d.name);
            pin = pin->next;
        }
        printf("Entire string: <%s>\n", newstr);
        free(newstr);
    }

Now, since you used fgets() to read the lines into the buffer, they probably have '\n' characters at the end of each line, so if you want one long string without any newline chars you will have to get rid of them somehow. Actually, you probably want to replace '\n' chars with space chars (I would probably use strchr() to locate the newline chars and to tell me where to put the space chars). You could do that as you read the lines in or when you concatenate them. Your choice.


Regards,

Dave
  #3  
Old 03-May-2006, 00:43
jaro's Avatar
jaro jaro is offline
Junior Member
 
Join Date: Nov 2005
Location: somewhere in souteast asia
Posts: 63
jaro will become famous soon enough

Re: controlling contents of a linked list ?


Quote:
Originally Posted by davekw7x
I'm not sure what warning messages you are getting (you might tell us if you still have problems after what I am about to show you), but the following is the way that I usually handle typedefs for self-referential structs. There are other ways, but for me, this lets the thing compile with no messages:

CPP / C++ / C Code:
typedef struct sNODE *nodePtr;

typedef struct sNODE {
    data d;
    nodePtr next;
} NODE;


Thanks Dave, adding these remove all the warning message.

I'll try to implement your suggestions to my program, will post again for some update.
-jaro
__________________
Action.....reAction
  #4  
Old 03-May-2006, 02:17
jaro's Avatar
jaro jaro is offline
Junior Member
 
Join Date: Nov 2005
Location: somewhere in souteast asia
Posts: 63
jaro will become famous soon enough

Re: controlling contents of a linked list ?


just finished working on the program.
Did some adjustment and add some extra function along the way.
Just want to hear some comment about the code and if I violate some coding rules.

here is the code:
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
  char name[1000];
} data;

typedef struct sNODE *nodePtr;

typedef struct sNODE {
    data d;
    nodePtr next;
} NODE;


#define CFG_FILE "Read.txt" // name of file to be open
//make global
NODE* head = NULL;  // this should always point to the 1st node
NODE* pin = NULL;   // used to iterate through nodes. 


char* removeNewline(char line[])
{
	if (line[strlen(line)-1] == '\n')
	{
		line[strlen(line)-1] = '\0';
	}
	return line;
}



int stringLength(NODE* current) {
	int count = 0;
	current = head;
		while (current != NULL) {
			printf("%s", &current->d.name); // for display purpose only
			count = count + strlen(current->d.name);
			current = current->next;
		}
return count;
}


void linkCleanUp(NODE* last){
	while( head != NULL )
	{
		last = head->next;
		free( head );
		head = last;
	}
}



int main(void)
{
  
int strLen;
FILE *cfgfile;
char strdata[80];
int count = 1;		//display purposes
char *newstr;


	cfgfile = fopen ( CFG_FILE, "r" ); // open file, simulate the data retrieve from the database
		if ( cfgfile == NULL )
		{
			perror ( "Error in reading text file containing cfg file location!!!" );
		}
		else
		{
			while ( fgets ( strdata, 80, cfgfile ) != NULL )  //delinmeter is newLine
			{
				printf("%d Line Read: %s",count++,strdata); // prints out the contents of Read.txt
					if( head == NULL )
					{
						head = (NODE*) malloc(sizeof(NODE));
						head->next = NULL;
						pin = head;
					}
					else {
						pin->next = (NODE*) malloc(sizeof(NODE));
						pin = pin->next;
						pin->next = NULL;
					}
				strcpy(pin->d.name,strdata);
			}
		fclose ( cfgfile );
		}


	//Printing the data. Goes through the linked list node by node.
	printf("\n\n");
	strLen= stringLength(pin);
	printf("\ntotal size of string is %d\n",strLen);

	/* string them together.*/
	newstr = malloc(strLen + 1);
	newstr[0] = 0;
	pin = head;
		while (pin != NULL) {
			strcat(newstr, pin->d.name);
			removeNewline(newstr);
			pin = pin->next;
		}
	printf("\n======= Entire string: =======\n%s\n", newstr);
	free(newstr);

	// cleanup
	linkCleanUp(pin);

	return 0;
}

The content of the Read.txt remains the same.
output
Quote:
1 Line Read: <DATA> 1
2 Line Read: <DATA> 2
3 Line Read: <DATA> 3
4 Line Read: <DATA> 4


<DATA> 1
<DATA> 2
<DATA> 3
<DATA> 4

total size of string is 36

======= Entire string: =======
<DATA> 1<DATA> 2<DATA> 3<DATA> 4

Forgot to mention on my first post that I'm using MVC6 under Win2K.

~jaro
__________________
Action.....reAction
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 2) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
linked list error message Krandygrl00 C++ Forum 4 22-Jun-2005 14:13
search linked list itsmecathys C++ Forum 20 18-Apr-2005 01:34

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:37.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.