|
楼主 |
发表于 2022-11-19 00:34:22
|
显示全部楼层
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define FALSE 0
#define TRUE 1
typedef int Status;
typedef int ElemType;
typedef struct Node
{
ElemType data;
struct Node* next;
}Node,*LinkList;
//typedef struct Node* LinkList;
Status visit(ElemType e);
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);
void CreatHead(LinkList& L, int n);
void CreatTail(LinkList& L, int n);
Node* FindeLast(Node* L);
LinkList Connect(LinkList& La, LinkList& Lb);
Status ListTravers(LinkList L);
Status visit(ElemType e)
{
cout << e << " ";
return OK;
}
Status InitList(LinkList &L)
{
L = new Node;
if (!L)
{
return ERROR;
cout << "创建内存失败" << endl;
}
L->next = L;
return OK;
}
Status EmptyList(LinkList L)
{
if (L->next==L)
{
return TRUE;
cout << "链表为空" << endl;
}
else
{
return FALSE;
cout << "链表非空" << endl;
}
}
Status ClearList(LinkList &L)
{
LinkList p, q;
p = L->next;
while (p!=L)
{
q = p->next;
delete p;
p = q;
}
L->next = L; //单链表记得置空 循环链表指向头结点
return OK;
/*ElemType e;
int length = ListLength(L);
for (int i=1;i<=length;i++)
{
ListDelete(L, i, e);
}
return OK;*/
}
Status DestroyList(LinkList& L)
{
LinkList p;
while (L->next!=L)
{
p = L;
L = L->next;
delete p;
}
delete L;
return OK;
/*ClearList(L);
delete L;
L= NULL;
return OK;*/
}
int ListLength(LinkList L)
{
int i = 0;
LinkList p = L->next;
while (p!=L)
{
i++;
p = p->next;
}
return i;
}
Status GetElem(LinkList L,int i,ElemType &e)
{
/*LinkList p = L->next;
int j = 1;
while (p!=L &&j<i)
{
j++;
p = p->next;
}
if (p==L||j>i)
{
return ERROR;
}
e = p->data;
return OK;*/
int length = ListLength(L);
if (i<1 || i>length||length==0)
{
return ERROR;
}
LinkList p = L->next;
for (int j=1;j<i;j++)
{
p = p->next;
}
e = p->data;
return OK;
}
int LocateElem(LinkList L,ElemType e)
{
/*LinkList p = L->next;
int i = 0;
while (p!=L)
{
i++;
if (p->data==e)
{
return i;
}
p = p->next;
}
return 0;*/
int length = ListLength(L);
LinkList p = L->next;
for (int i=1;i<length;i++)
{
if (p->data==e)
{
return i;
}
p = p->next;
}
return 0;
}
Node* LocateElem2(LinkList L,ElemType e)
{
LinkList p = L->next;
while (p!=L)
{
if (p->data==e)
{
return p;
}
p = p->next;
}
return p;
}
Status ListInsert(LinkList &L,int i,ElemType e)
{
int length = ListLength(L);
if (i<1||i>length+1)
{
return ERROR;
}
int j = 0;
LinkList p,q;
p = L;
while (j<i-1)
{
++j;
p = p->next;
}
/*if (j>i-1)
{
return ERROR;
}*/
q = new Node;
q->data = e;
q->next = p->next;
p->next = q;
return OK;
}
Status ListDelete(LinkList &L,int i,ElemType &e)
{
int length = ListLength(L);
if (i<1||i>length)
{
return ERROR;
}
LinkList p, q;
p = L;
int j = 0;
while (p->next!=L &&j<i-1)
{
++j;
p = p->next;
}
q = p->next;
e = q->data;
p->next = q->next;
delete q;
return OK;
}
void CreatHead(LinkList &L,int n)
{
LinkList p;
L = new Node;
L->next = L;
for (int i = 0; i < n; i++)
{
p = new Node;
cin >>p->data;
p->next = L->next;
L->next = p;
}
}
void CreatTail(LinkList &L,int n)
{
LinkList s, r;
L = new Node;
L->next = L;
r = L;
for (int i=0;i<n;i++)
{
s = new Node;
cin >> s->data;
r->next = s;
r = s; //r移动到尾节点
}
r->next = L;
}
Node * FindeLast(Node*L)
{
Node *p = L;
while (p->next!=L)
{
p = p->next;
}
return p;
}
LinkList Connect(LinkList &La,LinkList &Lb)
{
LinkList La_last, Lb_last;
LinkList p;
La_last = FindeLast(La);
Lb_last = FindeLast(Lb);
p = La_last->next;
La_last->next = Lb_last->next->next;
delete Lb_last->next;
Lb_last->next = p;
return Lb_last->next;
}
void MerGerList(LinkList &La,LinkList &Lb,LinkList &Lc)
{
Node* pa, *pb, *pc;
pa = La->next;
pb = Lb->next;
Lc = pc = La;
LinkList Lb_last;
Lb_last = FindeLast(Lb);
while (pa!=La&& pb!=Lb)
{
if (pa->data <= pb->data)
{
pc->next = pa;
pc = pa;
pa = pa->next;
}
else
{
pc->next = pb;
pc = pb;
pb = pb->next;
}
}
if (pa!=La)
{
pc->next = pa;
}
else
{
pc->next = pb;
Lb_last->next = La;
}
delete Lb;
}
Status ListTravers(LinkList L)
{
ElemType e;
int length = ListLength(L);
for (int i=1;i<=length;i++)
{
GetElem(L, i, e);
visit(e);
}
cout << endl;
return OK;
}
int main()
{
int i;
int j;
int k;
ElemType e;
LinkList L, p;
LinkList La, Lb;
i = InitList(L);
cout << "初始化链表(1.成功 0.失败)" << i << endl;
for (j = 1; j <= 5; j++) {
ListInsert(L, 1, j);
}
cout << "表头插入5个元素后,L.data=";
ListTravers(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 (j = 1; j <= 10; j++) {
ListInsert(L, j, j);
}
cout << "表尾插入10个元素后,L.data=";
ListTravers(L);
GetElem(L, 5, e);
cout << "第5个元素的值为" << e << endl;
e = 9;
i = LocateElem(L, e);
if (i == 0) {
cout << "没有找到值为" << e << "的元素" << endl;
}
else {
cout << "值为" << e << "的元素的位序为" << i << endl;
}
p = LocateElem2(L, e);
cout << "值为" << e << "元素的地址为" << (int)p << endl;
for (i = 1; i <= 2; i++) {
ListDelete(L, 1, e);
}
cout << "删除第一和第二个元素以后,L.data=";
ListTravers(L);
k = ListLength(L);
int T = 0;
for (int j = 1; j <= k + 1; j++) {
T++;
i = ListDelete(L, 1, e);
if (i == ERROR) {
cout << "删除第" << T << "个元素失败" << endl;
}
else {
cout << "删除第" << T << "个元素的值为" << e << endl;
}
}
i = EmptyList(L);
cout << "L是否为空(1.是 0.否)" << i << endl;
i= DestroyList(L);
cout << "插入元素La(头插法)" << endl;
CreatHead(La, 5);
cout << "整体创建La的元素(头插法)" << endl;
ListTravers(La);
cout << "插入元素Lb(尾插法)" << endl;
CreatTail(Lb, 5);
cout << "整体创建Lb的元素(尾插法)" << endl;
ListTravers(Lb);
L=Connect(La, Lb);
cout << "Lb合并到La后,L=";
ListTravers(L);
cout << "Lb合并到La后,La=";
ListTravers(La);
i = DestroyList(L);
cout << "插入元素La(尾插法)" << endl;
CreatTail(La, 5);
cout << "整体创建La的元素(尾插法)" << endl;
ListTravers(La);
cout << "插入元素Lb(尾插法)" << endl;
CreatTail(Lb, 5);
cout << "整体创建Lb的元素(尾插法)" << endl;
ListTravers(Lb);
MerGerList(La, Lb, L);
cout << "有序合并La,Lb后,L=";
ListTravers(L);
return 0;
}
大L,我尝试用DestroyList直接操作底层结构,发现出现了“已在 Project1.exe 中执行断点指令(__debugbreak()语句或类似调用)。”这种问题,原因是什么呢 |
|