#include <iostream>
using namespace std;
#define MAXSIZE 20
#define TRUE 1
#define FALSE 0
typedef bool Status; /* Status是函数的类型,其值是函f数结果状态代码*/
typedef char ElemType; /* ElemType类型根据实际情况而定,这里假设为int */
typedef struct Node {
ElemType data;
struct Node* next;
}LinkList;
void DisplayList(LinkList* List)
{
Node* p = List;
while (p->next)
{
p = p->next;
cout << p->data << ' ';
}
cout << endl;
}
void InitList(LinkList*& List)
{
List = new LinkList;
List->next = NULL;
}
void CreateListAtHead(LinkList*& List, ElemType arr[], int len)
{
Node* temp;
List = new LinkList;
List->next = NULL; // 作为链表结束标志
for (int i = 0; i < len; i++)
{
temp = new Node;
temp->data = arr[i]; // 把数据放进去
temp->next = List->next;
List->next = temp;
}
}
void CreateListAtTail(LinkList*& List, ElemType arr[], int len)
{
Node* temp, * p; // temp用来存放临时的地址, p用来指向最后的那个节点
List = new LinkList;
List->next = NULL; // 作为链表结束标志
p = List;
for (int i = 0; i < len; i++)
{
temp = new Node;
temp->data = arr[i];
p->next = temp;
p = temp;
}
p->next = NULL;
}
Status ListInsert(LinkList*& List, int position, ElemType e)
{
if (position <= 0)
return FALSE;
LinkList* p = List;
LinkList* temp;
int count = 0;
while (p->next && count < position - 1)
{
p = p->next;
count++;
}
if (count != position - 1)
return FALSE;
temp = new Node;
temp->data = e;
temp->next = p->next;
p->next = temp;
return TRUE;
}
Status ListDelete(LinkList*& List, int position, ElemType& e)
{
if (position <= 0)
return FALSE;
LinkList* p = List;
LinkList* temp;
int count = 0;
while (p->next && count < position - 1)
{
p = p->next;
count++;
}
if (count != position - 1)
return FALSE;
temp = p->next;
e = temp->data;
p->next = p->next->next;
delete temp;
return TRUE;
}
void ClearList(LinkList*& List)
{
LinkList* p = List;
if (p->next == NULL)
return;
else
p = p->next;
LinkList* c;
while (p->next)
{
c = p;
p = p->next;
delete c;
}
}
Status ListEmpty(LinkList* List)
{
if (List->next == NULL)
return TRUE;
else
return FALSE;
}
int ListLength(LinkList* List)
{
LinkList* p = List;
int count = 0;
while (p->next)
{
p = p->next;
count++;
}
return count;
}
Status GetElem(LinkList* List, int position, ElemType& e)
{
LinkList* p = List;
int count = 0;
if (position <= 0)
return FALSE;
while (p->next && count < position)
{
p = p->next;
count++;
}
e = p->data;
return TRUE;
}
int LocateElem(LinkList* List, ElemType e)
{
Node* p = List;
int count = 0;
while (p->next)
{
p = p->next;
count++;
if (p->data == e)
return count;
}
return 0;
}
void DestroyList(LinkList*& List)
{
ClearList(List);
delete List;
}
/*去两个集合的合集, LA和LB位要去合集的对象, LC为结果*/
void UnionList(LinkList *LA, LinkList *LB, LinkList *&LC)
{
int lena, lenb, i;
ElemType e;
lena = ListLength(LA);
lenb = ListLength(LB);
for(i = 1; i <= lena; i++) // 把LA放到LC上
{
GetElem(LA, i ,e);
ListInsert(LC, i ,e);
}
for(i = 1; i <= lenb; i++)
{
GetElem(LB, i, e);
if(!LocateElem(LA, e))
ListInsert(LC, ++lena, e);
}
}
void CommonList(LinkList *LA, LinkList *LB, LinkList *&LC)
{
ElemType e;
int lena = ListLength(LA);
for(int i = 1; i <= lena; i++)
{
GetElem(LA, i, e);
if(LocateElem(LB, e))
ListInsert(LC, ListLength(LC) + 1, e);
}
}
void DifferenceSetList(LinkList *LA, LinkList *LB, LinkList *&LC)
{
ElemType e;
int lena = ListLength(LA);
for(int i = 1; i <= lena; i++)
{
GetElem(LA, i, e);
if(!LocateElem(LB, e))
ListInsert(LC, ListLength(LC) + 1, e);
}
}
int main()
{
LinkList *listA, *listB, *listC, *listD, *listE, *listF;
ElemType list_a[4] = {'a', 'b', 'c', 'd'};
ElemType list_b[5] = {'b', 'c', 'e', 'f', 'g'};
CreateListAtTail(listA, list_a, 5);
cout << "线性表A的元素是:";
DisplayList(listA);
CreateListAtTail(listB, list_b, 6);
cout << "线性表B的元素是:";
DisplayList(listB);
InitList(listC);
InitList(listD);
InitList(listE);
InitList(listF);
cout << "A ∪ B: ";
UnionList(listA, listB, listC);
DisplayList(listC);
cout << "A ∩ B: ";
CommonList(listA, listB, listD);
DisplayList(listD);
cout << "A - B: ";
DifferenceSetList(listA, listB, listE);
DisplayList(listE);
cout << "B - A: ";
DifferenceSetList(listB, listA, listF);
DisplayList(listF);
return 0;
}
单链表在集合中的应用(交、并、差)
最新推荐文章于 2022-06-26 13:31:34 发布