| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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) 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:
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:
any kind of help or comment will be much appreciated. regards, jaro |
|
#2
|
|||
|
|||
|
you could just walk through the linked list and strcat() curnode.data.name to a cumulative buffer (make sure the buffer is long enough)
|
|
#3
|
|||
|
|||
|
Thanks for all of your responses.
I now got an idea on how would approach this problem. Will post back soon for the complete code or if I found some weird error. -jaro |
|
#4
|
|||
|
|||
|
final code,need ur comment
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: 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", ¤t->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:
Forgot to mention on my first post that I'm using MVC6 under Win2K. ~jaro |
|
#5
|
|||
|
|||
|
Good work! I just noticed a couple of minor things.
in function removeNewline: strlen() is a time-consuming function depending on the length of the string, because it has to walk through every character in the string looking for the NUL byte. To avoid this delay twice, consider saving strlen()'s return value the first time in a variable. in function stringLength: you take current as a parameter, but then you discard whatever was passed in by assigning head to current. What's the point of the parameter? very minor: to make your code cleaner you can use: count = count + x IS THE SAME AS count += x this works for many binary operations: += -= *= /= >>= <<= &= |= ^= (those last five are bitwise operations) in function linkCleanUp: again, you take last as a parameter but never used the passed-in value, immediately assigning last to something else in function main: you don't check for failure of malloc one general thing: you might want to consider making data.name a dynamically allocated string (if you are willing to add another layer of complexity to the program) because to just store "<DATA> 1" (which is 8 characters, 9 including the terminating NUL) in a 1000-character array wastes approximately 991 bytes -- for each node. And a megabyte per node, especially when most of the space may not even be used, is a hell of a lot of space. otherewise looks good |
|
#6
|
|||
|
|||
|
thanks for your comment ubergeek.
i'll try to make some changes regarding your suggestion. -jaro |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Controlling contents of a linked list |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|