C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 2nd, 2006, 11:47 AM
jaro jaro is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Posts: 35 jaro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 16 m 44 sec
Reputation Power: 4
Send a message via Yahoo to jaro
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:
<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

Reply With Quote
  #2  
Old May 2nd, 2006, 04:07 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
you could just walk through the linked list and strcat() curnode.data.name to a cumulative buffer (make sure the buffer is long enough)

Reply With Quote
  #3  
Old May 3rd, 2006, 03:08 AM
jaro jaro is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Posts: 35 jaro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 16 m 44 sec
Reputation Power: 4
Send a message via Yahoo to jaro
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

Reply With Quote
  #4  
Old May 3rd, 2006, 04:16 AM
jaro jaro is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Posts: 35 jaro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 16 m 44 sec
Reputation Power: 4
Send a message via Yahoo to jaro
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", &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

Reply With Quote
  #5  
Old May 3rd, 2006, 03:45 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
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

Reply With Quote
  #6  
Old May 4th, 2006, 02:31 AM
jaro jaro is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Posts: 35 jaro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 16 m 44 sec
Reputation Power: 4
Send a message via Yahoo to jaro
thanks for your comment ubergeek.

i'll try to make some changes regarding your suggestion.

-jaro

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Controlling contents of a linked list


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT