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 07-Feb-2004, 14:57
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light

[Include] list1.h -- Linked list class


GID Forums C/C++ Forums
Code Submittal Form


Name/Brief Description:
Singly-linked list
Date of Original Submission:
February 7, 2004
Submitted by:
dsmith
License:
None - there is nothing ground breaking here
Detailed Description:
I have posted this previously in a reply, but I wanted to explain it a little more. This is an extremely small and simple singly-linked list routine. It is written entirely as an inline function, so there is no external libraries to call. It is less than 200 lines with comments. This may seem like really simple or redundant code, but almost every project that I do includes this file. In real world programs, the amount of your data is rarely known, so a dynamic storage solution is crucial. With this routine, I am confident that errors are not being caused by my storage code. I have written a tiny example showing the use of the common functions. This may seem like a simple thing where an array would be easier to use, but what size is that array going to be? Also, typecasting can be confusing, but in reality I can make a list that contains any type of data all in the same list.
CPP / C++ / C Code:
#include <stdio.h>
#include "list1.h"

int main()
{
	list*	numbers;
	int*	entry;

	numbers = new list;
	do{
		printf("Enter number(0 to end): ");
		entry = new int;
		scanf("%d",entry);
		if(*entry != 0)
			numbers->add_data((void*) entry);
	}while(*entry != 0);

	printf("Here are your numbers:\n");
	if(!numbers->is_empty()){
		numbers->home();
		do{
			printf("Number: %d\n", *((int*) numbers->get_data()));
		}while(numbers->next());
	}
	return 0;
}
If anyone has any question, comments or improvements, you can post them here.
Problems/Limitations:
  • I have not done alot of testing on the set_mark/goto_mark calls
  • All data has to be typecast
  • Uses C++ class, so it is not portable to strictly c compilers
Last edited by dsmith : 04-May-2004 at 09:43.
  #2  
Old 07-Mar-2004, 08:34
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
I have done several updates to my linked list implementation.
  1. I changed the home() call to return a zero if the list is empty so instead of doing this:
    CPP / C++ / C Code:
    if(!numbers->is_empty()){
    	numbers->home();
    
    You can just do this:
    CPP / C++ / C Code:
    if(!numbers->home()){
    
    Not a huge savings, but every little bit helps.
  2. After doing some coding using set_mark/goto_mark I found that these needed to be modified.

I have reposted this file. Again comments or questions are encouraged.
Last edited by dsmith : 04-May-2004 at 09:42.
  #3  
Old 04-May-2004, 09:42
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Okay, in testing my linked list with a data structure test, I found a major flaw in my logic that was extremely inefficient in the placing of a lot of data into the list. By removing one line of this code, I fixed that inefficiency. I have posted this new implementation. (I also deleted the old ones, since I notice that people have been downloading my old implementation).
Attached Files
File Type: zip list1.zip (1.0 KB, 341 views)
 
 

Recent GIDBlogMore photos on Flickr 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
Merge sort on a linked list Temujin_12 C++ Forum 1 06-Mar-2008 20:33
need help to define a class in C++ yannoush C++ Forum 7 09-Sep-2003 00:28

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

All times are GMT -6. The time now is 14:38.


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