#include <stdio.h>
#define MaxSize 50//定义线性表的最大长度
typedef int ElemType;
typedef struct SqList {
ElemType data[MaxSize];
int length;
}SqList;
bool Del_Min(SqList& L) {
if (L.length== 0) {
printf("线性表为空");
return false;
}
int temp = L.data[0];
int pos = 0;//记录最小值的位置
for (int i = 0; i < L.length; i++)
{
if (temp > L.data[i]) {
temp = L.data[i];//找出最小值元素
pos = i;
}
}
printf("最小值元素为:%d", temp);
L.data[pos] = L.data[L.length - 1];//将最后一个元素填补进去
L.length--;
return true;
}
int main() {
SqList L;
L.length = 0;
for (int i = 0; i < 10; i++)
{
printf("请输入第%d个数:", i + 1);
scanf_s("%d", &L.data[i]);
L.length++;
}
Del_Min(L);
}
程序结果如下:
#include <stdio.h>
#define MaxSize 50//定义线性表的最大长度
typedef int ElemType;
typedef struct SqList {
ElemType data[MaxSize];
int length;
}SqList;
void Reverse(SqList& L) {
int 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;
}
for (int i = 0; i < L.length;i++)
{
printf("经过逆置后的第%d个元素为%d\n",i + 1, L.data[i]);
}
}
int main() {
SqList L;
L.length = 0;
for (int i = 0; i < 6; i++)
{
printf("请输入第%d个数:", i + 1);
scanf_s("%d", &L.data[i]);
L.length++;
}
Reverse(L);
}
运行后的代码结果是:
void Del_X(SqList& L,int x) {
int k = 0;//用来计数,删除元素
for (int i = 0; i < L.length; i++)
{
if (x == L.data[i]) {
k++;
}
else {
L.data[i-k] = L.data[i];//将不跟X相等的数据元素向前移动
}
}
L.length = L.length - k;
for (int i = 0; i < L.length; i++)
{
printf("经过改变后的第%d个元素为%d\n", i + 1, L.data[i]);
}
}
int main() {
SqList L;
L.length = 0;
for (int i = 0; i < 6; i++)
{
printf("请输入第%d个数:", i + 1);
scanf_s("%d", &L.data[i]);
L.length++;
}
int y;
printf("请输入要删除元素的值:");
scanf_s("%d", &y,sizeof(int));
Del_X(L,y);
}
运行结果为:
bool del_s_t(SqList& L, int s, int t) {//有序顺序表
int k=0;
if (s > t) {
printf("输入的值不合法");
return false;
}
if (L.length==0)
{
printf("线性表为空");
return false;
}
for (int i = 0; i < L.length; i++)
{
if(L.data[i] > s && L.data[i] < t){
k++;
}
else {
L.data[i - k] = L.data[i];
}
}
L.length = L.length - k;
return true;
}
int main() {
SqList L;
L.length = 0;
for (int i = 0; i < 6; i++)
{
printf("请输入第%d个数:", i + 1);
scanf_s("%d", &L.data[i]);
L.length++;
}
int s,t;
printf("请输入s的值:");
scanf_s("%d", &s,sizeof(int));
printf("/n请输入t的值:");
scanf_s("%d", &t, sizeof(int));
del_s_t(L, s, t);
for (int i = 0; i < L.length; i++)
{
printf("经过改变后的第%d个元素为%d\n", i + 1, L.data[i]);
}
}
运行结果为:
void Del_same(SqList& L) {
int k = 0;
int i=0;
for (int j=1; j < L.length; j++)
{
if (L.data[i] != L.data[j]) {
L.data[++i] = L.data[j];
}
}
L.length = i+1;
}
int main() {
SqList L;
L.length =0;
for (int i = 0; i < 6; i++)
{
printf("请输入第%d个数:", i + 1);
scanf_s("%d", &L.data[i]);
L.length++;
}
Del_same(L);
for (int i = 0; i < L.length; i++)
{
printf("经过改变后的第%d个元素为%d\n", i + 1, L.data[i]);
}
}
运行结果为: