第二十三篇,单向双向循环链表模型。

一、传统链表模型?
详细参考:传统链表模型.jpg

单向链表:    节点中只有一个指针,最后一个节点指针域指向NULL。
单向循环链表:节点中只有一个指针,最后一个节点指针域指向头节点。
双向链表:    节点中有两个指针,头节点往前指向NULL,最后一个节点往后指向NULL。
双向循环链表:节点中有两个指针,头节点往前指向最后一个节点,最后一个节点往后指向头节点。

二、单向循环链表。
1、经过分析之后,单向循环链表只需要一个指针即可,所以节点模型与单向链表一致。

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

2、假设有一条单向循环链表,每一个节点都是存放int类型的数据,那么该链表的增删改查怎么写?

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

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

struct list_node *init_list_head()
{
    //1. 为头节点申请空间
    struct list_node *head = malloc(sizeof(struct list_node));
    if(head == NULL)
        printf("malloc error!\n");
    
    //2. 为头节点赋值。
    head->next = head;
    
    return head;
}

void list_add_tail(struct list_node *head,int num)
{
    //1. 为新节点申请空间
    struct list_node *new = malloc(sizeof(struct list_node));
    if(new == NULL)
        printf("malloc new error!\n");
    
    //2. 为新节点赋值
    new->data = num;
    new->next = head;
    
    //3. 寻找最后一个节点
    struct list_node *p = NULL;
    for(p=head;p->next!=head;p=p->next);
    
    //p->next == head
    //p现在就是指向最后一个节点
    
    //4. 让最后一个节点指向新节点
    p->next = new;

    return;
}

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

struct list_node *search_list_node(struct list_node *head,int num)
{
    struct list_node *p = NULL;
    for(p=head->next;p!=head;p=p->next)
    {
        if(p->data == num)
        {
            return p;
        }
    }
    return NULL;
}

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->data == 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=head;p->next!=head;p=p->next);
    p->next = NULL;
    
    for(p=q=head;p!=NULL;p=q)
    {
        q = p->next;
        free(p);
    }    
}

int main(int argc,char *argv[])
{
    //1. 初始化头节点
    struct list_node *head = NULL;
    head = init_list_head();
    
    //2. 尾插
    list_add_tail(head,10);
    list_add_tail(head,20);
    list_add_tail(head,30);
    list_add_tail(head,40);
    list_add_tail(head,50);
    
    //3. 遍历
    show_list_node(head);
    
    //4. 搜索
    struct list_node *p = search_list_node(head,30);
    if(p != NULL)
    {
        printf("p->data:%d\n",p->data);
    }
    
    //5. 删除节点
    delete_list_node(head,20);
    show_list_node(head);
    
    //6. 删除整条链表
    delete_list(head);
    
    return 0;
}

三、双向链表。
1、双向链表节点模型?
由于双向链表节点指针域有两个指针,所以节点模型应该这么写:
struct list_node{
    int data;  //数据域
    struct list_node *prev; //前驱指针
    struct list_node *next; //后继指针
};

2、假设有一条双向链表,里面每一个节点都是存储int类型的数据,请实现这条链表的增删改查?


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

struct list_node{
    int data;
    struct list_node *prev;  //前驱指针
    struct list_node *next;  //后继指针
};

struct list_node *init_list_head()
{
    //1. 为头节点申请空间
    struct list_node *head = malloc(sizeof(struct list_node));
    if(head == NULL)
        printf("malloc head error!\n");
    
    //2. 为头节点赋值
    head->prev = NULL;
    head->next = NULL;
    
    //3. 将头节点返回。
    return head;
}

void list_add_tail(struct list_node *head,int num)
{
    //1. 为新节点申请空间
    struct list_node *new = malloc(sizeof(struct list_node));
    if(new == NULL)
        printf("malloc new error!\n");
    
    //2. 为新节点赋值
    new->data = num;
    new->next = NULL;
    
    //3. 寻找最后一个节点
    struct list_node *p = NULL;
    for(p=head;p->next!=NULL;p=p->next);
    
    //4. 让最后一个节点后继指针指向新节点
    //让新节点前驱指针指向最后一个节点
    p->next = new;
    new->prev = p;
    
    return;
}

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

void backward_show_list_node(struct list_node *head)
{
    struct list_node *p = NULL;
    for(p=head;p->next!=NULL;p=p->next);  //p现在就是指向最后一个节点
    
    for(;p!=head;p=p->prev)
    {
        printf("p->data:%d\n",p->data);
    }
    return;
}

void list_add_head(struct list_node *head,int num)
{
    //1. 为新节点申请空间
    struct list_node *new = malloc(sizeof(struct list_node));
    if(new == NULL)
        printf("malloc new error!\n");
    
    //2. 为新节点赋值
    new->data = num;
    
    new->next = head->next;
    new->prev = head;
    
    if(head->next!=NULL)  //代表后面有人
    {
        head->next->prev = new; //就让后面那个人指向我
    }
    
    head->next = new;
    
    return;
}

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->data == num)
        {
            q->next = p->next;
            
            if(p->next != NULL)
            {
                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!=NULL;p=q)
    {
        q = p->next;
        free(p);
    }    
}


int main(int argc,char *argv[])
{
    //1. 初始化头节点
    struct list_node *head = NULL;
    head = init_list_head();
    
    //2. 尾插
    list_add_tail(head,10);
    list_add_tail(head,20);
    list_add_tail(head,30);
    list_add_tail(head,40);
    
    //3. 头插
    list_add_head(head,8);
    list_add_head(head,5);
    
    //4. 遍历
    forward_show_list_node(head); //往后遍历
    printf("-------------------\n");
    backward_show_list_node(head); //往前遍历
    
    //5. 删除节点
    delete_list_node(head,20);
    
    forward_show_list_node(head); //往后遍历
    printf("-------------------\n");
    backward_show_list_node(head); //往前遍历
    
    //6. 删除整条链表。
    delete_list(head);  //双向链表删除整条链表与单向链表一致。

    return 0;
}

1、使用链表存储以下的数据,并按要求输出想要的内容。使用双向链表来实现。
     书名           总页数      封面颜色
-------------------------------------------
   C    language     428        red
   C++  language     352        yellow
   java language     569        blue
   C#   language     245        red
-------------------------------------------

  要求执行程序之后,显示打印第一本书的信息,这时候如果输入next,就打印下一本书的信息,如果输入prev,就打印上一本书的信息,如果输入quit,则程序结束。


2、在开发板的根目录下有一个目录叫bmp_dir,里面全部都是800*480的bmp格式图片,但是里面的图片有多少张,名字叫什么,我们不知道。
要求使用链表来存储图片的名字,实现当程序执行之后,先显示第一张图片,点击屏幕左边区域松手,就显示上一张,点击屏幕右边就显示下一张,点击屏幕中间,就显示全品黑色退出程序。使用双向链表来实现。


四、双向循环链表。
1、双向循环链表节点模型?
与双向链表一致。
struct list_node{
    int data;
    struct list_node *prev;
    struct list_node *next;
};

2、假设有一条链表,里面每一个节点都是存储int类型的数据,实现增删改查。

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

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

struct list_node *init_list_head()
{
    //1. 为头节点申请空间
    struct list_node *head = malloc(sizeof(struct list_node));
    if(head == NULL)
        printf("malloc head error!\n");
    
    //2. 为头节点赋值
    head->prev = head;
    head->next = head;
    
    //3. 将头节点返回
    return head;
}

void list_add_tail(struct list_node *head,int num)
{
    //1. 为新节点申请空间
    struct list_node *new = malloc(sizeof(struct list_node));
    if(new == NULL)
        printf("malloc new error!\n");
    
    //2. 为新节点赋值
    new->data = num;
    new->next = head;
    
    //3. 寻找最后一个节点
    struct list_node *p = head->prev;
    
    p->next = new;
    new->prev = p;
    head->prev = new;
    
    return;
}

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

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

void list_add_head(struct list_node *head,int num)
{
    //1. 为新节点申请空间
    struct list_node *new = malloc(sizeof(struct list_node));
    if(new == NULL)
        printf("malloc new error!\n");
    
    //2. 为新节点赋值
    new->data = num;
    new->next = head->next;
    new->prev = head;
    head->next->prev = new;
    head->next = new;

    return;
}

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->data == 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;
    head->prev->next = NULL;
    
    for(p=q=head;p!=NULL;p=q)
    {
        q = p->next;
        free(p);
    }
    
    return;
}

int main(int argc,char *argv[])
{
    //1. 初始化头节点
    struct list_node *head = NULL;
    head = init_list_head();
    
    //2. 尾插
    list_add_tail(head,10);
    list_add_tail(head,20);
    list_add_tail(head,30);
    list_add_tail(head,40);
    
    //3. 头插
    list_add_head(head,8);
    list_add_head(head,5);
    
    //4. 遍历
    forward_show_list(head);
    printf("--------------------\n");
    backward_show_list(head);
    
    //5. 删除节点
    delete_list_node(head,30);
    
    printf("=============================================\n");
    //4. 遍历
    forward_show_list(head);
    printf("--------------------\n");
    backward_show_list(head);
    
    //6. 删除整条链表
    delete_list(head);
    
    return 0;
}

   练习: 使用双向循环链表来做第2题。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肖爱Kun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值