Linked List

Time limit: 5000ms
Memory limit: 256mb

Description:

Complete the following implementation for Linked List.

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

typedef enum { FALSE = 0, TRUE = 1 } Boolean;

typedef struct listNode *listPointer;
struct listNode {
  int data;
  listPointer link;
};

listPointer create();
Boolean IsEmptyL(listPointer list);
listPointer nth(listPointer list, int n);
int length(listPointer list);
listPointer last(listPointer list);
listPointer prepend(listPointer list, int element);
listPointer append(listPointer list, int element);
listPointer deleteFirst(listPointer list, int element);

int main() {
  int T;
  listPointer l;
  scanf("%d", &T);
  for (int i = 0; i < T; i++) {
    char cmd[12];
    scanf("%s", cmd);
    if (strcmp("create", cmd) == 0) {
      l = create();
    }
    if (strcmp("IsEmptyL", cmd) == 0) {
      printf("IsEmptyL: %s\n", IsEmptyL(l) ? "yes" : "no");
    }
    if (strcmp("nth", cmd) == 0) {
      int n;
      scanf("%d", &n);
      printf("nth: %d\n", nth(l, n)->data);
    }
    if (strcmp("length", cmd) == 0) {
      printf("length: %d\n", length(l));
    }
    if (strcmp("last", cmd) == 0) {
      printf("last: %d\n", last(l)->data);
    }
    if (strcmp("prepend", cmd) == 0) {
      int e;
      scanf("%d", &e);
      l = prepend(l, e);
    }
    if (strcmp("append", cmd) == 0) {
      int e;
      scanf("%d", &e);
      l = append(l, e);
    }
    if (strcmp("deleteFirst", cmd) == 0) {
      int e;
      scanf("%d", &e);
      l = deleteFirst(l, e);
    }
  }
}

/* Please keep the code above unchanged
 * You can only edit the following code */

listPointer create() {
  /* return a empty list */
  // WRITE YOUR OWN CODE HERE
}
Boolean IsEmptyL(listPointer list) {
  /* return TRUE if the list is empty; return FALSE otherwise*/
  // WRITE YOUR OWN CODE HERE
}
listPointer nth(listPointer list, int n) {
  /* return a pointer which points to the nth node in the list and it is
   * guaranteed that 0 <= n < the length of the list. Please note that the index
   * starts with 0 */
  // WRITE YOUR OWN CODE HERE
}
int length(listPointer list) {
  /* return the length of the list */
  // WRITE YOUR OWN CODE HERE
}
listPointer last(listPointer list) {
  /* return a pointer which points to the last element of the list*/
  // WRITE YOUR OWN CODE HERE
}

listPointer prepend(listPointer list, int element) {
  /* insert to the front of list and return the new list*/
  // WRITE YOUR OWN CODE HERE
}

listPointer append(listPointer list, int element) {
  /* append an element at the end and return the new list */
  // WRITE YOUR OWN CODE HERE
}

listPointer deleteFirst(listPointer list, int element) {
  /* delete the first node whose data is of value element from the list and
   * return the new list and it is guaranteed that at least one node in the list
   * has element as its data*/
  // WRITE YOUR OWN CODE HERE
}

Sample input:
17
create
IsEmptyL
length
prepend 1
append 2
append 3
append 1
nth 0
nth 1
nth 2
nth 3
last
deleteFirst 1
nth 0
nth 1
nth 2
last

Sample output:
IsEmptyL: yes
length: 0
nth: 1
nth: 2
nth: 3
nth: 1
last: 1
nth: 2
nth: 3
nth: 1
last: 1
Submit