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

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *last, *next;
};
int main()
{
    int n, i, m, k;
    scanf("%d %d", &n, &m);
    struct node *h, *q, *p;
    h = (struct node *)malloc(sizeof(struct node ));
    h -> next = NULL;
    h -> last = NULL;
    scanf("%d", &h -> data);
    q = h;
    n--;
    for(i = 0; i < n; i++)
    {
        p = (struct node *)malloc(sizeof(struct node ));
        p -> next = NULL;
        p -> last = NULL;
        scanf("%d", &p -> data);
        q -> next = p;
        p -> last = q;
        q = p;
    }
    for(i = 0; i < m; i++)
    {
        scanf("%d", &k);
        p = h;
        while(p)
        {
            if(p -> data == k)break;
            p = p -> next;
        }
        if(p -> last == NULL)printf("%d\n", p -> next -> data);
        else if(p -> next == NULL)printf("%d\n", p -> last -> data);
        else printf("%d %d\n", p -> last -> data, p -> next -> data);
    }
    return 0;

}

猜你喜欢

转载自blog.csdn.net/Miracle_QSH/article/details/81699967