- //单链表的基本操作
- #include <stdio.h>
- #include <stdlib.h>
- /*定义单链表结构体*/
- typedef struct node{
- int data;
- struct node *next;
- }node;
- int destory_linklist(node **head);
- /*初始化LinkList*/
- int init_linklist(node **head)
- {
- destory_linklist(head);
- if(head == NULL){
- return -1;
- }
- *head = (node *)malloc(sizeof(node));
- if(*head == NULL){
- return -1;
- }
- (*head)->next = NULL;
- return 0;
- }
- /*清空LinkList,不是销毁*/
- int clear_linklist(node *head)
- {
- if(head == NULL)
- return -1;
- node *p = head ->next,*q;
- while(p)
- {
- q = p->next;
- free(p);
- p = q;
- }
- head->next = NULL; //不要忘记把头节点的next指针赋空
- return 0;
- }
- /*获取LinkList的长度*/
- int link_length(node *head)
- {
- if(head == NULL)
- return -1;
- node *p = head->next;
- int i = 0;
- while(p)
- {
- ++i;
- p = p->next;
- }
- return i;
- }
- /*销毁LinkList*/
- int destory_linklist(node **head)
- {
- if(head == NULL || *head == NULL)
- return -1;
- //先清空LinkList,再释放头节点
- clear_linklist(*head);
- if(*head){
- free(*head);
- *head = NULL;
- }
- return 0;
- }
- /*在第i个位置上插入新节点*/
- int insert_link(node **head,int i,int data)
- {
- if(head == NULL)
- return -1;
- node *p = *head ,*s = NULL;
- int j = 0;
- while(p && j < i-1)
- {
- ++j;
- p = p ->next;
- }
- if(p == NULL || j > i -1)
- {
- return -1;
- }
- //p为要插入节点的前一个节点
- s = (node *)malloc(sizeof(node));
- if(s == NULL)
- return -1;
- s->data = data;
- s->next = p->next;
- p->next = s;
- return 0;
- }
- /*输出LinkList元素*/
- void ouput_linklist(node *head)
- {
- if(head == NULL)
- return ;
- node *p = head->next;
- while(p)
- {
- printf("%d ",p->data);
- p = p->next;
- }
- printf("\\n");
- }
- /*删除第i个位置上的节点*/
- int delete_linklist(node **head,int i)
- {
- if(head == NULL || *head == NULL)
- return -1;
- node *p = *head,*q = NULL;
- int j = 0;
- //先找到要删除节点的前一个节点
- while(p && j < i -1)
- {
- ++j;
- p = p->next;
- }
- if(p == NULL || j > i -1)
- {
- return -1;
- }
- q = p->next; //q为要删除的节点
- if(q == NULL){
- return -1;
- }
- p->next = q->next; //将要删除节点的前一个节点指向它的(要删除的节点)后一个节点
- free(q);
- return 0;
- }
- /*删除指定元素的节点*/
- int remove_linklist(node **head,int target)
- {
- if(head == NULL || *head == NULL)
- return -1;
- node *pre = *head ,*tar = (*head)->next;
- while(tar)
- {
- if(target == tar->data){
- break;
- }else{
- pre = tar;
- tar = tar ->next;
- }
- }
- if(tar == NULL){
- return 0; //没有找到,直接返回
- }else{
- pre->next = tar->next; //这儿居然写反了(汗!!!)
- free(tar);
- tar = NULL;
- }
- return 1;
- }
- int main()
- {
- node *La = NULL;
- if(init_linklist(&La) < 0){
- printf("init error\\n");
- return -1;
- }
- int i;
- for(i = 0; i<5 ;i++){
- if(insert_link(&La,i+1,i+1) < 0){
- printf("insert error\\n");
- return -1;
- }
- }
- printf("LinkLength: %d\\n",link_length(La));
- //
- // if(clear_linklist(La) < 0){
- // printf("clear error\\n");
- // return -1;
- // }
- //printf("LinkLength: %d\\n",link_length(La));
- ouput_linklist(La);
- // if(insert_link(&La,8,6) < 0){
- // printf("insert error\\n");
- // return -1;
- // }
- // if(delete_linklist(&La,5) < 0){
- // printf("delete error\\n");
- // return -1;
- // }
- //
- // if(remove_linklist(&La,10) < 0){
- // printf("remove error\\n");
- // return -1;
- // }
- //ouput_linklist(La);
- //init_linklist(&La);
- if(destory_linklist(&La) < 0){
- return -1;
- }
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/070820135011.html
来源: http://www.codesnippet.cn/detail/070820135011.html