#include <iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
typedef struct node1
{
    int data;
    struct node1 *next;
}node1;
node1 *create();
void insert_node(node1 *head,int x);
void delete_node(node1 *head,int x);
void destroy(node1 *head);
void display(node1 *head);
int main()
{
    node1*head=create();
    int x;
    cin>>x;
    while(x!=-1)
    {
        insert_node(head,x);
        cin>>x;
    }
    display(head);
    int a;
    cin>>a;//要删除的数据
    delete_node(head,a);
    display(head);
    destroy(head);
    return 0;
}

node1 *create()
{
    node1 *head = new node1();
    head->next = NULL;
    return head;
}
void insert_node(node1 *head,int x)
{
    node1 *p=head;
    while(p->next!=NULL)
    {
        p = p->next;
    }
    node1 *q = create();
    q->data = x;
    p->next = q;
}
void delete_node(node1 *head,int x)
{
    node1 *p = head->next;
    node1 *q = head;
    while(p!=NULL)
    {
        if(p->data==x)
        {
            node1 *t = p;
            p = p->next;
            q->next = p;
            delete(t);
        }
        else
        {
            q = q->next;
            p = p->next;
        }
    }

}
void destroy(node1 *head)
{
    node1 *p = head;
    while(p!=NULL)
    {
        node1 *t = p;
        p = p->next;
        delete(t);
    }
}
void display(node1 *head)
{
    node1 *p =head->next;
    while(p!=NULL)
    {
        cout<<p->data<<" ";
        p = p->next;
    }
}