Table of contents
(All written before) The definition of a linear table and its operation:
(All written before) the definition of linked list and its operation:
review:
(All written before) The definition of a linear table and its operation:
Practice all functions of linear tables by yourself:
Create, define Status and node data types
open up space
destroy
empty
Determine whether it is an empty table
return length
Return the value of the i-th element
Find the position of the specified element (locate the specified element bit order)
insert element
delete element
Precursor node
successor node
Unified visit() operation for all elements
that's all
//线性表的定义及其基础操作
#include<iostream>
using namespace std;
#include<stdlib.h>//存放exit
#include<math.h>//OVERFLOW,exit
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXlength 100 //初始大小为100,可按需修改
struct Poly
{
float p;
int e;
bool operator==(Poly t)
{
return t.p == p && t.e == e;
}
bool operator!=(Poly t)
{
return t.p != p || t.e != e;
}
};
struct Sqlist
{
Poly* elem;
int length;
};
typedef int Status; //函数调用状态
//(1):使用引用<模板>
Status InitList(Sqlist L)
{
// L.elem = (表中数据基本单位类型)malloc(MAXlength * sizeof(表中数据基本单位类型));
//C语言写法,C++写法如下:
//L.elem = new 表中数据基本单位类型[MAXlength]; //在堆区开辟动态内存
if (!L.elem)//分配失败
cerr << "error" << endl; //cerr:常被用于输出出错信息,类似cout
L.length = 0;
return false;
}
//(1):使用引用<实例>
Status InitList(Sqlist& L)//前面定义过了typedef int Status;
{
L.elem = new Poly[100]; //在堆区开辟动态内存
if (!L.elem)//分配失败
exit(OVERFLOW);
L.length = 0;
return OK;
}
//(2):使用指针<实例>
/*初始化线性表*/
Status InitList(Sqlist* L)
{
L->elem = (Poly*)malloc(MAXlength * sizeof(Poly*));
if (!L->elem)
exit(OVERFLOW);
L->length = 0;
return OK;
}
void DestroyList(Sqlist& L)
{
if (L.elem) delete L.elem;
}
void ClearList(Sqlist& L)
{
L.length = 0;
//将线性表的长度设置为0
}
int IsEmpty(Sqlist L)
{
if (L.length == 0)
return true;
else
return false;
}
int Listlength(Sqlist L)
{
return L.length;
}
int GetElem(Sqlist L, int i, Poly ele)//element:元素
{
if (i <= 0 || i > L.length)
return ERROR;
ele = L.elem[i - 1];
return OK;
}
int LocateElem(Sqlist L, Poly i)//查找元素
{
for (int a = 0; a <= L.length; a++)
{
if (i == L.elem[a])
return a + 1;
return 0;
}
}
Status LoacteElem(Sqlist L, Poly e)
{
int i = 0;
if (!&L)//初始条件:L存在
return false;
while (L.elem[i] != e)i++;
if (e == L.elem[i])
cout << "元素位序为:" << i + 1 << endl;
return false;
}
Status LoacteElem(Sqlist& L, Poly e)
{
int i = 0;
if (!&L)//初始条件:L存在
return false;
while (*(L.elem) != e)
{
L.elem = L.elem + 1;
i++;
}
if (e == *(L.elem))
cout << "元素位序为:" << i + 1 << endl;
return false;
}
int ListInsert(Sqlist L, int i, Poly e)
{//insert:插入//i:插入位置(位置序号)
if (i<1 || i>L.length + 1)
return ERROR;
if (L.length == MAXlength)return ERROR;//当前储存空间已满
//把插入位置后面的元素全部往后移
for (int j = L.length - 1; j >= i - 1; j--)//j为下标
L.elem[j + 1] = L.elem[j];
//放元素
L.elem[i - 1] = e;
L.length++;
return 0;
}
int ListDelete(Sqlist L, int i)
{
if (i < 1 || i > L.length)
return ERROR;
for (int j = i - 1; j <= L.length - 1; j++)
L.elem[j] = L.elem[j + 1];
L.length--;
}
Status PriorElem(Sqlist& L, Poly e_current, Poly& e_prior)
{
int i = 0;
if (!&L || L.elem[i] != e_current)
//L存在且e_current不是第一个元素
cerr << "无意义" << endl;
return false;
while (L.elem[i] != e_current)i++;
e_prior = L.elem[i - 1];
return true;
}
Status NextElem(Sqlist& L, Poly e_current, Poly& e_next)
{
int i = L.length - 1;
if (!&L || L.elem[i] != e_current)
//L存在且e_current不是最后一个元素
cerr << "无意义" << endl;
return false;
while (L.elem[i] != e_current)i--;
e_next = L.elem[i + 1];
return true;
}
int visit(Sqlist& L)
{
int i = 0;
while (i < L.length - 1)
{
L.elem->p++;
i++;
}
return true;
}
Status ListTraverse(Sqlist& L)
{
if (!&L)
return false;
int visit();
return true;
}
int main()
{
}
When it is really used directly as a pre-statement, we will not write multiple initialization functions to avoid overloading:
//线性表的定义及其基础操作
#include<iostream>
using namespace std;
#include<stdlib.h>//存放exit
#include<math.h>//OVERFLOW,exit
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXlength 100 //初始大小为100,可按需修改
struct Poly
{
float p;
int e;
bool operator==(Poly t)
{
return t.p == p && t.e == e;
}
bool operator!=(Poly t)
{
return t.p != p || t.e != e;
}
};
struct Sqlist
{
Poly* elem;
int length;
};
typedef int Status; //函数调用状态
//(1):使用引用<实例>
Status InitList(Sqlist& L)//前面定义过了typedef int Status;
{
L.elem = new Poly[100]; //在堆区开辟动态内存
if (!L.elem)//分配失败
exit(OVERFLOW);
L.length = 0;
return OK;
}
void DestroyList(Sqlist& L)
{
if (L.elem) delete L.elem;
}
void ClearList(Sqlist& L)
{
L.length = 0;
//将线性表的长度设置为0
}
int IsEmpty(Sqlist L)
{
if (L.length == 0)
return true;
else
return false;
}
int Listlength(Sqlist L)
{
return L.length;
}
int GetElem(Sqlist L, int i, Poly ele)//element:元素
{
if (i <= 0 || i > L.length)
return ERROR;
ele = L.elem[i - 1];
return OK;
}
int LocateElem(Sqlist L, Poly i)//查找元素
{
for (int a = 0; a <= L.length; a++)
{
if (i == L.elem[a])
return a + 1;
return 0;
}
}
Status LoacteElem(Sqlist L, Poly e)
{
int i = 0;
if (!&L)//初始条件:L存在
return false;
while (L.elem[i] != e)i++;
if (e == L.elem[i])
cout << "元素位序为:" << i + 1 << endl;
return false;
}
Status LoacteElem(Sqlist& L, Poly e)
{
int i = 0;
if (!&L)//初始条件:L存在
return false;
while (*(L.elem) != e)
{
L.elem = L.elem + 1;
i++;
}
if (e == *(L.elem))
cout << "元素位序为:" << i + 1 << endl;
return false;
}
int ListInsert(Sqlist L, int i, Poly e)
{//insert:插入//i:插入位置(位置序号)
if (i<1 || i>L.length + 1)
return ERROR;
if (L.length == MAXlength)return ERROR;//当前储存空间已满
//把插入位置后面的元素全部往后移
for (int j = L.length - 1; j >= i - 1; j--)//j为下标
L.elem[j + 1] = L.elem[j];
//放元素
L.elem[i - 1] = e;
L.length++;
return 0;
}
int ListDelete(Sqlist L, int i)
{
if (i < 1 || i > L.length)
return ERROR;
for (int j = i - 1; j <= L.length - 1; j++)
L.elem[j] = L.elem[j + 1];
L.length--;
}
Status PriorElem(Sqlist& L, Poly e_current, Poly& e_prior)
{
int i = 0;
if (!&L || L.elem[i] != e_current)
//L存在且e_current不是第一个元素
cerr << "无意义" << endl;
return false;
while (L.elem[i] != e_current)i++;
e_prior = L.elem[i - 1];
return true;
}
Status NextElem(Sqlist& L, Poly e_current, Poly& e_next)
{
int i = L.length - 1;
if (!&L || L.elem[i] != e_current)
//L存在且e_current不是最后一个元素
cerr << "无意义" << endl;
return false;
while (L.elem[i] != e_current)i--;
e_next = L.elem[i + 1];
return true;
}
int visit(Sqlist& L)
{
int i = 0;
while (i < L.length - 1)
{
L.elem->p++;
i++;
}
return true;
}
Status ListTraverse(Sqlist& L)
{
if (!&L)
return false;
int visit();
return true;
}
(All written before) the definition of linked list and its operation:
Initialize singly linked list
Is the linked list empty
Destroy singly linked list
Clear the linked list
Ask for table length
take the i-th element
Find element order
insert element
delete element
head insertion
tail plugging
that's all
//链表的定义及其基础操作
#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)
{//等价于Lnode *&L
L = new Lnode;
L->next = NULL;
return true;
}
Status 链表是否为空(LinkList L)
{
if (L->next)
return true;
else
return false;
}
Status 销毁单链表(LinkList& L)
{
//判断链表是否为空
if (链表是否为空(L))
{
cerr << "empty List!" << endl;
return false;
}
while (L)//链表还未到达尾端
{
auto temp = L->next;//将头指针指向下一个结点
delete L;
L = temp;
}
return true;
}
Status 清空链表(LinkList L)
{
if (链表是否为空(L))
cerr << "链表为空" << endl;
auto temp1 = L->next;
while (temp1)
{
auto temp2 = temp1->next;
delete temp1;
temp1 = temp2;
}
L->next = NULL;
return true;
}
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
Status 删除(LinkList& L, int i)
{
LinkList p = L, s; int j = 1;
while (p && j < i - 1)
{
p = p->next;
j++;
}
if (!p || j > i - 1)
return false;
s = p->next;
p->next = s->next;
//auto e = s->data;
delete s;
return true;
}
Status 头插法(LinkList& A, int n)
{
//创建空链表
auto L = new Lnode;
L->next = NULL;//别忘了这句
int i = 1;
while (i <= n)
{
auto p = new Lnode;
cin >> p->data.a;
cin >> p->data.b;
cin >> p->data.c;
p->next = L->next;
L->next = p;
i++;
}
A = L;
return true;
}
Status 尾插法(LinkList& A, int n)
{
auto L = new Lnode;
L->next = NULL;
LinkList r = L;
for (auto i = 1; i <= n; i++)
{
auto p = new Lnode;
cin >> p->data.a;
cin >> p->data.b;
cin >> p->data.c;
p->next = NULL;
r->next = p;//尾插
r = p;
}
A = L;
return true;
}
int main()
{
}
I wish you all a happy new year in advance