BUPT-homework/semester2/pset1/1-linkedList.c
2022-03-08 14:55:11 +08:00

68 lines
1.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
Node *appendNode(Node *n) {
int data;
scanf("%d", &data);
Node *node = malloc(sizeof(struct Node));
node->data = data;
node->next = NULL;
n->next = node;
return node;
}
Node *makeList(int col) {
Node *head, *ptr;
head = malloc(sizeof(Node));
head->next = NULL;
ptr = head;
for (int i = 0; i < col; i++) {
ptr = appendNode(ptr);
}
return head;
}
void delete(Node *head) {
if (head->next == NULL) {
free(head);
} else {
delete(head->next);
}
}
int main(void) {
int rows;
int col;
int count;
scanf("%d", &rows);
Node *ptr;
for (int i = 0; i < rows; i++) {
scanf("%d", &col);
Node *head = makeList(col);
count = 0;
for (ptr = head->next; ptr != NULL; ptr = ptr->next) {
if (count % 2 == 1) {
if (count == 1) {
printf("%d", ptr->data);
} else {
printf(" %d", ptr->data);
}
}
count++;
}
printf("\n");
delete(head);
}
return 0;
}