C语言学习-链表

1.链表概念

  • 链表特点
    (1)n个节点离散分配
    (2)每一个节点之间通过指针相连
    (3)每一个节点有一个前驱节点和一个后继节点
    (4)首节点没有前驱节点,尾节点没有后继节点
struct link{
       int data;          //定义数据域
       struct link *next; //定义指针域,存储直接后继的节点信息
};

数据域的内容可以自己指定,指针域用来存放下一个节点的地址。

  • 一些关于链表的定义
    **首节点:**存放第一个有效数据的节点

**头节点:**在单链表的第一个结点之前附设一个结点,它没有直接前驱,称之为头结点,头结点的数据域可以不存储任何信息,指针域指向第一个节点(首节点)的地址。头结点的作用是使所有链表(包括空表)的头指针非空

**头指针:**指向头节点的指针

**尾节点:**存放最后一个有效数据的节点

**尾指针:**指向尾节点的指针

  • 链表定义:
#include <stdio.h>
#include <stdlib.h>
struct  Test
{
    int a;
    struct Test * next;
};
void printlink(struct Test * point)
{
    while(point!=NULL){
        printf("%d ",point->a);
        point=point->next;
        
    }
    putchar('\n');

}

int main()
{   
    struct Test *head=NULL;
    struct Test t1={1,NULL};
    struct Test t2={2,NULL};
    struct Test t3={3,NULL};
    struct Test t4={4,NULL};
    head = &t1;
    t1.next=&t2;
    t2.next=&t3;
    t3.next=&t4;
    printlink(head);
    system("pause");
    return 0;
}

2.链表动态创建

(1)头插法

struct Test *insertFromHead(struct Test*head,struct Test *new)
{
    if(head == NULL){
        head = new;
    }else{
    	new->next=head;
		head=new;
	}
    return head;
}

用头插法动态创建链表

#include <stdio.h>
#include <stdlib.h>
struct  Test
{
    int a;
    struct Test * next;
};
void printlink(struct Test * point)
{
    while(point!=NULL){
        printf("%d ",point->a);
        point=point->next;
        
    }
    putchar('\n');

}
struct Test *insertFromHead(struct Test*head,struct Test *new)
{
    if(head == NULL){
        head = new;
    }else{
        new->next=head;
        head=new;
    }
    return head;
}
struct Test *createLink(struct Test *head)
{   
    struct Test *new;
    while(1){
        new=(struct Test *)malloc(sizeof(struct Test));
        printf("输入数据(输入0结束输入)\n");
        scanf("%d",&(new->a));
        new->next = NULL; 
        if(new->a==0){
            free(new);
            return head;
        }
        head=insertFromHead(head,new);
    }
}

int main()
{   
    struct Test *head=NULL;
    head = createLink(head);
    printlink(head);
    system("pause");
    return 0;
}

(2)尾插法

struct Test *insertFromTail(struct Test*head,struct Test *new)
{   
    struct Test *p=head;
    if(p==NULL){
        head=new;
        return head;
    }
    while(p->next!=NULL){
        p=p->next;
    }
    p->next=new;
    return head;
}

用尾插法动态创建链表

#include <stdio.h>
#include <stdlib.h>
struct  Test
{
    int a;
    struct Test * next;
};
void printlink(struct Test * point)
{
    while(point!=NULL){
        printf("%d ",point->a);
        point=point->next;
        
    }
    putchar('\n');

}
struct Test *insertFromTail(struct Test*head,struct Test *new)
{   
    struct Test *p=head;
    if(p==NULL){
        head=new;
        return head;
    }
    while(p->next!=NULL){
        p=p->next;
    }
    p->next=new;
    return head;
}
struct Test *createLink(struct Test *head)
{   
    struct Test *new;
    while(1){
        new=(struct Test *)malloc(sizeof(struct Test));
        printf("输入数据(输入0结束输入)\n");
        scanf("%d",&(new->a));
        new->next = NULL; 
        if(new->a==0){
            free(new);
            return head;
        }
        head=insertFromTail(head,new);
    }
}

int main()
{   
    struct Test *head=NULL;
    head = createLink(head);
    printlink(head);
    system("pause");
    return 0;
}

3.链表的“增删改查”

(1)增

  • 在指定数据前插入节点
int insertFro(struct Test *head,int data,struct Test *new)
{

    struct Test *p;
    p = head;
    if(p->a == data){
        new->next = head;
        head = new;
        return 1;
    }
    while(p->next!=NULL){

        if(p->next->a == data){
            new -> next = p->next;
            p->next = new;
            return 1;
        }
        p = p->next;
    }   
    return 0 ;
}
  • 在指定数据后插入节点
int insertBehind(struct Test *head,int data,struct Test * new1)
{   struct Test*p=head;
    while(p!=NULL){
        if(p->a==data){
            new1->next=p->next;
            p->next=new1;
            return 1; }       
        p=p->next;
    }
    return 0;
}
  • 示例代码
#include <stdio.h>
#include <stdlib.h>
struct  Test
{
    int a;
    struct Test * next;
};
void printlink(struct Test * point)
{
    while(point!=NULL){
        printf("%d ",point->a);
        point=point->next;
        
    }
    putchar('\n');

}
struct Test *insertFromTail(struct Test*head,struct Test *new)
{   
    struct Test *p=head;
    if(p==NULL){
        head=new;
        return head;
    }
    while(p->next!=NULL){
        p=p->next;
    }
    p->next=new;
    return head;
}
struct Test *createLink(struct Test *head)
{   
    struct Test *new;
    while(1){
        new=(struct Test *)malloc(sizeof(struct Test));
        printf("输入数据(输入0结束输入)\n");
        scanf("%d",&(new->a));
        new->next = NULL; 
        if(new->a==0){
            free(new);
            return head;
        }
        head=insertFromTail(head,new);
    }
}
/*
    在指定数据后节点
*/
int insertBehind(struct Test *head,int data,struct Test * new1)
{   struct Test*p=head;
    while(p!=NULL){
        if(p->a==data){
            new1->next=p->next;
            p->next=new1;
            return 1; }       
        p=p->next;
    }
    return 0;
}

int insertFro(struct Test *head,int data,struct Test *new)
{

    struct Test *p;
    p = head;
    if(p->a == data){
        new->next = head;
        head = new;
        return 1;
    }
    while(p->next!=NULL){

        if(p->next->a == data){
            new -> next = p->next;
            p->next = new;
            return 1;
        }
        p = p->next;
    }   
    return 0 ;
}
int main()
{   
    struct Test *head=NULL;
    struct Test stu1 ;
    int data;
    int ret;
    head = createLink(head);
    printf("=================插入节点前=================\n");
    printlink(head);

    printf("请输入在哪个节点前插入节点:\n");
    scanf("%d",&data);
    printf("请输入需要插入的节点\n");
    scanf("%d",&(stu1.a));
    stu1.next = NULL;

    ret = insertFro(head,data,&stu1);
    if(ret){
        printf("插入节点成功!\n");
    }else{
        printf("插入失败\n");
    }
    printf("=================插入节点后=================\n");
    printlink(head);
    system("pause");
    return 0;
}

在这里插入图片描述
在这里插入图片描述

(2)删
删除指定的节点

int deleteNode(struct Test *head,int data)
{
    struct Test *p,*ptemp;
    p=head;
    if (p->a == data)
    {
        ptemp = head;
        head = head->next;
        free(ptemp);
        return 1;
    }
    while(p->next!=NULL){
        if(p->next->a == data){
            ptemp = p->next;
            p->next = p->next->next;
            free(ptemp);    
            return 1;
        }
        p = p->next;
    }

    return 0;
}
  • 示例代码:
#include <stdio.h>
#include <stdlib.h>
struct  Test
{
    int a;
    struct Test * next;
};
void printlink(struct Test * point)
{
    while(point!=NULL){
        printf("%d ",point->a);
        point=point->next;
        
    }
    putchar('\n');

}
struct Test *insertFromTail(struct Test*head,struct Test *new)
{   
    struct Test *p=head;
    if(p==NULL){
        head=new;
        return head;
    }
    while(p->next!=NULL){
        p=p->next;
    }
    p->next=new;
    return head;
}
struct Test *createLink(struct Test *head)
{   
    struct Test *new;
    while(1){
        new=(struct Test *)malloc(sizeof(struct Test));
        printf("输入数据(输入0结束输入)\n");
        scanf("%d",&(new->a));
        new->next = NULL; 
        if(new->a==0){
            free(new);
            return head;
        }
        head=insertFromTail(head,new);
    }
}
int deleteNode(struct Test *head,int data)
{
    struct Test *p,*ptemp;
    p=head;
    if (p->a == data)
    {
        ptemp = head;
        head = head->next;
        free(ptemp);
        return 1;
    }
    while(p->next!=NULL){
        if(p->next->a == data){
            ptemp = p->next;
            p->next = p->next->next;
            free(ptemp);    
            return 1;
        }

        p = p->next;

    }

    return 0;
}
int main()
{   
    struct Test *head=NULL;
    struct Test stu1 ;
    int data;
    int ret;
    head = createLink(head);
    printf("=================插入节点前=================\n");
    printlink(head);

    printf("请输入要删除的节点:\n");
    scanf("%d",&data);

    ret = deleteNode(head,data);
    if(ret){
        printf("删除节点成功!\n");
    }else{
        printf("删除失败\n");
    }
    printf("=================删除节点后=================\n");
    printlink(head);
    system("pause");
    return 0;
}

在这里插入图片描述

(3)改

  • 修改指定节点的内容
/*
    修改指定节点
*/
int alterNode(struct Test *head,int data,int newData)
{   
    struct Test *p = head;

    while(p!=NULL){
        if(p->a==data){
            p->a = newData;
            return 1;
        }
        p=p->next;
    }
    return 0;
}
  • 示例代码
/*
    修改指定节点
*/
#include <stdio.h>
#include <stdlib.h>
struct  Test
{
    int a;
    struct Test * next;
};
void printlink(struct Test * point)
{
    while(point!=NULL){
        printf("%d ",point->a);
        point=point->next;
        
    }
    putchar('\n');

}
struct Test *insertFromTail(struct Test*head,struct Test *new)
{   
    struct Test *p=head;
    if(p==NULL){
        head=new;
        return head;
    }
    while(p->next!=NULL){
        p=p->next;
    }
    p->next=new;
    return head;
}
struct Test *createLink(struct Test *head)
{   
    struct Test *new;
    while(1){
        new=(struct Test *)malloc(sizeof(struct Test));
        printf("输入数据(输入0结束输入)\n");
        scanf("%d",&(new->a));
        new->next = NULL; 
        if(new->a==0){
            free(new);
            return head;
        }
        head=insertFromTail(head,new);
    }
}
int alterNode(struct Test *head,int data,int newData)
{   
    struct Test *p = head;

    while(p!=NULL){
        if(p->a==data){
            p->a = newData;
            return 1;
        }
        p=p->next;
    }
    return 0;
}
int main()
{   
    struct Test *head=NULL;
    struct Test stu1 ;
    int data;
    int newData;
    int ret;
    head = createLink(head);
    printf("=================修改节点前=================\n");
    printlink(head);

    printf("请输入要修改的节点:\n");
    scanf("%d",&data);
    printf("请输入修改的内容:\n");
    scanf("%d",&newData);
    ret = alterNode(head,data,newData);
    if(ret){
        printf("修改节点成功!\n");
    }else{
        printf("修改失败\n");
    }
    printf("=================修改节点后=================\n");
    printlink(head);
    system("pause");
    return 0;
}

在这里插入图片描述

(4)查

-在链表中查找相应的节点

int searchNode(struct Test *head,int data)
{   
    while(head!=NULL){
        if(head->a==data){
            return 1;
        }
        head=head->next;
    }
    return 0;
}
  • 示例代码
#include <stdio.h>
#include <stdlib.h>
struct  Test
{
    int a;
    struct Test * next;
};
void printlink(struct Test * point)
{
    while(point!=NULL){
        printf("%d ",point->a);
        point=point->next;
        
    }
    putchar('\n');

}
struct Test *insertFromTail(struct Test*head,struct Test *new)
{   
    struct Test *p=head;
    if(p==NULL){
        head=new;
        return head;
    }
    while(p->next!=NULL){
        p=p->next;
    }
    p->next=new;
    return head;
}
struct Test *createLink(struct Test *head)
{   
    struct Test *new;
    while(1){
        new=(struct Test *)malloc(sizeof(struct Test));
        printf("输入数据(输入0结束输入)\n");
        scanf("%d",&(new->a));
        new->next = NULL; 
        if(new->a==0){
            free(new);
            return head;
        }
        head=insertFromTail(head,new);
    }
}
/*
    查找相应节点
*/
int searchNode(struct Test *head,int data)
{   
    while(head!=NULL){
        if(head->a==data){
            return 1;
        }
        head=head->next;
    }
    return 0;
}
int main()
{   
    struct Test *head=NULL;
    struct Test stu1 ;
    int data;
    int newData;
    int ret;
    head = createLink(head);
    printf("=================链表数据=================\n");
    printlink(head);
    printf("想要查找的节点\n");
    scanf("%d",&data);
    ret = searchNode(head,data);
    if(ret){
        printf("%d在链表中\n",data);
    }else{
        printf("%d不在链表中\n",data);
    }
    system("pause");
    return 0;
}

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值