程序:
#include <stdio.h>
#include <stdlib.h>
#define N 6
//定义一个叫做 Seqlist 的结构体类型
typedef struct
{
int date[N]; //存储顺序表中的数据
int pos; //存储即将操作的顺序表位置下标,同时还能代表顺序表长度
}Seqlist;
//创建顺序表
Seqlist* create_seqlist(void)
{
//需要创建一个数组,需要创建一个pos
//所以定义一个结构体类型包含书序date和pos
Seqlist *sq = (Seqlist*)malloc(sizeof(Seqlist));//为了方便操作,使用堆空间,不手动释放一直存在
if(NULL == sq)
{
printf("创建顺序表失败\n");
return NULL;
}
//数组date不做初始化为随机值,函数的数据在后续插入增函数中会被覆盖,因此是否赋值置0都行
for(int i=0; i<N; i++)
{
sq->date[i] = 0;
}
//验证:遍历输出数组date
for (int j=0; j<N; j++)
{
printf("%d", sq->date[j]);
}
putchar(10);
//pos 置0
sq->pos = 0;
printf("%d\n", sq->pos);
return sq; //返回堆空间首地址,该堆空间中包含了数组date和pos
}
//插入:增
int insert_seqlist(Seqlist* sq, int num)
{
//判断顺序表是否为满
if(N == sq->pos)
{
printf("顺序表为满,失败\n");
return -1;
}
//在pos位置插入数据
sq->date[sq->pos] = num;
//更新pos位置
sq->pos++;
return 0;
}
//查:遍历顺序表
void show_seqlist(Seqlist* sq)
{
for(int i=0; i<sq->pos; i++)
{
printf("%d ", sq->date[i]);
}
putchar(10);
return;
}
//删:删除顺序表中的数据
int delete_seqlist(Seqlist* ptr1)
{
//判断顺序表是否为空
if(0 == ptr1->pos)
{
printf("顺序表为空,无法删除\n");
return -1;
}
//pos下移1位,顺序表长度减1;即顺序表中pos的位置更新,做--
ptr1->pos--;
//将删除的数据返回出去
return ptr1->date[ptr1->pos];
}
//按位置插入
int weizhicharu(Seqlist* sq,int index, int num)
{
//判断顺序表是否为满
if(N == sq->pos)
{
printf("顺序表为满,插入失败\n");
return -1;
}
//判断插入位置是否合法
if(0 > index || index > sq->pos)
{
printf("index=%d插入位置非法\n",index);
return -1;
}
//将下标index及其后面的数据循环后移1个位置
for(int i=sq->pos; i>=index+1; i--)
{
sq->date[i]=sq->date[i-1];
}
//将num插入到date下标为index的位置
sq->date[index]=num;
//插入完毕更新pos位置
sq->pos++;
//可以返回插入的数据做验证
return sq->date[index];
}
//按位置删除数据
void weizhishanchu(Seqlist* str2, int index)
{
//判断顺序表是否为空
if(0 == str2->pos)
{
printf("顺序表为空,删除失败\n");
return;
}
//判断删除位置是否合法
if(0>index || index > str2->pos-1)
{
printf("删除位置不合法\n");
return;
}
//将date下标为index的数据删除,后面的数据循环往前覆盖
for(int i=index; i<str2->pos-1; i++)
{
str2->date[i]=str2->date[i+1];
}
//删除完毕更新pos
str2->pos--;
return;
}
int main(int argc, const char *argv[])
{
//创建Seqlist类型的指针sq,用来接create_seqlist函数的(Seqlist*)类型的返回值
Seqlist* sq;
sq = create_seqlist();
//调用插入用函数
printf("%d",'\n');
insert_seqlist(sq, 65);
insert_seqlist(sq, 12);
insert_seqlist(sq, 23);
insert_seqlist(sq, 77);
insert_seqlist(sq, 12);
insert_seqlist(sq, 45);
//调用遍历函数查验结果,显示 65 12 23 77 12 45
show_seqlist(sq);
//调用删除函数,打印返回值,即删除的数据
int a=delete_seqlist(sq);
printf("被删除的数据为 %d\n", a);
//调用遍历函数查验结果,显示 65 12 23 77 12
show_seqlist(sq);
//调用位置插入函数,打印返回值,即插入的数据
int b=weizhicharu(sq, 2, 100);
printf("插入的数据为 %d\n", b);
//调用遍历函数查验结果,显示 65 12 100 23 77 12
show_seqlist(sq);
//调用函数weizhishanchu
weizhishanchu(sq, 3);
//调用遍历函数查验结果,显示 65 12 100 77 12
show_seqlist(sq);
return 0;
}
T2:
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #define N 15
5
6 //定义一个叫做 Seqlist 的结构体类型
7 typedef struct
8 {
9 int date[N]; //存储顺序表中的数据
0 int pos; //存储即将操作的顺序表位置下标,同时还能代表顺序表长度
1 }Seqlist;
2
3 //创建顺序表
4 Seqlist* create_seqlist(void)
5 {
6 //需要创建一个数组,需要创建一个pos
7 //所以定义一个结构体类型包含书序date和pos
8
9 Seqlist *sq = (Seqlist*)malloc(sizeof(Seqlist));//为了方便操作,使用堆空间,不手动释放一直存在
0 if(NULL == sq)
1 {
2 printf("创建顺序表失败\n");
3 return NULL;
4 }
5 //pos 置0
6 sq->pos = 0;
7
8 return sq; //返回堆空间首地址,该堆空间中包含了数组date和pos
9
0 }
1
2 //插入:增
3 int insert_seqlist(Seqlist* sq, int num)
4 {
5 //判断顺序表是否为满
6 if(N == sq->pos)
7 {
8 printf("顺序表为满,失败\n");
9 return -1;
0 }
1 //在pos位置插入数据
2 sq->date[sq->pos] = num;
3 //更新pos位置
4 sq->pos++;
5
6 return 0;
7 }
8
9 //按位置删除数据
0 void weizhishanchu(Seqlist* str2, int index)
1 {
2 //判断顺序表是否为空
3 if(0 == str2->pos)
4 {
5 printf("顺序表为空,删除失败\n");
6 return;
7 }
8 //判断删除位置是否合法
9 if(0>index || index > str2->pos-1)
0 {
1 printf("删除位置不合法\n");
2 return;
3 }
4 //将date下标为index的数据删除,后面的数据循环往前覆盖
5 for(int i=index; i<str2->pos-1; i++)
6 {
7 str2->date[i]=str2->date[i+1];
8 }
9 //删除完毕更新pos
0 str2->pos--;
1
2 return;
3
4 }
5
6 //查:遍历顺序表
7 void show_seqlist(Seqlist* sq)
8 {
9 for(int i=0; i<sq->pos; i++)
0 {
1 printf("%d ", sq->date[i]);
2
3 }
4 putchar(10);
5
6 return;
7 }
8
9 //去重
0 void quchong(Seqlist* str)
1 {
2 int i,j;
3
4 //判断顺序表是否为空
5 if(0 == str->pos)
6 {
7 printf("顺序表为空\n");
8 return;
9 }
0 for(i=0; i<str->pos-1; i++)
1 {
2 for(j=i+1; j<=str->pos-1; j++)
3 {
4 if(str->date[i] == str->date[j])
5 {
6 weizhishanchu(str, j);
7 //删除的原理是将j后面的数据前移一个位置
//若此时j++会倒置刚前移的数据比较不到(如有连续多个相同的数据)
//所以先j--,再j++,将j的位置重新比较一次
j--;
8 }
9 }
0 }
1 return ;
2
3 }
4
5 主函数
6
7 int main(int argc, const char *argv[])
8 {
9
0 //创建Seqlist类型的指针sq,用来接create_seqlist函数的(Seqlist*)类型的返回值
1 Seqlist* sq;
2 sq = create_seqlist();
3 //调用插入用函数
4 insert_seqlist(sq, 1);
5 insert_seqlist(sq, 2);
6 insert_seqlist(sq, 1);
7 insert_seqlist(sq, 1);
8 insert_seqlist(sq, 3);
9 insert_seqlist(sq, 4);
0 insert_seqlist(sq, 3);
1 insert_seqlist(sq, 2);
2 insert_seqlist(sq, 4);
3 insert_seqlist(sq, 5);
4 insert_seqlist(sq, 3);
5 insert_seqlist(sq, 4);
6 insert_seqlist(sq, 5);
7 insert_seqlist(sq, 4);
8 insert_seqlist(sq, 1);
9 //调用遍历函数查验结果,1 2 1 1 3 4 3 2 4 5 3 4 5 4 1
0 show_seqlist(sq);
1 //调用去重函数
2 quchong(sq);
3 //调用遍历函数查验结果,1 2 3 4 5
4 show_seqlist(sq);
5
6
7
8
9 return 0;
0 }
1