#include <stdio.h> #include <malloc.h> #define N 10 typedef struct Node { int data; struct Node *next; }Node, *LinkedList; void initList(LinkedList *L); int ListInsert(LinkedList *L, int index, int e); int ListDelete(LinkedList *L, int index, int *e); void printList(LinkedList L); int main(int argc, char const *argv[]) { int e; LinkedList L=NULL; initList(&L); for (int i = 1; i <= N; ++i) { ListInsert(&L,i,i); } printList(L); for (int i = L->data; i >=1; --i) { ListDelete(&L,i,&e); printf("%d ", e); } return 0; } void initList(LinkedList *L) { (*L) = (Node*)malloc(sizeof(Node)); (*L)->data = 0; (*L)->next = NULL; } int ListInsert(LinkedList *L, int index, int e) { if(index>(*L)->data+1 || index<=0) return -1; int i = 0; Node *p = (*L); while(p) { if(i==index-1) { Node *t = (Node*)malloc(sizeof(Node)); t->data = e; t->next = p->next; p->next = t; (*L)->data += 1; return 0; } ++i; p = p->next; } } int ListDelete(LinkedList *L, int index, int *e) { if(index<=0 || index>=(*L)->data+1) return -1; int i=0; Node* p = (*L); while(p) { if(i==index-1) { Node *q = p->next; p->next = q->next; *e = q->data; free(q); q = NULL; return 0; } ++i; p = p->next; } } void printList(LinkedList L) { Node *p = L; while(p->next) { p = p->next; printf("%d ",p->data); } }