zoukankan      html  css  js  c++  java
  • 双向链表的删除

    删除表中第i个元素

    • 头结点及尾结点指针域的变化
    • 查找过程中循环条件的变化
    • 删除元素过程中的指针运算

    如图:

    程序;

    #include<stdio.h>
    #include<stdlib.h>
    #define OK 1
    #define ERROR 0
    #define OVERFLOW 0
    typedef struct DuLNode{
            int data;
            struct DuLNode *prior;
            struct DuLNode *next;
    }DuLNode,*DuLinkList;
    //建立一个只含头结点的空双向循环链表
    int InitList_DuL(DuLinkList &L){
            L=(DuLinkList)malloc(sizeof(DuLNode));
            if(!L){
                    exit(OVERFLOW);
            }
            L->prior=L;
            L->next=L;
            return OK;
    }
    int CreateList_DuL(DuLinkList &L,int n){
            DuLinkList p,q;
            int i;
            printf("Input the datas:");
            q=L;
            for(i=0;i<n;i++){
                    p=(DuLinkList)malloc(sizeof(DuLNode));
                    scanf("%d",&p->data);
                    p->next=q->next;
    //              p->next=L->next;
                    q->next=p;
                    p->prior=q;
                    L->prior=p;
                    q=p;
            }
                    return OK;

    }
    int ListDelete_DuL(DuLinkList &L,int i,int &e){  //删除表中第i个元素,由变量e返回其值
            DuLinkList p;
            int j=1;
            p=L->next;
    while(p!=L&&j<i){
                    p=p->next;//查找第i个节点
                    ++j;
            }
            while(p==L||j>i){
                    return ERROR;
            }
            e=p->data;
            p->prior->next=p->next;
            p->next->prior=p->prior;
            free(p);
            return OK;


    }
    int TraverseList_DuL(DuLinkList L){//遍历
            DuLinkList p;
            p=L->next;
            while(p!=L){
                    printf("%d",p->data);
                    p=p->next;
            }
            return OK;
    }
    main(){
            int i,n,e;
            DuLinkList L;
            InitList_DuL(L);
            printf("Input the length of the list L:");
            scanf("%d",&n);
            CreateList_DuL(L,n);
            printf("Input the delete location:");
            scanf("%d",&i);
            if(ListDelete_DuL(L,i,e)){
                    printf("Output the datas:");
                    TraverseList_DuL(L);
            }else{
                    printf("Can't delete the data!");
       }
            printf(" ");
    }
    结果:
    android@android-Latitude-E4300:~/work/c/doublelianbiao$ ./listdelete
    Input the length of the list L:5
    Input the datas:1 3 5 7 9
    Input the delete location:2
    Output the datas:1579



  • 相关阅读:
    HADOOP_HIVE安装和配置
    LInux__增加交换空间(SWAP)的大小
    ORACLE__Linux下Oracle数据库的卸载、删除
    HADOOP__HBASE集群安装(自带ZOOKEEPER)
    HADOOP__PIG安装与配置
    HADOOP__HADOOP基础安装和配置
    PYTHON__Thread达到上限的解决方案(设置线程上限)
    MYSQL__MYSQL的一些基础设置
    Android Studio Unable to access Android SDK addon list Mac
    dmg 文件打不开,双击没反应
  • 原文地址:https://www.cnblogs.com/shamoguzhou/p/6920658.html
Copyright © 2011-2022 走看看