Download code

Jump to: navigation, search

Back to Singly_linked_list_(C)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

slist.c

  1 /* The authors of this work have released all rights to it and placed it
  2 in the public domain under the Creative Commons CC0 1.0 waiver
  3 (http://creativecommons.org/publicdomain/zero/1.0/).
  4 
  5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 12 
 13 Retrieved from: http://en.literateprograms.org/Singly_linked_list_(C)?oldid=19215
 14 */
 15 
 16 #include<stdlib.h>
 17 #include<stdio.h>
 18 #include<string.h>
 19 typedef struct node_s {
 20 	void *data;
 21 	struct node_s *next;	
 22 } NODE;
 23 
 24 NODE *list_create(void *data)
 25 {
 26 	NODE *node;
 27 	if(!(node=malloc(sizeof(NODE)))) return NULL;
 28 	node->data=data;
 29 	node->next=NULL;
 30 	return node;
 31 }
 32 NODE *list_insert_after(NODE *node, void *data)
 33 {
 34 	NODE *newnode;
 35         newnode=list_create(data);
 36         newnode->next = node->next;
 37         node->next = newnode;
 38 	return newnode;
 39 }
 40 NODE *list_insert_beginning(NODE *list, void *data)
 41 {
 42 	NODE *newnode;
 43         newnode=list_create(data);
 44         newnode->next = list;
 45 	return newnode;
 46 }
 47 int list_remove(NODE *list, NODE *node)
 48 {
 49 	while(list->next && list->next!=node) list=list->next;
 50 	if(list->next) {
 51 		list->next=node->next;
 52 		free(node);
 53 		return 0;		
 54 	} else return -1;
 55 }
 56 int list_foreach(NODE *node, int(*func)(void*))
 57 {
 58 	while(node) {
 59 		if(func(node->data)!=0) return -1;
 60 		node=node->next;
 61 	}
 62 	return 0;
 63 }
 64 NODE *list_find(NODE *node, int(*func)(void*,void*), void *data)
 65 {
 66 	while(node) {
 67 		if(func(node->data, data)>0) return node;
 68 		node=node->next;
 69 	}
 70 	return NULL;
 71 }
 72 int printstring(void *s)
 73 {
 74 	printf("%s\n", (char *)s);
 75 	return 0;
 76 }
 77 int findstring(void *listdata, void *searchdata)
 78 {
 79 	return strcmp((char*)listdata, (char*)searchdata)?0:1;
 80 }
 81 int main()
 82 {
 83 	NODE *list, *second, *inserted;
 84 	NODE *match;
 85 
 86 	/* Create initial elements of list */
 87 	list=list_create((void*)"First");
 88 	second=list_insert_after(list, (void*)"Second");
 89 	list_insert_after(second, (void*)"Third");
 90 
 91 	printf("Initial list:\n");
 92 	list_foreach(list, printstring);
 93 	putchar('\n');
 94 
 95 	/* Insert 1 extra element in front */
 96 	list=list_insert_beginning(list, "BeforeFirst");
 97 	printf("After list_insert_beginning():\n");
 98 	list_foreach(list, printstring);
 99 	putchar('\n');
100 
101 	/* Insert 1 extra element after second */
102 	inserted=list_insert_after(second, "AfterSecond");
103 	printf("After list_insert_after():\n");
104 	list_foreach(list, printstring);
105 	putchar('\n');
106 
107 	/* Remove the element */
108 	list_remove(list, inserted);
109 	printf("After list_remove():\n");
110 	list_foreach(list, printstring);
111 	putchar('\n');
112 
113 	/* Search */
114 	if((match=list_find(list, findstring, "Third")))
115 		printf("Found \"Third\"\n");
116 	else printf("Did not find \"Third\"\n");
117 
118 	return 0;
119 }


hijacker
hijacker
hijacker
hijacker