第二章
01题目,存储相对紧凑,所以存储的密度大。
04题目,顺序表可以按照序号随机存取,时间的复杂度为O(1)。
第7题目分析
bash
#include <iostream>
using namespace std;
#define MaxSize 50
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];
int length;
}SqList;
//-----------------代码核心--------------------//
bool Del_Min(SqList &L, ElemType& value)
{
if (L.length == 0)
return false;
value = L.data[0];
int pos = 0;
for (int i = 1; i < L.length; i++)
{
if (value > L.data[i])
{
value = L.data[i];
pos = i;
}
}
L.data[pos] = L.data[L.length - 1];
L.length--;
return true;
}
//---------------------------------------------------//
int main(void)
{
SqList L;
//初始化顺序表
//...
int value = 0;
Del_Min(L, value);
return 0;
}
cpp
#include <stdio.h>
#include <iostream>
using namespace std;
#define MaxSize 50
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];
int length;
}SqList;
//1 2 3 4 5 6 ---- 6 5 4 3 2 1 length = 6
// 1 2 3 4 5 6 7 ---- 7 6 5 4 3 2 1 length = 7
//-------------------------代码核心------------------------//
bool reverse(SqList& L)
{
ElemType temp; //辅助变量
for (int i = 0; i <(L.length) / 2; i++)
{
temp = L.data[i];
L.data[i] = L.data[L.length - 1 - i];
L.data[L.length - 1 - i] = temp;
}
return true;
}
//-------------------------代码核心------------------------//
int main(void)
{
SqList L;
//初始化顺序表
L.length = 6;
for (int i = 0; i < L.length; i++)
L.data[i] = i;
reverse(L);
for (int i = 0; i < L.length; i++)
//cout << L.data[i] << endl;
printf("L.data[%d]=%d\n", i, L.data[i]);
return 0;
}
cpp
#include <stdio.h>
#include <iostream>
using namespace std;
#define MaxSize 50
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];
int length;
}SqList;
//-------------------------代码核心------------------------//
bool Delete_all_x_value(SqList& L, ElemType x) //1 2 3 3 4 5
{
int k = 0;
for (int i = 0; i < L.length; i++)
{
if (L.data[i] != x)
{
L.data[k] = L.data[i];
k++;
}
}
L.length = k;
return true;
}
//-------------------------代码核心------------------------//
int main(void)
{
SqList L;
//初始化顺序表
L.length = 6;
L.data[0] = 1;
L.data[1] = 2;
L.data[2] = 3;
L.data[3] = 3;
L.data[4] = 4;
L.data[5] = 5;
int value = 3;
Delete_all_x_value(L,value);
for (int i = 0; i < L.length; i++)
//cout << L.data[i] << endl;
printf("L.data[%d]=%d\n", i, L.data[i]);
return 0;
}
cpp
#include <stdio.h>
#include <iostream>
using namespace std;
#define MaxSize 50
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];
int length;
}SqList;
//-------------------------代码核心------------------------//
//法1
//bool Delete_all_range_value(SqList& L, ElemType s, ElemType t) //1 2 3 3 4 5
//{
// int k = 0;
// if (L.length == 0||s>=t)
// return false;
// for (int i = 0; i < L.length; i++)
// {
// if (L.data[i] < s|| L.data[i] > t)
// {
// L.data[k] = L.data[i];
// k++;
// }
// }
// L.length = k;
// return true;
//}
bool Delete_all_range_value(SqList& L, ElemType s, ElemType t) //1 2 3 3 4 5
{
int i = 0, j = 0;
if (L.length == 0||s>=t)//判断
return false;
for (i = 0; i < L.length && L.data[i] < s; i++); //寻找大于或等于s的第一个元素
if(i>=L.length) //所有的数都比s小
return false;
for (j = i; j < L.length && L.data[j] <= t; j++);//寻找大于t的第一个元素
for (; j < L.length; i++, j++)
L.data[i] = L.data[j];
L.length = i;
return true;
}
//-------------------------代码核心------------------------//
int main(void)
{
SqList L;
//初始化顺序表
L.length = 6;
L.data[0] = 1;
L.data[1] = 2;
L.data[2] = 3;
L.data[3] = 3;
L.data[4] = 4;
L.data[5] = 5;
int s = 2;
int t = 3;
Delete_all_range_value(L, s, t);
for (int i = 0; i < L.length; i++)
{
printf("L.data[%d]=%d\n", i, L.data[i]);
}
printf("L.length=%d\n", L.length);
return 0;
}
cpp
#include <stdio.h>
#include <iostream>
using namespace std;
#define MaxSize 50
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];
int length;
}SqList;
//-------------------------代码核心------------------------//
bool Delete_all_range_value(SqList& L, ElemType s, ElemType t) //1 2 3 3 4 5
{
int k = 0;
if (L.length == 0||s>=t)
return false;
for (int i = 0; i < L.length; i++)
{
if (L.data[i]<s|| L.data[i] > t)
{
L.data[k] = L.data[i];
k++;
}
}
L.length = k;
return true;
}
//-------------------------代码核心------------------------//
int main(void)
{
SqList L;
//初始化顺序表
L.length = 6;
L.data[0] = 1;
L.data[1] = 2;
L.data[2] = 3;
L.data[3] = 3;
L.data[4] = 4;
L.data[5] = 5;
int s = 2;
int t = 3;
Delete_all_range_value(L, s, t);
for (int i = 0; i < L.length; i++)
{
printf("L.data[%d]=%d\n", i, L.data[i]);
}
printf("L.length=%d\n", L.length);
return 0;
}
cpp
#include <stdio.h>
#include <iostream>
using namespace std;
#define MaxSize 50
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];
int length;
}SqList;
//-------------------------代码核心------------------------//
bool Delete_same(SqList& L) //1 2 2 2 3 3 3 3 4 4 4 5 5 5
{
if(L.length==0)
return false;
int i = 0, j = 0;
for (i = 0, j = 1; j < L.length; j++)
{
if (L.data[i] != L.data[j])
L.data[++i] = L.data[j];
}
L.length = i + 1;
return true;
}
//-------------------------代码核心------------------------//
int main(void)
{
SqList L;
//初始化顺序表
L.length = 14;
L.data[0] = 1;
L.data[1] = 2;
L.data[2] = 2;
L.data[3] = 2;
L.data[4] = 3;
L.data[5] = 3;
L.data[6] = 3;
L.data[7] = 3;
L.data[8] = 4;
L.data[9] = 4;
L.data[10] = 4;
L.data[11] = 5;
L.data[12] = 5;
L.data[13] = 5;
Delete_same(L);
for (int i = 0; i < L.length; i++)
{
printf("L.data[%d]=%d\n", i, L.data[i]);
}
printf("L.length=%d\n", L.length);
return 0;
}
cpp
#include <stdio.h>
#include <iostream>
using namespace std;
#define MaxSize 50
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];
int length;
}SqList;
//-------------------------代码核心------------------------//
bool merge(SqList A, SqList B, SqList &C)
{
if (A.length + B.length > MaxSize)
return false;
int i = 0, j = 0, k = 0;
while (i < A.length && j < B.length)
{
if (A.data[i] <= B.data[j])
C.data[k++] = A.data[i++];
else //B.data[j] <= A.data[j]
C.data[k++] = B.data[j++];
}
while (i < A.length)
C.data[k++] = A.data[i++];
while (j < B.length)
C.data[k++] = B.data[j++];
C.length = k;
return true;
}
//-------------------------代码核心------------------------//
int main(void)
{
SqList A;
SqList B;
SqList C;//合并后的顺序表
//初始化顺序表
//-------------L1-------------//
A.length = 3;
A.data[0] = 5;
A.data[1] = 6;
A.data[2] = 7;
//-------------L2-------------//
B.length = 4;
B.data[0] = 1;
B.data[1] = 2;
B.data[2] = 3;
B.data[3] = 4;
merge(A, B, C);
for (int i = 0; i < C.length; i++)
{
printf("L.data[%d]=%d\n", i, C.data[i]);
}
printf("L.length=%d\n", C.length);
return 0;
}
cpp
#include <stdio.h>
#include <iostream>
using namespace std;
#define MaxSize 50
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];
int length;
}SqList;
//-------------------------代码核心------------------------//
typedef int DataType;
bool Reverse(DataType A[], int left, int right, int arraysize)
{
if (left >= right || right >= arraysize)
return false; //0 1 2 3 4 5 //0 1 2 3 4 5 6
int mid = (left + right) / 2; //11 12 13 14 15 16 奇数 //21 22 23 24 25 26 27 偶数
for (int i = 0; i <= mid - left; i++)
{
DataType temp = A[left + i];
A[left + i] = A[right - i];
A[right - i] = temp;
}
}
void Exchange(DataType A[], int m, int n, int arraysize)
{
Reverse(A, 0, m + n - 1, arraysize);
Reverse(A, 0, n - 1, arraysize);
Reverse(A, n, m + n - 1, arraysize);
}
//-------------------------代码核心------------------------//
int main(void)
{
SqList A;
SqList B;
int arraysize = 50;
int data[MaxSize] = { 0 };
//初始化顺序表
//-------------L1-------------//
A.length = 3;
data[0] = A.data[0] = 5;
data[1] = A.data[1] = 6;
data[2] = A.data[2] = 7;
//-------------L2-------------//
B.length = 4;
data[3] = B.data[0] = 1;
data[4] = B.data[1] = 2;
data[5] = B.data[2] = 3;
data[6] = B.data[3] = 4;
Exchange(data, A.length,B.length, arraysize);
for (int i = 0; i < A.length + B.length; i++)
{
printf("data[%d]=%d\n", i,data[i]);
}
printf("L.length=%d\n", A.length + B.length);
return 0;
}