写了个单链表(要的朋友拿去吧)
list.h程序代码:
#ifndef LIST_H_ #define LIST_H_ #ifndef DATA_SIZE /* 数据所占的字节数 */ #define DATA_SIZE 8 #endif #ifndef NULL /* 空值 */ #define NULL 0 #endif typedef char Data[DATA_SIZE]; typedef struct node { Data data; struct node * next; } Node, * PNode; /* 初始化链表 */ PNode initializing(void); /* 添加一个元素到链表末尾,需要提供头结点、数据的地址 */ void add(PNode, const void *); /* 插入一个元素到链表指定位置,需要提供头结点、非负整数索引、数据的地址 */ void insert(PNode, unsigned, const void *); /* 返回当前链表元素的个数 需要提供头结点*/ unsigned get_size(PNode); /* 检测链表是否为空,为空返回1否则返回0,需要提供头结点 */ int is_empty(PNode); /* 从链表中移除所有元素,需要提供头结点 */ void clear(PNode); /* 返回链表中指定索引的元素,如果索引越界则返回空指针,需要提供头结点、非负整数索引 */ void * get(PNode, unsigned); /* 在链表中移除该索引对应的值,需要提供头结点、非负整数索引,如果索引越界则不会有任何效果 */ void rem(PNode, unsigned); /* 在链表中替换该索引对应的值,需要提供头结点、非负整数索引、一个新值,如果索引越界则不会有任何效果 */ void set(PNode, unsigned, const void *); #endif
list.c
程序代码:
#include <stdlib.h> #include <string.h> #include "list.h" PNode initializing(void) { PNode head = (PNode)malloc(sizeof(Node)); if(head == NULL) { printf("Memory allocation failed\n"); else head->next = NULL; return head; } void add(PNode head, const void * data) { if(head == NULL) return; while(head->next != NULL) head = head->next; head->next = (PNode)malloc(sizeof(Node)); if(head->next == NULL) printf("Memory allocation failed\n"); else { memcpy(&head->next->data, data, DATA_SIZE); head->next->next = NULL; } } void insert(PNode head, unsigned index, const void * data) { if(head == NULL || index >= get_size()) return; int i = 0; PNode i_temp = head; PNode d_temp = (PNode)malloc(sizeof(Node)); if(d_temp == NULL) printf("Memory allocation failed\n"); else { memcpy(&d_temp->data, data, DATA_SIZE); while(i++ < index) i_temp = i_temp->next; d_temp->next = i_temp->next; i_temp->next = d_temp; } } unsigned get_size(PNode head) { unsigned count = 0; while(head = head->next) count++; return count; } void clear(PNode head) { if(head != NULL) { PNode temp1 = head->next, temp2 = temp1; while(NULL != temp1) { temp1 = temp2->next; free(temp2); temp2 = temp1; } free(head); } } int is_empty(PNode head) { return head->next == NULL; } void * get(PNode head, unsigned index) { if(index >= get_size(head)) return NULL; int i = 0; PNode temp = head; while(i++ < index) temp = temp->next; return (void *)temp->next->data; } void rem(PNode head, unsigned index) { if(index >= get_size(head)) return; int i = 0; PNode temp1 = head, temp2; while(i++ < index) temp1 = temp1->next; temp2 = temp1->next; temp1->next = temp2->next; free(temp2); } void set(PNode head, unsigned index, const void * data) { if(index >= get_size(head)) return; int i = 0; PNode temp = head; while(i++ < index) temp = temp->next; memcpy(&temp->next->data, data, DATA_SIZE); }
下面是测试用的代码:
test1.c
程序代码:
#include <stdio.h> #include "list.h" int main(void) { PNode head = initializing(); char str1[] = "How ???? apples?", str2[] = "many"; int i = 0, j = 0; while(str1[i] != '\0') add(head, str1 + i++); while(j < get_size(head)) printf("%c", *(char *)get(head, j++)); printf("\n"); j = 4, i = 0; while(j < 8) set(head, j++, str2 + i++); j = 0; while(j < get_size(head)) printf("%c", *(char *)get(head, j++)); clear(head); return 0; } /* Output: How ???? apples? How many apples? Process returned 0 (0x0) execution time : 0.281 s Press any key to continue. */
test2.c
程序代码:
#include "list.h" #include <stdio.h> int main(void) { PNode head = initializing(); int i, j = 47; for(i = 0; i < 10; i++) add(head, &i); for(i = 0; i < get_size(head); i++) printf("%-4d", *(int *)get(head, i)); printf("\n"); insert(head, 3, &j); for(i = 0; i < get_size(head); i++) printf("%-4d", *(int *)get(head, i)); printf("\n"); set(head, 8, &j); for(i = 0; i < get_size(head); i++) printf("%-4d", *(int *)get(head, i)); printf("\n"); rem(head, 3); for(i = 0; i < get_size(head); i++) printf("%-4d", *(int *)get(head, i)); clear(head); return 0; } /* Output: 0 1 2 3 4 5 6 7 8 9 0 1 2 47 3 4 5 6 7 8 9 0 1 2 47 3 4 5 6 47 8 9 0 1 2 3 4 5 6 47 8 9 Process returned 0 (0x0) execution time : 0.234 s Press any key to continue. */
自己测试了一会儿,还没发现Bug,如果有哪位朋友发现了,请您及时发信息给我哦。
每个结点数据的大小可以在list.h里面改,这里我写的是8字节
感谢2楼的建议,代码已经修正。
[ 本帖最后由 lz1091914999 于 2011-6-21 08:20 编辑 ]