Table of contents
Union set: Merge of linear tables (no order, no repetition)
PPT: Chapter 2 P173;
Union set: Merge of linear tables (no order, no repetition)
Linear table:
Status Union(Sqlist& A, Sqlist& B)//并集
{
int len_A = A.length;
int len_B = B.length;
for (int i = 1; i <= len_B; i++)
{
Poly e=*A.elem;
//这里只是给我们设定的元素e赋一个任意初值
//只要保证e在初始化时由初值不为空即可
//至于该e元素的内容是什么其实并没有什么所谓
//因为后面我们总归是会改的
GetElem(B, i, e);
if (LocateElem(A, e))
return ERROR;
else
ListInsert(A, ++len_A, e);
//注意插入函数中输入的是位序,不是数组下标
}
return true;
}
Time complexity of the algorithm: O(ListLenth(La) * ListLength(Lb))
The last table A is the new table after the merger
linked list:
Status Union(Lnode& A, Lnode& B)
{
for (int i = 1; i <= 求表长(&B); i++)
{
int len_A = 求表长(&A);
Elemtype e;
取第i个元素(&A, i, e);
if (!LocateELem(&B, e))
Listlnsert(&A, ++len_A, e);
}
return true;
}
Result: (default in (7): Summary: Run under the preset pre-statements in the definition and operation of linked lists and linear lists)
The reason for this result is related to the statement defining the insertion function in the preceding statement:
Status Listlnsert(LinkList& L, int i, Elemtype e)
To be more precise: the problem stems from the fact that the type of "&A" we gave does not match the type of "LinkList& L" in the definition
Here, we make a systematic summary of LinkList & A and other formats:
What does &A stand for? ?
Sqlist A: variable A of the linear table node type
Sqlist &A: It still represents the variable A of the node type of the linear table, but (just) represents the method of passing by value by reference
LinkList A: Pointer variable A whose target object is the node type of linear listLinkList &A: It is still a pointer variable A indicating that the target object is a linear list node type
It just
means that the address is conveyed (delivered) using the value-by-reference method
If you want the program to run normally, there are two ways to modify it:
1:
Modify the function body of the <insert> function: (the <merge> function does not need to be modified at this time)
Change the "declaration" (outside the function body) part of the merged function to read:
Status Listlnsert(LinkList L, int i, Elemtype e)
At this point, the preconditions (simplest version) required by the merge function are:
//链表的定义及其基础操作
#include<iostream>
using namespace std;
#include<stdlib.h>//存放exit
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXlength 100 //初始大小为100,可按需修改
typedef int Status; //函数调用状态
struct K
{
float a;
int b;
string c;
bool operator==(K& t)
{
return t.a == a && t.b == b;
//&& t.c = c;
}
bool operator!=(K& t)
{
return t.a != a || t.b != b;
//|| t.c = c;
}
};
typedef K Elemtype; //函数调用状态
struct Lnode
//node:结; 结点;
{
Elemtype data;
Lnode* next;
};
typedef Lnode* LinkList;
Status 链表是否为空(LinkList L)
{
if (L->next)
return true;
else
return false;
}
Status 求表长(LinkList L)
{
if (链表是否为空(L))
cerr << "链表为空" << endl;
LinkList p = L->next;
//特别注意:因为这里从首元结点开始算起(计算)
//所以:L->next;
int i = 0;
while (p)//不要写成if
{
p = p->next;
i++;
}
//cout << "表长为: " << i << endl;
return i;
}
Status 取第i个元素(LinkList L, int i, Elemtype e)
{// GetElem“i”
LinkList p;
p = L->next;
int j = 1;
while (p && i > j)
{
p = p->next;
j++;
}
if (i < 0 || i < j || !p)
return false;
e = p->data;
return true;
}
Status LocateELem(LinkList L, Elemtype e)
{
//在线性表L中查找值为e的数据元素
//找到,则返回L中值为e的数据元素的地址,查找失败返回NULL
auto p = L->next; int i = 1;
while (p && p->data != e)
{
i++;
if (e == p->data)
{
cout << "地址为: " << p << ";" << endl;
cout << "位置序号为: " << i << ";" << endl;
}
p = p->next;
}
if (p == NULL)
return NULL;
return true;
}
Status Listlnsert(LinkList L, int i, Elemtype e)
{//插入(把元素e插到第i个位置结点上)
auto p = L; int j = 0;
while (p && j < i - 1)
{
p = p->next; ++j;
}
if (!p || j > i - 1)
return false;
auto s = new Lnode;
s->data = e;
s->next = p->next;
p->next = s;
return true;
}//Listlnsert_L
2:
Modify the function body of the <merge> function: (when the preceding statement remains unchanged and does not need to be changed)
Status Union(Lnode& A, Lnode& B)
{
for (int i = 1; i <= 求表长(&B); i++)
{
int len_A = 求表长(&A);
Elemtype e;
LinkList p = &A;
取第i个元素(&A, i, e);
if (!LocateELem(&B, e))
Listlnsert(p, ++len_A, e);
}
return true;
}
Then at this time, an interesting (very strange) phenomenon (situation) happened to me
question:
Why does the type of "&A" in the front and the "LinkList& L" in the definition not match
But as long as we put "&A" into a new variable of this type, let the information be executed in the program in the form of a variable
It's obviously the same thing (&A) put in, so why (how) can the program run when it turns out? ? ?