Download code
From LiteratePrograms
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 /* Copyright (c) 2010 the authors listed at the following URL, and/or 2 the authors of referenced articles or incorporated external code: 3 http://en.literateprograms.org/Singly_linked_list_(C)?action=history&offset=20081126164854 4 5 Permission is hereby granted, free of charge, to any person obtaining 6 a copy of this software and associated documentation files (the 7 "Software"), to deal in the Software without restriction, including 8 without limitation the rights to use, copy, modify, merge, publish, 9 distribute, sublicense, and/or sell copies of the Software, and to 10 permit persons to whom the Software is furnished to do so, subject to 11 the following conditions: 12 13 The above copyright notice and this permission notice shall be 14 included in all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 24 Retrieved from: http://en.literateprograms.org/Singly_linked_list_(C)?oldid=15494 25 */ 26 27 #include<stdlib.h> 28 #include<stdio.h> 29 #include<string.h> 30 31 typedef struct node_s { 32 void *data; 33 struct node_s *next; 34 } NODE; 35 36 NODE *list_create(void *data) 37 { 38 NODE *node; 39 if(!(node=malloc(sizeof(NODE)))) return NULL; 40 node->data=data; 41 node->next=NULL; 42 return node; 43 } 44 45 NODE *list_insert_after(NODE *node, void *data) 46 { 47 NODE *newnode; 48 newnode=list_create(data); 49 newnode->next = node->next; 50 node->next = newnode; 51 return newnode; 52 } 53 54 NODE *list_insert_beginning(NODE *list, void *data) 55 { 56 NODE *newnode; 57 newnode=list_create(data); 58 newnode->next = list; 59 return newnode; 60 } 61 62 int list_remove(NODE *list, NODE *node) 63 { 64 while(list->next && list->next!=node) list=list->next; 65 if(list->next) { 66 list->next=node->next; 67 free(node); 68 return 0; 69 } else return -1; 70 } 71 72 int list_foreach(NODE *node, int(*func)(void*)) 73 { 74 while(node) { 75 if(func(node->data)!=0) return -1; 76 node=node->next; 77 } 78 return 0; 79 } 80 81 NODE *list_find(NODE *node, int(*func)(void*,void*), void *data) 82 { 83 while(node) { 84 if(func(node->data, data)>0) return node; 85 node=node->next; 86 } 87 return NULL; 88 } 89 90 int printstring(void *s) 91 { 92 printf("%s\n", (char *)s); 93 return 0; 94 } 95 96 int findstring(void *listdata, void *searchdata) 97 { 98 return strcmp((char*)listdata, (char*)searchdata)?0:1; 99 } 100 101 int main() 102 { 103 NODE *list, *second, *inserted; 104 NODE *match; 105 106 /* Create initial elements of list */ 107 list=list_create((void*)"First"); 108 second=list_insert_after(list, (void*)"Second"); 109 list_insert_after(second, (void*)"Third"); 110 111 printf("Initial list:\n"); 112 list_foreach(list, printstring); 113 putchar('\n'); 114 115 /* Insert 1 extra element in front */ 116 list=list_insert_beginning(list, "BeforeFirst"); 117 printf("After list_insert_beginning():\n"); 118 list_foreach(list, printstring); 119 putchar('\n'); 120 121 /* Insert 1 extra element after second */ 122 inserted=list_insert_after(second, "AfterSecond"); 123 printf("After list_insert_after():\n"); 124 list_foreach(list, printstring); 125 putchar('\n'); 126 127 /* Remove the element */ 128 list_remove(list, inserted); 129 printf("After list_remove():\n"); 130 list_foreach(list, printstring); 131 putchar('\n'); 132 133 /* Search */ 134 if((match=list_find(list, findstring, "Third"))) 135 printf("Found \"Third\"\n"); 136 else printf("Did not find \"Third\"\n"); 137 138 return 0; 139 } 140 141
