main 函数
程序代码:
#include <stdio.h>
#include "linkedList.h"
void print() {
int i;
for (i = 0; i < size; ++i) {
printf("%d ", get(i));
}
printf("\n");
}
int main() {
head = NULL;
size = 0;
int n, i, position, value;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d%d", &position, &value);
if (insert(position, value)) {
print();
} else {
printf("position is not valid\n");
}
}
clear();
return 0;
}
头文件
程序代码:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <stdbool.h>
typedef struct node {
int value;
struct node* next;
} node ;
int size; // the size of linked list
node* head; // the head of linkedlist
//insert the value to the right position
//if the position is not valid, return false
//if insert successfully, return true
bool insert(int position, int value);
// return the value in the given position
int get(int position);
//clear the linkedlist, remember to free the memory you allocated
void clear();
#endif