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

2023-03-09 16:49:45 浏览数 (1)

代码语言:javascript复制
#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("%dn",p->next->data);
                else if(p -> next == NULL)printf("%dn",p->last->data);
                else printf("%d %dn",p -> last -> data, p -> next -> data);
                break;
            }
            else p = p -> next;
        }
    }

    return 0;
}

0 人点赞