#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct LNode {
ElemType data;
struct LNode *next;
} LNode;
LNode* List_HeadInsert(LNode *L) {
int x;
LNode *s;
L = (LNode*) malloc(sizeof(LNode));
L->next = NULL;
scanf("%d", &x);
while (x != 999) {
s = (LNode*) malloc(sizeof(LNode));
s->data = x;
s->next = L->next;
L->next = s;
scanf("%d", &x);
}
return L;
}
LNode* List_TailInsert() {
int x;
LNode *L = (LNode*) malloc(sizeof(LNode));
LNode *s;
LNode *r = L;
scanf("%d", &x);
while (x != 999) {
s = (LNode*) malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf("%d", &x);
}
r->next = NULL;
return L;
}
LNode* GetElem(LNode *L, int i) {
int j = 1;
LNode *p;
p = L->next;
if (i == 0)
return L;
if (i < 1)
return NULL;
while (p != NULL && j < i) {
p = p->next;
j++;
}
return p;
}
void ListInsert(LNode *L, int i, int e) {
LNode *p;
p = GetElem(L, i - 1);
LNode *s;
s = (LNode*) malloc(sizeof(LNode));
s->next = p->next;
p->next = s;
s->data = e;
}
void ListDelete(LNode *L, int i) {
LNode *p, *q;
p = GetElem(L, i - 1);
q = p->next;
p->next = q->next;
free(q);
}
void PrintList(LNode *L) {
LNode *p;
p = L->next;
while (p->next != NULL) {
printf("%d ", p->data);
p = p->next;
}
if (p->next == NULL) {
printf("%d ", p->data);
}
}
LNode* LocateElem(LNode *L, int e) {
LNode *p;
p = L->next;
while (p != NULL && p->data != e)
p = p->next;
return p;
}
int main() {
LNode *ln = List_TailInsert();
PrintList(ln);
printf("\n");
ListDelete(ln, 2);
PrintList(ln);
printf("\n");
ListInsert(ln, 2, 2);
PrintList(ln);
}