数据结构-----双向链表

一、双向循环列表

head.h

复制代码
#ifndef __head_h__                                                
#define __head_h__                                                
                                                                  
                                                                  
#include <stdio.h>                                                
#include <string.h>                                               
#include <stdlib.h>                                               
                                                                  
enum                                                              
{                                                                 
    SUCCESS,                                                      
    FALSE=-1                                                      
};                                                                
typedef char datatype;                                            
//双向列表结构体                                                  
typedef struct Node                                               
{                                                                 
    //存数据域                                                    
    datatype data;                                                
    //存上一个地址                                                
    struct Node *back;                                            
    //存下一个地址                                                
    struct Node *next;                                            
}*Doublelink;                                                     
//头插                                                            
Doublelink insert_head(datatype element,Doublelink head);         
//输出                                                            
void output_head(Doublelink head);                                
//头删                                                            
Doublelink delete_head_link(Doublelink head);                     
//尾插                                                            
Doublelink insert_back_link(Doublelink head,datatype element);    
//尾删                                                            
Doublelink delete_back_link(Doublelink head);                     
                                                                  
#endif                                                            

main.c

复制代码
#include "head.h"                             
                                              
int main(int argc, const char *argv[])        
{                                             
    Doublelink head=NULL;//定义双向列表头节点 
    int n;                                    
    printf("请输入n的值:");                  
    scanf("%d",&n);                           
    datatype element;                         
    for(int i=0;i<n;i++)                      
    {                                         
        printf("请输入%d个数的值:",i+1);     
        getchar();                            
        element=getchar();                    
        head=insert_head(element,head);       
    }                                         
    //输出                                    
    output_head(head);                        
    //头删                                    
    printf("头删后的结果为:\n");             
    head=delete_head_link(head);              
    output_head(head);                        
    //尾插                                    
    printf("请输入在末尾要插入的值:");       
    getchar();                                
    element=getchar();                        
    printf("尾插后的结果为:\n");             
    head=insert_back_link(head,element);      
    output_head(head);                        
    //尾删                                    
    printf("尾删后的结果为:\n");             
    head=delete_back_link(head);              
    output_head(head);                        
    return 0;                                 
}                                             

test.c

复制代码
#include "head.h"
//创建节点
Doublelink create_node()
{
        Doublelink s=(Doublelink)malloc(sizeof(struct Node));
        if(NULL==s)
                return NULL;
        //新节点的数据域初始化
        s->data=0;
        //指针域初始化
        s->next=s->back=s;
        return s;
}
//头插
Doublelink insert_head(datatype element,Doublelink head)
{
        //创建新节点
        Doublelink s=create_node();
        s->data=element;
        //链表为空
        if(head==NULL)
                head=s;
        //存在至少一个节点
        else
        {
                Doublelink rear=head->back;
                s->next=head;
                head->back=s;
                head=s;
                rear->next=head;
                head->back=rear;
        }
        return head;
}
//遍历(正向)
void output_head(Doublelink head)
{
        //链表为空
        if(head==NULL)
        {
                printf("输出失败\n");
                return;
        }
        else
        {
                Doublelink p=head;
                do
                {
                        printf("%c ",p->data);
                        p=p->next;
                }while(p!=head);
                putchar(10);
        }
}
//头删
Doublelink delete_head_link(Doublelink head)
{
        //链表为空
        if(head==NULL)
        {
                printf("删除失败\n");
                return head;
        }
        else
        {
                Doublelink p=head;
                head=head->next;
                head->back=p->back;
                p->back->next=head;
                free(p);
                p=NULL;
                return head;
        }
}

//尾插
Doublelink insert_back_link(Doublelink head,datatype element)
{
        Doublelink s=create_node();
        s->data=element;
        if(head==NULL)
                head=s;
        Doublelink p=head;
        while(p->next!=head)
        {
                p=p->next;
        }
        p->next=s;
        s->next=head;
        s->back=p;
        head->back=s;
        return head;
}


//尾删                                      
Doublelink delete_back_link(Doublelink head)
{
    //判断为空
    if(head==NULL)
    {
        printf("尾删失败\n");
        return head;
    }
    Doublelink p=head->back;
    Doublelink q=p->back;                   
    q->next=head;
    head->back=q;
    free(p);
    p=NULL;
    return head;
}   

二、双向链表的尾删

复制代码
//尾删                                 
void delete_back_link(Doublelink head) 
{                                      
    //判断为空                         
    if(head==NULL)                     
    {                                  
        printf("尾删失败\n");          
        return ;                       
    }                                  
    Doublelink p=head;                 
    if(p->next==NULL)                  
    {                                  
        head=delete_head_link(head);   
        return;                        
    }                                  
    while(p->next!=NULL)               
    {                                  
        p=p->next;                     
    }                                  
    p->back->next=NULL;                
    free(p);                           
    p=NULL;                            
    return ;                           
}                                      
相关推荐
刘马想放假14 小时前
Modbus 全栈技术解析:TCP、RTU、ASCII、RTU over TCP
数据结构·网络协议
北域码匠2 天前
冒泡排序太慢?鸡尾酒排序双向优化,原生 C# 零第三方库完整代码
数据结构·排序算法·泛型·c# 算法·鸡尾酒排序·原生 c# 开发·冒泡排序优化·嵌入式算法
Darling噜啦啦8 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
小小工匠9 天前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化
玖玥拾9 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
Qres8219 天前
算法复键——树状数组
数据结构·算法
牛油果子哥q10 天前
并查集(DSU)超精讲,路径压缩、按秩合并、万能模板、连通性判定、最小生成树与刷题实战全解
数据结构·c++·最小生成树·并查集
凌波粒10 天前
LeetCode--491.递增子序列(回溯算法)
数据结构·算法·leetcode
WL学习笔记10 天前
单项不带头不循环链表
数据结构·链表
小糯米60110 天前
JS 数组
数据结构·算法·排序算法