mirror of
https://github.com/Ruvikm/Wangdao-Data-Structures.git
synced 2026-10-28 19:05:35 +08:00
又完成五道题
This commit is contained in:
97
线性表/链式实现大题/P40.6.cpp
Normal file
97
线性表/链式实现大题/P40.6.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
//P40.6
|
||||
//有一个带头结点的单链表L,设计一个算法使其元素递增有序
|
||||
#define ElemType int
|
||||
#define _for(i,a,b) for(int i=(a);i<(b);i++)
|
||||
#define INF 0x3f3f3f3f
|
||||
|
||||
#pragma region 建立带头结点的链表
|
||||
|
||||
typedef struct LNode {
|
||||
ElemType data;
|
||||
struct LNode* next;
|
||||
}LNode, * LinkList;
|
||||
|
||||
//初始化链表
|
||||
bool InitList(LinkList& L) {
|
||||
//分配一个头结点
|
||||
L = (LNode*)malloc(sizeof(LNode));
|
||||
//内存不足,分配失败
|
||||
if (L == NULL) {
|
||||
return false;
|
||||
}
|
||||
//头结点之后暂时还没有节点
|
||||
L->next = NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//判断是否为空
|
||||
bool IsEmpty(LinkList L) {
|
||||
return ((L->next) == NULL);
|
||||
}
|
||||
|
||||
//使用尾插法建表
|
||||
LinkList CreatList(vector<int> data) {
|
||||
if (data.size() < 1) {
|
||||
return NULL;
|
||||
}
|
||||
//头结点
|
||||
LNode* head = (LinkList)malloc(sizeof(LNode));
|
||||
head->data = NULL;
|
||||
head->next = NULL;
|
||||
LinkList p = head;
|
||||
_for(i, 0, data.size()) {
|
||||
LNode* s = (LinkList)malloc(sizeof(LNode));
|
||||
s->data = data[i];
|
||||
s->next = NULL;
|
||||
p->next = s;
|
||||
p = s;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
//输出链表
|
||||
void PrintList(LinkList list) {
|
||||
list = list->next;
|
||||
while (list != NULL) {
|
||||
printf("%d ", list->data);
|
||||
list = list->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
|
||||
//P40.6
|
||||
//算法思想:将链表的头结点后第一个节点与后面的节点断开,然后重新在合适的位置插入
|
||||
void SortList(LinkList &head) {
|
||||
LinkList p = head->next;
|
||||
LinkList q = p->next;
|
||||
LinkList pre;
|
||||
p->next = NULL;
|
||||
p = q;
|
||||
while (q != NULL) {
|
||||
p = q->next;
|
||||
pre = head;
|
||||
for (; pre->next != NULL && (pre->next->data < q->data); pre = pre->next);
|
||||
q->next = pre->next;
|
||||
pre->next = q;
|
||||
q = p;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<int> data{ 3,4,6,8,1,3,9,5 };
|
||||
LinkList head;
|
||||
InitList(head);
|
||||
head = CreatList(data);
|
||||
PrintList(head);
|
||||
SortList(head);
|
||||
PrintList(head);
|
||||
return 0;
|
||||
}
|
||||
104
线性表/链式实现大题/P40.7.cpp
Normal file
104
线性表/链式实现大题/P40.7.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
//P40.7
|
||||
//设在一个带表头结点的单链表中所有元素结点的数据值无序,试编写一个函数,删除表
|
||||
//中所有介于给定的两个值(作为函数参数给出)之间的元素的元素(若存在)
|
||||
#define ElemType int
|
||||
#define _for(i,a,b) for(int i=(a);i<(b);i++)
|
||||
#define INF 0x3f3f3f3f
|
||||
|
||||
#pragma region 建立带头结点的链表
|
||||
|
||||
typedef struct LNode {
|
||||
ElemType data;
|
||||
struct LNode* next;
|
||||
}LNode, * LinkList;
|
||||
|
||||
//初始化链表
|
||||
bool InitList(LinkList& L) {
|
||||
//分配一个头结点
|
||||
L = (LNode*)malloc(sizeof(LNode));
|
||||
//内存不足,分配失败
|
||||
if (L == NULL) {
|
||||
return false;
|
||||
}
|
||||
//头结点之后暂时还没有节点
|
||||
L->next = NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//判断是否为空
|
||||
bool IsEmpty(LinkList L) {
|
||||
return ((L->next) == NULL);
|
||||
}
|
||||
|
||||
//使用尾插法建表
|
||||
LinkList CreatList(vector<int> data) {
|
||||
if (data.size() < 1) {
|
||||
return NULL;
|
||||
}
|
||||
//头结点
|
||||
LNode* head = (LinkList)malloc(sizeof(LNode));
|
||||
head->data = NULL;
|
||||
head->next = NULL;
|
||||
LinkList p = head;
|
||||
_for(i, 0, data.size()) {
|
||||
LNode* s = (LinkList)malloc(sizeof(LNode));
|
||||
s->data = data[i];
|
||||
s->next = NULL;
|
||||
p->next = s;
|
||||
p = s;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
//输出链表
|
||||
void PrintList(LinkList list) {
|
||||
list = list->next;
|
||||
while (list != NULL) {
|
||||
printf("%d ", list->data);
|
||||
list = list->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
|
||||
//P40.7
|
||||
|
||||
void DelBetweenAB(LinkList& head, int A, int B) {
|
||||
if (A > B) {
|
||||
return;
|
||||
}
|
||||
LinkList pre = head;
|
||||
LinkList p = pre->next;
|
||||
while (p != NULL) {
|
||||
|
||||
if (pre->next->data >= A && pre->next->data <= B) {
|
||||
LNode* del = p;
|
||||
pre->next = p->next;
|
||||
p = p->next;
|
||||
delete del;
|
||||
}
|
||||
else {
|
||||
pre = p;
|
||||
p = p->next;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<int> data{ 3,5,6,8,1,3,9,3 };
|
||||
LinkList head;
|
||||
InitList(head);
|
||||
head = CreatList(data);
|
||||
PrintList(head);
|
||||
DelBetweenAB(head,3,7);
|
||||
PrintList(head);
|
||||
return 0;
|
||||
}
|
||||
111
线性表/链式实现大题/P41.10.cpp
Normal file
111
线性表/链式实现大题/P41.10.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
//P41.10
|
||||
//将一个带头结点的单链表A分解为两个带头结点的单链表A和B, 使得A表中含有原表
|
||||
//中序号为奇数的元素, 而B表中含有原表中序号为偶数的元素, 且保持其相对顺序不变
|
||||
|
||||
#define ElemType int
|
||||
#define _for(i,a,b) for(int i=(a);i<(b);i++)
|
||||
#define INF 0x3f3f3f3f
|
||||
|
||||
#pragma region 建立带头结点的链表
|
||||
|
||||
typedef struct LNode {
|
||||
ElemType data;
|
||||
struct LNode* next;
|
||||
}LNode, * LinkList;
|
||||
|
||||
//初始化链表
|
||||
bool InitList(LinkList& L) {
|
||||
//分配一个头结点
|
||||
L = (LNode*)malloc(sizeof(LNode));
|
||||
//内存不足,分配失败
|
||||
if (L == NULL) {
|
||||
return false;
|
||||
}
|
||||
//头结点之后暂时还没有节点
|
||||
L->next = NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//判断是否为空
|
||||
bool IsEmpty(LinkList L) {
|
||||
return ((L->next) == NULL);
|
||||
}
|
||||
|
||||
//使用尾插法建表
|
||||
LinkList CreatList(vector<int> data) {
|
||||
if (data.size() < 1) {
|
||||
return NULL;
|
||||
}
|
||||
//头结点
|
||||
LNode* head = (LinkList)malloc(sizeof(LNode));
|
||||
head->data = NULL;
|
||||
head->next = NULL;
|
||||
LinkList p = head;
|
||||
_for(i, 0, data.size()) {
|
||||
LNode* s = (LinkList)malloc(sizeof(LNode));
|
||||
s->data = data[i];
|
||||
s->next = NULL;
|
||||
p->next = s;
|
||||
p = s;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
//输出链表
|
||||
void PrintList(LinkList list) {
|
||||
if (IsEmpty(list)) {
|
||||
cout << "the list is null" << endl;
|
||||
return;
|
||||
}
|
||||
list = list->next;
|
||||
while (list != NULL) {
|
||||
printf("%d ", list->data);
|
||||
list = list->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
|
||||
//P41.10
|
||||
|
||||
void Split(LinkList &head) {
|
||||
LinkList B = (LinkList)malloc(sizeof(LNode));
|
||||
B->next = NULL;
|
||||
LinkList p = head->next;
|
||||
LinkList pre = head;
|
||||
LinkList Bp = B;
|
||||
int i = 0;
|
||||
while (p != NULL) {
|
||||
i++;
|
||||
if (i % 2 == 0) {
|
||||
LNode* q = p;
|
||||
Bp->next = q;
|
||||
Bp = q;
|
||||
pre->next = p->next;
|
||||
}else {
|
||||
pre = p;
|
||||
p = p->next;
|
||||
}
|
||||
}
|
||||
cout << "A:" << endl;
|
||||
PrintList(head);
|
||||
cout << "B:" << endl;
|
||||
PrintList(B);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<int> data{ 3,5,6,8,1,3,9,3 };
|
||||
LinkList head;
|
||||
InitList(head);
|
||||
head = CreatList(data);
|
||||
PrintList(head);
|
||||
Split(head);
|
||||
return 0;
|
||||
}
|
||||
126
线性表/链式实现大题/P41.8.cpp
Normal file
126
线性表/链式实现大题/P41.8.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
//P41.8
|
||||
//给定两个单链表,编写算法找出两个链表的公共结点
|
||||
#define ElemType int
|
||||
#define _for(i,a,b) for(int i=(a);i<(b);i++)
|
||||
#define INF 0x3f3f3f3f
|
||||
|
||||
#pragma region 建立带头结点的链表
|
||||
|
||||
typedef struct LNode {
|
||||
ElemType data;
|
||||
struct LNode* next;
|
||||
}LNode, * LinkList;
|
||||
|
||||
//初始化链表
|
||||
bool InitList(LinkList& L) {
|
||||
//分配一个头结点
|
||||
L = (LNode*)malloc(sizeof(LNode));
|
||||
//内存不足,分配失败
|
||||
if (L == NULL) {
|
||||
return false;
|
||||
}
|
||||
//头结点之后暂时还没有节点
|
||||
L->next = NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//判断是否为空
|
||||
bool IsEmpty(LinkList L) {
|
||||
return ((L->next) == NULL);
|
||||
}
|
||||
|
||||
//使用尾插法建表
|
||||
LinkList CreatList(vector<int> data) {
|
||||
if (data.size() < 1) {
|
||||
return NULL;
|
||||
}
|
||||
//头结点
|
||||
LNode* head = (LinkList)malloc(sizeof(LNode));
|
||||
head->data = NULL;
|
||||
head->next = NULL;
|
||||
LinkList p = head;
|
||||
_for(i, 0, data.size()) {
|
||||
LNode* s = (LinkList)malloc(sizeof(LNode));
|
||||
s->data = data[i];
|
||||
s->next = NULL;
|
||||
p->next = s;
|
||||
p = s;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
//输出链表
|
||||
void PrintList(LinkList list) {
|
||||
while (list != NULL) {
|
||||
printf("%d ", list->data);
|
||||
list = list->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int GetLenth(LinkList L) {
|
||||
int len = 0;
|
||||
LinkList p = L->next;
|
||||
while (p != NULL) {
|
||||
p = p->next;
|
||||
len++;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
|
||||
//P41.8
|
||||
//公共节点后全是公共的,“Y”型
|
||||
LinkList FindCommonList(LinkList A,LinkList B) {
|
||||
int LenA = GetLenth(A), LenB = GetLenth(B), DiffLen=abs(LenA-LenB);
|
||||
LinkList LongList, ShortList;
|
||||
if (LenA > LenB) {
|
||||
LongList = A->next;
|
||||
ShortList = B->next;
|
||||
}
|
||||
else {
|
||||
LongList = B->next;
|
||||
ShortList = A->next;
|
||||
}
|
||||
while (DiffLen--) {
|
||||
LongList = LongList->next;
|
||||
}
|
||||
while (LongList != NULL) {
|
||||
if (LongList == ShortList) {
|
||||
return LongList;
|
||||
}
|
||||
else {
|
||||
LongList = LongList->next;
|
||||
ShortList = ShortList->next;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<int> data1{ 3,5,6,8,1,4,9,77 };
|
||||
vector<int> data2{ 2,5,7,88,31};
|
||||
LinkList head1;
|
||||
LinkList head2;
|
||||
InitList(head1);
|
||||
InitList(head2);
|
||||
head1 = CreatList(data1);
|
||||
head2 = CreatList(data2);
|
||||
LinkList com = head1->next;
|
||||
LinkList P = head2;
|
||||
for (; com != NULL && com->data != 8; com = com->next);
|
||||
for (; P ->next!= NULL; P = P->next);
|
||||
P->next = com;
|
||||
PrintList(head1->next);
|
||||
PrintList(head2->next);
|
||||
LinkList CommList = FindCommonList(head1, head2);
|
||||
PrintList(CommList);
|
||||
return 0;
|
||||
}
|
||||
109
线性表/链式实现大题/P41.9.cpp
Normal file
109
线性表/链式实现大题/P41.9.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
//P41.9
|
||||
//给定一个带表头结点的单链表, 设head为头指针, 结点结构为(data, next), data
|
||||
//为整型元素, next为指针, 试写出算法:按递增次序输出单链表中各结点的数据元素,
|
||||
//并释放结点所占的存储空间(要求 : 不允许使用数组作为辅助空间)
|
||||
|
||||
#define ElemType int
|
||||
#define _for(i,a,b) for(int i=(a);i<(b);i++)
|
||||
#define INF 0x3f3f3f3f
|
||||
|
||||
#pragma region 建立带头结点的链表
|
||||
|
||||
typedef struct LNode {
|
||||
ElemType data;
|
||||
struct LNode* next;
|
||||
}LNode, * LinkList;
|
||||
|
||||
//初始化链表
|
||||
bool InitList(LinkList& L) {
|
||||
//分配一个头结点
|
||||
L = (LNode*)malloc(sizeof(LNode));
|
||||
//内存不足,分配失败
|
||||
if (L == NULL) {
|
||||
return false;
|
||||
}
|
||||
//头结点之后暂时还没有节点
|
||||
L->next = NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//判断是否为空
|
||||
bool IsEmpty(LinkList L) {
|
||||
return ((L->next) == NULL);
|
||||
}
|
||||
|
||||
//使用尾插法建表
|
||||
LinkList CreatList(vector<int> data) {
|
||||
if (data.size() < 1) {
|
||||
return NULL;
|
||||
}
|
||||
//头结点
|
||||
LNode* head = (LinkList)malloc(sizeof(LNode));
|
||||
head->data = NULL;
|
||||
head->next = NULL;
|
||||
LinkList p = head;
|
||||
_for(i, 0, data.size()) {
|
||||
LNode* s = (LinkList)malloc(sizeof(LNode));
|
||||
s->data = data[i];
|
||||
s->next = NULL;
|
||||
p->next = s;
|
||||
p = s;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
//输出链表
|
||||
void PrintList(LinkList list) {
|
||||
if (IsEmpty(list)) {
|
||||
cout << "the list is null" << endl;
|
||||
return;
|
||||
}
|
||||
list = list->next;
|
||||
while (list != NULL) {
|
||||
printf("%d ", list->data);
|
||||
list = list->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
|
||||
//P41.9
|
||||
|
||||
void SortPrintAndFree(LinkList &head) {
|
||||
while (head ->next!= NULL) {
|
||||
LNode* p = head->next;
|
||||
LNode* minpre = head;
|
||||
while (p->next != NULL) {
|
||||
if (p->next->data < minpre->next->data) {
|
||||
minpre = p;
|
||||
}
|
||||
else {
|
||||
p = p->next;
|
||||
}
|
||||
}
|
||||
LNode* del = minpre->next;
|
||||
minpre->next = del->next;
|
||||
cout <<del->data << " ";
|
||||
delete del;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<int> data{ 3,5,6,8,1,3,9,3 };
|
||||
LinkList head;
|
||||
InitList(head);
|
||||
head = CreatList(data);
|
||||
PrintList(head);
|
||||
SortPrintAndFree(head);
|
||||
PrintList(head);
|
||||
delete head;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user