数据结构与算法课后题-第二章(链表和顺序表综合)

数据结构与算法课后题-第二章(链表和顺序表综合)_第1张图片
数据结构与算法课后题-第二章(链表和顺序表综合)_第2张图片
数据结构与算法课后题-第二章(链表和顺序表综合)_第3张图片
数据结构与算法课后题-第二章(链表和顺序表综合)_第4张图片
数据结构与算法课后题-第二章(链表和顺序表综合)_第5张图片
数据结构与算法课后题-第二章(链表和顺序表综合)_第6张图片
在这里插入图片描述
在这里插入图片描述
数据结构与算法课后题-第二章(链表和顺序表综合)_第7张图片
数据结构与算法课后题-第二章(链表和顺序表综合)_第8张图片
数据结构与算法课后题-第二章(链表和顺序表综合)_第9张图片
数据结构与算法课后题-第二章(链表和顺序表综合)_第10张图片
数据结构与算法课后题-第二章(链表和顺序表综合)_第11张图片
数据结构与算法课后题-第二章(链表和顺序表综合)_第12张图片
数据结构与算法课后题-第二章(链表和顺序表综合)_第13张图片
在这里插入图片描述

#include   //引入头文件
using namespace std;

typedef int Elemtype;

#define Maxsize 100
#define ERROR 0
#define OK    1

typedef struct LNode
{
	Elemtype data;//数据域
	struct LNode* next;//指针域
}LNode, * LinkList;

bool ListEmpty(LinkList L)  //判断是否为空
{
	if (L!= NULL)
	{
		cout << "not empty,不空" << endl;
		return OK;
	}
	else
	{
		cout << "empty" << endl;
		return ERROR;
	}
}

LinkList List_HeadInsert(LinkList& L)  //头插法
{
	LNode* s = NULL;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		s->next = L;
		L = s;
		if (cin.get() == '\n') break;
	}
	return L;
}
//------------------------算法核心------------------------//
bool Del_X(LinkList& L, Elemtype x)
{
	LNode* p = NULL;
	if (L == NULL)
		return ERROR;
	if (L->data == x)
	{
		p = L;
		L = L->next;
		free(p);
		Del_X(L, x);   //
	}
	else
		Del_X(L->next, x);//
}

//------------------------算法核心------------------------//
int main(void)
{
	LinkList L = NULL;
	List_HeadInsert(L);  //for example 1 3 3 4 5
	cout << L << " " << L->data << " " << L->next << endl;
	cout << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	cout << L->next->next->next->next << " " << L->next->next->next->next->data << " " << L->next->next->next->next->next << endl;
	ListEmpty(L);
	Del_X(L, 3);
	cout << L << " " << L->data << " " << L->next << endl;
	cout << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	return 0;
}



在这里插入图片描述

#include   //引入头文件
using namespace std;

typedef int Elemtype;

#define Maxsize 100
#define ERROR 0
#define OK    1

typedef struct LNode
{
	Elemtype data;//数据域
	struct LNode* next;//指针域
}LNode, * LinkList;

bool InitList(LinkList& L)   //初始化
{
	L = (LinkList)malloc(sizeof(LNode));
	if (L == NULL)
		return ERROR;
	L->data = 0;
	L->next = NULL;
	return OK;
}

bool ListEmpty(LinkList L)  //判断是否为空
{
	if (L->next != NULL)
	{
		cout << "not empty" << endl;
		return OK;
	}
	else
	{
		cout << "empty,只有头节点" << endl;
		return ERROR;
	}
}

int ListLength(LinkList L)
{
	LNode* p;
	p = L->next;
	int i = 0;
	while (p != NULL)
	{
		p = p->next;
		i++;
	}
	return i;
}

LinkList List_HeadInsert(LinkList& L)  //头插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		s->next = L->next;
		L->next = s;
		if (cin.get() == '\n') break;
	}
	return L;
}

LinkList List_TailInsert(LinkList& L)  //尾插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL, * r = NULL;
	r = L;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		r->next = s;
		r = s;
		if (cin.get() == '\n') break;
	}
	s->next = NULL;
	return L;
}

//------------------------算法核心------------------------//
bool Del_X(LinkList& L, Elemtype x)
{
	LNode* p = L->next;
	LNode* pre = L;
	LNode* q = NULL;
	if (L->next == NULL)
		return ERROR;
	while (p != NULL)
	{
		if (p->data == x)
		{
			q = p;
			p = p->next;
			pre->next = p;
			free(q);
		}
		else
		{
			pre = p;
			p = p->next;
		}
	}
}
//------------------------算法核心------------------------//

int main(void)
{
	LinkList L = NULL;
	List_TailInsert(L); //尾插法
	cout << L << " " << L->data << " " << L->next << endl;
	cout << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	cout << L->next->next->next->next << " " << L->next->next->next->next->data << " " << L->next->next->next->next->next << endl;
	cout << L->next->next->next->next->next << " " << L->next->next->next->next->next->data << " " << L->next->next->next->next->next->next << endl;
	ListEmpty(L);
	Del_X(L, 3);
	cout << L << " " << L->data << " " << L->next << endl;
	cout << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	return 0;
}

在这里插入图片描述

#include   //引入头文件
using namespace std;

typedef int Elemtype;

#define Maxsize 100
#define ERROR 0
#define OK    1

typedef struct LNode
{
	Elemtype data;//数据域
	struct LNode* next;//指针域
}LNode, * LinkList;

bool InitList(LinkList& L)   //初始化
{
	L = (LinkList)malloc(sizeof(LNode));
	if (L == NULL)
		return ERROR;
	L->data = 0;
	L->next = NULL;
	return OK;
}

bool ListEmpty(LinkList L)  //判断是否为空
{
	if (L->next != NULL)
	{
		cout << "not empty" << endl;
		return OK;
	}
	else
	{
		cout << "empty,只有头节点" << endl;
		return ERROR;
	}
}

int ListLength(LinkList L)
{
	LNode* p;
	p = L->next;
	int i = 0;
	while (p != NULL)
	{
		p = p->next;
		i++;
	}
	return i;
}

LinkList List_HeadInsert(LinkList& L)  //头插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		s->next = L->next;
		L->next = s;
		if (cin.get() == '\n') break;
	}
	return L;
}

LinkList List_TailInsert(LinkList& L)  //尾插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL, * r = NULL;
	r = L;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		r->next = s;
		r = s;
		if (cin.get() == '\n') break;
	}
	s->next = NULL;
	return L;
}

//------------------------算法核心------------------------//
void R_Print(LinkList L)
{
	if (L->next != NULL)
		R_Print(L->next);
	if (L != NULL) printf("L->data=%d\n",L->data);
}
void R_Ignore_Head(LinkList L)
{
	if (L->next != NULL) R_Print(L->next);
}

//------------------------算法核心------------------------//
int main(void)
{
	LinkList L = NULL;
	List_TailInsert(L); //尾插法
	cout << L << " " << L->data << " " << L->next << endl;
	cout << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	cout << L->next->next->next->next << " " << L->next->next->next->next->data << " " << L->next->next->next->next->next << endl;
	cout << L->next->next->next->next->next << " " << L->next->next->next->next->next->data << " " << L->next->next->next->next->next->next << endl;
	ListEmpty(L);
	R_Ignore_Head(L);
	return 0;
}

在这里插入图片描述

#include   //引入头文件
using namespace std;

typedef int Elemtype;

#define Maxsize 100
#define ERROR 0
#define OK    1

typedef struct LNode
{
	Elemtype data;//数据域
	struct LNode* next;//指针域
}LNode, * LinkList;

bool InitList(LinkList& L)   //初始化
{
	L = (LinkList)malloc(sizeof(LNode));
	if (L == NULL)
		return ERROR;
	L->data = 0;
	L->next = NULL;
	return OK;
}

bool ListEmpty(LinkList L)  //判断是否为空
{
	if (L->next != NULL)
	{
		cout << "not empty" << endl;
		return OK;
	}
	else
	{
		cout << "empty,只有头节点" << endl;
		return ERROR;
	}
}

int ListLength(LinkList L)
{
	LNode* p;
	p = L->next;
	int i = 0;
	while (p != NULL)
	{
		p = p->next;
		i++;
	}
	return i;
}

LinkList List_HeadInsert(LinkList& L)  //头插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		s->next = L->next;
		L->next = s;
		if (cin.get() == '\n') break;
	}
	return L;
}

LinkList List_TailInsert(LinkList& L)  //尾插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL, * r = NULL;
	r = L;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		r->next = s;
		r = s;
		if (cin.get() == '\n') break;
	}
	s->next = NULL;
	return L;
}

//------------------------算法核心------------------------//
/*算法思想:用p从头至尾扫描单链表,pre指向* p结点的前驱,用minp保存值最小的结
点指针(初值为p), minpre指向* minp结点的前驱(初值为pre)。一边扫描,一边比较,若
p->data小于minp->data, 则将p、pre分别赋值给minp、minpre。当p扫描完毕时,
minp指向最小值结点,minpre指向最小值结点的前驱结点,再将minp所指结点删
除即可*/

void Delete_Min(LinkList& L)
{
	LNode* pre = L, * p = pre->next; //p为工作指针,pre指向其前驱
	LNode* minpre = pre, * minp = p; //保存最小值结点及其前驱
	while (p != NULL)
	{
		if (p->data < minp->data) //保存最小值结点及其前驱
		{
			minp = p;
			minpre = pre;
		}
		pre = p;
		p = p->next;//继续扫描下一个结点
	}
	minpre->next = minp->next;
	free(minp);
}
//------------------------算法核心------------------------//
int main(void)
{
	LinkList L = NULL;
	List_TailInsert(L); //尾插法
	cout << L << " " << L->data << " " << L->next << endl;
	cout << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	cout << L->next->next->next->next << " " << L->next->next->next->next->data << " " << L->next->next->next->next->next << endl;
	cout << L->next->next->next->next->next << " " << L->next->next->next->next->next->data << " " << L->next->next->next->next->next->next << endl;
	ListEmpty(L);
	Delete_Min(L);
	cout << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	cout << L->next->next->next->next << " " << L->next->next->next->next->data << " " << L->next->next->next->next->next << endl;
	return 0;
}

在这里插入图片描述

#include   //引入头文件
using namespace std;

typedef int Elemtype;

#define Maxsize 100
#define ERROR 0
#define OK    1

typedef struct LNode
{
	Elemtype data;//数据域
	struct LNode* next;//指针域
}LNode, * LinkList;

bool InitList(LinkList& L)   //初始化
{
	L = (LinkList)malloc(sizeof(LNode));
	if (L == NULL)
		return ERROR;
	L->data = 0;
	L->next = NULL;
	return OK;
}

bool ListEmpty(LinkList L)  //判断是否为空
{
	if (L->next != NULL)
	{
		cout << "not empty" << endl;
		return OK;
	}
	else
	{
		cout << "empty,只有头节点" << endl;
		return ERROR;
	}
}

int ListLength(LinkList L)
{
	LNode* p;
	p = L->next;
	int i = 0;
	while (p != NULL)
	{
		p = p->next;
		i++;
	}
	return i;
}

LinkList List_HeadInsert(LinkList& L)  //头插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		s->next = L->next;
		L->next = s;
		if (cin.get() == '\n') break;
	}
	return L;
}

LinkList List_TailInsert(LinkList& L)  //尾插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL, * r = NULL;
	r = L;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		r->next = s;
		r = s;
		if (cin.get() == '\n') break;
	}
	s->next = NULL;
	return L;
}

//------------------------算法核心------------------------//
LinkList Reverse_1(LinkList L)
{
	LNode* p, * r;
	p = L->next;
	L->next = NULL;
	while (p != NULL)
	{
		r = p->next;
		p->next = L->next;
		L->next = p;
		p = r;
	}
	return L;
}
LinkList Reverse_2(LinkList L)
{
	LNode* pre, * p = L->next, * r = p->next;
	p->next = NULL;
	while (r != NULL)
	{
		pre = p;
		p = r;
		r = r->next;
		p->next = pre;
	}
	L->next = p;
	return L;
}
//------------------------算法核心------------------------//
int main(void)
{
	LinkList L = NULL;
	List_TailInsert(L); //尾插法
	cout << L << " " << L->data << " " << L->next << endl;
	cout << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	cout << L->next->next->next->next << " " << L->next->next->next->next->data << " " << L->next->next->next->next->next << endl;
	cout << L->next->next->next->next->next << " " << L->next->next->next->next->next->data << " " << L->next->next->next->next->next->next << endl;
	ListEmpty(L);
	//Reverse_1(L);
	Reverse_2(L);
	cout << L << " " << L->data << " " << L->next << endl;
	cout << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	cout << L->next->next->next->next << " " << L->next->next->next->next->data << " " << L->next->next->next->next->next << endl;
	cout << L->next->next->next->next->next << " " << L->next->next->next->next->next->data << " " << L->next->next->next->next->next->next << endl;
	return 0;
}

在这里插入图片描述

#include   //引入头文件
using namespace std;

typedef int Elemtype;

#define Maxsize 100
#define ERROR 0
#define OK    1

typedef struct LNode
{
	Elemtype data;//数据域
	struct LNode* next;//指针域
}LNode, * LinkList;

bool InitList(LinkList& L)   //初始化
{
	L = (LinkList)malloc(sizeof(LNode));
	if (L == NULL)
		return ERROR;
	L->data = 0;
	L->next = NULL;
	return OK;
}

bool ListEmpty(LinkList L)  //判断是否为空
{
	if (L->next != NULL)
	{
		cout << "not empty" << endl;
		return OK;
	}
	else
	{
		cout << "empty,只有头节点" << endl;
		return ERROR;
	}
}

int ListLength(LinkList L)
{
	LNode* p;
	p = L->next;
	int i = 0;
	while (p != NULL)
	{
		p = p->next;
		i++;
	}
	return i;
}

LinkList List_HeadInsert(LinkList& L)  //头插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		s->next = L->next;
		L->next = s;
		if (cin.get() == '\n') break;
	}
	return L;
}

LinkList List_TailInsert(LinkList& L)  //尾插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL, * r = NULL;
	r = L;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		r->next = s;
		r = s;
		if (cin.get() == '\n') break;
	}
	s->next = NULL;
	return L;
}

//------------------------算法核心------------------------//
/*
算法思想:采用直接插入排序算法的思想,先构成只含一个数据结点的有序单链表,然后依
次扫描单链表中剩下的结点(直至p==NULL为止),在有序表中通过比较查找插入*p的前驱结点*pre,
然后将*p插入到*pre之后
*/
void Sort(LinkList& L)
{
	LNode* p = L->next, * pre;
	LNode* r = p->next;  //r保持*p后继结点指针,以保证不断链
	p->next = NULL;  //构造只含一个数据结点的有序表
	p = r;
	while (p != NULL)
	{
		r = p->next;
		pre = L;
		while (pre->next != NULL && pre->next->data < p->data)
			pre = pre->next;
		p->next = pre->next;
		pre->next = p;
		p = r;
	}
}
//------------------------算法核心------------------------//
int main(void)
{
	LinkList L = NULL;
	List_TailInsert(L); //尾插法
	cout << L << " " << L->data << " " << L->next << endl;
	cout << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	cout << L->next->next->next->next << " " << L->next->next->next->next->data << " " << L->next->next->next->next->next << endl;
	cout << L->next->next->next->next->next << " " << L->next->next->next->next->next->data << " " << L->next->next->next->next->next->next << endl;
	ListEmpty(L);
	Sort(L);
	cout << L << " " << L->data << " " << L->next << endl;
	cout << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	cout << L->next->next->next->next << " " << L->next->next->next->next->data << " " << L->next->next->next->next->next << endl;
	cout << L->next->next->next->next->next << " " << L->next->next->next->next->next->data << " " << L->next->next->next->next->next->next << endl;

	return 0;
}

在这里插入图片描述

#include   //引入头文件
using namespace std;

typedef int Elemtype;

#define Maxsize 100
#define ERROR 0
#define OK    1

typedef struct LNode
{
	Elemtype data;//数据域
	struct LNode* next;//指针域
}LNode, * LinkList;

bool InitList(LinkList& L)   //初始化
{
	L = (LinkList)malloc(sizeof(LNode));
	if (L == NULL)
		return ERROR;
	L->data = 0;
	L->next = NULL;
	return OK;
}

bool ListEmpty(LinkList L)  //判断是否为空
{
	if (L->next != NULL)
	{
		cout << "not empty" << endl;
		return OK;
	}
	else
	{
		cout << "empty,只有头节点" << endl;
		return ERROR;
	}
}

int ListLength(LinkList L)
{
	LNode* p;
	p = L->next;
	int i = 0;
	while (p != NULL)
	{
		p = p->next;
		i++;
	}
	return i;
}

LinkList List_HeadInsert(LinkList& L)  //头插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		s->next = L->next;
		L->next = s;
		if (cin.get() == '\n') break;
	}
	return L;
}

LinkList List_TailInsert(LinkList& L)  //尾插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL, * r = NULL;
	r = L;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		r->next = s;
		r = s;
		if (cin.get() == '\n') break;
	}
	s->next = NULL;
	return L;
}

//------------------------算法核心------------------------//
void RangeDelete(LinkList& L, int min,int max)
{
	LNode* p = L->next, * pre = L, *q = NULL;
	while (p != NULL)
	{
		if ((min < (p->data)) && ((p->data) < max))
		{
			q = p;
			pre->next = p->next;
			p = p->next;
			free(q);
		}
		pre = p;
		p = p->next;
	}
}
//------------------------算法核心------------------------//
int main(void)
{
	LinkList L = NULL;
	List_TailInsert(L); //尾插法
	cout << L << " " << L->data << " " << L->next << endl;
	cout << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	cout << L->next->next->next->next << " " << L->next->next->next->next->data << " " << L->next->next->next->next->next << endl;
	cout << L->next->next->next->next->next << " " << L->next->next->next->next->next->data << " " << L->next->next->next->next->next->next << endl;
	ListEmpty(L);
	RangeDelete(L, 3, 5);
	cout << L << " " << L->data << " " << L->next << endl;
	cout << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	cout << L->next->next->next->next << " " << L->next->next->next->next->data << " " << L->next->next->next->next->next << endl;
}

在这里插入图片描述

#include   //引入头文件
using namespace std;

typedef int Elemtype;

#define Maxsize 100
#define ERROR 0
#define OK    1

typedef struct LNode
{
	Elemtype data;//数据域
	struct LNode* next;//指针域
}LNode, * LinkList;

bool InitList(LinkList& L)   //初始化
{
	L = (LinkList)malloc(sizeof(LNode));
	if (L == NULL)
		return ERROR;
	L->data = 0;
	L->next = NULL;
	return OK;
}

bool ListEmpty(LinkList L)  //判断是否为空
{
	if (L->next != NULL)
	{
		cout << "not empty" << endl;
		return OK;
	}
	else
	{
		cout << "empty,只有头节点" << endl;
		return ERROR;
	}
}

int ListLength(LinkList L)
{
	LNode* p;
	p = L->next;
	int i = 0;
	while (p != NULL)
	{
		p = p->next;
		i++;
	}
	return i;
}

LinkList List_HeadInsert(LinkList& L)  //头插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		s->next = L->next;
		L->next = s;
		if (cin.get() == '\n') break;
	}
	return L;
}

LinkList List_TailInsert(LinkList& L)  //尾插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL, * r = NULL;
	r = L;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		r->next = s;
		r = s;
		if (cin.get() == '\n') break;
	}
	s->next = NULL;
	return L;
}

//------------------------算法核心------------------------//
LinkList Search_Common(LinkList L1, LinkList L2)
{
	int length1 = 0, length2 = 0;
	int distance = 0;
	length1=ListLength(L1);
	length2=ListLength(L2);
	cout << "length1=" << length1 << "      length2=" << length2 << endl;
	LinkList longList=NULL, shortList= NULL;
	if (length1 > length2)
	{
		longList = L1->next;
		shortList = L2->next;
		distance = length1 - length2;
	}
	else
	{
		longList = L2->next;
		shortList = L1->next;
		distance = length2 - length1;
	}
	while (distance--)
		longList = longList->next;
	while (longList != NULL)
	{
		if (longList = shortList)
			return longList;
		else
		{
			longList = longList->next;
			shortList = shortList->next;
		}
	}
	return NULL;
}
//------------------------算法核心------------------------//
int main(void)
{
	LinkList L1 = NULL;
	List_TailInsert(L1); //尾插法
	cout << L1 << " " << L1->data << " " << L1->next << endl;
	cout << L1->next << " " << L1->next->data << " " << L1->next->next << endl;
	cout << L1->next->next << " " << L1->next->next->data << " " << L1->next->next->next << endl;
	cout << L1->next->next->next << " " << L1->next->next->next->data << " " << L1->next->next->next->next << endl;
	cout << L1->next->next->next->next << " " << L1->next->next->next->next->data << " " << L1->next->next->next->next->next << endl;
	cout << L1->next->next->next->next->next << " " << L1->next->next->next->next->next->data << " " << L1->next->next->next->next->next->next << endl;
	ListEmpty(L1);
	LinkList L2 = NULL;
	List_TailInsert(L2); //尾插法
	cout << L2 << " " << L2->data << " " << L2->next << endl;
	cout << L2->next << " " << L2->next->data << " " << L2->next->next << endl;
	cout << L2->next->next << " " << L2->next->next->data << " " << L2->next->next->next << endl;
	cout << L2->next->next->next << " " << L2->next->next->next->data << " " << L2->next->next->next->next << endl;
	ListEmpty(L2);
	cout<<"same:  "<<Search_Common(L1, L1)<<endl;
	return 0;
}

在这里插入图片描述

#include   //引入头文件
using namespace std;

typedef int Elemtype;

#define Maxsize 100
#define ERROR 0
#define OK    1

typedef struct LNode
{
	Elemtype data;//数据域
	struct LNode* next;//指针域
}LNode, * LinkList;

bool InitList(LinkList& L)   //初始化
{
	L = (LinkList)malloc(sizeof(LNode));
	if (L == NULL)
		return ERROR;
	L->data = 0;
	L->next = NULL;
	return OK;
}

bool ListEmpty(LinkList L)  //判断是否为空
{
	if (L->next != NULL)
	{
		cout << "not empty" << endl;
		return OK;
	}
	else
	{
		cout << "empty,只有头节点" << endl;
		return ERROR;
	}
}

int ListLength(LinkList L)
{
	LNode* p;
	p = L->next;
	int i = 0;
	while (p != NULL)
	{
		p = p->next;
		i++;
	}
	return i;
}

LinkList List_HeadInsert(LinkList& L)  //头插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		s->next = L->next;
		L->next = s;
		if (cin.get() == '\n') break;
	}
	return L;
}

LinkList List_TailInsert(LinkList& L)  //尾插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL, * r = NULL;
	r = L;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		r->next = s;
		r = s;
		if (cin.get() == '\n') break;
	}
	s->next = NULL;
	return L;
}

//------------------------算法核心------------------------//
void Min__Delete(LinkList &head)  //1 5 4  100 88
{
	while (head->next != NULL)
	{
		LNode* pre = head;
		LNode* p = pre->next;
		LNode* u;
		while (p->next != NULL)
		{
			if (p->next->data < pre->next->data)
				pre = p;
			p = p->next;
		}
		cout<<pre->next->data<<endl;
		u = pre->next;
		pre->next = u->next;
		free(u);
	}
	free(head);
}
//------------------------算法核心------------------------//
int main(void)
{
	LinkList L = NULL;
	List_TailInsert(L); //尾插法
	cout << L << " " << L->data << " " << L->next << endl;
	cout << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	cout << L->next->next->next->next << " " << L->next->next->next->next->data << " " << L->next->next->next->next->next << endl;
	cout << L->next->next->next->next->next << " " << L->next->next->next->next->next->data << " " << L->next->next->next->next->next->next << endl;
	ListEmpty(L);
	Min__Delete(L);
	return 0;
}

在这里插入图片描述

#include   //引入头文件
using namespace std;

typedef int Elemtype;

#define Maxsize 100
#define ERROR 0
#define OK    1

typedef struct LNode
{
	Elemtype data;//数据域
	struct LNode* next;//指针域
}LNode, * LinkList;

bool InitList(LinkList& L)   //初始化
{
	L = (LinkList)malloc(sizeof(LNode));
	if (L == NULL)
		return ERROR;
	L->data = 0;
	L->next = NULL;
	return OK;
}

bool ListEmpty(LinkList L)  //判断是否为空
{
	if (L->next != NULL)
	{
		cout << "not empty" << endl;
		return OK;
	}
	else
	{
		cout << "empty,只有头节点" << endl;
		return ERROR;
	}
}

int ListLength(LinkList L)
{
	LNode* p;
	p = L->next;
	int i = 0;
	while (p != NULL)
	{
		p = p->next;
		i++;
	}
	return i;
}

LinkList List_HeadInsert(LinkList& L)  //头插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		s->next = L->next;
		L->next = s;
		if (cin.get() == '\n') break;
	}
	return L;
}

LinkList List_TailInsert(LinkList& L)  //尾插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL, * r = NULL;
	r = L;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		r->next = s;
		r = s;
		if (cin.get() == '\n') break;
	}
	s->next = NULL;
	return L;
}

//------------------------算法核心------------------------//
LinkList DisCreate(LinkList& A)
{
	int i = 0;
	LinkList B = (LinkList)malloc(sizeof(LNode));
	B->next = NULL;
	LNode* ra = A, * rb = B, * p;
	p = A->next;
	A->next = NULL;
	while (p != NULL)
	{
		i++;
		if (i % 2 == 0)
		{
			rb->next = p;
			rb = p;
		}
		else
		{
			ra->next = p;
			ra = p;
		}
		p = p->next;
	}
	ra->next = NULL;
	rb->next = NULL;
	return B;
}
//------------------------算法核心------------------------//
int main(void)
{
	LinkList L = NULL;
	List_TailInsert(L); //尾插法   //1 2 3 4 5
	cout << "-------------------------------原始数据----------------------------" << endl;
	cout << "--------节点地址-数据-指针--------" << endl;
	cout <<"头节点: " << L << " " << L->data << " " << L->next << endl;
	cout << "数据1 : " << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << "数据2 : " << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << "数据3 : " << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	cout << "数据4 : " << L->next->next->next->next << " " << L->next->next->next->next->data << " " << L->next->next->next->next->next << endl;
	cout << "数据5 : " << L->next->next->next->next->next << " " << L->next->next->next->next->next->data << " " << L->next->next->next->next->next->next << endl;
	cout << "-------------------------------奇数数据-----------------------------"<<endl;
	LinkList L1 = NULL;
	L1 = DisCreate(L);
	L1->data = 0;
	cout << "--------节点地址-数据-指针--------" << endl;
	cout <<"头节点: " << L << " " << L->data << " " << L->next << endl;
	cout << "数据1: " << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << "数据2: " << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << "数据3: " << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	cout << "-------------------------------偶数数据-----------------------------" << endl;
	cout << "--------节点地址-数据-指针--------" << endl;
	cout << "头节点: " << L1 << " " << L1->data << " "<< L1->next << endl;
	cout << "数据1: " << L1->next << " " << L1->next->data << " " << L1->next->next << endl;
	cout << "数据2: " << L1->next->next << " " << L1->next->next->data << " " << L1->next->next->next << endl;
	return 0;
}

在这里插入图片描述

#include   //引入头文件
using namespace std;

typedef int Elemtype;

#define Maxsize 100
#define ERROR 0
#define OK    1

typedef struct LNode
{
	Elemtype data;//数据域
	struct LNode* next;//指针域
}LNode, * LinkList;

bool InitList(LinkList& L)   //初始化
{
	L = (LinkList)malloc(sizeof(LNode));
	if (L == NULL)
		return ERROR;
	L->data = 0;
	L->next = NULL;
	return OK;
}

bool ListEmpty(LinkList L)  //判断是否为空
{
	if (L->next != NULL)
	{
		cout << "not empty" << endl;
		return OK;
	}
	else
	{
		cout << "empty,只有头节点" << endl;
		return ERROR;
	}
}

int ListLength(LinkList L)
{
	LNode* p;
	p = L->next;
	int i = 0;
	while (p != NULL)
	{
		p = p->next;
		i++;
	}
	return i;
}

LinkList List_HeadInsert(LinkList& L)  //头插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		s->next = L->next;
		L->next = s;
		if (cin.get() == '\n') break;
	}
	return L;
}

LinkList List_TailInsert(LinkList& L)  //尾插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL, * r = NULL;
	r = L;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		r->next = s;
		r = s;
		if (cin.get() == '\n') break;
	}
	s->next = NULL;
	return L;
}

//------------------------算法核心------------------------//
LinkList DisCreate(LinkList& A)
{
	LinkList B = (LinkList)malloc(sizeof(LNode));
	B->next = NULL;
	LNode* p = A->next, * q;
	LNode* ra = A;
	while (p != NULL)
	{
		ra->next = p;
		ra = p;
		p = p->next;
		if (p != NULL)
		{
			q = p->next;
			p->next = B->next;
			B->next = p;
			p = q;
		}
	}
	ra->next = NULL;
	return B;
}
//------------------------算法核心------------------------//
int main(void)
{
	LinkList L = NULL;
	List_TailInsert(L); //尾插法   //1 2 3 4 5
	cout << "-------------------------------原始数据----------------------------" << endl;
	cout << "--------节点地址-数据-指针--------" << endl;
	cout << "头节点: " << L << " " << L->data << " " << L->next << endl;
	cout << "数据1 : " << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << "数据2 : " << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << "数据3 : " << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	cout << "数据4 : " << L->next->next->next->next << " " << L->next->next->next->next->data << " " << L->next->next->next->next->next << endl;
	cout << "数据5 : " << L->next->next->next->next->next << " " << L->next->next->next->next->next->data << " " << L->next->next->next->next->next->next << endl;
	cout << "-------------------------------奇数数据-----------------------------" << endl;
	LinkList L1 = NULL;
	L1 = DisCreate(L);
	L1->data = 0;
	cout << "--------节点地址-数据-指针--------" << endl;
	cout << "头节点: " << L << " " << L->data << " " << L->next << endl;
	cout << "数据1: " << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << "数据2: " << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << "数据3: " << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	cout << "-------------------------------偶数数据-----------------------------" << endl;
	cout << "--------节点地址-数据-指针--------" << endl;
	cout << "头节点: " << L1 << " " << L1->data << " " << L1->next << endl;
	cout << "数据1: " << L1->next << " " << L1->next->data << " " << L1->next->next << endl;
	cout << "数据2: " << L1->next->next << " " << L1->next->next->data << " " << L1->next->next->next << endl;
	return 0;
}

数据结构与算法课后题-第二章(链表和顺序表综合)_第14张图片

#include   //引入头文件
using namespace std;

typedef int Elemtype;

#define Maxsize 100
#define ERROR 0
#define OK    1

typedef struct LNode
{
	Elemtype data;//数据域
	struct LNode* next;//指针域
}LNode, * LinkList;

bool InitList(LinkList& L)   //初始化
{
	L = (LinkList)malloc(sizeof(LNode));
	if (L == NULL)
		return ERROR;
	L->data = 0;
	L->next = NULL;
	return OK;
}

bool ListEmpty(LinkList L)  //判断是否为空
{
	if (L->next != NULL)
	{
		cout << "not empty" << endl;
		return OK;
	}
	else
	{
		cout << "empty,只有头节点" << endl;
		return ERROR;
	}
}

int ListLength(LinkList L)
{
	LNode* p;
	p = L->next;
	int i = 0;
	while (p != NULL)
	{
		p = p->next;
		i++;
	}
	return i;
}

LinkList List_HeadInsert(LinkList& L)  //头插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		s->next = L->next;
		L->next = s;
		if (cin.get() == '\n') break;
	}
	return L;
}

LinkList List_TailInsert(LinkList& L)  //尾插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL, * r = NULL;
	r = L;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		r->next = s;
		r = s;
		if (cin.get() == '\n') break;
	}
	s->next = NULL;
	return L;
}

//------------------------算法核心------------------------//
bool Delete_same(LinkList& L)
{
	LNode* p = L->next;
	LNode* q = NULL;
	if (p == NULL)
		return ERROR;
	while (p->next!= NULL)
	{
		q = p->next;
		if (p->data == q->data)
		{
			p->next = q->next;
			free(q);
		}
		else
			p = p->next;
	}
	return OK;
}
//------------------------算法核心------------------------//
int main(void)
{
	LinkList L = NULL;
	List_TailInsert(L); //尾插法   //1 2 2 2 5
	cout << "-------------------------------原始数据----------------------------" << endl;
	cout << "--------节点地址-数据-指针--------" << endl;
	cout << "头节点: " << L << " " << L->data << " " << L->next << endl;
	cout << "数据1 : " << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << "数据2 : " << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << "数据3 : " << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	cout << "数据4 : " << L->next->next->next->next << " " << L->next->next->next->next->data << " " << L->next->next->next->next->next << endl;
	cout << "数据5 : " << L->next->next->next->next->next << " " << L->next->next->next->next->next->data << " " << L->next->next->next->next->next->next << endl;
	Delete_same(L);
	cout << "-------------------------------修改后数据----------------------------" << endl;
	cout << "--------节点地址-数据-指针--------" << endl;
	cout << "头节点: " << L << " " << L->data << " " << L->next << endl;
	cout << "数据1 : " << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << "数据2 : " << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << "数据3 : " << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	return 0;
}

在这里插入图片描述

#include   //引入头文件
using namespace std;

typedef int Elemtype;

#define Maxsize 100
#define ERROR 0
#define OK    1

typedef struct LNode
{
	Elemtype data;//数据域
	struct LNode* next;//指针域
}LNode, * LinkList;

bool InitList(LinkList& L)   //初始化
{
	L = (LinkList)malloc(sizeof(LNode));
	if (L == NULL)
		return ERROR;
	L->data = 0;
	L->next = NULL;
	return OK;
}

bool ListEmpty(LinkList L)  //判断是否为空
{
	if (L->next != NULL)
	{
		cout << "not empty" << endl;
		return OK;
	}
	else
	{
		cout << "empty,只有头节点" << endl;
		return ERROR;
	}
}

int ListLength(LinkList L)
{
	LNode* p;
	p = L->next;
	int i = 0;
	while (p != NULL)
	{
		p = p->next;
		i++;
	}
	return i;
}

LinkList List_HeadInsert(LinkList& L)  //头插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		s->next = L->next;
		L->next = s;
		if (cin.get() == '\n') break;
	}
	return L;
}

LinkList List_TailInsert(LinkList& L)  //尾插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL, * r = NULL;
	r = L;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		r->next = s;
		r = s;
		if (cin.get() == '\n') break;
	}
	s->next = NULL;
	return L;
}

//------------------------算法核心------------------------//
void MergeList(LinkList& La, LinkList& Lb)
{
	LNode* r, * pa = La->next, * pb = Lb->next;
	La->next = NULL;
	while (pa && pb)
		if (pa->data <= pb->data)
		{
			r = pa->next;
			pa->next = La->next;
			La->next = pa;
			pa = r;
		}
		else
		{
			r = pb->next;
			pb->next = La->next;
			La->next = pb;
			pb = r;
		}
	if (pa)
		pb = pa;
	while (pb)
	{
		r = pb->next;
		pb->next = La->next;
		La->next = pb;
		pb = r;
	}
	free(Lb);
}
//------------------------算法核心------------------------//
int main(void)
{
	LinkList L = NULL;
	List_TailInsert(L); //尾插法   //1 2 3
	cout << "-------------------------------L原始数据----------------------------" << endl;
	cout << "--------节点地址-数据-指针--------" << endl;
	cout << "头节点: " << L << " " << L->data << " " << L->next << endl;
	cout << "数据1 : " << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << "数据2 : " << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << "数据3 : " << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	LinkList L1 = NULL;
	List_TailInsert(L1); //尾插法   //4 5
	cout << "-------------------------------L1原始数据----------------------------" << endl;
	cout << "--------节点地址-数据-指针--------" << endl;
	cout << "头节点: " << L1 << " " << L1->data << " " << L1->next << endl;
	cout << "数据1 : " << L1->next << " " << L1->next->data << " " << L1->next->next << endl;
	cout << "数据2 : " << L1->next->next << " " << L1->next->next->data << " " << L1->next->next->next << endl;
	MergeList(L, L1);
	cout << "-------------------------------修改后数据----------------------------" << endl;
	cout << "--------节点地址-数据-指针--------" << endl;
	cout << "头节点: " << L << " " << L->data << " " << L->next << endl;
	cout << "数据1 : " << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << "数据2 : " << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << "数据3 : " << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	cout << "数据4 : " << L->next->next->next->next << " " << L->next->next->next->next->data << " " << L->next->next->next->next->next << endl;
	cout << "数据5 : " << L->next->next->next->next->next << " " << L->next->next->next->next->next->data << " " << L->next->next->next->next->next->next << endl;
	return 0;
}

在这里插入图片描述

#include   //引入头文件
using namespace std;

typedef int Elemtype;

#define Maxsize 100
#define ERROR 0
#define OK    1

typedef struct LNode
{
	Elemtype data;//数据域
	struct LNode* next;//指针域
}LNode, * LinkList;

bool InitList(LinkList& L)   //初始化
{
	L = (LinkList)malloc(sizeof(LNode));
	if (L == NULL)
		return ERROR;
	L->data = 0;
	L->next = NULL;
	return OK;
}

bool ListEmpty(LinkList L)  //判断是否为空
{
	if (L->next != NULL)
	{
		cout << "not empty" << endl;
		return OK;
	}
	else
	{
		cout << "empty,只有头节点" << endl;
		return ERROR;
	}
}

int ListLength(LinkList L)
{
	LNode* p;
	p = L->next;
	int i = 0;
	while (p != NULL)
	{
		p = p->next;
		i++;
	}
	return i;
}

LinkList List_HeadInsert(LinkList& L)  //头插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		s->next = L->next;
		L->next = s;
		if (cin.get() == '\n') break;
	}
	return L;
}

LinkList List_TailInsert(LinkList& L)  //尾插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL, * r = NULL;
	r = L;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		r->next = s;
		r = s;
		if (cin.get() == '\n') break;
	}
	s->next = NULL;
	return L;
}

//------------------------算法核心------------------------//
LinkList Get_Common(LinkList A, LinkList B)
{
	LinkList C = (LinkList)malloc(sizeof(LNode));
	LNode* pa = A->next;
	LNode* pb = B->next;
	LNode* r = C;
	LNode* s = NULL;
	while ((pa != NULL) && (pb != NULL))
	{
		if (pa->data < pb->data)
			pa = pa->next;
		else if (pa->data > pb->data)
			pb = pb->next;
		else
		{
			LNode* s = (LinkList)malloc(sizeof(LNode));
			s->data = pa->data;
			r->next = s;
			r = s;
			pa = pa->next;
			pb = pb->next;
		}
	}
	r->next = NULL;
	return C;
}
//------------------------算法核心------------------------//
int main(void)
{
	LinkList A = NULL;
	List_TailInsert(A); //尾插法   //1 3 4 5 8
	cout << "-------------------------------L原始数据----------------------------" << endl;
	cout << "--------节点地址-数据-指针--------" << endl;
	cout << "头节点: " << A << " " << A->data << " " << A->next << endl;
	cout << "数据1 : " << A->next << " " << A->next->data << " " << A->next->next << endl;
	cout << "数据2 : " << A->next->next << " " << A->next->next->data << " " << A->next->next->next << endl;
	cout << "数据3 : " << A->next->next->next << " " << A->next->next->next->data << " " << A->next->next->next->next << endl;
	cout << "数据4 : " << A->next->next->next->next << " " << A->next->next->next->next->data << " " << A->next->next->next->next->next << endl;
	cout << "数据5 : " << A->next->next->next->next->next << " " << A->next->next->next->next->next->data << " " << A->next->next->next->next->next->next << endl;
	LinkList B = NULL;
	List_TailInsert(B); //尾插法   //2 3 4
	cout << "-------------------------------L1原始数据----------------------------" << endl;
	cout << "--------节点地址-数据-指针--------" << endl;
	cout << "头节点: " << B << " " << B->data << " " << B->next << endl;
	cout << "数据1 : " << B->next << " " << B->next->data << " " << B->next->next << endl;
	cout << "数据2 : " << B->next->next << " " << B->next->next->data << " " << B->next->next->next << endl;
	cout << "数据3 : " << B->next->next->next << " " << B->next->next->next->data << " " << B->next->next->next->next << endl;
	LinkList C = NULL;
	C  = Get_Common(A, B);
	C->data = 0;
	cout << "-------------------------------修改后数据----------------------------" << endl;
	cout << "--------节点地址-数据-指针--------" << endl;
	cout << "头节点: " << C << " " << C->data << " " << C->next << endl;
	cout << "数据1 : " << C->next << " " << C->next->data << " " << C->next->next << endl;
	cout << "数据2 : " << C->next->next << " " << C->next->next->data << " " << C->next->next->next << endl;
	return 0;
}

在这里插入图片描述

#include   //引入头文件
using namespace std;

typedef int Elemtype;

#define Maxsize 100
#define ERROR 0
#define OK    1

typedef struct LNode
{
	Elemtype data;//数据域
	struct LNode* next;//指针域
}LNode, * LinkList;

bool InitList(LinkList& L)   //初始化
{
	L = (LinkList)malloc(sizeof(LNode));
	if (L == NULL)
		return ERROR;
	L->data = 0;
	L->next = NULL;
	return OK;
}

bool ListEmpty(LinkList L)  //判断是否为空
{
	if (L->next != NULL)
	{
		cout << "not empty" << endl;
		return OK;
	}
	else
	{
		cout << "empty,只有头节点" << endl;
		return ERROR;
	}
}

int ListLength(LinkList L)
{
	LNode* p;
	p = L->next;
	int i = 0;
	while (p != NULL)
	{
		p = p->next;
		i++;
	}
	return i;
}

LinkList List_HeadInsert(LinkList& L)  //头插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		s->next = L->next;
		L->next = s;
		if (cin.get() == '\n') break;
	}
	return L;
}

LinkList List_TailInsert(LinkList& L)  //尾插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL, * r = NULL;
	r = L;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		r->next = s;
		r = s;
		if (cin.get() == '\n') break;
	}
	s->next = NULL;
	return L;
}

//------------------------算法核心------------------------//
LinkList Get_Common(LinkList A, LinkList B)
{
	LNode* pa = A->next;
	LNode* pb = B->next;
	LNode* u = NULL;
	LNode* pc= A;
	while ((pa != NULL) && (pb != NULL))
	{
		if (pa->data == pb->data)
		{
			pc->next = pa;
			pc = pa;
			pa = pa->next;
			u = pb;
			pb = pb->next;
			free(u);
		}
		else if (pa->data < pb->data)
		{
			u = pa;
			pa = pa->next;
			free(u);
		}
		else
		{
			u = pb;
			pb = pb->next;
			free(u);
		}
	}
	while (pa)
	{
		u = pa;
		pa = pa->next;
		free(u);
	}
	while (pb)
	{
		u = pb;
		pb = pb->next;
		free(u);
	}
	pc->next = NULL;
	free(B);
	return A;
}
//------------------------算法核心------------------------//
int main(void)
{
	LinkList A = NULL;
	List_TailInsert(A); //尾插法   //1 3 4 5 8
	cout << "-------------------------------L原始数据----------------------------" << endl;
	cout << "--------节点地址-数据-指针--------" << endl;
	cout << "头节点: " << A << " " << A->data << " " << A->next << endl;
	cout << "数据1 : " << A->next << " " << A->next->data << " " << A->next->next << endl;
	cout << "数据2 : " << A->next->next << " " << A->next->next->data << " " << A->next->next->next << endl;
	cout << "数据3 : " << A->next->next->next << " " << A->next->next->next->data << " " << A->next->next->next->next << endl;
	cout << "数据4 : " << A->next->next->next->next << " " << A->next->next->next->next->data << " " << A->next->next->next->next->next << endl;
	cout << "数据5 : " << A->next->next->next->next->next << " " << A->next->next->next->next->next->data << " " << A->next->next->next->next->next->next << endl;
	LinkList B = NULL;
	List_TailInsert(B); //尾插法   //2 3 4
	cout << "-------------------------------L1原始数据----------------------------" << endl;
	cout << "--------节点地址-数据-指针--------" << endl;
	cout << "头节点: " << B << " " << B->data << " " << B->next << endl;
	cout << "数据1 : " << B->next << " " << B->next->data << " " << B->next->next << endl;
	cout << "数据2 : " << B->next->next << " " << B->next->next->data << " " << B->next->next->next << endl;
	cout << "数据3 : " << B->next->next->next << " " << B->next->next->next->data << " " << B->next->next->next->next << endl;
	LinkList C = NULL;
	C = Get_Common(A, B);
	C->data = 0;
	cout << "-------------------------------修改后数据----------------------------" << endl;
	cout << "--------节点地址-数据-指针--------" << endl;
	cout << "头节点: " << C << " " << C->data << " " << C->next << endl;
	cout << "数据1 : " << C->next << " " << C->next->data << " " << C->next->next << endl;
	cout << "数据2 : " << C->next->next << " " << C->next->next->data << " " << C->next->next->next << endl;
	return 0;
}

你可能感兴趣的:(算法与数据结构,数据结构,算法)