线性表---4双向链表

@Adrian
在这里插入图片描述
在这里插入图片描述
1、指针域:用于指向当前节点的直接前驱节点;
2、数据域:用于存储数据元素。
3、指针域:用于指向当前节点的直接后继节点;

双链表中对数据进行 “增删查改” 操作的完整实现代码

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

//双向链表
typedef struct line{
    struct line*prior;
    int data;
    struct line*next;
}line;
//双向链表的创建
line* initLine(line*head){
    head=(line*)malloc(sizeof(line));//创建链表第一个结点(首元结点)
    head->prior=NULL;
    head->next=NULL;
    head->data=1;
    line*list=head;
    for(int i = 2;i<=5; i++){
        //创建并初始化一个新结点
        line*body=(line*)malloc(sizeof(line));
        body->prior=NULL;
        body->next=NULL;
        body->data=i;

        list->next=body;//直接前驱结点的next指针指向新结点
        body->prior=list;//新结点指向直接的前驱结点
        list=list->next;
    }
    return head;
}
//输出双链表的实现函数
void display(line*head){
    line * temp=head;
    while(temp){
        //如果该结点无后继结点,说明此结点是链表的最后一个结点
        if(temp->next==NULL){
            printf("%d\n",temp->data);
        }else{
            printf("%d <-> ",temp->data);
        }
        temp=temp->next;
    }
}
//双向链表添加结点
line*insertLine(line*head,int data,int add){
    //新建数据域为data的结点
    line * temp=(line*)malloc(sizeof(line));
    temp->data=data;
    temp->prior=NULL;
    temp->next=NULL;
    //插入到链表头,要特殊考虑
    if(add==1){
        temp->next=head;
        head->prior=temp;
        head=temp;//头结点换成temp
    }else{
        line*body=head;
        //找到要插入的前一个结点
        for(int i=1;i<add-1;i++){
            body=body->next;
        }
        //判断条件为真,说明插入的位置为链表尾
        if(body->next==NULL){
            body->next=temp;
            temp->prior=body;
        }else{
            body->next->prior=temp;
            temp->next=body->next;
            temp->prior=body;
            body->next=temp;
        }
    }
    return head;
}

//双向链表删除结点
//data要为删除结点的数据域的值
line * delLine(line*head,int data){
    line*temp=head;
    //遍历链表
    while(temp){
        //判断当前结点中数据域和data是否相等,若相等,摘除该结点
        if(temp->data==data){
            temp->prior->next=temp->next;
            temp->next->prior=temp->prior;
            free(temp);
            return head;
        }
        temp=temp->next;
    }
    printf("链表中无该数据元素");
    return head;
}

//双向链表查找结点
//head为原双链表,elem表示被查找的元素
int selectElem(line*head,int elem){
    //新建一个指针t,初始化为头指针head
    line*t=head;
    int i=1;
    while(t){//t不为空就继续
        if(t->data==elem){
            return i;
        }
        i++;
        t=t->next;
    }
    //程序执行至此处,表示查找失败
    return -1;
}
//双链表更改结点
//更新函数,其中,add表示更改结点在双链表中的位置,newElem为新的数据值
line*amendElem(line*p,int add,int newElem){
    line*temp=p;
    //遍历到被更改的结点
    for(int i=1;i<add;i++){
        temp=temp->next;
    }
    temp->data=newElem;
    return p;


}
int main()
{
    //创建一个头指针
    line * head=NULL;
    //调用链表的创建函数
    head=initLine(head);
    //输出创建好的链表
    display(head);
    //显示双链表的优点
    printf("链表中第4个结点的直接前驱是:%d\n",head->next->next->next->prior->data);

    printf("---------------------------------------------------\n");

    //在表中第三个位置插入元素7;
    head=insertLine(head,7,3);
    printf("表中第三个位置插入元素7\n");
    display(head);

    printf("---------------------------------------------------\n");

    //表中删除元素2
    head=delLine(head,2);
    printf("表中删除元素2\n");
    display(head);

    printf("---------------------------------------------------\n");

    printf("元素3的位置是:%d\n",selectElem(head,3));

    printf("---------------------------------------------------\n");
    //表中第3个结点中的数据改为存储6
    head=amendElem(head,3,6);
    printf("表中第3个结点中的数据改为存储6\n");
    display(head);
    return 0;
}

输出结果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值