数据结构-链表2(基本操作)

前言

hi,这一次继上一次的链表建立(详细内容如下)数据结构-链表1(简单建立)-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/2401_86597463/article/details/141201506

再介绍双向链表的使用

还有做几个链表的基本操作~

1.双向链表的建立

双向链表的概述

与单向链表的区别:有两个指针,分别指向其前驱和后继

问题

学会了单向链表,我们又多了一种解决问题的能力,单链表利用一个指针就能在内存中找到下一个位置,这是一个不会轻易断裂的链。但单链表有一个弱点——不能回指。比如在链表中有两个节点A,BA,B,他们的关系是BB是AA的后继,AA指向了BB,便能轻易经AA找到B,但从BB却不能找到AA。一个简单的想法便能轻易解决这个问题——建立双向链表。在双向链表中,AA有一个指针指向了节点BB,同时,BB又有一个指向AA的指针。这样不仅能从链表头节点的位置遍历整个链表所有节点,也能从链表尾节点开始遍历所有节点。对于给定的一列数据,按照给定的顺序建立双向链表,按照关键字找到相应节点,输出此节点的前驱节点关键字及后继节点关键字。

样例

输入

10 3
1 2 3 4 5 6 7 8 9 0
3
5
0

输出

2 4
4 6
9

思路

同单向链表,增加一个指针就行啦

代码实现

节点定义:

#include<bits/stdc++.h>
using namespace std;
struct node{//节点定义
    int data;
    struct node *llink,*rlink;//前驱和后继指针
};

基本信息定义 

int main(){
    int n,m,i;
    int num;
    struct node *head,*tail,*temp;
    cin>>n>>m;
    head=(struct node *)malloc(sizeof(struct node));
    head->llink=head->rlink=NULL;

​

 输入并统计数据、输出: 

​
    for(i=0;i<n;i++){
        cin>>num;
        temp=(struct node *)malloc(sizeof(struct node));
        temp->data=num;
        temp->rlink=NULL;
        temp->llink=tail;
        tail->rlink=temp;
        tail=temp;
    }
    for(i=0;i<m;i++){
        cin>>num;
        int f=1;
        temp=head->rlink;
        while(temp!=NULL){
            if(temp->data==num) break;
            else temp=temp->rlink;
        }
        if(temp->llink!=head){
            cout<<temp->llink->data;
            f=0;
        }
        if(temp->rlink!=NULL){
            if(flag==0) cout<<" "<<temp->rlink->data;
            else cout<<temp->rlink->data
        cout<<endl;
    }
    return 0;
}

​

2.插入

方法

将插入元素的前驱指向插入元素,再将插入元素指向插入元素的后继

伪代码

//l,r分别为p的前驱、后继
//p是要插入的节点
//next是指针域
l.next=p;
p.next=r;

3.删除

方法

将删除元素的前驱和后继指针域隔开,再将前驱指向后继

伪代码

//l,r分别为p的前驱、后继
//p是要删除的节点
//next是指针域
l.next=p.next=NULL;
l.next->r;

4.逆置

问题

输入多个整数,以-1作为结束标志,顺序建立一个带头结点的单链表,之后对该单链表的数据进行逆置,并输出逆置后的单链表数据。

样例

输入

12 56 4 6 55 15 33 62 -1

输出

62 33 15 55 6 4 56 12

思路

先顺序建立链表,再逆置

逆置方法:

将头节点断开,再将节点用头插法重新逆置

代码实现

基本代码:

#include<bits/stdc++.h>
using namespace std;
struct node{ //节点定义
    int data;
    struct node *next;
};
int main(){
    int num;
    struct node *head,*tail,*temp;
    temp=(struct node *)malloc(sizeof(struct node);
    temp->next=NULL;
    head=tail=temp;
    //数据保存
    while(cin>>num){
        if(num==-1) break;
        temp=(struct node *)malloc(sizeof(struct node);
        temp->data=num;
        temp->next=NULL;
        tail->next=temp;
        tail=temp;
    }
    //逆置(下一篇代码)
    return 0;
}

逆置代码:

//逆置
tail=head->next;
head->next=NULL;
temp=tail;
while(temp!=NULL){
    tail=tail->next;
    temp->next=head->next;
    head->next=temp;
    temp=tail;
}
//输出
temp=head->next;
while(temp!=NULL){
    cout<<temp->data;
    temp=temp->next;
}

下一篇-栈

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值