zoukankan      html  css  js  c++  java
  • 纯C语言实现循环双向链表创建,插入和删除

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef int ElemType;
    
    typedef struct DLNode{
        ElemType data;
        struct DLNode *next;
        struct DLNode *prior;
    }DLNode;
    
    DLNode *InitList(DLNode *DL);//初始化
    int ListEmpty(DLNode *DL);//判空
    int ListLength(DLNode *DL);//返回链表长度
    int ListInsert(DLNode *DL, int i, ElemType e);//插入元素
    int ListDelete(DLNode *DL, int i);//删除第i个元素
    void TraverseList(DLNode *DL);//遍历线性表
    
    //初始化
    DLNode* InitList(DLNode *DL){
        int x;
        DLNode *p = NULL;
        DLNode *r = NULL;
    
        DL = (DLNode *)malloc(sizeof(DLNode));
        DL->next = DL;
        DL->prior = DL;
        r = DL;
    
        printf("输入直到-1为止
    ");
        while(1){
            scanf("%d", &x);
            if(x == -1){
                printf("初始化成功
    ");
                break;
            }
            p = (DLNode *)malloc(sizeof(DLNode));
            if(p){
                p->data = x;
                p->prior = r;
                p->next = DL;
                r->next = p;
                DL->prior = p;
                r = p;
            }else{
                printf("空间不足初始化失败
    ");
                return NULL;
            }
    
        }
        return DL;
    
    }
    
    //判空
    int ListEmpty(DLNode *DL){
        return (DL->next == DL);
    }
    
    //插入元素
    int ListInsert(DLNode *DL, int i, ElemType e){
        if(i>ListLength(DL)+1 || i<=0){
            printf("插入位置有误,插入失败
    ");
            return 0;
        }
        DLNode *p = DL;
        int j = 0;
        while(j<i){
            p = p->next;
            j++;
        }
    
        DLNode *nDLNode = (DLNode *)malloc(sizeof(DLNode));
        nDLNode->data = e;
        nDLNode->prior = p->prior;
        p->prior->next = nDLNode;
        p->prior = nDLNode;
        nDLNode->next = p;
        printf("插入成功
    ");
        return 1;
    }
    
    //删除第i个元素
    int ListDelete(DLNode *DL, int i){
        if(i>ListLength(DL) || i<=0){
            printf("删除位置有误,插入失败
    ");
            return 0;
        }
        DLNode *p = DL;
        int j = 0;
        while(j<i){
            p = p->next;
            j++;
        }
        p->prior->next = p->next;
        p->next->prior = p->prior;
    
        free(p);
        printf("删除成功
    ");
        return 1;
    }
    
    
    //返回链表长度
    int ListLength(DLNode *DL){
        int len = 0;
        if(ListEmpty(DL)) return 0;
        DLNode *p = DL->next;
        while(p->data!=DL->data){
            len++;
            p = p->next;
        }
        return len;
    }
    
    //遍历线性表
    void TraverseList(DLNode *DL){
        if(ListEmpty(DL)){
            printf("空链表");
        }
        DLNode *p = DL->next;
        //终止循环遍历
        while(p->data != DL->data){
            printf("%d ", p->data);
            p = p->next;
        }
        printf("
    ");
    }
    
    
    int main(){
        ElemType e = NULL;
        DLNode *DL = NULL;
    
        //初始化测试
        DL = InitList(DL);
    
    //    //等价测试
    //    DLNode *d = DL->next->next;
    //    if(d->next->prior == d->prior->next){
    //        printf("d->next->prior == d->prior->next
    ");
    //    }
    //    if(d->next->prior == d){
    //        printf("d->next->prior == d
    ");
    //    }
    //    if(d == d->prior->next){
    //        printf("d == d->prior->next
    ");
    //    }
    
        //遍历测试
        TraverseList(DL);
    //
    //    printf("双向循环链表长度为%d
    ",ListLength(DL));
    
    
        //插入元素测试
        printf("第3个位置插入999
    ");
        ListInsert(DL, 3, 999);
        TraverseList(DL);
    //-----------------------------------------------------
        //非法操作?循环双向链表插入一个巨大的位置是否合法?
        //和老师讨论完,算不合法
        printf("第567位置插入999
    ");
        ListInsert(DL, 567, 999);
        TraverseList(DL);
    //------------------------------------------------------
        //删除元素测试
    //    printf("删除第1个位置
    ");
    //    ListDelete(DL, 1);
    //    TraverseList(DL);
    //------------------------------------------------------
        //非法操作?同上
        //新问题,1,2,3,4,-1,删除第5个是头节点。
        //和老师讨论完,算不合法
    //    printf("删除第55位置
    ");
    //    ListDelete(DL, 55);
    //    TraverseList(DL);
    //------------------------------------------------------
    
    
    }
    作者:PowerZZJ
    本博客文章大多为原创,转载请请在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    websocket协议
    vue组件之间的传值
    vue中非父子组件的传值bus的使用
    $.proxy的使用
    弹性盒模型display:flex
    箭头函数与普通函数的区别
    粘贴到Excel的图片总是有些轻微变形
    centos rhel 中文输入法的安装
    vi ,默认为 .asm .inc 采用nasm的语法高亮
    how-to-convert-ppk-key-to-openssh-key-under-linux
  • 原文地址:https://www.cnblogs.com/powerzzjcode/p/10896470.html
Copyright © 2011-2022 走看看