这篇文章是一份纯代码分享。
代码包含了线性表的链式实现时的基本函数,包括:
CreateLinkList、DestroyLinkList、ClearLinkList、LinkListEmpty、LinkListLength
GetElem、LocateElem、PriorElem、NextElem、LinkListInsert、LinkListDelete、Traverse
以及为完成SCAU8579、SCAU8580、SCAU8581的必要函数和代码段。
//此项目功能:完成链式表的基本操作以及OJ给出的三道题:8579、8580、8581
#include <stdio.h>
#include <malloc.h>
#define ElemType int
#define Status int
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef struct LNode
{
ElemType data;
struct LNode *next;
} LNode, *LinkList;
Status CreateLinkList(LinkList &L, int n)
{
LinkList tail, p;
ElemType e;
L = new LNode;
L->data = 0;
L->next = NULL;
tail = L;
for (int i = 0; i < n; i++)
{
scanf("%d", &e);
p = new LNode;
p->data = e;
p->next = NULL;
tail->next = p;
tail = p;
L->data++;
}
return OK;
}
Status DestroyLinkList(LinkList &L)
{
LinkList p = L->next, q;
L->next = NULL;
while (p)
{
q = p->next;
delete p;
p = q;
}
delete L;
return OK;
}
Status ClearLinkList(LinkList &L)
{
LinkList p = L->next, q;
L->next = NULL;
while (p)
{
q = p->next;
delete p;
p = q;
}
return OK;
}
Status LinkListEmpty(LinkList L)
{
if (L->next)
return FALSE;
else
return TRUE;
}
int LinkListLength(LinkList L)
{
return L->data;
}
Status GetElem(LinkList L, int i, ElemType &e)
{
if (i > L->data || i < 1)
return ERROR;
else
{
LinkList p = L;
for (int j = 0; j < i; j++)
p = p->next;
e = p->data;
}
return OK;
}
int LocateElem(LinkList L, ElemType e)
{
int i = 1;
LinkList p = L->next;
for (; p; p = p->next)
{
if (p->data == e)
return i;
i++;
}
return 0;
}
Status PriorElem(LinkList L, ElemType cur_e, ElemType &pre_e)
{
int Location = LocateElem(L, cur_e);
if (Location == 0 || Location == 1)
return ERROR;
else
GetElem(L, Location - 1, pre_e);
return OK;
}
Status NextElem(LinkList L, ElemType cur_e, ElemType &next_e)
{
int Location = LocateElem(L, cur_e);
if (Location == 0 || Location == L->data)
return ERROR;
else
GetElem(L, Location + 1, next_e);
return OK;
}
Status LinkListInsert(LinkList &L, int i, ElemType e)
{
if (i < 1 || i > L->data + 1)
return ERROR;
else
{
LinkList p = new LNode;
p->data = e;
p->next = NULL;
LinkList q = L;
for (int j = 1; j < i; j++)
q = q->next;
p->next = q->next;
q->next = p;
L->data++;
}
return OK;
}
Status LinkListDelete(LinkList &L, int i, ElemType &e)
{
if (i < 1 || i > L->data)
return ERROR;
else
{
LinkList p = L;
for (int j = 1; j < i; j++)
p = p->next;
LinkList q = p->next;
e = q->data;
p->next = q->next;
delete q;
L->data--;
}
return OK;
}
Status Traverse(LinkList L)
{
if (LinkListEmpty(L))
{
printf("The List is empty!\n");
return ERROR;
}
else
{
//printf("The LinkList is:");
LinkList p = L->next;
while (p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
return OK;
}
void PreMerge(LinkList &C, LinkList &A)
{
ElemType x;
LinkListDelete(A, 1, x);
LinkListInsert(C, C->data + 1, x);
}
void Merge(LinkList &C, LinkList &A, LinkList &B)
{
while (A->data && B->data)
{
if (A->next->data == B->next->data)
{
PreMerge(C, A);
PreMerge(C, B);
}
else if (A->next->data < B->next->data)
PreMerge(C, A);
else
PreMerge(C, B);
}
while (A->data || B->data)
{
while (A->data)
PreMerge(C, A);
while (B->data)
PreMerge(C, B);
}
}
void TurnLinkList(LinkList &L)
{
for (int i = 0; i < L->data; i++)
{
ElemType x;
LinkListDelete(L, 1, x);
LinkListInsert(L, L->data + 1 - i, x);
}
}
void FuctionsTest(LinkList &L)
{
int a, i;
ElemType e, x;
ElemType cur_e, pre_e, next_e;
while (1)
{
scanf("%d", &a);
switch (a)
{
case 1:
CreateLinkList(L, 0);
break;
case 2:
ClearLinkList(L);
break;
case 3:
scanf("%d", &i);
if (!GetElem(L, i, e))
printf("GetElem Error.\n");
else
printf("%d\n", e);
break;
case 4:
scanf("%d", &e);
if (!LocateElem(L, e))
printf("LocateElem Error.\n");
else
printf("%d\n", LocateElem(L, e));
break;
case 5:
scanf("%d", &cur_e);
if (!PriorElem(L, cur_e, pre_e))
printf("No cur_e or no pre_e.\n");
else
printf("%d\n", pre_e);
break;
case 6:
scanf("%d", &cur_e);
if (!NextElem(L, cur_e, next_e))
printf("No cur_e or no next_e.\n");
else
printf("%d\n", next_e);
break;
case 7:
scanf("%d%d", &i, &x);
if (!LinkListInsert(L, i, x))
printf("Insert Error!\n");
else
printf("The Element %d is Successfully Inserted!\n", x);
break;
case 8:
scanf("%d", &i);
if (!LinkListDelete(L, i, e))
printf("Delete Error!\n");
else
printf("The Element %d is Successfully Deleted!\n", e);
break;
case 9:
Traverse(L);
break;
case 0:
return;
}
}
}
int main()
{
/*SCAU 8579
LinkList T;
int a,n,i;
ElemType x, e;
printf("Please input the init size of the linklist:\n");
scanf("%d",&n);
printf("Please input the %d element of the linklist:\n", n);
if(CreateLinkList(T,n))
{
printf("A Link List Has Created.\n");
Traverse(T);
}
while(1)
{
printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n");
scanf("%d",&a);
switch(a)
{
case 1: scanf("%d%d",&i,&x);
if(!LinkListInsert(T,i,x)) printf("Insert Error!\n");
else printf("The Element %d is Successfully Inserted!\n", x);
break;
case 2: scanf("%d",&i);
if(!LinkListDelete(T,i,e)) printf("Delete Error!\n");
else printf("The Element %d is Successfully Deleted!\n", e);
break;
case 3: Traverse(T);
break;
case 0: return 1;
}
}
/*
/*SCAU 8580
LinkList A,B,C;
CreateLinkList(A,0);
CreateLinkList(B,0);
CreateLinkList(C,0);
int a,b;
scanf("%d", &a);
for (int i = 0; i < a; i++)
{
int x;
scanf("%d", &x);
LinkListInsert(A,A->data+1,x);
}
scanf("%d", &b);
for (int i = 0; i < b; i++)
{
int x;
scanf("%d", &x);
LinkListInsert(B,B->data+1,x);
}
printf("List A:");
Traverse(A);
printf("List B:");
Traverse(B);
Merge(C,A,B);
printf("List C:");
Traverse(C);
*/
/*SCAU 8581
LinkList L;
CreateLinkList(L,0);
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
int x;
scanf("%d", &x);
LinkListInsert(L,L->data+1,x);
}
printf("The List is:");
Traverse(L);
TurnLinkList(L);
printf("The turned List is:");
Traverse(L);
*/
/*
LinkList L;
FuctionsTest(L);
*/
return 0;
}
欢迎讨论交流。