链表一

1.链表和数组区别及实现

复制代码
#include<stdio.h>

struct Test
{
    int data;
    struct Test *next;
};

int main()
{
    int i;
    int arr[] = {1,2,3};
    for(i=0;i<sizeof(arr)/sizeof(arr[0]);i++){
        printf("%d ",arr[i]);
    }
    putchar('\n');

    struct Test t1 = {1,NULL};
    struct Test t2 = {2,NULL};
    struct Test t3 = {3,NULL};
    
    t1.next = &t2;
    t2.next = &t3;
    
    printf("use link output:\n");
    printf("%d %d %d\n",t1.data,t2.data,t3.data);
    return 0;
}
复制代码

2.链表的静态添加和动态遍历

复制代码
#include<stdio.h>

struct Test
{
    int data;
    struct Test *next;
};

void printLink(struct Test *head)
{
    while(1){
        if(head !=0){
            printf("%d ",head->data);
            head = head->next;
        }else{
            putchar('\n');
            break;
        }
    }
}

int main()
{
    int i;
    int arr[] = {1,2,3};
    for(i=0;i<sizeof(arr)/sizeof(arr[0]);i++){
        printf("%d ",arr[i]);
    }
    putchar('\n');

    struct Test t1 = {1,NULL};
    struct Test t2 = {2,NULL};
    struct Test t3 = {3,NULL};
    
    t1.next = &t2;
    t2.next = &t3;
    
    printf("use link output:\n");
    printf("%d %d %d\n",t1.data,t2.data,t3.data);
    printLink(&t1);
    return 0;
}
复制代码

3.统计链表节点个数及链表查找

复制代码
#include<stdio.h>

struct Test
{
    int data;
    struct Test *next;
};

void printLink(struct Test *head)
{
    while(1){
        if(head !=0){
            printf("%d ",head->data);
            head = head->next;
        }else{
            putchar('\n');
            break;
        }
    }
}

int nodeSumLink(struct Test *head)
{
    struct Test *point;
    point = head;
    int nodeSum = 0;
    while(point != NULL){
        nodeSum++;
        point = point->next;
    }
    return nodeSum;
}

int searchLink(struct Test *head,int data)
{
    int num = 0;
    while(head != NULL){
        num++;
        if(head->data == data){
            return num;
        }else{
            return -1;
        }
    }
}

int main()
{
    int i;
    int arr[] = {1,2,3};
    for(i=0;i<sizeof(arr)/sizeof(arr[0]);i++){
        printf("%d ",arr[i]);
    }
    putchar('\n');

    struct Test t1 = {1,NULL};
    struct Test t2 = {2,NULL};
    struct Test t3 = {3,NULL};
    
    t1.next = &t2;
    t2.next = &t3;
    
    printf("use link output:\n");
    printf("%d %d %d\n",t1.data,t2.data,t3.data);
    printLink(&t1);
    printf("link node sum:%d\n",nodeSumLink(&t1));
    printf("link node search:%d\n",searchLink(&t1,1));
    return 0;
}
复制代码

4.链表从节点后方插入新节点

复制代码
int insertFromBehind(struct Test *head,int data,struct Test *new)
{
        struct Test *p = head;
        while(p != NULL){
                if(p->data == data){
                        new->next = p->next;
                        p->next = new;
                        return 1;
                }
                p = p->next;
        }
        return 0;
}
复制代码

5.链表从节点前方插入新节点

复制代码
int insertFromFort(struct Test *head,int data,struct Test *new)
{
    struct Test *p = head;
    int temp;
    while(p != NULL){
        if(p->data == data){
            new->next = p->next;
            p->next = new;  
            temp = new->data;
            new->data = p->data;
            p->data = temp;
            return 1;
        }
        p = p->next;
    }    
    return 0;
}
复制代码

6.链表的结点删除

复制代码
struct Test* deletNode(struct Test *head,int data)
{    
    struct Test *p = head;    
    if(p->data == data){
        head =head->next;
        return head;    
    }
    while(p->next != NULL){
        if(p->next->data == data){
            p->next = p->next->next;        
        }
        p = p->next;
    }
    return head;
}
复制代码

 

posted @   林奇老师  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
点击右上角即可分享
微信分享提示