双向链表

数据结构实验之链表九:双向链表

Description

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

第一行两个正整数n(代表节点个数),m(代表要找的关键字的个数)。第二行是n个数(n个数没有重复),利用这n个数建立双向链表。接下来有m个关键字,每个占一行。
Output

对给定的每个关键字,输出此关键字前驱节点关键字和后继节点关键字。如果给定的关键字没有前驱或者后继,则不输出。
注意:每个给定关键字的输出占一行。
一行输出的数据之间有一个空格,行首、行末无空格。

Sample
Input

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

Output

2 4
4 6
9

一、

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;//数据域
    struct node *prior;//前驱指针
    struct node *next;//后继指针
};
int main()
{
    int n,m,i;
    scanf("%d %d",&n,&m);//链表元素个数,关键字个数
    struct node *head,*tail,*p;
    head=(struct node *)malloc(sizeof(struct node));
    head->next=NULL;
    tail=head;
    for(i=0;i<n;i++)
    {
        p=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next=NULL;
        tail->next=p;
        p->prior=tail;
        tail=p;
    }
    while(m>0)
    {
        int x;//关键字
        scanf("%d",&x);
        p=head->next;
        while(p)
        {
            if(p->data==x)
                break;//找到关键字
            p=p->next;
        }
        if(p->next!=NULL&&p->prior!=head)//这里的前驱指针必须不等于head,而不能不等于NULL,
            printf("%d %d\n",p->prior->data,p->next->data);
        else if(p->prior!=head)//前驱指针不等于head,后继指针等于NULL
            printf("%d\n",p->prior->data);
        else//前驱指针等于head,后继指针不等于NULL
            printf("%d\n",p->next->data);
        m--;
    }
    return 0;
}


二、

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;//后继指针域
    struct node *front;//前驱指针域
};
struct node *creatseq(int n);//顺序建立双向链表
void displaynode(struct node *head,struct node *p);//显示链表head中指定节点p的前驱和后继
struct node *search(struct node *head,int key);//查找关键字为key的结点,返回指向该结点的指针
int main()
{
    struct node *head,*p;
    int i,n,m,key;
    scanf("%d %d",&n,&m);
    head=creatseq(n);
    for(i=0;i<m;i++)
    {
        scanf("%d",&key);
        p=search(head,key);
        displaynode(head,p);
    }
    return 0;
}
struct node *creatseq(int n)
{
    struct node *head,*p,*tail;
    int i;
    head=(struct node *)malloc(sizeof(struct node));
    head->next=NULL;
    head->front=NULL;//处理前驱结点
    tail=head;
    for(i=0;i<n;i++)
    {
        p=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next=NULL;
        p->front=tail;//处理前驱结点
        tail->next=p;
        tail=p;
    }
    return head;
};
struct node *search(struct node *head,int key)
{
    struct node *p;
    p=head->next;
    while(p)//遍历链表
    {
        if(p->data==key)//找到key所在结点
        {
            return p;//返回key所在结点指针
        }
        else
        {
            p=p->next;//否则遍历下一个结点
        }
    }
    return p;
};
void displaynode(struct node *head,struct node *p)
{
    if(p->next&&p->front!=head)//同时有前驱和后继
    {
        printf("%d %d\n",p->front->data,p->next->data);
    }
    else if(p->next)//只有后继
    {
        printf("%d\n",p->next->data);
    }
    else if(p->front!=head)//只有前驱
    {
        printf("%d\n",p->front->data);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值