双向链表的增删(插入,删除)

先创建双向链表1,2,3:

 在第二节点的位置插入数据域为4的新节点:

插入的步骤如图顺序来插入

 删除节点和单链表操作相似

代码如下:

#include <stdio.h>
#include <stdlib.h>
typedef struct line{
    struct line * prior;		//前驱 
    int data;					//数据域 
    struct line * next;			//后驱 
}line;							//line替代了struct line 

line* initLine(line * head);							//因为main函数写在前,所以提前声明函数 
line * insertLine(line * head,int data,int add);
line * delLine(line * head,int data);
void display(line * head);	
int main() {
    line * head=NULL;									//创建头结点 
    head=initLine(head);								//创建双向链表 1 , 2 , 3 
    head=insertLine(head, 4, 2);						//在第二个节点插入新节点,数据域值是4 
    display(head);										//打印表  
    head=delLine(head, 2);								//删除数据域是 2 的节点 
    display(head);										//打印表  
   
    return 0;
}
line* initLine(line * head){
	int i;
    head=(line*)malloc(sizeof(line));				//分配动态内存 
    head->prior=NULL;								//前驱指向空 
    head->next=NULL;								//后驱指向空 
    head->data=1;									//头结点的数据域是 1  
    line * list=head;								//头结点插入链表 
    for (i=2; i<=3; i++) {							//创建属于是 2 3 的两个新节点 
        line * body=(line*)malloc(sizeof(line));	//创建个body节点用来带入下个节点 
        body->prior=NULL;
        body->next=NULL;
        body->data=i;
       
        list->next=body;							//插入操作 
        body->prior=list;
        list=list->next;
    }
    return head;									//返回链表 
}
line * insertLine(line * head,int data,int add){	//插入节点 
	int i;
    //新建数据域为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;									//新节点代替头结点 
    }else{
        line * body=head;
        //找到要插入位置的前一个结点
        for (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;
            body->next=temp;
            temp->prior=body;
        }
    }
    return head;									//返回链表 
}

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;
}
//输出链表的功能函数
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;							//往后遍历替代 
    }
}

运行结果如下:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wiyoo0

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

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

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

打赏作者

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

抵扣说明:

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

余额充值