主文件
#include <stdio.h>
#include "./03_headsx.h"
int main(int argc, const char *argv[])
{
SeqList* list = create_seqList();
//插入数据
charu(list,10);
charu(list,10);
charu(list,20);
charu(list,20);
charu(list,30);
//遍历
bianli(list);
//删除
/*deletesj(list);
deletesj(list);
deletesj(list);
deletesj(list);
deletesj(list);*/
//按下标插入数据
/* xb_charu(list);
bianli(list); */
//按下标删除数据
/* xb_delete(list,1);
bianli(list); */
//按下标修改数据
/* xb_xiugai(list,1,777);
bianli(list); */
//按下标查找数据
/* xb_chazhao(list,1); */
//按数据修改数据
/* sj_xiugai(list,20,90);
bianli(list); */
//按数据查找位置
/* sj_select(list,20);
sj_select(list,99); */
//顺序表去重
return 0;
}
调用的函数文件
#include <stdio.h>
#include <stdlib.h>
#include "./03_headsx.h"
//创建顺序表
SeqList* create_seqList()
{
SeqList* list = (SeqList*)malloc(sizeof(SeqList));
if(list==NULL)
{
return NULL;
}
list->pos = 0; //初始化操作的位置为0
return list;
}
//增 插入数据
void charu(SeqList* list,datasj num)
{
//判断顺序表是否为满
if(list->pos >= N)
{
printf("顺序表已满,无法在插入\n");
return;
}
//在pos的位置插入数据
list->data[list->pos] = num;
//pos++
(list->pos)++;
printf("插入成功\n");
return;
}
//遍历数据
void bianli(SeqList* list)
{
int i = 0;
for(i = 0;i<list->pos;i++)
{
printf("%d ",list->data[i]);
}
printf("\n");
return;
}
//删除数据
int deletesj(SeqList* list)
{
//判断是否为空
if(list->pos <= 0)
{
printf("顺序表为空,无法再删除!\n");
return -1;
}
//然后pos--
(list->pos)--;
//num=data[pos]
int num = list->data[list->pos];
printf("删除成功\n");
printf("%d\n",num);
return num;
}
//按下标插入数据
void xb_charu(SeqList* list)
{
if(list->pos >= N)
{
printf("顺序表已满\n");
return;
}
int i = list->pos;
printf("请输入对应下标插入数据:");
int index;
scanf("%d",&index);
for(i=(list->pos); i>index ;i--)
{
list->data[i] = list->data[i-1];
}
list->data[index] = 55;
//printf("请输入要插入的数据:");
//int n=list->data[index];
//scanf("%d",&n);
if(index<0 || index>i) //判断输入的下标是否正确
{
printf("下标输入不正确,请正确输入!!!\n");
}
(list->pos)++;
printf("按下标插入成功\n");
return;
}
//按下标删除数据
datasj xb_delete(SeqList* list,int index)
{
if(list->pos <= 0)
{
printf("顺序表为空,无法删除\n");
return -1;
}
if(index<0 || index>=list->pos) //判断输入的下标是否正确
{
printf("下标输入不正确,请正确输入!!!\n");
return -1;
}
int n = list->data[index];
int i = index;
for(i=index; i<(list->pos)-1; i++)
{
list->data[i] = list->data[i+1];
}
(list->pos)--;
printf("按下标删除成功\n");
printf("%d\n",n);
return n;
}
//按下标修改数据
int xb_xiugai(SeqList* list,int index, datasj newNum)
{
if(index<0 || index>=list->pos)
{
printf("修改位置非法,请重新输入\n");
return -1;
}
list->data[index] = newNum;
printf("修改成功\n");
return 0;
}
//按下标查找数据
datasj xb_chazhao(SeqList* list,int index)
{
if(index<0 || index>=list->pos)
{
printf("修改位置非法,请重新输入\n");
return -1;
}
printf("查找成功\n");
int n = list->data[index];
printf("%d\n",n);
return n;
}
//按数据修改数据
void sj_xiugai(SeqList* list, datasj old, datasj new)
{
int i = 0;
for(i=0; i<list->pos; i++)
{
if(list->data[i] == old)
{
list->data[i] = new;
}
}
printf("修改成功\n");
return;
}
//按数据查找位置
int sj_select(SeqList* list, datasj num)
{
int i = 0;
for(i = 0; i < list->pos; i++) //找到该数据后,将下标返回
{
if(list->data[i] == num)
{
printf("%d\n",i);
return i;
}
}
//数据不存在
return -1;
}
//顺序表去重
void quchong(SeqList* list, datasj n)
{
}
头文件
#ifndef __fun_h__ #define __fun_h__ #define N 10 typedef int datasj; //实际应用中不一定是int类型,取个别名 typedef struct { int data[N]; int pos; }SeqList; SeqList* create_seqList(); void charu(SeqList* list,datasj num); //插入 void bianli(SeqList* list); //遍历 int deletesj(SeqList* list); //删除 void xb_charu(SeqList* list); //按下标插入数据 datasj xb_delete(SeqList* list,int index); //按下标删除数据 //按下标修改数据 int xb_xiugai(SeqList* list,int index, datasj newNum); //按下标查找数据 datasj xb_chazhao(SeqList* list,int index); //按数据修改数据 void sj_xiugai(SeqList* list, datasj old, datasj new); //按数据查找位置 int sj_select(SeqList* list, datasj num); #endif