目录
1:双向链表的介绍
双向链表与单链表是一样的,只不过在定义时多了一个可以向前的指针,单链表只有next指向后面的指针,双向既有指向前方也有指向后方。
typedef struct node{
int data;
struct node *next;//后指针
struct node *prior;//前指针
}node;
2:双向链表的定义以及建立
(1)尾插法建立
node* weicha(node *L)
{
node *s,*t;
L=(node*)malloc(sizeof(node));
L->next=NULL;
s=L;
int x;
char ch;
printf("请输入数据:");
scanf("%d",&x);
ch=getchar();
while(ch!=10){
t=(node*)malloc(sizeof(node));
t->data=x;
t->prior=s;
t->next=NULL;
s->next=t;
s=t;//
scanf("%d",&x);
ch=getchar();
}
t=(node*)malloc(sizeof(node));
t->data=x;
t->next=NULL;
t->prior=s;
s->next=t;
s=t;
s->next=NULL;
return L;
}
(2)头插法建立
node* toucha(node *L)
{
node *s,*r;
L=(node*)malloc(sizeof(node));
L->next=NULL;
int x;
char ch;
printf("请输入数据:");
scanf("%d",&x);
ch=getchar();
while(ch!=10){
s=(node*)malloc(sizeof(node));
s->data=x;
s->next=L->next;
s->prior=NULL;
L->prior=s;
L->next=s;
scanf("%d",&x);
ch=getchar();
}
s=(node*)malloc(sizeof(node));
s->data=x;
s->next=L->next;
s->prior=NULL;
L->prior=s;
L->next=s;
return L;
}
3:双向链表的操作
(1)删除某个值为x的元素
单链表无法删除指定的元素,是因为要删除当前元素,则需要用到该元素的前面一个指针,修改它的next值,单链表如果这样做,通过一个计数器可以获得该位置,可是当删除的元素很多时,这样子的复杂度就很大,所以双向链表用这个很方便。
//删除值为x的元素
void del(int x,node *L)
{
node *s=L->next;
while(s){
if(s->data==x){
s->prior->next=s->next;
}
s=s->next;
}
}
(2)删除pos位置的元素
//删除位置pos的元素
void shanchu(node *L,int pos)
{
int sum=0;
node *s=L;
while(sum<pos){
sum++;
s=s->next;
}
s->prior->next=s->next;
s->next->prior=s->prior;
}
(3)在位置pos插入值为x的元素
//在pos位置插入值为x的元素
void inser(int pos,int x,node *L)
{
node *s=L->next;
int sum=1;
while(sum<pos-1){
sum++;
s=s->next;
}
node *t;//插入则要新创建一个空间
t=(node*)malloc(sizeof(node));
t->data=x;
t->next=s->next;
t->prior=s;
s->next=t;
t->next->prior=t;
}
4:完整代码演示
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
struct node *prior;
}node;
node* toucha(node *L)
{
node *s,*r;
L=(node*)malloc(sizeof(node));
L->next=NULL;
int x;
char ch;
printf("请输入数据:");
scanf("%d",&x);
ch=getchar();
while(ch!=10){
s=(node*)malloc(sizeof(node));
s->data=x;
s->next=L->next;
s->prior=NULL;
L->prior=s;
L->next=s;
scanf("%d",&x);
ch=getchar();
}
s=(node*)malloc(sizeof(node));
s->data=x;
s->next=L->next;
s->prior=NULL;
L->prior=s;
L->next=s;
return L;
}
//尾插建立
node* weicha(node *L)
{
node *s,*t;
L=(node*)malloc(sizeof(node));
L->next=NULL;
s=L;
int x;
char ch;
printf("请输入数据:");
scanf("%d",&x);
ch=getchar();
while(ch!=10){
t=(node*)malloc(sizeof(node));
t->data=x;
t->prior=s;
t->next=NULL;
s->next=t;
s=t;//
scanf("%d",&x);
ch=getchar();
}
t=(node*)malloc(sizeof(node));
t->data=x;
t->next=NULL;
t->prior=s;
s->next=t;
s=t;
s->next=NULL;
return L;
}
//遍历
//删除值为x的元素
void del(int x,node *L)
{
node *s=L->next;
while(s){
if(s->data==x){
s->prior->next=s->next;
}
s=s->next;
}
}
//删除位置pos的元素
void shanchu(node *L,int pos)
{
int sum=0;
node *s=L;
while(sum<pos){
sum++;
s=s->next;
}
s->prior->next=s->next;
s->next->prior=s->prior;
}
void shuchu(node *L)
{
node *s=L->next;
while(s){
printf("%d ",s->data);
s=s->next;
}
printf("\n");
}
//在pos位置插入值为x的元素
void inser(int pos,int x,node *L)
{
node *s=L->next;
int sum=1;
while(sum<pos-1){
sum++;
s=s->next;
}
node *t;//插入则要新创建一个空间
t=(node*)malloc(sizeof(node));
t->data=x;
t->next=s->next;
t->prior=s;
s->next=t;
t->next->prior=t;
}
int main()
{
node *L;
L=weicha(L);
shuchu(L);
shanchu(L,3);
shuchu(L);
return 0;
}