C语言版本

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

typedef struct forward_list{
  int val;
  struct forward_list *next;
} forward_list;

void insert(forward_list *list, int x, int y) {
  forward_list *cur = list, *pre = list;
  while (cur && cur->val != x) {
    pre = cur;
    cur = cur->next;
  }
  forward_list *tmp = (forward_list *)malloc(sizeof(forward_list));
  pre->next = tmp;
  tmp->next = cur;
  tmp->val = y;
}

void delete(forward_list *list, int x) {
  forward_list *pre = list, *cur = list;
  while (cur && cur->val != x) {
    pre = cur;
    cur = cur->next;
  }
  if (cur) {
    pre->next = cur->next;
    free(cur);
  }
}

int main(int argc, char *argv[]) {
  //  头节点
  forward_list *head = (forward_list *)malloc(sizeof(forward_list));
  head->next = NULL;
  head->val = -1;
  
  int count = 0;
  scanf("%d", &count);
  
  while (--count >= 0) {
    char str[7];
    scanf("%s", str);
    
    if (!strcmp(str, "insert")) {
      int x, y;
      scanf("%d", &x);
      scanf("%d", &y);
      
      insert(head, x, y);
    } else if (!strcmp(str, "delete")) {
      int x;
      scanf("%d", &x);
      
      delete(head, x);
    }
  }
  
  forward_list *cur = head->next;
  if (cur) {
    while (cur) {
      printf("%d ", cur->val);
      cur = cur->next;
    }
  } else {
    printf("NULL");
  }
  
  cur = head;
  while (head) {
    cur = head->next;
    free(head);
    head = cur;
  }
  
  return 0;
}