虽然我的代码是用C语言写的,但是你们建立文件的时候记得创建C++文件哦!
废话不多说,直接上代码:
首先我们先建立起我们自己的头文件,并且将函数的声明放入头文件(在此我的头文件名称是:”seqlist11.h”)中(建立头文件的相关操作可参考我上一期文章)
这是我的项目文件注意你们创建头文件源文件的时候要将他们放在同一文件夹目录下。
#ifndef SEQLIST11_H_INCLUDED
#define SEQLIST11_H_INCLUDED
#define ADD_SIZE 10
#include
#include
#include
#define SEQLIST_INIT_SIZE 10
typedef int Elemtype;
typedef struct Seqlist
{
Elemtype*base;
int capacity;
int size;
} Seqlist;
bool add(Seqlist*list);
void InitSeqlist(Seqlist*list);
void push_back(Seqlist*list,Elemtype x) ;
void push_front(Seqlist*list,Elemtype x);
void show_list(Seqlist*list);
void pop_front(Seqlist*list);
void pop_back(Seqlist*list);
void insert_pos(Seqlist*list, int pos,Elemtype x);
int find(Seqlist*list,Elemtype key);
int length(Seqlist*list);
void delete_pos(Seqlist*list, int pos);
void delete_val(Seqlist*list, Elemtype key);
void sort(Seqlist*list);
void resver(Seqlist*list);
void clear(Seqlist*list);
void destroy(Seqlist*list);
void merge(Seqlist*lt,Seqlist*la,Seqlist*lb);
#endif // SEQLIST11_H_INCLUDED
#include"seqlist11.h"//引入自己编写的头文件
bool add(Seqlist*list)
{
Elemtype*newbase=(Elemtype*)realloc(list->base,sizeof(Elemtype)*(list->capacity+ADD_SIZE));
if(newbase==NULL)
{
printf("增加空间失败,内存不足\n");
return false;
}
list->base=newbase;
list->capacity+=ADD_SIZE;
return true;
}
void InitSeqlist(Seqlist*list)
{
list->base = (Elemtype*)malloc(sizeof(Elemtype)*SEQLIST_INIT_SIZE);
assert(list->base != NULL);
list->capacity = SEQLIST_INIT_SIZE;
list->size = 0;
}
void push_back(Seqlist*list,Elemtype x)
{
if(list->size>=list->capacity&&!add(list))
{
printf("顺序表空间已满,无法尾部插入数据“\n");
return;
}
list->base[list->size]=x;
list->size++;
}
void push_front(Seqlist*list,Elemtype x)
{
if(list->size>=list->capacity&&!add(list))
{
printf("顺序表空间已满,无法头部插入数据“\n");
return;//直接结束该函数
}
for(int i=list->size; i>0; i--)
{
list->base[i]=list->base[i-1];
}
list->base[0]=x;
list->size++;
}
void show_list(Seqlist*list)
{
for(int i=0; isize; i++)
printf("%d ",list->base[i]);
printf("\n");
}
void pop_front(Seqlist*list)
{
if(list->size==0)
{
printf("该线性表已为空,无法进行头部删除\n");
return;
}
for(int i=0; isize-1; i++)
{
list->base[i]=list->base[i+1];
}
list->size--;
}
void pop_back(Seqlist*list)
{
if(list->size==0)
{
printf("该线性表已为空,无法进行尾部删除\n");
return;
}
list->size--;
}
void insert_pos(Seqlist*list, int pos,Elemtype x)
{
if(list->size>=list->capacity&&!add(list))
{
printf("顺序表空间已满,无法插入数据“\n");
return;
}
if(pos<0||pos>list->size)
{
printf("插入数据的位置不合法\n");
return;
}
for(int i=list->size; i>=pos; --i)
{
list->base[i]=list->base[i-1];
}
list->base[pos]=x;
list->size++;
}
int find(Seqlist*list,Elemtype key)
{
for(int i=0; isize; ++i)
{
if(list->base[i]==key)
return i;
}
return -1;
}
int length(Seqlist*list)
{
return list->size;
}
void delete_pos(Seqlist*list, int pos)
{
if(list->size==0)
{
printf("该线性表已为空,无法进行位置删除\n");
return;
}
if(pos<0||pos>list->size)
{
printf("删除的位置(下标)不合法\n");
return;
}
// if(pos==0)
// pop_front(&Seqlist);
//if(pos==list->size-1)
// pop_back(&Seqlist);
for(int i=pos; isize-1; i++)
{
list->base[i]=list->base[i+1];
}
list->size--;
}
void delete_val(Seqlist*list, Elemtype key)
{
int pos=find(list,key);
if(pos==-1)
{
printf("要删除的元素%d不存在\n",key);
return;
}
delete_pos(list,pos);
}
void sort(Seqlist*list)
{
Elemtype tem;
for(int i=0; isize-1; i++)
{
for(int j=0; jsize-i-1; j++)
{
if(list->base[j]>list->base[j+1])
{
tem=list->base[j+1];
list->base[j+1]=list->base[j];
list->base[j]=tem;
}
}
}
}
void resver(Seqlist*list)
{
Elemtype tem;
if(list->size==0||list->size==1)
return;
int low=0;
int high=list->size-1;
while(lowbase[high];
list->base[high]=list->base[low];
list->base[low]=tem;
low++;
high--;
}
}
void clear(Seqlist*list)
{
list->size=0;
}
void destroy(Seqlist*list)
{
free(list->base);
list->size=0;
list->capacity=0;
list->base=NULL;
}
void merge(Seqlist*lt,Seqlist*la,Seqlist*lb)
{
lt->capacity=la->size+lb->size;
lt->base=(Elemtype*)malloc(sizeof(Elemtype)*lt->capacity);
assert(lt->base!=NULL);
int ia=0;
int ib=0;
int ic=0;
while(iasize&&ibsize)
{
if(la->base[ia]base[ib])
lt->base[ic++]=la->base[ia++];
else
lt->base[ic++]=lb->base[ib++];
}
while(iasize)
{
lt->base[ic++]=la->base[ia++];
}
while(ibsize)
{
lt->base[ic++]=lb->base[ib++];
}
lt->size=la->size+lb->size;
}
#include"seqlist11.h"
int main()
{
Seqlist mylist;
Seqlist yourlist;
Seqlist list;
InitSeqlist(&mylist);
InitSeqlist(&yourlist);
push_back(&mylist,1);
push_back(&mylist,3);
push_back(&mylist,5);
push_back(&mylist,7);
push_back(&mylist,9);
push_back(&yourlist,2);
push_back(&yourlist,4);
push_back(&yourlist,6);
push_back(&yourlist,8);
push_back(&yourlist,10);
merge(&list,&mylist,&yourlist);
show_list(&list);
return 0;
}
/*int main()
{
Seqlist mylist;
InitSeqlist(&mylist);
int select = 1;
int item;
int pos;
while (select)
{
printf("-----------------------------------------------------------------\n");
printf("* [1] push_back [2] push_front *\n");
printf("* [3] showlist [4] pop_front *\n");
printf("* [5] pop_back [6] insert_pos *\n");
printf("* [7] find [8] length *\n");
printf("* [9] delete_pos [10] delete_val *\n");
printf("* [11] sort [12] resver *\n");
printf("* [13] clear [14*] destroy *\n");
printf("* [0] quit_system [00] void *\n");
printf("-----------------------------------------------------------------\n");
printf("请输入相应的数字代表你要进行的操作:");
scanf("%d", &select);
if(select==0)break;
switch (select)
{
case 1:
printf("请输入要插入的元素(尾插)(-1结束):");
while(scanf("%d",&item),item!=-1)//持续插入元素
{
push_back(&mylist,item);
}
break;
case 2:
printf("请输入要插入的元素(头插)(-1结束):");
while(scanf("%d",&item),item!=-1)
{
push_front(&mylist,item);
}
break;
case 3:
show_list(&mylist);
break;
case 4:
pop_front(&mylist);
break;
case 5 :
pop_back(&mylist);
break;
case 6 :
printf("请输入要插入的元素:\n");
scanf("%d",&item);
printf("请输入要插入的位置:\n");
scanf("%d",&pos);
insert_pos(&mylist,pos,item);
break;
case 7 :
printf("请输入你要查找的元素:\n");
scanf("%d",&item);
pos=find(&mylist,item);
if(pos==-1)
printf("你要查的元素%d不在线性表内\n",item);
else
printf("你要查找的元素%d下标为%d\n",item,pos);
break;
case 8 :
printf("该线性表的长度为%d\n",length(&mylist));
break;
case 9 :
printf("请输入要删除的位置:\n");
scanf("%d",&pos);
delete_pos(&mylist, pos);
break;
case 10 :
printf("请输入要删除的元素:\n");
scanf("%d",&item);
delete_val(&mylist, item);
break;
case 11 :
sort(&mylist);
break;
case 12 :
resver(&mylist);
break;
case 13 :
clear(&mylist);
break;
//case 14:
// destroy(&mylist);
default:
printf("输入的操作不存在\n");
break;
}
}
destroy(&mylist);
return 0;
}*/
以上代码(测试文件的代码)很多都注释掉了,兄弟姐妹们你们可以参考我的代码,然后自己进行对应的函数功能测试。
以上就是线性表的顺序实现代码了,谢谢大家的阅读,以后我还会将线性表的链式实现代码上传,希望大家多多支持!谢谢大家!(如果各位有什么不懂的可以在评论区留言或者加Q:2742256636询问,本人也是一个代码小白,欢迎大家积极指出我的错误)