[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] hægtet liste - Array af pointere til strukturer



On Thu, 13 Mar 2003, Søren H.Sørensen wrote:

> Jeg overvejer om ikke det var smartere, når nu Henrik ikke kender n,
> at lave en hægtet liste af FooBar-strukturer

Jeg turde ikke rigtigt foreslå det, men her noget kode til simpel 
illustration af hægtede lister.

Hvis ikke det er pænt nok skrevet, sig hører jeg det gerne.

/*
 * Hans Schou
 * gcc -o linked linked.c
 * ./linked hello world "hej dav"
 */

#include <stdio.h>

typedef struct link {
	struct link *next;
	struct link *prev;
	char *text; /* payload */
} *plink;

plink head = NULL, tail = NULL;

plink add(char *newtext) {
	plink temp;

	if (temp = (plink)malloc(sizeof(plink))) {
		temp->text = newtext;
		temp->next = NULL;
		temp->prev = tail;
		if (tail)
			tail->next = temp;
		else
			head = temp;
		tail = temp;
	}

	return temp;
}

int list(plink temp) {
	int forward = -1;

	if (temp) {
		forward = temp->prev == NULL;
		while (temp) {
			printf("%s ", temp->text);
			if (forward)
				temp = temp->next;
			else
				temp = temp->prev;
		}
		printf("\n");
	}

	return forward;
}

int closelist(void) {
	plink temp;

	while (temp = head) {
		head = head->next;
		free(temp);
	}

	head = tail = NULL;

	return 0;
}

int main(int argc, char **argv) {
	int i = 1;

	while (i < argc)
		add(argv[i++]);

	list(head);
	list(tail);

	closelist();

	return 0;
}

mvh/hans
-- 
Hamletsgade 4 - 201, DK-2200 København N, Phone: +45 3582 9079
Schou Industries ApS      http://schou.dk/    CVR: 26 13 44 39
--------------------------------------------------------------
Please don't call me at all, (E-)mail is sufficient)
				-- Anne Bezemer


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:09 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *