# 顺序表之单向链表

//exp2-2.c ---------------- 单链表
#include <stdio.h>
#include <stdlib.h>

#define OK 1
#define ERROR 0

typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
}ListNode;

//=====================模块 定义==================
/* 内置 申 请节点函数  */
if(p != NULL)
p->next = NULL;
return p;
}
//初始化
*L = newNode();
(*L)->next = NULL;
}

//销毁表
while(q != NULL){
free(p);
p = q;
q = p->next;
}
free(p);
}
//判断表空
return L->next == NULL;
}
//判断表长
int i = 0;
while(p->next != NULL){
i++;
p = p->next;
}
return i;
}
//输出表
while(p != NULL){
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}

//取元素
int GetElem(LinkList L, int i, ElemType *e){
int j = 0;
while(j < i && p != NULL){
j++;
p = p->next;
}
if(p == NULL)
return ERROR;
else{
*e = p->data;
return OK;
}
}
//求元素位置
int i = 1;
while(p != NULL && p->data != e){
i++;
p = p->next;
}
if(p == NULL)
return ERROR;
else
return i;
}

//插入元素
int InsertList(LinkList L, int i, ElemType e){
int j = 0;
while(j < i - 1 && p != NULL){            //查找前驱
j++;
p = p->next;
}
if(p == NULL)
return ERROR;
else{
pNew = newNode();
pNew->data = e;
pNew->next = p->next;
p->next = pNew;
return OK;
}
}

//删除元素
int DeleteList(LinkList L, int i, ElemType *e){
int j = 0;

while(j < i - 1 && p != NULL){
j++;
p = p->next;
}
if(p == NULL)
return ERROR;
else{
q = p->next;
if(q == NULL)
return ERROR;
*e = q->data;
p->next = q->next;
free(q);
return OK;
}
}