#include <stdio.h>
#include <stdlib.h>

typedef struct Node{
    int data;
    struct Node* next;
}Node;

struct Node* top = NULL;

Node* alloc_node(int e){
    Node* p = (Node*)malloc(sizeof(Node));
    p->data=e;
    p->next=NULL;
    return p;
}

int is_empty(){
    if(top == NULL){
        return 1;
    }
    else{
        return 0;
    }
}

void add(int e){
    Node* p = alloc_node(e);
    if(is_empty()){
        top = p;
    }
    else{
        p->next = top;
        top = p;
    }
}

void insert(Node* t){
    if(t == NULL){
        return;
    }
    else{
        insert(t->next);
        printf("%d ", t->data);
    }
}

void insertAfter(int K, int X){
    if(is_empty()){
        printf("None\n");
        return;
    }
    else{
        if(top->data == K){
            Node* p = alloc_node(X);
            p->next = top;
            top = p;
            insert(top);
            printf("\n");
            return;
        }
        
        Node* A = top;
        Node* p = alloc_node(X);
        while(A->next != NULL){
            if(A->next->data == K){
            p->next = A->next;
            A->next = p;
            insert(top);
            return;
            }
            else{
                A = A->next;
            }
        }
    }
    printf("None\n");
}

int main(void){

    int N;
    scanf("%d", &N);
    
    int a;
    for(int i = 0 ; i < N ; i++){
        scanf("%d", &a);
        add(a);
    }

    int K,X;
    scanf("%d", &K);
    scanf("%d", &X);

    insertAfter(K,X);

    return 0;
}























Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: