数据结构实验之链表九:双向链表(SDUT 2054)

#include <bits/stdc++.h>
using namespace std;
typedef struct node
{
    int data;
    struct node *next, *last;
} Node;
int main()
{
    int n, m, a;
    scanf("%d%d", &n, &m);
    Node *head,*p,*tail;
    head = (Node*)malloc(sizeof(Node));
    head -> next = NULL;
    head -> last = NULL;
    tail = (Node *)malloc(sizeof(Node));
    tail = head;
    int i;
    for(i =0; i < n; i++)
    {
        p = (Node *)malloc(sizeof(Node));
        p -> next = NULL;
        scanf("%d",&p ->data);
        tail -> next = p;
        p -> last = tail;
        tail =p;
    }

    int j;
    for(j = 0; j < m; j ++)
    {
        scanf("%d", &a);
        p = head -> next;
        for(i = 0; i < n; i ++)
        {
            if(p -> data == a)
            {
                if(p -> last == head) printf("%d
",p->next->data);
                else if(p -> next == NULL)printf("%d
",p->last->data);
                else printf("%d %d
",p -> last -> data, p -> next -> data);
                break;
            }
            else p = p -> next;
        }
    }

    return 0;
}

原文地址:https://www.cnblogs.com/lcchy/p/10139493.html