码迷,mamicode.com
首页 > 其他好文 > 详细

链表程序详解Linklist.c

时间:2016-06-09 13:21:38      阅读:398      评论:0      收藏:0      [点我收藏+]

标签:

#include <stdio.h>
#include <stdlib.h>

typedef int datatype;
typedef struct node/*链表的每一个节点都是结构体*/
{
    datatype data;
    struct node *next; 
}linklist;/*linklist 是struct node 的别名,以后就可以用linklist来替代struct node*/
/*函数声明区*/ linklist
* list_create(); int head_insert(linklist *H,datatype value); int head_delete(linklist *H); void list_show(linklist *H); //空----1 非空-----0 int list_empty(linklist *H); int list_insert(linklist *H,datatype value,int pos); int list_delete(linklist *H,datatype value); int list_replace(linklist *H,datatype old,datatype new); int list_search(linklist *H,datatype value); void list_recorver(linklist *H); int main(int argc, const char *argv[]) { linklist *H = NULL; if((H = list_create()) == NULL) { printf("list_create failed\n"); return -1; } head_insert(H,1); head_insert(H,2); head_insert(H,3); head_insert(H,4); head_insert(H,5); list_show(H); head_delete(H); list_show(H); list_insert(H,10,2); list_show(H); list_delete(H,3); list_show(H); list_replace(H,10,100); list_show(H); printf("search:%d\n",list_search(H,1)); list_recorver(H); list_show(H); return 0; } linklist * list_create() { linklist *H = NULL; if((H = malloc(sizeof(linklist))) == NULL) { printf("malloc failed\n"); return NULL; } H->next = NULL;//防止野指针。 return H; } int head_insert(linklist *H,datatype value) { linklist *p = NULL; if((p = malloc(sizeof(linklist))) == NULL) { printf("malloc node failed\n"); return -1; } p->data = value; p->next = H->next; H->next = p; return 0; } void list_show(linklist *H) { while(H->next != NULL) { printf("%d ",H->next->data); H = H->next; } printf("\n"); } int head_delete(linklist *H) { linklist *p = H->next; if(list_empty(H)) { printf("list is empty\n"); return -1; } H->next = p->next; free(p); p = NULL; return 0; } int list_empty(linklist *H) { if(H->next != NULL) return 0; else return 1; } int list_insert(linklist *H,datatype value,int pos) { int i = 0; linklist *p = H,*q = NULL; while(i < pos && p != NULL) { p = p->next; i++; } if(p == NULL) { printf("pos error\n"); return -1; } if((q = malloc(sizeof(linklist))) == NULL) { printf("malloc node failed\n"); return -1; } q->data = value; q->next = p->next; p->next = q; return 0; } int list_delete(linklist *H,datatype value) { linklist *p = NULL; while(H->next != NULL) { if(H->next->data == value) { p = H->next; H->next = p->next; free(p); p = NULL; return 0; } else H = H->next; } printf("no value"); return -1; } int list_replace(linklist *H,datatype old,datatype new) { while(H->next != NULL) { if(H->next->data == old) { H->next->data = new; return 0; } else { H = H->next; } } return -1; } int list_search(linklist *H,datatype value) { int pos = 0; while(H->next != NULL) { if(H->next->data == value) return pos; else { H = H->next; pos++; } } printf("no found\n"); return -1; } void list_recorver(linklist *H) { linklist *q = NULL,*p = H->next; H->next = NULL; while(p != NULL) { q = p; p = p->next; q->next = H->next; H->next = q; } }

 

链表程序详解Linklist.c

标签:

原文地址:http://www.cnblogs.com/renchong/p/5572194.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!