#include<stdio.h>
#include<stdlib.h>
typedef int Status;
typedef int Elemtype;
typedef struct LNode
{
Elemtype data;
struct LNode* next;
}LNode,linklist;
void CreateList( linklist L, int n)
{
L = (LNode)malloc(sizeof(LNode));
LNode p;
(L)->next =NULL;
int i;
for ( i = 1; i <= n; i++) {
p = (linklist)malloc(sizeof(LNode));
scanf("%d",&(p->data));
p->next = (L)->next;
(L)->next = p;
}
}
void OutputLNode(LNode L)
{
L = L->next;
while (L)
{
printf("%d", L->data);
L = L->next;
}
}
int GetElem(linklist L, int i, Elemtype e)
{
LNode p;
int j = 1;
p = L->next;
while (p&&j<=i)
{
p = p->next;
++j;
}
if (!p || j > i)
return 0;
else
{
e = p->data;
return 1;
}
}
int InsertList(linklist L, int i, Elemtype e)
{
LNode p, * s;
int j = 0;
p = L;
while (p&&j<i-1)
{
p = p->next;
++j;
}
if (!p )
return 0;
else
{
s = (linklist)malloc(sizeof(LNode));
s->data = e;
s->next = p->next;
p->next = s;
return 1;
}
}
int DeleteList(linklist L, int i, Elemtype e)
{
LNode* p, * q;
int j = 0;
p = L;
while (p->next&&j<i-1)
{
p = p->next;
++j;
}
if (!(p->next) )
return 0;
else
{
q = p->next;
p->next = q->next;
e = q->data;
free(q);
return 1;
}
}
int main()
{
LNode L;
CreateList(&L, 4);
/if(L->next==NULL){
printf("!");
}/
printf(“表中元素:\n”);
OutputLNode(L);
Elemtype e;
GetElem(L, 2, &e);
printf(“将56插入第二个位置:\n”);
InsertList(&L, 2, 56);
OutputLNode(L);
printf(“删除第三个元素:\n”);
DeleteList(&L, 3, e);
OutputLNode(L);
}