|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 100
typedef int Status;
typedef int ElemType;
typedef struct Node
{
ElemType data;
struct Node* next;
}Node, * LinkList;
Status visit(ElemType c);
Status InitList(LinkList* L);
Status EmptyList(LinkList L);
Status ClearList(LinkList* L);
Status DestroyList(LinkList* L);
int ListLength(LinkList L);
Status GetElem(LinkList L, int i, ElemType* e);
int LocateElem(LinkList L, ElemType e);
Node* LocateElem2(LinkList L, ElemType e);
Status ListInsert(LinkList* L, int i, ElemType e);
Status ListDelete(LinkList* L, int i, ElemType* e);
Status ListTraverse(LinkList L);
void CreatListHead(LinkList* L, int n);
void CreatListTail(LinkList* L, int n);
Status visit(ElemType c)
{
cout << c << " ";
return OK;
}
Status InitList(LinkList* L)
{
*L = new Node;
if (!(*L))
{
return ERROR;
}
(*L)->next = NULL;
}
Status EmptyList(LinkList L)
{
if (L->next)
{
return FALSE;
}
else
{
return TRUE;
}
}
Status ClearList(LinkList* L)
{
LinkList p, q;
p = (*L)->next;
while (p)
{
q = p->next;
delete p;
p = q;
}
(*L)->next = NULL;
return OK;
}
Status DestroyList(LinkList* L)
{
LinkList p;
while (*L)
{
p = (*L);
(*L) = (*L)->next;
delete p;
}
return OK;
}
int ListLength(LinkList L)
{
int i = 0;
LinkList p = L->next;
while (p)
{
i++;
p = p->next;
}
return i;
}
Status GetElem(LinkList L, int i, ElemType* e)
{
int j = 1;
LinkList p = L->next;
while (p && j < i)
{
p = p->next;
j++;
}
if (!p || j > i)
{
return ERROR;
}
*e = p->data;
return OK;
}
int LocateElem(LinkList L, ElemType e)
{
int i = 0;
LinkList p = L->next;
while (p)
{
i++;
if (p->data == e)
{
return i;
}
p = p->next;
}
return 0;
}
Node* LocateElem2(LinkList L, ElemType e)
{
LinkList p = L->next;
while (p)
{
if (p->data == e)
{
return p;
}
p = p->next;
}
return p;
}
Status ListInsert(LinkList* L, int i, ElemType e)
{
int j = 0;
LinkList p, s;
p = *L;
while (p && j < i - 1)
{
p = p->next;
++j;
}
if (!p || j > i - 1)
{
return ERROR;
}
s = new Node;
s->data = e;
s->next = p->next;
p->next = s;
return OK;
}
Status ListDelete(LinkList* L, int i, ElemType* e)
{
int j;
LinkList p, q;
p = *L;
j = 0;
while (p->next && j < i - 1)
{
p = p->next;
++j;
}
if (!(p->next) || j > i - 1)
{
return ERROR;
}
q = p->next;
p->next = q->next;
*e = q->data;
delete q;
return OK;
}
Status ListTraverse(LinkList L)
{
LinkList p = L->next;
while (p)
{
visit(p->data);
p = p->next;
}
cout << endl;
return OK;
}
void CreatListHead(LinkList* L, int n)
{
LinkList p;
*L = new Node;
(*L)->next = NULL;
for (int i = 0; i < n; i++)
{
p = new Node;
cin >> p->data;
p->next = (*L)->next;
(*L)->next = p;
}
}
void CreatListTail(LinkList* L, int n)
{
LinkList p, r;
*L = new Node;
(*L)->next = NULL;
r = *L;
for (int i = 0; i < n; i++)
{
p = new Node;
cin >> p->data;
p->next = NULL;
r->next = p;
r = p;
}
}
void MergeList(LinkList La, LinkList Lb, LinkList& Lc)
{
Node* pa, * pb, * pc;
Lc = pc = La;
pa = La->next;
pb = Lb->next;
while (pa && pb)
{
if (pa->data <= pb->data)
{
pc->next = pa;
pc = pa;
pa = pa->next;
}
else
{
pc->next = pb;
pc = pb;
pb = pb->next;
}
}
pc->next = pa ? pa : pb;
}
int main()
{
LinkList L;
LinkList Lb, Lc;
ElemType e;
Status i;
i = InitList(&L);
/*for (int j = 1; j <= 10; j++)
{
ListInsert(&L, 1, j);
}
cout << "在表头依次插入1~10后,data=";
ListTraverse(L);
i = EmptyList(L);
cout << "L是否为空(1.是 0.否)" << i << endl;
i = ClearList(&L);
cout << "清空L后,ListLength(L)=" << ListLength(L) << endl;
i = EmptyList(L);
cout << "L是否为空(1.是 0.否)" << i << endl;
for (int j = 1; j <= 10; j++)
{
ListInsert(&L, j, j);
}
cout << "在表尾依次插入1~10后,data=";
ListTraverse(L);
int j = 1;
while (j <= 5)
{
i = ListDelete(&L, 1, &e);
++j;
}
cout << "从表头依次删除5个元素以后" << endl;
ListTraverse(L);
j = 5;
while (j != 3)
{
i = ListDelete(&L, j, &e);
j--;
}
cout << "从表尾依次删除2个元素以后" << endl;
ListTraverse(L);
i = ClearList(&L);
cout << "清空L后,ListLength(L)=" << ListLength(L) << endl;
cout << "插入元素(头插法)" << endl;
CreatListHead(&L, 5);
cout << "整体创建L的元素(头插法)" << endl;
ListTraverse(L);
i = ClearList(&L);
cout << "清空L后,ListLength(L)=" << ListLength(L) << endl;
i = EmptyList(L);
cout << "L是否为空(1.是 0.否)" << i << endl;
cout << "插入元素(尾插法)" << endl;
CreatListTail(&L, 5);
cout << "整体创建L的元素(尾插法)" << endl;
ListTraverse(L);
i = ClearList(&L);
cout << "清空L后,ListLength(L)=" << ListLength(L) << endl;
i = EmptyList(L);
cout << "L是否为空(1.是 0.否)" << i << endl;*/
cout << "插入L元素(尾插法)" << endl;
CreatListTail(&L, 5);
cout << "整体创建L的元素(尾插法)" << endl;
ListTraverse(L);;
InitList(&Lb);
cout << "插入Lb元素(尾插法)" << endl;
CreatListTail(&Lb, 5);
cout << "整体创建Lb的元素(尾插法)" << endl;
ListTraverse(Lb);;
InitList(&Lc);
MergeList(L, Lb, Lc);
cout << "有序合并L,Lb后,Lc=" << endl;
ListTraverse(Lc);
cout << "L=" << endl;
ListTraverse(L);
cout << "Lb=" << endl;
ListTraverse(Lb);
return 0;
}
我一直认为linklist是一个指针类型,在形参中只是定义L的话是值传递,加上*变成二级指针或者引用了以后才会改变指向L的地址,改变L里面的值,但是我调用了void MergeList(LinkList La, LinkList Lb, LinkList& Lc)以后,发现只是引用了Lc,L,La,的值也发生了改变,有点纠结
当然,由于转入的是二级指针的引用,那么,一个二级指针能干什么,在函数内就能干什么,当然也包括修改二级指针所指向变量的值。
|
|