#include<iostream>
using namespace std;
#define ElemType int
typedef struct LNode
{
ElemType data;
struct LNode* next;
}LNode,*LinkList;
bool InitList(LinkList& L)
{
L = NULL;
return true;
}
bool empty(LinkList L)
{
return (L == NULL);
}
bool ListInsert(LinkList& L, int i, ElemType e) {
if (i < 1)
{
return false;
}
LNode* s = (LNode*)malloc(sizeof(LNode));
s->data = e;
if (i == 1)
{
s->next = L;
L = s;
return true;
}
LNode* p;
int j = 1;
p = L;
while (p != NULL && j < i - 1)
{
p = p->next;
j++;
}
if (p == NULL) {
return false;
}
s->next = p->next;
p->next = s;
return true;
}
bool InsertPriorNode(LNode* p, ElemType e)
{
if (p == NULL)
{
return false;
}
LNode* s = (LNode*)malloc(sizeof(LNode));
if (s == NULL)
{
return false;
}
s->next = p->next;
p->next = s;
s->data = p->data;
p->data = e;
return true;
}
typedef struct LNode
{
ElemType data;
struct LNode* next;
}LNode, * LinkList;
bool InitList2(LinkList& L)
{
L = (LNode*)malloc(sizeof(LNode));
if (L == NULL)
{
return false;
}
L->next = NULL;
return true;
}
bool empty1(LinkList L) {
return (L->next == NULL);
}
bool ListInsert1(LinkList& L, int i, ElemType e)
{
if (i < 1) return false;
LNode* p = L;
int j = 0;
while (p != NULL && j < i - 1)
{
p = p->next;
j++;
}
if (p == NULL)return false;
LNode* s = (LNode*)malloc(sizeof(LNode));
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
bool ListDelete(LinkList& L, int i, ElemType& e)
{
if (i < 1)
{
return false;
}
LNode* p;
int j = 0;
p = L;
while (p != NULL && j < i - 1)
{
p = p->next;
j++;
}
if (p == NULL&&p->next==NULL)
{
return false;
}
LNode* q = p->next;
e = q->data;
p->next = q->next;
free(p);
return true;
}
bool DeleteNode(LNode* p) {
if (p == NULL) {
return false;
}
LNode* q = p->next;
p->data = p->next->data;
p->next = q->next;
free(q);
return true;
}