C语言循序渐进之链表

链表:增、删、改、查

静态创建

#include <stdio.h>
struct Person
{
        int data;
        struct Person *next;
};
typedef struct Person P;

void printfLink(struct Person *head)
{
        struct Person *point = head;
        while(head != NULL){
                printf("%d\t",head->data);
                head = head->next;
        }
}
int main()
{
        P str1 = {1,NULL};
        P str2= {2,NULL};
        P str3= {3,NULL};

        str1.next = &str2;
        str2.next = &str3;

        printfLink(&str1);    //输出链表数据
        return 0;
}

统计链表数量&查找链表

#include <stdio.h>
struct Person
{
        int data;
        struct Person *next;
};
typedef struct Person P;

void printfLink(struct Person *head)
{
        struct Person *point = head;
        while(head != NULL){
                printf("%d\t",head->data);
                head = head->next;
        }
}
int getLinkNumber(struct Person *head)
{
        int cnt = 0;
        struct Person *point = head;

        while(point != NULL){
                cnt++;
                point = point->next;
        }

        return cnt;
}
int  searchLink(struct Person *head,int data)
{
        struct Person *point = head;
        int ret = 0;
        while(point != NULL){
                if(point->data == data){
                        ret = 1;
                }
                point = point->next;
        }

        return ret;
}

int main()
{
        P str1 = {1,NULL};
        P str2= {2,NULL};
        P str3= {3,NULL};
        int ret = 0;
        int number = 0;
        str1.next = &str2;
        str2.next = &str3;

        printfLink(&str1);
        number = getLinkNumber(&str1);
        printf("linkNumber = %d\n",number);
        ret = searchLink(&str1,2);
        if(ret == 1){
                printf("have 2\n");
        }else{
                printf("no 2\n");

        }
        return 0;

}

链表节点前后插入

#include <stdio.h>
struct Person
{
        int data;
        struct Person *next;
};
typedef struct Person P;

void printfLink(struct Person *head)    //输出链表
{
        struct Person *point = head;
        while(head != NULL){
                printf("%d\t",head->data);
                head = head->next;
        }
}
int getLinkNumber(struct Person *head)  //获得链表个数
{
        int cnt = 0;
        struct Person *point = head;

        while(point != NULL){
                cnt++;
                point = point->next;
        }

        return cnt;
}
int  searchLink(struct Person *head,int data)    //查找链表
{
        struct Person *point = head;
        int ret = 0;
        while(point != NULL){
                if(point->data == data){
                        ret = 1;
                }
                point = point->next;
        }

        return ret;
}
int insertBehind(struct Person *head,int data,struct Person *new)//节点后插入
{
        struct Person *point = head;
        while(point != NULL){
                if(point->data == data){
                        new->next = point->next;
                        point->next = new;
                        return 1;
                }
                point = point->next;
        }

        return 0;
}

struct Person *insertAfter(struct Person *head,int data,struct Person *new)//节点前插入
{
        struct Person *point = head;
                if(point->data == data){
                        new->next = point;
                        return new;
                }
        while(point != NULL){
                if(point->next->data == data){
                        new->next = point->next;
                        point->next = new;
                        puts("success!");
                        return head;
                }
                point = point->next;
        }
        puts("NO find");
        return head;
}

int main()
{
        P str1 = {1,NULL};
        P str2= {2,NULL};
        P str3= {3,NULL};
        P new= {4,NULL};
        P new2= {5,NULL};
        int ret = 0;
        int number = 0;
        P *head;
        head = &str1;
        str1.next = &str2;
        str2.next = &str3;

        printfLink(&str1);
        number = getLinkNumber(&str1);
        printf("linkNumber = %d\n",number);
        ret = searchLink(&str1,2);
        if(ret == 1){
                printf("have 2\n");
        }else{
                printf("no 2\n");

        }
        insertBehind(&str1,2,&new);
        printfLink(&str1);
        head = insertAfter(&str1,2,&new2);
        printfLink(head);
        return 0;

}

删除链表节点

#include <stdio.h>
struct Person
{
        int data;
        struct Person *next;
};
typedef struct Person P;

void printfLink(struct Person *head)    //输出链表
{
        struct Person *point = head;
        while(head != NULL){
                printf("%d\t",head->data);
                head = head->next;
        }
}
int getLinkNumber(struct Person *head)  //获得链表个数
{
        int cnt = 0;
        struct Person *point = head;

        while(point != NULL){
                cnt++;
                point = point->next;
        }

        return cnt;
}
int  searchLink(struct Person *head,int data)    //查找链表
{
        struct Person *point = head;
        int ret = 0;
        while(point != NULL){
                if(point->data == data){
                        ret = 1;
                }
                point = point->next;
        }

        return ret;
}
int insertBehind(struct Person *head,int data,struct Person *new)//节点后插入
{
        struct Person *point = head;
        while(point != NULL){
                if(point->data == data){
                        new->next = point->next;
                        point->next = new;
                        return 1;
                }
                point = point->next;
        }

        return 0;
}

struct Person *insertAfter(struct Person *head,int data,struct Person *new)//节点前插入
{
        struct Person *point = head;
                if(point->data == data){
                        new->next = point;
                        return new;
                }
        while(point != NULL){
                if(point->next->data == data){
                        new->next = point->next;
                        point->next = new;
                        puts("success!");
                        return head;
                }
                point = point->next;
        }
        puts("NO find");
        return head;
}
struct Person *deleteLink(struct Person *head,int data) //删除链表节点
{
        struct Person *point = head;
        if(point->data == data){
                return point->next;
        }
        while(point != NULL){
                if(point->next->data == data){
                        point->next = point->next->next;
                        return head;
                }
                point = point->next;
        }

}

int main()
{
        P str1 = {1,NULL};
        P str2= {2,NULL};
        P str3= {3,NULL};
        P new= {4,NULL};
        P new2= {5,NULL};
        int ret = 0;
        int number = 0;
        P *head;
        head = &str1;
        str1.next = &str2;
        str2.next = &str3;

        printfLink(&str1);    //打印链表
        number = getLinkNumber(&str1);    //获取链表个数
        printf("linkNumber = %d\n",number);
        ret = searchLink(&str1,2);
        if(ret == 1){
                printf("have 2\n");
        }else{
                printf("no 2\n");

        }
        insertBehind(&str1,2,&new);    //指定链表后插入新节点
        printfLink(&str1);
        head = insertAfter(&str1,2,&new2);//指定链表前插入新节点
        printfLink(head);
        head = deleteLink(head,3);    //删除指定链表节点
        printfLink(head);
        return 0;


}

链表动态创建头插法

#include <stdio.h>
#include <stdlib.h>
struct Person
{
        int data;
        struct Person *next;
};
typedef struct Person P;

void printfLink(struct Person *head)    //输出链表
{
        struct Person *point = head;
        while(head != NULL){
                printf("%d\t",head->data);
                head = head->next;
        }
}
struct Person *insertLinkForHead(struct Person *head) //头插
{
        struct Person *new;
        while(1){
                new =  (P*)malloc(sizeof(P));
                new->next = NULL;
                puts("please inter link data");
                scanf("%d",&(new->data));
                if(new->data == 0){
                        printf("0 quit\n");
                        return head;
                }
                if(head == NULL){
                        head = new;
                }else{
                        new->next = head;
                        head = new;
                }
        }
        return head;

}


int main()
{
        P str1 = {1,NULL};
        P str2= {2,NULL};
        P str3= {3,NULL};
        P new= {4,NULL};
        P new2= {5,NULL};
        int ret = 0;
        int number = 0;
        P *head;
        head = &str1;
        str1.next = &str2;
        str2.next = &str3;

        printfLink(&str1);
        head = insertLinkForHead(head);
        printfLink(head);
       
        return 0;

}
#include <stdio.h>
#include <stdlib.h>
struct Person
{
        int data;
        struct Person *next;
};
//typedef struct Person P;

void printfLink(struct Person *head)    //输出链表
{
        struct Person *point = head;
        while(point != NULL){
                printf("%d\t",point->data);
                point = point->next;
        }
        puts("");
}

struct Person *insertLinkForHead(struct Person *head,struct Person *new)
{
        
        if(head == NULL){
            head = new;
        }else{
            new->next = head;
            head = new;
        }
        return head;
}
struct Person *createLink(struct Person *head)
{
    struct Person *new;
        while(1){
        new =  (struct Person*)malloc(sizeof(struct Person));
        new->next = NULL;
        puts("please inter link data");
        scanf("%d",&(new->data));
        if(new->data == 0){
            printf("0 quit\n");
            free(new);
            return head;
        }
        head = insertLinkForHead(head,new);
    }
    
}
int main()
{        
        struct Person *head = NULL;
        struct Person str1 = {66,NULL};
        head = createLink(head);
        printfLink(head);
        head = insertLinkForHead(head,&str1);
        printfLink(head);
        
        return 0;
}

链表动态创建尾插法

#include <stdio.h>
#include <stdlib.h>
struct Person
{
        int data;
        struct Person *next;
};
//typedef struct Person P;

void printfLink(struct Person *head)    //输出链表
{
        struct Person *point = head;
        while(point != NULL){
                printf("%d\t",point->data);
                point = point->next;
        }
        puts("");
}

struct Person *insertLinkForHead(struct Person *head,struct Person *new)
{
        
        if(head == NULL){
            head = new;
        }else{
            new->next = head;
            head = new;
        }
        return head;
}
struct Person *insertLinkForTail(struct Person *head,struct Person *new)
{
        struct Person *point = head;
        if(head == NULL){    //判断是不是为空
            head = new;
            return head;
        }
        while(point->next != NULL){    //一直检测是不是到尾巴了
            point = point->next;
        }
        point->next = new;    //到了尾部插入新节点
        
        return head;
}
struct Person *createLinkForHead(struct Person *head)
{
    struct Person *new;
        while(1){
        new =  (struct Person*)malloc(sizeof(struct Person));
        new->next = NULL;
        puts("please inter link data");
        scanf("%d",&(new->data));
        if(new->data == 0){
            printf("0 quit\n");
            free(new);
            return head;
        }
        head = insertLinkForHead(head,new);
    }
    
}
struct Person *createLinkForTail(struct Person *head)
{
    struct Person *new;
        while(1){
        new =  (struct Person*)malloc(sizeof(struct Person));
        new->next = NULL;
        puts("please inter link data");
        scanf("%d",&(new->data));
        if(new->data == 0){
            printf("0 quit\n");
            free(new);
            return head;
        }
        head = insertLinkForHead(head,new);
    }
    
}
int main()
{        
        struct Person *head = NULL;
        struct Person str1 = {66,NULL};
        struct Person str2 = {77,NULL};
        
        head = createLinkForHead(head);    //动态创建头插
        printfLink(head);
        head = insertLinkForHead(head,&str1);    //头插
        printfLink(head);
        
        head = createLinkForTail(head);    //动态创建尾插
        printfLink(head);
        head = insertLinkForTail(head,&str2);//尾插
        printfLink(head);
        
        return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值