C语言|双向链表

这篇博客展示了如何使用C语言实现链表,通过输入值查找链表中特定节点,并输出其前一个和后一个节点的数据。涉及到节点定位和链表遍历技巧。

 

#include<stdio.h>
struct node{
    int data;
    struct node *next;
    struct node *prior;
};
int main()
{
    int n,m,k,i,j,h;
    struct node *head,*tail,*p,*q;
    scanf("%d%d",&n,&m);
    head=(struct node*)malloc(sizeof(struct node));
    head->next=NULL;
    tail=head;
    scanf("%d",&tail->data);
    for(i=1;i<n;i++)
    {
        p=(struct node*)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        tail->next=p;
        p->prior=tail;
        tail=p;
    }
    while(m--)
    {
        scanf("%d",&k);
        tail=head;
        for(p=tail;p!=NULL;p=p->next)
        {
            if(p->data==k&&p->next!=NULL&&p->prior!=NULL)
                printf("%d %d\n",p->prior->data,p->next->data);
            else if(p->data==k&&p->next==NULL&&p->prior!=NULL)
                printf("%d\n",p->prior->data);
            else if(p->data==k&&p->next!=NULL&&p->prior==NULL)
                printf("%d\n",p->next->data);
        }
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值