LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 07-05-2008, 11:58 AM   #1
rubadub
Member
 
Registered: Jun 2004
Posts: 233

Rep: Reputation: 33
wobbly lists in c


Hi,
Never being too sure, can you comment on the following implementation of a list, i've wrapped 'em in a struct to be object orientated, but...

main.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include "lists.h"

int main()
{
	struct S_LIST l = slist_init();
	
	char *s = "Hello";
	slist_add(&l, s);
	slist_add(&l, "Good");
	slist_add(&l, "bye");
	
	slist_print(&l);
	printf("count: %d \n", l.count);
	
	slist_delete(&l, 1);
	
	slist_print(&l);
	printf("count: %d \n", l.count);
	
	slist_foreach(&l, print_it);
	
	slist_delete_all(&l);
	slist_print(&l);
	
	return 0;
}
lists.h
Code:
#ifndef S_LIST_H_INCLUDED
#  define S_LIST_H_INCLUDED

#include <stdio.h>
#include <stdlib.h>

struct S_LIST
{
	int count;
	struct S_LIST_E *head;
	struct S_LIST_E *tail;
};

struct S_LIST_E
{
	void *data;
	struct S_LIST_E *next;
	struct S_LIST_E *prev;
};


struct S_LIST slist_init()
{
	struct S_LIST l = {0,NULL,NULL};
	return l;
}

void slist_add(struct S_LIST *sl, void *data)
{
	struct S_LIST_E *e;
	if((e = (struct S_LIST_E*) malloc(sizeof(struct S_LIST_E))) != NULL)
	{
		if(sl->head==NULL)
		{
		
			sl->head = e;
			sl->tail = sl->head;
			sl->tail->next = NULL;
			sl->tail->prev = NULL;
		}
		else
		{
			sl->tail->next = e;
			sl->tail->next->prev = sl->tail;
			sl->tail = sl->tail->next;
			sl->tail->next = NULL;
		}
		sl->tail->data = data;
		sl->count++;
	}
}

void slist_delete(struct S_LIST *sl, int pos)
{
	struct S_LIST_E *e = sl->head;
	int c = 0;
	while(e!=NULL)
	{
		if(c == pos)
		{
			struct S_LIST_E *tmp = e;
			e->prev->next = tmp->next;
			e->next->prev = tmp->prev;
			free(e);
			//free(tmp);
			sl->count--;
			return;
		}
		e = e->next;
		c++;
	}
}

void slist_delete_all(struct S_LIST *sl)
{
	struct S_LIST_E *e = sl->head;
	struct S_LIST_E *v = NULL;
	while(e != NULL)
	{
		v = e;
		e = e->next;
		free(v);
	}
	sl->head = NULL;
	sl->tail = NULL;
	sl->count = 0;
}

void slist_print(struct S_LIST *sl)
{
	struct S_LIST_E *e = sl->head;
	while(e!=NULL)
	{
		printf("%s \n", (char*)e->data);
		e = e->next;
	}
}

void print_it(void *s)
{
	printf("%s \n", (char*)s);
}

void slist_foreach(struct S_LIST *sl, void (*func)(void *data))
{
	struct S_LIST_E *e = sl->head;
	while(e!=NULL)
	{
		func(e->data);
		e = e->next;
	}
}
#endif
Just realised there's no insert yet...
 
Old 07-05-2008, 05:30 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,539

Rep: Reputation: 149Reputation: 149
Generally fine, but:
1. I'd check if sl (and other parameters) is not NULL at the beginning of (nearly) each function.
2. I'd return some kind of error codes: for instance if the position in slist_delete doesn't make sense.
3. Who frees data when element is deleted? As it is possible for data to keep pointers to other dynamic structures that should be free()'d, there should be a function pointer passed to delete or some other mechanism to handle that (very easy to forget about about doing it before calling slist_delete).
 
Old 07-06-2008, 05:17 PM   #3
smoked kipper
Member
 
Registered: May 2008
Location: UK
Distribution: Slackware,Slamd64
Posts: 81

Rep: Reputation: 15
Also, since the code is in a header file, you may want to make the functions static, otherwise when you start including it in more than one source file, you will end up with multiple definitions of each function.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C Lists rubadub Programming 11 03-22-2008 01:43 PM
Compiz wobbly menu sblantipodi Linux - Software 4 11-14-2007 01:36 PM
video dissapears wobbly windows radeon 9250 beryl snama Linux - Software 0 10-08-2007 12:13 PM
I need lists deathbyswiftwin Linux - General 4 04-21-2006 08:10 PM
QMail packing a wobbly still AMMullan Linux - Software 2 06-11-2004 10:36 AM


All times are GMT -5. The time now is 05:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration