前言
hi,这一次继上一次的链表建立(详细内容如下)数据结构-链表1(简单建立)-CSDN博客https://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