链表初级最终总结超级无敌大总结

一、单向链表

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct list_node
{
    int num;
    struct list_node *next;
};

struct list_node *init_list_head()
{
    struct list_node *head = NULL;
    head = malloc(sizeof(struct list_node));
    head->next = NULL;
    return head;
}

void insert_list_head(struct list_node *head, int num)
{
    struct list_node *new = malloc(sizeof(struct list_node));
    new->next = NULL;
    new->num = num;
    new->next = head->next;
    head->next = new;
}

void insert_list_last(struct list_node *head, int num)
{
    struct list_node *new = malloc(sizeof(struct list_node));
    new->next = NULL;
    new->num = num;
    struct list_node *p = NULL;
    for (p = head; p->next != NULL; p = p->next)
        ; //p指向最后一个节点
    new->next = p->next;
    p->next = new;
}

void show_list_first(struct list_node *head)
{
    struct list_node *p = NULL;
    for (p = head->next; p != NULL; p = p->next)
    {
        printf("%d\n", p->num);
    }
}

void delete_list_node(struct list_node *head, int num)
{
    struct list_node *p = NULL;
    struct list_node *q = NULL;
    for (q = head, p = head->next; p != NULL; q = p, p = p->next)
    {
        if (p->num == num)
        {
            q->next = p->next;
            free(p);

        }
    }
}

void delete_list(struct list_node *head)
{
    struct list_node *p = NULL;
    struct list_node *q = NULL;
    for (q = p = head; p != NULL; p = q)
    {
        q = p->next;
        free(p);
        return 0;
    }
return -1;
}

int main()
{
    //初始话链表头节点
    struct list_node *head = init_list_head();
    int num = 10;
    //像链表中插入信息,头插法
    insert_list_head(head, num);
    insert_list_head(head, 20);
    //尾插法
    show_list_first(head);
    printf("#\n");
    insert_list_last(head, 30);
    insert_list_last(head, 40);
    //向后遍历
    show_list_first(head);
    //向前遍历,链表结构体中没指向前的指针,没办法向前遍历
    //删除节点
    delete_list_node(head, 20);
    printf("#\n");
    show_list_first(head);
    //删除链表
    delete_list(head);

    return 0;
}

二、单向循环链表

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct list_node{
    int num;
    struct list_node*next;
};

struct list_node* init_list_node(){
    struct list_node*head=malloc(sizeof(struct list_node));
    head->next=head;
    return head;
}

void insert_node_head(struct list_node*head,int num){
    struct list_node*new=malloc(sizeof(struct list_node));
    new->num=num;
    new->next=head->next;
    head->next=new;
}

void insert_node_last(struct list_node*head,int num){
    struct list_node*new=malloc(sizeof(struct list_node));
    new->next=NULL;
    new->num=num;

    struct list_node*p=NULL;
    for(p=head;p->next!=head;p=p->next);
    new->next=p->next;
    p->next=new;
}

void show_list_first(struct list_node*head){
    struct list_node*p=NULL;
    for(p=head->next;p!=head;p=p->next){
        printf("%d\n",p->num);
    }

}

int deldete_list_node(struct list_node*head,int num){
    struct list_node*p=NULL;
    struct list_node*q=NULL;
    for(q=head,p=head->next;p!=head;q=p,p=p->next){
        if(p->num==num){
            q->next=p->next;
            free(p);
            return 0;
        }
    }
    return-1;
}

void delete_list(struct list_node*head){
    struct list_node*p=NULL;
    struct list_node*q=NULL;

    for(p=q=head;p!=head;p=q){
        q=p->next;
        free(p);
    }
}

int main(){
    //创建头节点
    struct list_node*head=init_list_node();
    //头插法插入数据
    insert_node_head(head,10);
    insert_node_head(head,20);
    printf("#\n");
    show_list_first(head);

    //尾插法插入数据
    insert_node_last(head,30);
    insert_node_last(head,40);
    printf("#\n");
    //向后遍历链表
    show_list_first(head);
    //删除节点
    deldete_list_node(head,20);
    printf("#\n");
    show_list_first(head);
    //删除链表
    delete_list(head);


}

三、双向不循环链表

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct list_node{
    int num;
    struct list_node*next;
    struct list_node*prev;
};

struct list_node*init_list_node(){
    struct list_node*head=malloc(sizeof(struct list_node));

    head->next=NULL;
    head->prev=NULL;
    return head;
}

void insert_list_head(struct list_node*head,int num){
    struct list_node*new=malloc(sizeof(struct list_node));
    new->num=num;
    new->next=NULL;
    new->prev=NULL;

    new->next=head->next;
    new->prev=head;
    
    if(head->next!=NULL){
        head->next->prev=new;
    }
    head->next=new;//这里是不能放到上面的
    
}

void insert_list_last(struct list_node*head,int num){
    struct list_node*new=malloc(sizeof(struct list_node));
    new->num=num;
    new->next=NULL;
    new->prev=NULL;

    struct list_node*p=NULL;
    for(p=head;p->next!=NULL;p=p->next);

    new->next=p->next;
    new->prev=p;
    p->next=new;

}

void show_list_first(struct list_node*head){
    struct list_node*p=NULL;
    for(p=head->next;p!=NULL;p=p->next){
        printf("%d\n",p->num);
    }

}

void show_list_last(struct list_node*head){
    struct list_node *p = NULL;
    for (p = head; p->next != NULL; p = p->next)
        ;

    for (; p != head; p = p->prev)
    {

        printf("%d\n", p->num);
    }

}

int delete_list_node(struct list_node*head,int num){
    struct list_node*p=NULL;
    struct list_node*q=NULL;

    for(q=head,p=head->next;p!=NULL;q=p,p=p->next){
        if(p->num==num){
            q->next=p->next;
            if(p->next!=NULL){
                p->next->prev=q;
            }
            free(p);
            return 0;
        }
    }

}

void delete_list(struct list_node*head){
    struct list_node*p=NULL;
    struct list_node*q=NULL;

    for(p=q=head;p!=NULL;p=q){
        q=p->next;
        free(p);
    }
}

int main(){

    //创建头节点
    struct list_node*head=init_list_node();
    printf("#\n");
    //头插法
    insert_list_head(head,10);
    insert_list_head(head,20);
    show_list_first(head);
    printf("#\n");
    //尾插法
    insert_list_last(head,30);
    insert_list_last(head,40);
    //从首部遍历
    show_list_first(head);
    printf("#\n");
    //从尾部遍历
    show_list_last(head);
    printf("#\n");
    //删除节点
    delete_list_node(head,20);
    show_list_first(head);
    //删除链表
    delete_list(head);


    return 0;
}

四、双向循环链表

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

struct list_node
{
    int num;
    struct list_node *next;
    struct list_node *prev;
};

struct list_node *init_list()
{
    struct list_node *head = malloc(sizeof(struct list_node));
    head->prev = head;
    head->next = head;
    return head;
}

void insert_list_head(struct list_node *head, int num)
{
    struct list_node *new = malloc(sizeof(struct list_node));
    new->num = num;
    new->next = NULL;
    new->prev = NULL;

    new->next = head->next;
    new->prev = head;
    head->next->prev = new;

    head->next = new;
}

void insert_list_last(struct list_node *head, int num)
{
    struct list_node *new = malloc(sizeof(struct list_node));
    new->num=num;
    new->next = head;
    new->prev = NULL;
    struct list_node *p = NULL;
    p = head->prev;
    p->next = new;
    new->prev = p;
    head->prev = new;
    
    
}

void show_list_head(struct list_node *head)
{
    struct list_node *p = NULL;
    for (p = head->next; p != head; p = p->next)
    {
        printf("%d\n", p->num);
    }
}

void show_list_last(struct list_node *head)
{
    struct list_node *p = NULL;
    for (p = head->prev; p != head; p = p->prev)
    {
        printf("%d\n", p->num);
    }
}

int delete_list_node(struct list_node *head, int num)
{
    struct list_node *p = NULL;
    struct list_node *q = NULL;

    for (q = head, p = head->next; p != head; q = p, p = p->next)
    {
        if (p->num == num)
        {
            q->next=p->next;
            p->next->prev=q;
            free(p);
            return 0;
        }
    }
    return -1;
}

void delete_list(struct list_node *head)
{
    struct list_node *p = NULL;
    struct list_node *q = NULL;
    for (p = q = head; p != head; p = q)
    {
        q = p->next;
        free(p);
    }
}

int main()
{
    //申请头指针
    struct list_node *head = init_list();
    //头插法
    insert_list_head(head, 10);
    insert_list_head(head, 30);
    show_list_head(head);
    printf("#\n");
    //尾插法
    insert_list_last(head, 20);
    insert_list_last(head, 40);
    //首部开始遍历
    show_list_head(head);
    printf("#\n");
    //尾部遍历
    show_list_last(head);
    printf("#\n");
    //删除节点
    delete_list_node(head, 20);
    show_list_last(head);

    printf("#\n");
    //删除链表
    delete_list(head);

    return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值