静态分配顺序表
#define MaxSize 50
typedef struct
{
int data[MaxSize];
int length;
} SqList;
void InitList(SqList &L)
{
L.length = 0;
}
bool ListInsert(SqList &L, int i, int e)
{
if (i < 1 || i > L.length + 1)
return false;
if (L.length >= MaxSize)
return false;
for (int j = L.length; j >= i; j--)
{
L.data[j] = L.data[j - 1];
}
L.data[i - 1] = e;
L.length++;
return true;
}
bool ListDelete(SqList &L, int i, int &e)
{
if (i < 1 || i > L.length)
return false;
e = L.data[i - 1];
for (int j = i; j <= L.length; j++)
{
L.data[j - 1] = L.data[j];
}
L.length--;
return true;
}
int GetElem(SqList L, int i)
{
return L.data[i - 1];
}
int LocateElem(SqList &L, int &e)
{
for (int i = 0; i < L.length; i++)
{
if (L.data[i] == e)
return i + 1;
}
return 0;
}
动态分配顺序表
#define InitSize 50
typedef struct
{
int *data;
int MaxSize, length;
} SqList;
void InitList(SqList &L)
{
L.data = (int *)malloc(sizeof(int) * InitSize);
L.length = 0;
L.MaxSize = InitSize;
}
bool ListInsert(SqList &L, int i, int e)
{
if (i < 1 || i > L.length + 1)
return false;
if (L.length >= MaxSize)
return false;
for (int j = L.length; j >= i; j--)
{
L.data[j] = L.data[j - 1];
}
L.data[i - 1] = e;
L.length++;
return true;
}
bool ListDelete(SqList &L, int i, int &e)
{
if (i < 1 || i > L.length)
return false;
e = L.data[i - 1];
for (int j = i; j <= L.length; j++)
{
L.data[j - 1] = L.data[j];
}
L.length--;
return true;
}
int GetElem(SqList L, int i)
{
return L.data[i - 1];
}
int LocateElem(SqList &L, int &e)
{
for (int i = 0; i < L.length; i++)
{
if (L.data[i] == e)
return i + 1;
}
return 0;
}
带头结点的单链表
typedef struct LNode
{
int data;
struct LNode *next;
} LNode, *LinkList;
bool InitList(LinkList &L)
{
L = (LNode *)malloc(sizeof(LNode));
if (L == NULL)
return false;
L->next = NULL;
return true;
}
bool InitList(LinkList &L)
{
L = NULL;
return true;
}
LinkList List_TailInsert(LinkList &L)
{
int x;
L = (LinkList)malloc(sizeof(LNode));
LNode *s, *r = L;
scanf("%d", &x);
while (x != 9999)
{
s = (LNode *)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf("%d", &x);
}
r->next = NULL;
return L;
}
LinkList List_HeadInsert(LinkList &L)
{
int x;
LNode *s;
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
scanf("%d", &x);
while (x != 9999)
{
s = (LNode *)malloc(sizeof(LNode));
s->data = x;
s->next = L->next;
L->next = s;
scanf("%d", &x);
}
return L;
}
bool ListInsert(LinkList &L, int i, int 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)
return false;
LNode *s = (LNode *)malloc(sizeof(LNode));
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
bool ListInsert(LinkList &L, int i, int e)
{
if (i < 1)
return false;
if (i == 1)
{
LNode *s = (LNode *)malloc(sizeof(LNode));
s->data = e;
s->next = L;
L = s;
return true;
}
LNode *p;
p = L;
int j = 1;
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 InsertPriorNode(LNode *p, int 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;
}
bool ListDelete(LinkList &L, int i, int 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)
return false;
if (p->next == NULL)
return false;
LNode *q = p->next;
e = q->data;
p->next = p->next->next;
free(q);
return true;
}
bool DeleteNode(LNode *p)
{
if (p == NULL)
return false;
LNode *q = p->next;
p->next = q->next;
free(q);
return true;
}
LNode *GetElem(LinkList L, int i)
{
int j = 1;
LNode *p = L->next;
if (i == 0)
return L;
if (i < 1)
return NULL;
while (p != NULL && j < i)
{
p = p->next;
j++;
}
return p;
}
LNode *LocateElem(LinkList L, int e)
{
LNode *p = L;
if (p == NULL)
return false;
while (p != NULL && p->date != e)
{
p = p->next;
}
return p;
}
int Length(LinkList L)
{
int len = 0;
LNode *p = L;
while (p->next != NULL)
{
p = p->next;
len++;
}
return len;
}