一、main函数
cs
#include <stdio.h>
#include <stdlib.h>
#include "./3.sequence.h"
int main(int argc, const char *argv[])
{
SeqList*list = create_seqlist();
insert_seqlist(list,10);
insert_seqlist(list,100);
insert_seqlist(list,1000);
insert_seqlist(list,10000);
insert_seqlist(list,200);
show_list(list);
dataype num=del_seqlist(list);
printf("%d\n",num);
int index;
dataype temp;
printf("请输入要插入的位数和数据>>>");
scanf("%d",&index);
scanf("%d",&temp);
mid_inser_seqlist(list,index,temp);
show_list(list);
int n;
printf("请输入要删除的位数");
scanf("%d",&n);
printf("%d\n",mid_delseqlist(list,n));
show_list(list);
updata_list(list,2,777);
show_list(list);
select_seqlist(list,1);
show_list(list);
int oldnum=999;
int newnum=10086;
updataBydata_list(list,999,10086);
show_list(list);
int a=selectBydata_seqlist(list,10086);
printf("%d\n",a);
return 0;
}
二、功能函数
cs
#include <stdio.h>
#include <stdlib.h>
#include "./3.sequence.h"
SeqList*create_seqlist()
{
SeqList *list = (SeqList*)malloc(sizeof(SeqList));
if(NULL == list)
{
return NULL;
}
list->pos =0;
return list;
}
void insert_seqlist(SeqList*list,dataype num) //从尾部插入数据
{
if(list->pos >=N)
{
printf("顺序表满,插入失败\n");
return;
}
list->data[list->pos] = num;
(list->pos)++;
return;
}
void show_list(SeqList*list) //遍历顺序表
{
int n;
for(n=0;n<list->pos;n++)
{
printf("%d ",list->data[n]);
}
printf("\n");
return;
}
dataype del_seqlist(SeqList*list) //从尾部删除数据
{
if(list->pos>0)
{
(list->pos)--;
dataype num=list->data[list->pos];
return num;
}
}
void mid_inser_seqlist(SeqList*list,int index,dataype temp) //按照下标插入数据
{
int i;
if(index>=N || list->pos>=N || index<=0)
{
printf("插入位置不合法或顺序表已满\n");
return;
}
for(i=list->pos;i>index;i--)
{
list->data[i]=list->data[i-1];
}
list->pos++;
list->data[i]=temp;
}
dataype mid_delseqlist(SeqList*list,int index) //按照下标删除数据
{
int i=index;
dataype temp;
if(index>list->pos || list->pos<=0 || index<0)
{
printf("删除位置不合法或顺序表为空\n");
return 0;
}
temp=list->data[index];
for(i=index;i<list->pos-1;i++)
{
list->data[i]=list->data[i+1];
}
list->pos--;
return temp;
}
int updata_list(SeqList*list,int index,dataype newnum) //按照下标修改数据
{
if(index<0 || index>=list->pos)
{
printf("修改位置不合法!\n");
return -1;
}
list->data[index]=newnum;
printf("修改成功!\n");
return 0;
}
dataype select_seqlist(SeqList*list,int index) //按照下标查找数据
{
if(index<0 || index>=list->pos)
{
printf("查找位置不合法!\n");
return -1;
}
return list->data[index];
}
void updataBydata_list(SeqList*list,dataype oldnum,dataype newnum) //按数据修改数据
{
int i=0;
for(i;i<list->pos;i++)
{
if(list->data[i] == oldnum)
{
list->data[i] = newnum;
}
}
return;
}
int selectBydata_seqlist(SeqList*list,dataype num) //按数据查找位置
{
int i=0;
for(i;i<list->pos;i++)
{
if(list->data[i]==num)
{
return i;
}
}
return -1;
}
三、头文件
cs
#ifndef __SEQ__
#define __SEQ__
#define N 4
typedef int dataype;
typedef struct
{
dataype data[N];
int pos;
}SeqList;
SeqList*create_seqlist();
void insert_seqlist(SeqList*list,dataype num);
void show_list(SeqList*list);
dataype del_seqlist(SeqList*list);
void mid_inser_seqlist(SeqList*list,int index,dataype temp);
dataype mid_delseqlist(SeqList*list,int index);
int updata_list(SeqList*list,int index,dataype newnum);
dataype select_seqlist(SeqList*list,int index);
void updataBydata_list(SeqList*list,dataype oldnum,dataype mewnum);
int selectBydata_seqlist(SeqList*list,dataype num);
#endif