结构体和动态内存分配实现的单向列表结构
好像贴出这么多代码来现丑有些不合适,不过我是博主, 我说了算:
#include<stdio.h> #include<stdlib.h> struct one_way_list{ int data; struct one_way_list *next_node_pt; }*head, *tail; //定义结构体, 单向列表的一个结点. int counter=0; //计数器 //添加数据: int app_data(int data) { if(counter==0){ head=tail=(struct one_way_list *)malloc(sizeof(struct one_way_list)); if(head==NULL){ return(-1); } (*head).data=data; } else { (*tail).next_node_pt=(struct one_way_list *)malloc(sizeof(struct one_way_list)); if((*tail).next_node_pt==NULL){ return(-1); } tail=(*tail).next_node_pt; (*tail).data=data; } counter++; return(1); } //某数据是否存在: int exists(int data) { if(counter==0){ return(-1); } else { int i=0; struct one_way_list *pt=head; do{ if((*pt).data==data) { return(i); } i++; pt=(*pt).next_node_pt; }while(pt != tail); return(-1); } } //查看计数器: int count(void) { return(counter); } //查看列表头: int head_data(void) { if(counter==0){ return(-1); } else { return((*head).data); } } //查看列表尾: int tail_data(void) { if(counter==0){ return(-1); } else { return((*tail).data); } } //查看第i个数据: int ith_data(int numth) { if(counter==0 || numth<0 || numth >= counter){ return(-1); } else { int i=0; struct one_way_list tmp; tmp=*head; for(i=0; i!=numth; i++){ tmp=*tmp.next_node_pt; } return(tmp.data); } } // 删除所有数据: int delete_all(void) { struct one_way_list *pt=head; while(counter!=0) { pt=head; head=(*head).next_node_pt; free(pt); counter--; } return(1); } // 删除第i个数据: int delete_ith(int numth) { if(counter==0 || numth<0 || numth>= counter) { return(-1); } else if(numth==0) { struct one_way_list *pt=head; head=(*head).next_node_pt; free(pt); return(1); } else { int i=0; struct one_way_list *pt_pr=head; struct one_way_list *pt=(*head).next_node_pt; for(i=1; i!=numth; i++) { pt_pr=(*pt_pr).next_node_pt; pt=(*pt).next_node_pt; } (*pt_pr).next_node_pt=(*pt).next_node_pt; free(pt); if(numth==counter) { tail=pt_pr; } return(1); } } //用于测试的主函数: int main(void) { if(app_data(1) == -1){ printf("Error1"); } printf("counter: %d\n",counter); if(app_data(2) == -1){ printf("Error2"); } printf("counter: %d\n",counter); if(app_data(3) == -1){ printf("Error3"); } printf("counter: %d\n",counter); if(app_data(4) == -1){ printf("Error4"); } printf("counter: %d\n",counter); if(exists(1)>=0){ printf("exits 1: %dth,\n", exists(1)); } else { printf("1 not exits\n"); } if(exists(9)>=0){ printf("exits 9: %dth,\n", exists(9)); } else { printf("9 not exits\n"); } printf("count: %d\nhead: %d\ntail_data: %d\n", count(), head_data(), tail_data()); printf("2th data:%d\n", ith_data(2)); delete_all(); printf("Just delete All.\n"); if(app_data(7) == -1){ printf("Error7"); } else { printf("7 added\n"); } printf("counter: %d\n",counter); if(app_data(6) == -1){ printf("Error6"); } else { printf("6 added\n"); } printf("counter: %d\n",counter); if(app_data(5) == -1){ printf("Error5"); } else { printf("5 added\n"); } printf("counter: %d\n",counter); if(delete_ith(1) == -1){ printf("1th deleted error\n"); } else { printf("1th deleted\n"); } printf("1th: %d\n",ith_data(1)); printf("head: %d\ntail: %d\n", head_data(), tail_data()); return(0); }