菜菜大学生自学数据结构,以下为24王道数据结构课后习题自己作答的集合,欢迎各路大佬指认错误 ~
持续学习,持续更新~
#include
using namespace std;
typedef struct{
int data[10] = {15, 16, 18, 10, 100, 15, 19, 14, 8};
int length = 9;
}sqlist;
int findmin(sqlist L)
{
int min = L.data[0], I = 0;
for(int i = 1; i < L.length; i++)
if(L.data[i] < min)
{
min = L.data[i];
I = i;
}
return I;
}
bool deletelist(sqlist &L, int &key)
{
if(L.length == 0 )
{
cout << "这是一个空列表" << endl;
return false;
}
key = L.data[findmin(L)];
L.data[findmin(L)] = L.data[L.length-1];
L.length--;
return true;
}
int main()
{
sqlist L;
int key = -1;
deletelist(L, key);
cout << key << endl;
for (int i = 0; i < L.length; i++)
cout << L.data[i] << " " << endl;
cout << L.length << endl;
system("pause");
return 0;
}
#include
using namespace std;
#define maxsize 10
typedef struct{
int data[maxsize] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int length = 9;
}sqlist;
void reverse(sqlist &L)
{
for(int i = 0;i < L.length/2; i++)
swap(L.data[i], L.data[L.length - i - 1]);
}
void swap(int &a, int &b)
{
a = a + b;
b = a - b;
a = a - b;
}
int main()
{
sqlist L;
reverse(L);
for (int i = 0; i < L.length; i++)
cout << L.data[i] << "\t";
system("pause");
return 0;
}
#include
using namespace std;
#define maxsize 10
typedef struct{
int data[maxsize];
int length;
}sqlist;
void DeleteElem(sqlist &L, int key)
{
for (int i = 0; i < L.length; i++)
cout << L.data[i] << " ";
cout << endl;
int k = 0;
for (int i = 0; i < L.length; i++)
if (L.data[i] != key)
{
L.data[k] = L.data[i];
k++;
}
else
cout << "等于key的元素值所在位置:" << i+1 << endl;
L.length = k;
}
void Initlist(sqlist &L, int length)
{
L.length = 0;
for(int i = 0; i < length; i++)
{
cout << "输入第" << i+1 << "个数据:";
cin >> L.data[i];
L.length++;
}
}
int main()
{
sqlist L;
int size;
cout << "数组长度: " ;
cin >> size;
Initlist(L, size);
cout << "长度:" << L.length << endl;
int key;
cout << "目的数据:";
cin >> key;
DeleteElem(L, key);
cout << L.length << endl;
for (int i = 0; i < L.length; i++)
cout << L.data[i] << " ";
system("pause");
return 0;
}
#include
using namespace std;
#define maxsize 20
typedef struct{
int data[maxsize];
int length;
}sqlist;
bool DeleteElem(sqlist &L, int s, int t)
{
if(s >= t || L.length == 0)
{
cout << "warning!" << endl;
return false;
}
int k = 0;
for(int i = 0; i < L.length; i++)
if(L.data[i] < s || L.data[i] > t)
L.data[k++] = L.data[i];
L.length = k;
}
void AddElem(sqlist &L, int length)
{
L.length = length;
for(int i = 0; i < length; i++)
cin >> L.data[i];
}
int main()
{
sqlist L;
int length;
cin >> length;
AddElem(L,length);
int s, t;
cin >> s >> t;
DeleteElem(L, s, t);
cout << L.length << endl;
for(int i = 0;i < L.length; i++)
cout << L.data[i] << " ";
system("pause");
return 0;
}
#include
using namespace std;
#define maxsize 20
typedef struct{
int data[maxsize];
int length;
}sqlist;
void InitElem(sqlist &L, int length)
{
L.length = length;
for(int i = 0; i < length; i++)
cin >> L.data[i];
}
bool DeleteElem(sqlist &L)
{
int count = 0;
for(int i = 1; i < L.length; i++)
if(L.data[count] != L.data[i])
L.data[++count] = L.data[i];
L.length = count+1;
}
int main()
{
sqlist L;
int length;
cin >> length;
InitElem(L,length);
DeleteElem(L);
cout << L.length << endl;
for(int i = 0;i < L.length; i++)
cout << L.data[i] << " ";
system("pause");
return 0;
}
#include
using namespace std;
#define maxsize 20
typedef struct{
int data[maxsize];
int length;
}sqlist;
void InitElem(sqlist &L, int length)
{
L.length = length;
for(int i = 0; i < length; i++)
cin >> L.data[i];
}
bool Add(sqlist &L, sqlist &I, sqlist &S)
{
if(L.length + I.length > maxsize)
return false;
int i = 0, j = 0, k = 0;
while(i < L.length && j < I.length)
if(L.data[i] >= I.data[j])
S.data[k++] = I.data[j++];
else
S.data[k++] = L.data[i++];
while(i < L.length)
S.data[k++] = L.data[i++];
while(j < I.length)
S.data[k++] = I.data[j++];
S.length = k;
return true;
}
int main()
{
sqlist A, B, C;
int a, b;
cin >> a;
InitElem(A, a);
cin >> b;
InitElem(B, b);
Add(A, B, C);
for(int i = 0; i < C.length; i++)
cout << C.data[i] << " ";
system("pause");
return 0;
}
#include
using namespace std;
#define maxsize 10
typedef struct{
int length;
int data[maxsize];
}sqlist;
void add(sqlist &A, sqlist &B, int * p, int a)
{
for(int i = 0; i < maxsize - a; i++)
p[i] = B.data[i];
for(int i = maxsize - a; i < maxsize; i++)
p[i] = A.data[i - maxsize + a];
}
void part(sqlist &B, sqlist &C, int * p, int a)
{
for (int i = 0; i < a; i++)
B.data[i] = p[i];
for(int i = a; i < maxsize; i++)
C.data[i-a] = p[i];
add(B, C, p, a);
}
int main()
{
sqlist B, C;
int num[maxsize] = {1, 5, 6, 9, 10, 15, 16, 19, 20, 25};
int a;
cin >> a;
B.length = a;
C.length = maxsize - a;
part(B, C, num, a);
for(int i = 0; i < maxsize; i++)
cout << num[i] << " ";
system("pause");
return 0;
}
#include
using namespace std;
#define maxsize 20
typedef struct{
int data[maxsize];
int length;
}sqlist;
int LocateElem(sqlist L, int key) // 折半查找法
{
int left = 0, right = L.length - 1;
while(left <= right)
{
int mid = (left + right)/2;
if(L.data[mid] > key)
right = mid - 1;
else if (L.data[mid] < key)
left = mid + 1;
else
return mid;
}
return -1;
}
void swap(int &a, int &b) // 交换函数
{
a = a + b;
b = a - b;
a = a - b;
}
bool ListInsert(sqlist &L, int i, int key) // 指定位置插入元素
{
if(i > L.length+1 || i < 1)
return false;
if(L.length >= maxsize)
return false;
for(int j=L.length; j>=i; j--)
L.data[j] = L.data[j-1];
L.data[i-1] = key;
L.length++;
return true;
}
bool find(sqlist &L, int key)
{
int i = LocateElem(L, key);
if(i != -1)// 如果可以找到
if(i != L.length-1) // 不是最后一个元素
{
swap(L.data[i], L.data[i+1]);
return true;
}
else // 是最后一个元素
return true;
for(int i = 0; i < L.length-1; i++) // 找到合适的插入位置
if(key >= L.data[i] && key < L.data[i+1])
{
ListInsert(L, i+2, key);
return true;
}
ListInsert(L, L.length+1, key); // 没有合适的插入位置
return true;
}
void InitElem(sqlist &L, int length)
{
L.length = length;
for(int i = 0; i < length; i++)
cin >> L.data[i];
}
int main()
{
sqlist L;
int length;
cin >> length;
InitElem(L, length);
int key;
cin >> key;
find(L, key);
for(int i = 0; i < L.length; i++)
cout << L.data[i] << " ";
system("pause");
return 0;
}
/*
设计思路:
将数组分为[0, step - 1]与[step, n-1]两段
交换两段位置
*/
#include
using namespace std;
#define maxsize 20
void addElem(int * p, int size)
{
for(int i = 0; i < size; i++)
cin >> p[i];
}
void reverse(int * p, int left, int right) // 倒置指定段元素
{
int mid = (right - left + 1) / 2;
for(int i = 0; i < mid; i++)
{
p[left + i] = p[left + i] + p[right - i];
p[right - i] = p[left + i] - p[right - i];
p[left + i] = p[left + i] - p[right - i];
}
}
int main()
{
int R[maxsize];
int size;
cin >> size;
addElem(R, size);
int step;
cin >> step;
reverse(R, 0, step - 1);
reverse(R, step, size - 1);
reverse(R, 0, size - 1);
for(int i = 0; i < size; i++)
cout << R[i] << " ";
system("pause");
return 0;
}
/*
空间复杂度:O(1)
时间复杂度:O(n)
*/
/*
先按递增顺序合并两个序列
再判断合序列的长度是偶数还是奇数
最后找中位数
*/
#include
using namespace std;
#define maxsize 10
void add(float * p, int size)
{
for(int i = 0; i < size; i++)
cin >> p[i];
}
float find(float * p, int size)
{
if(size % 2 != 0)
return p[size/2];
else
return (p[size/2] + p[(size/2)-1])/2;
}
void andand(float * p, float * q, float * num, int size1, int size2)
{
int i = 0, j = 0, k = 0;
while(i <= size1 && j <= size2)
if(p[i] < q[j])
num[k++] = p[i++];
else
num[k++] = q[j++];
while(i <= size1)
num[k++] = p[i++];
while(j <= size2)
num[k++] = q[j++];
}
int main()
{
float num1[maxsize], num2[maxsize];
int size1, size2;
cin >> size1 >> size2;
add(num1, size1);
add(num2, size2);
float num[maxsize];
if(size1 + size2 > maxsize)
{
cout << "数据错误" << endl;
return 0;
}
andand(num1, num2, num, size1, size2);
int size = size1 + size2;
cout << find(num, size) << endl;
system("pause");
return 0;
}
/*
时间复杂度:O(n)
空间复杂度:O(1)
*/
/*
已知一个整数序列a长度为n,其中若存在a中的个数大于n/2则称为a的主元素
*/
#include
using namespace std;
#define maxsize 20
int FindMain(int * num, int size)
{
int count[maxsize] = {0}, max = count[0], x;
for(int i = 0; i < size; i++)
if(++count[num[i]] > max)
{
x = num[i];
max = count[x];
}
if(count[x] > size / 2)
return x;
else
return -1;
}
int main()
{
int num[maxsize];
int size;
cin >> size;
for(int i = 0; i < size; i++)
cin >> num[i];
cout << FindMain(num, size) << endl;
system("pause");
return 0;
}
/*
时间复杂度 O(n)
空间复杂度 O(1)
*/
// 更优算法:Boyer-Moore投票算法
/*
1. 初始化一个候选主元素candidate和计数器count为0。
2. 遍历数组,当count为0时,将当前元素设为候选主元素。
3. 如果当前元素与候选主元素相同,则增加计数器count;否则,减少计数器count。
4. 验证候选主元素:对候选主元素进行验证,确保它出现次数超过一半。
重新遍历数组,统计候选主元素出现的次数。
如果出现次数超过一半,返回候选主元素;否则,表示没有主元素,返回-1。
*/
/*
先找到序列中的最大值,+1作为count数组的长度
对序列中每个数字进行统计,非正整数不计,避免count数组出错
遍历count数组,输出第一个count为零的i值
*/
#include
using namespace std;
#define maxsize 20
int find_min(int * num, int size, int max)
{
int count[max + 1] = {0};
for(int i = 0; i < size; i++)
if(num[i] >= 1)
count[num[i]]++;
for(int i = 1; i <= max; i++)
if(count[i] == 0)
return i;
return max+1;
}
int size_max(int * num, int size)
{
int max = num[0];
for(int i = 1; i < size; i++)
if(num[i] > max)
max = num[i];
return max;
}
int main()
{
int num[maxsize];
int size;
cin >> size;
for(int i = 0; i < size; i++)
cin >> num[i];
cout << find_min(num, size, size_max(num, size)) << endl;
system("pause");
return 0;
}
/*
时间复杂度 O(n)
空间复杂度 O(n)
*/
/*
设a为最小值,c为最大值,则 D = 2 * abs(c-a), 与中值b无关
固定三组中的c,寻找满足条件的a
不断更新Dmin
*/
#include
#include
using namespace std;
#define max 0x7fffffff
#define maxsize 20
bool ifmin(int a, int b, int c)
{
if(a <= b && a <= c)
return true;
return false;
}
int find(int * A, int * B, int * C, int sizea, int sizeb, int sizec)
{
int D, Dmin = max, i = 0, j = 0, k = 0;
int d, e, f;
while(i <= sizea && j <= sizeb && k <= sizec)
{
int a = A[i], b = B[j], c = C[k];
D = abs(a - b) + abs(b - c) + abs(c - a);
if(D <= Dmin)
{
d = a, e = b, f = c;
Dmin = D;
}
if(ifmin(a, b, c))
i++;
else if(ifmin(b, c, a))
j++;
else
k++;
}
return Dmin;
}
int main()
{
int A[3] = {-1, 0, 9}, B[4] = {-25, -10, 10, 11}, C[5] = {2, 9, 17, 30, 41};
cout << find(A, B, C, 3, 4, 5) << endl;
system("pause");
return 0;
}
/*
时间复杂度 O(n)
空间复杂度 O(1)
*/
#include
using namespace std;
typedef struct LNode{ // 定义单链表
int data;
LNode *next;
}LNode, *LinkList;
void TailForm(LinkList &L, int length) // 尾插法建立链表
{
L = new LNode;
L = NULL; // 初始化一个不带头结点的单链表
int i = 1, x;
LNode * p = L;
cout << "删除前:" << endl;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
if(p == NULL)
{
L = s;
p = s;
}
else
{
p -> next = s;
p = s;
}
i++;
}
p -> next = NULL;
}
void Output(LinkList L) // 输出一个不带头结点的单链表中所有的值
{
LNode * p = L;
while(p != NULL)
{
cout << p -> data << " ";
p = p -> next;
}
}
void DeleteP(LNode * &p) // 删除p结点
{
if(p == NULL)
return;
if(p -> next == NULL)
{
p = NULL;
return;
}
LNode * q = p -> next;
p -> data = q -> data;
p -> next = q -> next;
delete(q);
}
bool Delx(LinkList &L, int x)
{
if(L == NULL)
return false;
LNode * p = L;
LNode * prev = NULL; // 用于记录上一个节点
while (p != NULL)
if (p ->data == x)
if (prev != NULL)
{
// 删除p节点
DeleteP(p);
prev->next = p;
}
else
{
// 删除头节点
L = L->next;
delete p;
p = L;
}
else
{
prev = p;
p = p->next;
}
return true;
}
int main()
{
LinkList L;
int length;
cout << "请输入单链表的长度:" << endl;
cin >> length;
TailForm(L, length);
int x;
cout << "请输入要删除的结点的值:" << endl;
cin >> x;
Delx(L, x);
cout << "删除后:" << endl;
Output(L);
system("pause");
return 0;
}
// 貌似与上一题类似,几乎照抄上一题,偷个懒
#include
using namespace std;
typedef struct LNode{
LNode * next;
int data;
}LNode, * LinkList;
void TailForm(LinkList &L, int length) // 尾插法建立链表
{
L = new LNode;
L -> next = NULL; // 初始化一个带头结点的单链表
int i = 1, x;
LNode * p = L;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
p -> next = s;
p = s;
i++;
}
p -> next = NULL;
}
void Output(LinkList L) // 输出一个带头结点的单链表中所有的值
{
LNode * p = L -> next;
while(p != NULL)
{
cout << p -> data << " ";
p = p -> next;
}
}
void DeleteP(LNode * &p) // 删除p结点
{
if(p == NULL)
return;
if(p -> next == NULL)
{
p = NULL;
return;
}
LNode * q = p -> next;
p -> data = q -> data;
p -> next = q -> next;
delete(q);
}
bool Delx(LinkList &L, int x)
{
if(L -> next == NULL)
return false;
LNode * p = L -> next;
LNode * prev = NULL; // 用于记录上一个节点
while (p != NULL)
if (p ->data == x)
if (prev != NULL)
{
// 删除p节点
DeleteP(p);
prev->next = p;
}
else
{
// 删除头节点
L = L->next;
delete p;
p = L;
}
else
{
prev = p;
p = p->next;
}
return true;
}
int main()
{
LinkList L;
int length;
cin >> length;
TailForm(L, length);
int x;
cin >> x;
Delx(L, x);
Output(L);
system("pause");
return 0;
}
#include
using namespace std;
typedef struct LNode{
LNode * next;
int data;
}LNode, * LinkList;
bool InitLinkList(LinkList &L)
{
L = new LNode;
if(L == NULL)
return false;
L -> next = NULL;
return true;
}
void Output(LinkList L) // 输出一个带头结点的单链表中所有的值
{
LNode * p = L -> next;
while(p != NULL)
{
cout << p -> data << " ";
p = p -> next;
}
}
void TailForm(LinkList &L, int length) // 尾插法建立链表
{
InitLinkList(L);
int i = 1, x;
LNode * p = L;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
p -> next = s;
p = s;
i++;
}
p -> next = NULL;
}
void HeadForm(LinkList L, LinkList &T, int length) // 利用头插法对单链表实现逆置
{
if(L -> next == NULL)
return;
InitLinkList(T);
LNode * p = L -> next;
while(p != NULL)
{
LNode * s = new LNode;
s -> data = p -> data;
s -> next = T -> next;
T -> next = s;
p = p -> next;
}
}
void reverse(LinkList L, int length)
{
LinkList T;
TailForm(L, length);
HeadForm(L, T, length);
Output(T);
}
int main()
{
LinkList L;
int length;
cin >> length;
reverse(L, length);
system("pause");
return 0;
}
#include
using namespace std;
typedef struct LNode{
LNode * next;
int data;
}LNode, * LinkList;
bool InitLinkList(LinkList &L)
{
L = new LNode;
if(L == NULL)
return false;
L -> next = NULL;
return true;
}
void Output(LinkList L) // 输出一个带头结点的单链表中所有的值
{
LNode * p = L -> next;
while(p != NULL)
{
cout << p -> data << " ";
p = p -> next;
}
}
void TailForm(LinkList &L, int length) // 尾插法建立链表
{
L = new LNode;
L -> next = NULL; // 初始化一个带头结点的单链表
int i = 1, x;
LNode * p = L;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
p -> next = s;
p = s;
i++;
}
p -> next = NULL;
}
void DeleteP(LNode * &p) // 删除p结点
{
if(p == NULL)
return;
if(p -> next == NULL)
{
p = NULL;
return;
}
LNode * q = p -> next;
p -> data = q -> data;
p -> next = q -> next;
delete(q);
}
void findmin(LinkList &L, int length)
{
TailForm(L, length);
LNode * p = L -> next;
LNode * minp = p;
int min = p -> data;
while(p != NULL)
{
if(p -> data < min)
{
min = p -> data;
minp = p;
}
p = p -> next;
}
DeleteP(minp);
Output(L);
}
int main()
{
LinkList L;
int length;
cin >> length;
findmin(L, length);
system("pause");
return 0;
}
#include
using namespace std;
typedef struct LNode{
LNode * next;
int data;
}LNode, * LinkList;
bool InitLinkList(LinkList &L)
{
L = new LNode;
if(L == NULL)
return false;
L -> next = NULL;
return true;
}
void Output(LinkList L) // 输出一个带头结点的单链表中所有的值
{
LNode * p = L -> next;
while(p != NULL)
{
cout << p -> data << " ";
p = p -> next;
}
}
void TailForm(LinkList &L, int length) // 尾插法建立链表
{
InitLinkList(L);
int i = 1, x;
LNode * p = L;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
p -> next = s;
p = s;
i++;
}
p -> next = NULL;
}
void reverse(LinkList &L, int length)
{
TailForm(L, length);
LNode * p = L -> next;
L -> next = NULL;
while(p != NULL)
{
LNode * s = p -> next;
p -> next = L -> next;
L -> next = p;
p = s;
}
Output(L);
}
int main()
{
LinkList L;
int length;
cin >> length;
reverse(L, length);
system("pause");
return 0;
}
#include
using namespace std;
typedef struct LNode{
LNode * next;
int data;
}LNode, * LinkList;
bool InitLinkList(LinkList &L)
{
L = new LNode;
if(L == NULL)
return false;
L -> next = NULL;
return true;
}
void Output(LinkList L) // 输出一个带头结点的单链表中所有的值
{
LNode * p = L -> next;
while(p != NULL)
{
cout << p -> data << " ";
p = p -> next;
}
}
void TailForm(LinkList &L, int length) // 尾插法建立链表
{
InitLinkList(L);
int i = 1, x;
LNode * p = L;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
p -> next = s;
p = s;
i++;
}
p -> next = NULL;
}
void swap(LNode * &p, LNode * &q)
{
int temp = q -> data;
q -> data = p -> data;
p -> data = temp;
}
void bubble(LinkList &L)
{
LNode * p = L -> next;
LNode * q;
while(p != NULL)
{
q = p -> next;
while(q != NULL)
{
if(p -> data > q -> data)
swap(p, q);
q = q -> next;
}
p = p -> next;
}
}
int main()
{
LinkList L;
int length;
cin >> length;
TailForm(L, length);
bubble(L);
Output(L);
system("pause");
return 0;
}
07.删除无序单链表中介于给定两个值之间的元素的元素
#include
using namespace std;
typedef struct LNode{
LNode * next;
int data;
}LNode, * LinkList;
bool InitLinkList(LinkList &L) // 初始化一个带头结点的单链表
{
L = new LNode;
if(L == NULL)
return false;
L -> next = NULL;
return true;
}
void Output(LinkList L) // 输出一个带头结点的单链表中所有的值
{
LNode * p = L -> next;
while(p != NULL)
{
cout << p -> data << " ";
p = p -> next;
}
}
void TailForm(LinkList &L) // 尾插法建立链表
{
int length;
cout << "请输入链表初始长度:" << endl;
cin >> length;
InitLinkList(L);
int i = 1, x;
LNode * p = L;
cout << "请输入链表中的初始元素:" << endl;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
p -> next = s;
p = s;
i++;
}
p -> next = NULL;
}
void Del(LinkList &L, int x, int y)
{
LNode * p = L -> next;
LNode * prev = L;
while(p != NULL)
{
if (p->data >= x && p->data <= y)
{
LNode *temp = p;
prev -> next = p -> next;
p = p -> next;
delete temp;
}
else
{
prev = p;
p = p -> next;
}
}
}
int main()
{
LinkList L;
TailForm(L);
int x, y;
cout << "请输入要删除的元素的范围:" << endl;
cin >> x >> y;
Del(L, x, y);
Output(L);
system("pause");
return 0;
}
//时间复杂度:O(len1 + len2)
#include
#include
using namespace std;
typedef struct LNode{
LNode * next;
int data;
}LNode, * LinkList;
bool InitLinkList(LinkList &L) // 初始化一个带头结点的单链表
{
L = new LNode;
if(L == NULL)
return false;
L -> next = NULL;
return true;
}
void Output(LinkList L) // 输出一个带头结点的单链表中所有的值
{
LNode * p = L -> next;
while(p != NULL)
{
cout << p -> data << " ";
p = p -> next;
}
}
void TailForm(LinkList &L) // 尾插法建立链表
{
int length;
cout << "请输入链表初始长度:" << endl;
cin >> length;
InitLinkList(L);
int i = 1, x;
LNode * p = L;
cout << "请输入链表中的初始元素:" << endl;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
p -> next = s;
p = s;
i++;
}
p -> next = NULL;
}
int len(LinkList L)
{
LNode * p = L -> next;
int count = 0;
while(p != NULL)
{
count ++;
p = p -> next;
}
return count;
}
LNode * Node(LinkList L1, LinkList L2)
{
int lenn = len(L1) - len(L2);
LNode * p, * q;
int i = 0, x;
if(lenn > 0)
{
x = len(L2);
p = L1 -> next;
while(i <= lenn)
{
p = p -> next;
i ++;
}
q = L2 -> next;
}
else
{
x = len(L1);
p = L2 -> next;
while(i <= abs(lenn))
{
p = p -> next;
i ++;
}
q = L1 -> next;
}
i = 0;
while(i <= x)
{
if (p == q)
return p;
p = p -> next;
q = q -> next;
i++;
}
return NULL;
}
int main()
{
LinkList L1, L2;
TailForm(L1);
TailForm(L2);
LNode * p = Node(L1, L2);
system("pause");
return 0;
}
#include
using namespace std;
typedef struct LNode{
LNode * next;
int data;
}LNode, * LinkList;
bool InitLinkList(LinkList &L) // 初始化一个带头结点的单链表
{
L = new LNode;
if(L == NULL)
return false;
L -> next = NULL;
return true;
}
void Output(LinkList L) // 输出一个带头结点的单链表中所有的值
{
LNode * p = L -> next;
while(p != NULL)
{
cout << p -> data << " ";
p = p -> next;
}
}
void TailForm(LinkList &L) // 尾插法建立链表
{
int length;
cout << "请输入链表初始长度:" << endl;
cin >> length;
InitLinkList(L);
int i = 1, x;
LNode * p = L;
cout << "请输入链表中的初始元素:" << endl;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
p -> next = s;
p = s;
i++;
}
p -> next = NULL;
}
void swap(LNode * &p, LNode * &q)
{
int temp = q -> data;
q -> data = p -> data;
p -> data = temp;
}
void bubble(LinkList &L)
{
LNode * p = L -> next;
LNode * q;
while(p != NULL)
{
q = p -> next;
while(q != NULL)
{
if(p -> data > q -> data)
swap(p, q);
q = q -> next;
}
p = p -> next;
}
p = L -> next;
while(p != NULL)
{
cout << p -> data << " ";
q = p -> next;
L -> next = q;
delete(p);
p = q;
}
}
int main()
{
LinkList L;
TailForm(L);
bubble(L);
system("pause");
return 0;
}
#include
using namespace std;
typedef struct LNode{
LNode * next;
int data;
}LNode, * LinkList;
bool InitLinkList(LinkList &L) // 初始化一个带头结点的单链表
{
L = new LNode;
if(L == NULL)
return false;
L -> next = NULL;
return true;
}
void Output(LinkList L) // 输出一个带头结点的单链表中所有的值
{
LNode * p = L -> next;
while(p != NULL)
{
cout << p -> data << " ";
p = p -> next;
}
cout << endl;
}
void TailForm(LinkList &L) // 尾插法建立链表
{
int length;
cout << "请输入链表初始长度:" << endl;
cin >> length;
InitLinkList(L);
int i = 1, x;
LNode * p = L;
cout << "请输入链表中的初始元素:" << endl;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
p -> next = s;
p = s;
i++;
}
p -> next = NULL;
}
void devide(LinkList &A, LinkList &B)
{
InitLinkList(B);
LNode * p = A -> next;
LNode * pre = A;
LNode * q = B;
while(p != NULL)
{
if(p -> data % 2 == 0)
{
LNode * s = new LNode;
s -> data = p -> data;
s -> next = q -> next;
q -> next = s;
q = s;
LNode * n = p -> next;
pre -> next = n;
delete(p);
p = n;
}
else
{
p = p -> next;
pre = pre -> next;
}
}
}
int main()
{
LinkList L, B;
TailForm(L);
devide(L, B);
cout << "奇数:" << endl;
Output(L);
cout << "偶数:" << endl;
Output(B);
system("pause");
return 0;
}
#include
using namespace std;
typedef struct LNode{
LNode * next;
int data;
}LNode, * LinkList;
bool InitLinkList(LinkList &L) // 初始化一个带头结点的单链表
{
L = new LNode;
if(L == NULL)
return false;
L -> next = NULL;
return true;
}
void Output(LinkList L) // 输出一个带头结点的单链表中所有的值
{
LNode * p = L -> next;
while(p != NULL)
{
cout << p -> data << " ";
p = p -> next;
}
cout << endl;
}
void TailForm(LinkList &L) // 尾插法建立链表
{
int length;
cout << "请输入链表初始长度:" << endl;
cin >> length;
InitLinkList(L);
int i = 1, x;
LNode * p = L;
cout << "请输入链表中的初始元素:" << endl;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
p -> next = s;
p = s;
i++;
}
p -> next = NULL;
}
void devide(LinkList &A, LinkList &B)
{
InitLinkList(B);
LNode * p = A -> next;
LNode * pre = A;
int i = 1;
while(p != NULL)
{
if(i % 2 == 0)
{
LNode * s = new LNode;
s -> data = p -> data;
s -> next = B -> next;
B -> next = s;
LNode * n = p -> next;
pre -> next = n;
delete(p);
p = n;
}
else
{
p = p -> next;
pre = pre -> next;
}
i++;
}
}
int main()
{
LinkList L, B;
TailForm(L);
devide(L, B);
Output(L);
Output(B);
system("pause");
return 0;
}
#include
using namespace std;
#define maxnum 0x7fffffff
typedef struct LNode{
LNode * next;
int data;
}LNode, * LinkList;
bool InitLinkList(LinkList &L) // 初始化一个带头结点的单链表
{
L = new LNode;
if(L == NULL)
return false;
L -> next = NULL;
return true;
}
void Output(LinkList L) // 输出一个带头结点的单链表中所有的值
{
LNode * p = L -> next;
while(p != NULL)
{
cout << p -> data << " ";
p = p -> next;
}
cout << endl;
}
void TailForm(LinkList &L) // 尾插法建立链表
{
int length;
cout << "请输入链表初始长度:" << endl;
cin >> length;
InitLinkList(L);
int i = 1, x;
LNode * p = L;
cout << "请输入链表中的初始元素:" << endl;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
p -> next = s;
p = s;
i++;
}
p -> next = NULL;
}
void del(LinkList &L)
{
L -> data = maxnum;
LNode * p = L -> next;
LNode * pre = L;
while(p != NULL)
{
if(p -> data == pre -> data)
{
LNode * s = p -> next;
pre -> next = s;
delete(p);
p = s;
}
else
{
pre = p;
p = p -> next;
}
}
}
int main()
{
LinkList L;
TailForm(L);
del(L);
Output(L);
system("pause");
return 0;
}
#include
using namespace std;
typedef struct LNode{
LNode * next;
int data;
}LNode, * LinkList;
bool InitLinkList(LinkList &L) // 初始化一个带头结点的单链表
{
L = new LNode;
if(L == NULL)
return false;
L -> next = NULL;
return true;
}
void Output(LinkList L) // 输出一个带头结点的单链表中所有的值
{
LNode * p = L -> next;
while(p != NULL)
{
cout << p -> data << " ";
p = p -> next;
}
cout << endl;
}
void TailForm(LinkList &L) // 尾插法建立链表
{
int length;
cout << "请输入链表初始长度:" << endl;
cin >> length;
InitLinkList(L);
int i = 1, x;
LNode * p = L;
cout << "请输入链表中的初始元素:" << endl;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
p -> next = s;
p = s;
i++;
}
p -> next = NULL;
}
void add(LinkList &A, LinkList &B)
{
LNode * p1 = A -> next;
A -> next = NULL;
LNode * p2 = B -> next;
LNode * p;
while(p1 != NULL && p2 != NULL)
{
if(p1 -> data < p2 -> data)
{
p = p1 -> next;
p1 -> next = A -> next;
A -> next = p1;
p1 = p;
}
else
{
p = p2 -> next;
p2 -> next = A -> next;
A -> next = p2;
p2 = p;
}
}
while(p1 != NULL)
{
p = p1 -> next;
p1 -> next = A -> next;
A -> next = p1;
p1 = p;
}
while(p2 != NULL)
{
p = p2 -> next;
p2 -> next = A -> next;
A -> next = p2;
p2 = p;
}
}
int main()
{
LinkList L, B;
TailForm(L);
TailForm(B);
add(L, B);
Output(L);
system("pause");
return 0;
}
#include
using namespace std;
typedef struct LNode{
LNode * next;
int data;
}LNode, * LinkList;
bool InitLinkList(LinkList &L) // 初始化一个带头结点的单链表
{
L = new LNode;
if(L == NULL)
return false;
L -> next = NULL;
return true;
}
void Output(LinkList L) // 输出一个带头结点的单链表中所有的值
{
LNode * p = L -> next;
while(p != NULL)
{
cout << p -> data << " ";
p = p -> next;
}
cout << endl;
}
void TailForm(LinkList &L) // 尾插法建立链表
{
int length;
cout << "请输入链表初始长度:" << endl;
cin >> length;
InitLinkList(L);
int i = 1, x;
LNode * p = L;
cout << "请输入链表中的初始元素:" << endl;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
p -> next = s;
p = s;
i++;
}
p -> next = NULL;
}
LinkList add(LinkList &A, LinkList &B)
{
LinkList C;
InitLinkList(C);
LNode * a = A -> next;
LNode * b = B -> next;
LNode * p = C, * s;
while(a != NULL && b != NULL)
{
if(a -> data > b -> data)
b = b -> next;
else if(a -> data < b -> data)
a = a -> next;
else
{
s = new LNode;
s -> data = a -> data;
p -> next = s;
p = s;
a = a -> next;
}
}
p -> next = NULL;
return C;
}
int main()
{
LinkList A, B;
TailForm(A);
TailForm(B);
LinkList C = add(A, B);
Output(C);
system("pause");
return 0;
}
#include
using namespace std;
typedef struct LNode{
LNode * next;
int data;
}LNode, * LinkList;
bool InitLinkList(LinkList &L) // 初始化一个带头结点的单链表
{
L = new LNode;
if(L == NULL)
return false;
L -> next = NULL;
return true;
}
void Output(LinkList L) // 输出一个带头结点的单链表中所有的值
{
LNode * p = L -> next;
while(p != NULL)
{
cout << p -> data << " ";
p = p -> next;
}
cout << endl;
}
void TailForm(LinkList &L) // 尾插法建立链表
{
int length;
cout << "请输入链表初始长度:" << endl;
cin >> length;
InitLinkList(L);
int i = 1, x;
LNode * p = L;
cout << "请输入链表中的初始元素:" << endl;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
p -> next = s;
p = s;
i++;
}
p -> next = NULL;
}
void add(LinkList &A, LinkList &B)
{
LNode * a = A -> next;
A -> next = NULL;
LNode * b = B -> next;
LNode * p = A, * s;
while(a != NULL && b != NULL)
{
if(a -> data > b -> data)
b = b -> next;
else if(a -> data < b -> data)
a = a -> next;
else
{
s = new LNode;
s -> data = a -> data;
p -> next = s;
p = s;
a = a -> next;
}
}
p -> next = NULL;
}
int main()
{
LinkList A, B;
TailForm(A);
TailForm(B);
add(A, B);
Output(A);
system("pause");
return 0;
}
#include
using namespace std;
typedef struct LNode{
LNode * next;
int data;
}LNode, * LinkList;
bool InitLinkList(LinkList &L) // 初始化一个带头结点的单链表
{
L = new LNode;
if(L == NULL)
return false;
L -> next = NULL;
return true;
}
void Output(LinkList L) // 输出一个带头结点的单链表中所有的值
{
LNode * p = L -> next;
while(p != NULL)
{
cout << p -> data << " ";
p = p -> next;
}
cout << endl;
}
void TailForm(LinkList &L) // 尾插法建立链表
{
int length;
cout << "请输入链表初始长度:" << endl;
cin >> length;
InitLinkList(L);
int i = 1, x;
LNode * p = L;
cout << "请输入链表中的初始元素:" << endl;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
p -> next = s;
p = s;
i++;
}
p -> next = NULL;
}
bool IfIs(LinkList &A, LinkList &B)
{
LNode * a = A -> next;
LNode * b = B -> next;
while(a)
{
LNode * aa = a;
LNode * bb = b;
if(a -> data == b -> data)
{
bool flag = true;
while(a && b)
{
if(a -> data != b -> data)
{
flag = false;
break;
}
a = a -> next;
b = b -> next;
}
if(flag)
return true;
a = aa;
b = bb;
}
a = a -> next;
}
return false;
}
int main()
{
LinkList A, B;
TailForm(A);
TailForm(B);
if(IfIs(A, B))
cout << "yes!" << endl;
else
cout << "no!!!" << endl;
system("pause");
return 0;
}
#include
using namespace std;
typedef struct DNode{
DNode * next;
DNode * prior;
int data;
}DNode, * DLinkList;
bool InitDLinkList(DLinkList &L) // 初始化一个循环双链表
{
L = new DNode;
if (L == NULL)
return false;
L -> next = L;
L -> prior = L;
return true;
}
void TailForm(DLinkList &L) // 尾插法
{
InitDLinkList(L);
int length, i = 1;
cout << "请输入链表长度:" << endl;
cin >> length;
DNode * p = L;
int x;
cout << "链表初始数据: " << endl;
while(i <= length)
{
DNode * s = new DNode;
cin >> x;
s -> data = x;
p -> next = s;
s -> prior = p;
p = s;
i ++;
}
p -> next = L;
L -> prior = p;
}
void output(DLinkList L) // 输出
{
DNode * p = L -> next;
cout << "输出:" << endl;
while(p != L)
{
cout << p -> data << " ";
p = p -> next;
}
cout << endl;
}
bool IF(DLinkList L)
{
DNode * p = L;
DNode * q = L;
while(p -> next != q)
{
if(p == q && p != L)
break;
p = p -> next;
q = q -> prior;
if(p -> data != q -> data)
return false;
}
return true;
}
int main()
{
DLinkList L;
TailForm(L);
if(IF(L))
cout << "yes" << endl;
else
cout << "no" << endl;
system("pause");
return 0;
}
#include
using namespace std;
typedef struct LNode{
LNode * next;
int data;
}LNode, * LinkList;
bool InitLinkList(LinkList &L) // 初始化一个带头结点的单链表
{
L = new LNode;
if(L == NULL)
return false;
L -> next = L;
return true;
}
void Output(LinkList L) // 输出一个带头结点的单链表中所有的值
{
LNode * p = L -> next;
while(p != L)
{
cout << p -> data << " ";
p = p -> next;
}
cout << endl;
}
void TailForm(LinkList &L) // 尾插法建立链表
{
int length;
cout << "请输入链表初始长度:" << endl;
cin >> length;
InitLinkList(L);
int i = 1, x;
LNode * p = L;
cout << "请输入链表中的初始元素:" << endl;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
p -> next = s;
p = s;
i++;
}
p -> next = L;
}
void add(LinkList &A, LinkList &B)
{
LNode * r1 = A;
LNode * r2 = B;
while(r1 -> next != A)
r1 = r1 -> next;
while(r2 -> next != B)
r2 = r2 -> next;
r1 -> next = B;
B -> data = 520;
r2 -> next = A;
}
int main()
{
LinkList A, B;
TailForm(A);
TailForm(B);
add(A, B);
Output(A);
system("pause");
return 0;
}
#include
using namespace std;
#define maxnum 0x7fffffff
typedef struct LNode{
LNode * next;
int data;
}LNode, * LinkList;
bool InitLinkList(LinkList &L) // 初始化一个带头结点的循环单链表
{
L = new LNode;
if(L == NULL)
return false;
L -> next = L;
return true;
}
void Output(LinkList L) // 输出一个带头结点的循环单链表中所有的值
{
LNode * p = L -> next;
while(p != L)
{
cout << p -> data << " ";
p = p -> next;
}
cout << endl;
}
void TailForm(LinkList &L) // 尾插法建立循环单链表
{
int length;
cout << "请输入链表初始长度:" << endl;
cin >> length;
InitLinkList(L);
int i = 1, x;
LNode * p = L;
cout << "请输入链表中的初始元素:" << endl;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
p -> next = s;
p = s;
i++;
}
p -> next = L;
}
void putmin(LinkList &L)
{
L -> data = maxnum;
while(L -> next != L)
{
LNode * p = L -> next;
LNode * pre = L;
LNode * minp = L -> next;
LNode * minpre = L;
while(p != L)
{
if(p -> data < minp -> data)
{
minp = p;
minpre = pre;
}
pre = p;
p = p -> next;
}
cout << minp -> data << " ";
minpre -> next = minp -> next;
delete(minp);
}
cout << endl;
delete(L);
}
int main()
{
LinkList L;
TailForm(L);
putmin(L);
system("pause");
return 0;
}
#include
using namespace std;
typedef struct DNode{ // 非循环双链表结构体
DNode * pre;
DNode * next;
int data;
int freq;
}DNode, * DLinkList;
bool InitList(DLinkList &L) //初始化一个非循环双链表
{
L = new DNode;
if(L == NULL)
return false;
L -> next = NULL;
L -> pre = NULL;
return true;
}
void TailForm(DLinkList &L) // 尾插法建立非循环双链表
{
int length;
cout << "请输入链表初始长度:" << endl;
cin >> length;
InitList(L);
int i = 1, x;
DNode * p = L;
cout << "请输入链表中的初始元素:" << endl;
while(i <= length)
{
cin >> x;
DNode * s = new DNode;
s -> data = x;
s -> freq = 0;
p -> next = s;
s -> pre = p;
p = s;
i++;
}
p -> next = NULL;
}
void output(DLinkList L) // 输出非循环双链表
{
DNode * p = L -> next;
while(p != NULL)
{
cout << p -> data << " ";
p = p -> next;
}
cout << endl;
p = L -> next;
while(p != NULL)
{
cout << p -> freq << " ";
p = p -> next;
}
cout << endl;
}
void swap(DNode * p, DNode * q) // 交换两个节点
{
if(p == q)
return;
DNode * s = q -> pre;
if(p -> next == NULL)
{
s -> next = p;
p -> pre = s;
q -> next = NULL;
q -> pre = p;
p -> next = q;
return;
}
DNode * r = p -> next;
s -> next = p;
p -> pre = s;
r -> pre = q;
q -> next = r;
p -> next = q;
q -> pre = p;
}
void Locate(DLinkList &L, int x) // 目标函数
{
DNode * p = L -> next;
while(p != NULL)
{
if (p -> data == x)
{
p -> freq++;
DNode * q = p -> pre;
while (q != L && p -> freq >= q -> freq)
{
swap(p, q);
q = p -> pre;
}
break;
}
p = p -> next;
}
}
int main()
{
DLinkList L;
TailForm(L);
int x;
cout << "输入要调用的数据: " << endl;
while(cin >> x)
{
Locate(L, x);
output(L);
}
system("pause");
return 0;
}
/*
一. 先遍历链表,找到尾指针,存下尾指针的后继节点,记为point
再遍历链表,与point作对比,若相等则返回true
否则返回false
二. 弗洛伊德判圈法
*/
#include
using namespace std;
typedef struct LNode{
LNode * next;
int data;
}LNode, * LinkList;
bool hasCycle(LinkList L) // 弗洛伊德判圈法
{
LNode * slow = L;
LNode * fast = L;
if(L -> next == NULL || L == NULL)
return false;
while(fast != NULL && fast -> next != NULL)
{
slow = slow -> next;
fast = fast -> next -> next;
if(fast == slow)
return true;
}
return false;
}
/*
时间复杂度:O(n)
空间复杂度:O(1)
*/
/*
先找两个指针指向L -> next
先让p向后移动k个距离,再让p与q一起移动,直至p指向尾指针
此时q所指向的就是倒数第k个结点
*/
#include
using namespace std;
typedef struct LNode{
LNode * next;
int data;
}LNode, * LinkList;
bool InitLinkList(LinkList &L) // 初始化一个带头结点的单链表
{
L = new LNode;
if(L == NULL)
return false;
L -> next = NULL;
return true;
}
void Output(LinkList L) // 输出一个带头结点的单链表中所有的值
{
LNode * p = L -> next;
while(p != NULL)
{
cout << p -> data << " ";
p = p -> next;
}
cout << endl;
}
void TailForm(LinkList &L) // 尾插法建立链表
{
int length;
cout << "请输入链表初始长度:" << endl;
cin >> length;
InitLinkList(L);
int i = 1, x;
LNode * p = L;
cout << "请输入链表中的初始元素:" << endl;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
p -> next = s;
p = s;
i++;
}
p -> next = NULL;
}
int findk(LinkList L, int k)
{
LNode * p = L -> next;
LNode * q = L -> next;
int i = 1;
while(i < k && p != NULL)
{
p = p -> next;
i ++;
}
if(i < k || p == NULL)
return 0;
while(p -> next != NULL)
{
p = p -> next;
q = q -> next;
}
cout << q -> data << endl;
return 1;
}
int main()
{
LinkList L;
TailForm(L);
int k;
cout << "请输入欲得到的倒数第k个结点:" << endl;
cin >> k;
findk(L, k);
system("pause");
return 0;
}
/*
时间复杂度:O(n)
空间复杂度: O(1)
*/
/*
先求出两个链表的长度
让长度长的链表的指针先向后移动
再同步向后移动
*/
#include
#include
using namespace std;
typedef struct LNode{
LNode * next;
char data;
}LNode, * LinkList;
bool InitLinkList(LinkList &L) // 初始化一个带头结点的单链表
{
L = new LNode;
if(L == NULL)
return false;
L -> next = NULL;
return true;
}
void Output(LinkList L) // 输出一个带头结点的单链表中所有的值
{
LNode * p = L -> next;
while(p != NULL)
{
cout << p -> data << " ";
p = p -> next;
}
cout << endl;
}
void TailForm(LinkList &L) // 尾插法建立链表
{
int length;
cout << "请输入链表初始长度:" << endl;
cin >> length;
InitLinkList(L);
int i = 1;
char x;
LNode * p = L;
cout << "请输入链表中的初始元素:" << endl;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
p -> next = s;
p = s;
i++;
}
p -> next = NULL;
}
int length(LinkList L)
{
LNode * p = L -> next;
int i = 0;
while(p != NULL)
{
p = p -> next;
i ++;
}
return i;
}
LNode * After(LinkList A, LinkList B)
{
LNode * p = A -> next, * q = B -> next;
int lena = length(A), lenb = length(B);
int i = 1;
if(lena >= lenb)
while(i <= lena - lenb)
{
p = p -> next;
i ++;
}
else
while(i <= lenb - lena)
{
q = q -> next;
i ++;
}
while(p != NULL)
{
if(p -> data == q -> data)
break;
p = p -> next;
q = q -> next;
}
return p;
}
int main()
{
LinkList A, B;
TailForm(A);
TailForm(B);
LNode * p = After(A, B);
cout << p -> data << endl;
system("pause");
return 0;
}
/*
时间复杂度:O(lena + lenb)
空间复杂度:O(1)
*/
/*
用空间换时间
定义计数数组
*/
#include
#include
using namespace std;
typedef struct LNode{
LNode * next;
int data;
}LNode, * LinkList;
bool InitLinkList(LinkList &L) // 初始化一个带头结点的单链表
{
L = new LNode;
if(L == NULL)
return false;
L -> next = NULL;
return true;
}
void Output(LinkList L) // 输出一个带头结点的单链表中所有的值
{
LNode * p = L -> next;
while(p != NULL)
{
cout << p -> data << " ";
p = p -> next;
}
cout << endl;
}
void TailForm(LinkList &L) // 尾插法建立链表
{
int length;
cout << "请输入链表初始长度:" << endl;
cin >> length;
InitLinkList(L);
int i = 1;
int x;
LNode * p = L;
cout << "请输入链表中的初始元素:" << endl;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
p -> next = s;
p = s;
i++;
}
p -> next = NULL;
}
void function(LinkList &L, int * count, int n)
{
int i;
for(i = 0; i < n + 1; i++)
count[i] = 0;
LNode * p = L -> next;
LNode * pre = L;
while(p != NULL)
{
if(count[abs(p->data)] == 0)
{
count[abs(p->data)] ++;
pre = p;
p = p -> next;
}
else
{
pre -> next = p -> next;
delete(p);
p = pre -> next;
}
}
}
int main()
{
LinkList L;
int n;
cout << "请输入数组的最大值:" << endl;
cin >> n;
int count[n + 1];
TailForm(L);
function(L, count, n);
Output(L);
system("pause");
return 0;
}
/*
时间复杂度:O(max(n, length))
空间复杂度: O(n)
*/
/*
先找到单链表的中间结点r
将单链表后半段就地逆置
以r为工作指针将结点插入相应的位置
*/
#include
using namespace std;
typedef struct LNode{
LNode * next;
int data;
}LNode;
void TailForm(LNode * &L)
{
L = new LNode;
if(L == NULL)
return;
L -> next = NULL;
cout << "请输入链表初始长度:" << endl;
int length;
cin >> length;
int x, i = 1;
LNode * p = L;
cout << "链表初始元素:" << endl;
while(i <= length)
{
cin >> x;
LNode * s = new LNode;
s -> data = x;
s -> next = p -> next;
p -> next = s;
p = s;
i++;
}
p -> next = NULL;
}
void output(LNode * L)
{
LNode * p = L -> next;
while(p != NULL)
{
cout << p -> data << " ";
p = p -> next;
}
cout << endl;
}
int len(LNode * L)
{
int i = 1;
LNode * p = L->next;
while(p != NULL)
{
p = p->next;
i++;
}
return i;
}
void function(LNode * &L)
{
LNode * p = L->next;
LNode * r = L->next;
if(p->next == NULL)
return;
LNode * rpre = L;
int length = len(L);
int i = 1, l = length / 2;
if(length % 2 == 0)
while(i < l + 1)
{
rpre = rpre->next;
r = r->next;
i++;
}
else
while(i <= l + 1)
{
rpre = rpre->next;
r = r->next;
i++;
}
rpre->next = NULL;
while(r != NULL)
{
LNode * link = r -> next;
r->next = rpre->next;
rpre->next = r;
r = link;
}
r = rpre->next;
LNode * q = p->next;
while(r != NULL)
{
rpre->next = r->next;
r->next = p->next;
p->next = r;
p = q;
q = q->next;
r = rpre->next;
}
}
int main()
{
LNode * L;
TailForm(L);
function(L);
output(L);
system("pause");
return 0;
}
/*
时间复杂度:O(n)
空间复杂度:O(1)
*/