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

本文详细介绍了双向链表的构建过程,包括节点的创建、链接以及如何通过给定的关键字查找节点并输出其前后节点的关键字。通过具体代码示例,展示了双向链表相较于单向链表的优势,即能够从前向后或从后向前遍历链表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Time Limit: 1000 ms Memory Limit: 65536 KiB


Problem 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


Sample Output

2 4
4 6
9


Hint
Source


对于双向链表,只要对单向链表理解了,双向链表便是十分好理解,只要在定义节点形式时定义两个指针,一个指向后一个节点,一个指向前一个节点
代码一:

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int a;
    struct node *next,*back;
};


struct node *create(int n);
//建立链表;
void sreatch(struct node *head,int key);
//链表查找;



int main()
{
    int n,m,i;
    struct node *head;
    scanf("%d%d",&n,&m);
//定义变量,及输入数据;

    head = create(n);
//建立链表;

    for (i = 0; i < m; i++)
        //进行m次查找;
    {
        int key;
        scanf("%d",&key);
        sreatch(head,key);
    }
    return 0;
}



struct node *create(int n)
//建立链表;
{
    struct node *head,*p,*q;
    head = (struct node *)malloc(sizeof(struct node));
    head->next = NULL;
    head->back = NULL;
    //建立头结点;
    q = head;
    while (n--)
    {
        p = (struct node *)malloc(sizeof(struct node));
        p->next = NULL;
        p->back = q;
        scanf("%d",&p->a);
        //建立新节点;
        q->next = p;
        q = p;
    }
    return head;
};



void sreatch(struct node *head,int key)
//链表查找;
{
    struct node *qi;
    qi = head->next;
    while (qi)
        //查找结束条件;
    {
        if (qi->a == key)
        //如果找到,输出分三种情况;
        {
            if(qi->back == head)
            //如果是链表第一个存放数据节点;
            {
                printf("%d\n",qi->next->a);
            }
            else if (qi->next == NULL)
            //如果树尾节点;
            {
                printf("%d\n",qi->back->a);
            }
            else
            //中间节点;
            {
                printf("%d %d\n",qi->back->a,qi->next->a);
            }
            break;//找到后结束继续查找;
        }
        else
        {
            qi = qi->next;
        }
    }
}

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int a;
    struct node *next,*front;
};
//自定义三个函数;
struct node *creat(int n);//链表建立函数;
struct node *sreach(struct node *hear,int key);//关键字查找函数;
void display(struct node *head,struct node *p);//结果输出函数;
int main()
{
    int n,m;
    int key;
    struct node *head,*p;
    scanf("%d%d",&n,&m);
    head = creat(n);//建立双向链表;
    while(m--)
    {
        scanf("%d",&key);
        p = sreach(head,key);
        display(head,p);
    }

    return 0;
}
struct node * creat(int n)
//建立双向链表;
{

    struct node *head,*p,*q;
    head = (struct node*)malloc(sizeof(struct node));
    head -> next = NULL;
    head -> front = NULL;
    //要将新建立的节点指向前一个和指向后一个的指针全赋值为空
    q = head;
    while(n--)
    {
        p = (struct node*)malloc(sizeof(struct node));
        p -> next = NULL;
        p -> front = NULL;
        scanf("%d",&p->a);
        q -> next = p;
        p -> front = q;//建立双向链表,比建立单项链表主要多了这条语句;
        q = p;
    }
    return(head);
};
struct node *sreach(struct node *head,int key)
//查找函数十分简单,找到关键词返回该指针,否则指向下一个节点;
{
    struct node *p = head -> next;
    while(p)
    {
        if(p->a==key)
            return (p);
        else p = p -> next;
    }
    return 0 ;
};
void display(struct node *head,struct node *p)
{//输出时要注意分三种情况,查找的节点是第一个、最后一个还有中间个;
    if(p->next&&p->front!=head)
        printf("%d %d\n",p->front->a,p->next->a);
    else if(p->next)
        printf("%d\n",p->next->a);
    else if(p->front!=head)
        printf("%d\n",p->front->a);
}

代码二:

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int a;
    struct node *next,*last; //两个指针;

};
int main()
{
    int n,m,x,y;
    int i;
    struct node *head,*p,*q;
    head = (struct node *)malloc(sizeof(struct node));
    head -> next =NULL;
    head -> last =NULL;
    q = head;
    scanf("%d%d",&n,&m);
    for(i=0; i<n; i++)
    {
        scanf("%d",&x);
        p = (struct node *)malloc(sizeof(struct node));
        p -> a = x;
        p -> next =NULL;
        p -> last = q; 
        q -> next = p;
        q = p;

    }

    for(i=0; i<m; i++)
    {
        scanf("%d",&y);
        q = head -> next;
        while(q)  //查找节点;
        {
            if((q -> a) == y)  
            {
                if((q!= head -> next)&&q -> next) //如果查找的节点为中间的节点;
                    printf("%d %d\n",(q -> last) -> a,(q -> next) ->a); //输出该节点前后的节点;
                else
                {
                    if(q == head -> next)   //如果查找的节点为的一个节点;
                        printf("%d\n",(q->next)->a);
                   else if(q -> next==NULL)  //如果查找的节点为最后一个节点;
                        printf("%d\n",(q->last)->a);
                }
            }
            q = q -> next;
        }


    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

碧羽o(* ̄▽ ̄*)ブ回雪

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

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

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

打赏作者

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

抵扣说明:

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

余额充值