C语言 链表(Dev C++/分文件版)
头文件:quechain.h
struct Question { int _id; struct Question* pre; struct Question* next; }; void chain_print(struct Question* qFirst); void chain_add(struct Question* qFirst, struct Question* qAdd); void chain_remove(struct Question* qFirst, struct Question* qRemove); struct Question* chain_get(struct Question* qFirst, int index); int chain_count(struct Question* qFirst); struct Question* newQuestion(int id); void newQuestion2(int id, struct Question** q);
源文件:quechain.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "quechain.h" void chain_print(struct Question* qFirst) { puts("----------print------------"); struct Question* q = qFirst; if (qFirst->next == NULL) { puts("没有元素可以打印"); return; } else { q = qFirst->next; } // 遍历链表 for(q; (q->next) != NULL; q=q->next ) { printf("%d\n", q->_id); } // 最后一项特殊对待 printf("%d\n", q->_id); } void chain_add(struct Question* qFirst, struct Question* qAdd) { // 遍历链表 struct Question* q = qFirst; for(q; (q->next) != NULL; q=q->next ) { } // 最后一项 q->next = qAdd; qAdd->pre = q; } void chain_remove(struct Question* qFirst, struct Question* qRemove) { struct Question* qPre = NULL; struct Question* qNext = NULL; struct Question* q = qFirst; if (qFirst == qRemove) { (qFirst->next)->pre = NULL; qFirst = (qFirst->next); free(q); return; } // 遍历链表 for(q; (q->next) != NULL; q=q->next ) { if (q == qRemove) { qPre = q->pre; qNext = q->next; if (qPre!=NULL) { qPre->next= qNext; } if (qNext!=NULL) { qNext->pre = qPre; } free(qRemove); return; } } // 最后一项 if (q == qRemove) { qPre = q->pre; if (qPre!=NULL) { qPre->next= qNext; } free(qRemove); } } struct Question* chain_get(struct Question* qFirst, int index) { int i = 0; // 遍历链表 struct Question* q = qFirst; for(q; (q->next) != NULL; q=q->next,i++ ) { if (index == i) { return q; } } if (index == i) { // 获取最后一个元素 return q; } return NULL; } int chain_count(struct Question* qFirst) { int i = 0; // 遍历链表 struct Question* q = qFirst; for(q; (q->next) != NULL; q=q->next,i++ ) { } return i; } struct Question* newQuestion(int id) { struct Question* q = (struct Question*)malloc(sizeof(struct Question)); memset(q, 0, sizeof(struct Question)); q->_id = id; return q; } void newQuestion2(int id, struct Question** q) { *q = (struct Question*)malloc(sizeof(struct Question)); memset(*q, 0, sizeof(struct Question)); (*q)->_id = id; }
测试文件:testc.c
#include <stdio.h> #include <stdlib.h> #include "quechain.h" int main() { struct Question* qFirst = NULL; qFirst = newQuestion(0); struct Question* q1 = NULL; newQuestion2(1,&q1); chain_add(qFirst, q1); struct Question* q2 = newQuestion(2); chain_add(qFirst, q2); struct Question* q3 = newQuestion(3); chain_add(qFirst, q3); struct Question* q4 = newQuestion(4); chain_add(qFirst, q4); struct Question* q5 = newQuestion(5); chain_add(qFirst, q5); chain_print(qFirst); printf("get %d\n", chain_get(qFirst, 1)->_id); printf("get %d\n", chain_get(qFirst, 3)->_id); printf("get %d\n", chain_get(qFirst, 5)->_id); //------------------------------ chain_remove(qFirst, q3); chain_print(qFirst); chain_remove(qFirst, q5); chain_print(qFirst); chain_remove(qFirst, q1); chain_print(qFirst); printf("元素个数: %d\n", chain_count(qFirst)); return 0; }