实现线性表的各类操作:
void InitSeqList(SeqList *list); //初始化线性表
int begain(SeqList *list); //线性表的初始位置(返回第一个下标即0)
int end(SeqList *list); //末位置(返回最后一个元素的下标)
bool Isfull(SeqList *list); //判断是否已满
bool Isempty(SeqList *list); //判断是否为空
bool Insert_pos(SeqList *list,int n,ElemType x); //按下标插入元素
bool Push_back(SeqList *list,ElemType item); //尾插法
bool Push_front(SeqList *list,ElemType item); //头插法
bool show(SeqList *list); //输出线性表的所有元素
bool Pop_back(SeqList *list); //尾删法
bool Pop_front(SeqList *list); //头删法
int find(SeqList *list,ElemType val); //查找某个元素是否存在
bool Delete_pos(SeqList *list,int pos); //按下标删除
bool Delete_val(SeqList *list,ElemType val); //按值删除
bool Modify(SeqList *list,ElemType val); //修改
int Getval(SeqList *list,int pos); //求某个下标所对应的值
void next(SeqList *list,ElemType val); //求后继
void prio(SeqList *list,ElemType val); //求前驱
void reverse(SeqList *list); //反转数组元素
void clear(SeqList *list); //清除线性表
void destory(SeqList *list); //销毁线性表
void ascend_sort(SeqList *list); //升序排列
void descend_sort(SeqList *list); //降序排列
SeqList.h:
#ifndef __SEQLIST_H__
#define __SEQLIST_H__
#include
#include
#include
#include
#define FAULT_STRING_SIZE 100
typedef int ElemType;
typedef struct SeqList
{
ElemType *base;
size_t capacity;
size_t size;
}SeqList;
void InitSeqList(SeqList *list); //初始化线性表
int begain(SeqList *list); //线性表的初始位置(返回第一个下标即0)
int end(SeqList *list); //末位置(返回最后一个元素的下标)
bool Isfull(SeqList *list); //判断是否已满
bool Isempty(SeqList *list); //判断是否为空
bool Insert_pos(SeqList *list,int n,ElemType x);//按下标插入元素
bool Push_back(SeqList *list,ElemType item); //尾插法
bool Push_front(SeqList *list,ElemType item); //头插法
bool show(SeqList *list); //输出线性表的所有元素
bool Pop_back(SeqList *list); //尾删法
bool Pop_front(SeqList *list); //头删法
int find(SeqList *list,ElemType val); //查找某个元素是否存在
bool Delete_pos(SeqList *list,int pos); //按下标删除
bool Delete_val(SeqList *list,ElemType val); //按值删除
bool Modify(SeqList *list,ElemType val); //修改
int Getval(SeqList *list,int pos); //求某个下标所对应的值
void next(SeqList *list,ElemType val); //求后继
void prio(SeqList *list,ElemType val); //求前驱
void reverse(SeqList *list); //反转数组元素
void clear(SeqList *list); //清除线性表
void destory(SeqList *list); //销毁线性表
void ascend_sort(SeqList *list); //升序排列
void descend_sort(SeqList *list); //降序排列
#endif
SeqList.cpp
#include"SeqList.h"
//初始化线性表
void InitSeqList(SeqList *list)
{
list->capacity= FAULT_STRING_SIZE;
list->base = (ElemType *)malloc(sizeof(ElemType)*list->capacity);
list->size = 0;
}
//判断是否已满
bool Isfull(SeqList *list)
{
return (list->capacity == list->size);
}
//判断是否为空
bool Isempty(SeqList *list)
{
return (list->size == 0);
}
//尾插法
bool Push_back(SeqList *list,ElemType item)
{
Insert_pos(list,end(list),item);
return true;
}
/*bool Push_back(SeqList *list,ElemType item)
{
if(Isfull(list))
{
return false;
}
list->base[list->size] = item;
list->size++;
return true;
}*/
//头插法
bool Push_front(SeqList *list,ElemType item)
{
Insert_pos(list,begain(list),item);
return true;
}
/*bool Push_front(SeqList *list,ElemType item)
{
if(Isfull(list))
{
return false;
}
for(int i=list->size-1;i>=0;--i)
{
list->base[i+1] = list->base[i];
}
list->base[0] = item;
list->size++;
return true;
}*/
//输出线性表的所有元素
bool show(SeqList *list)
{
if(Isempty(list))
{
return false;
}
for(int i=0;isize;i++)
{
printf("%d\n",list->base[i]);
}
return true;
}
//按下标插入元素
bool Insert_pos(SeqList *list,int pos,ElemType item)
{
if(Isfull(list))
{
return false;
}
if(pos < 0 || pos > list->size)
{
return false;
}
for(int i=list->size-1;i>=pos;--i)
{
list->base[i+1] = list->base[i];
}
list->base[pos] = item;
list->size++;
return true;
}
//线性表的初始位置(返回第一个下标即0)
int begain(SeqList *list)
{
return 0;
}
//末位置(返回最后一个元素的下标)
int end(SeqList *list)
{
return list->size;
}
//尾删法
/*bool Pop_back(SeqList *list)
{
if(Isempty(list))
{
return false;
}
list->size--;
return true;
}*/
bool Pop_back(SeqList *list)
{
Delete_pos(list,end(list));
return true;
}
//头删法
/*bool Pop_front(SeqList *list)
{
if(Isempty(list))
{
return false;
}
for(int i=1;isize;i++)
{
list->base[i-1] = list->base[i];
}
list->size--;
return true;
}*/
bool Pop_front(SeqList *list)
{
Delete_pos(list,begain(list)-1);
return true;
}
//按下标删除
bool Delete_pos(SeqList *list,int pos)
{
if(Isempty(list))
{
return false;
}
if(pos<0 || pos>=list->size)
{
return false;
}
for(int i=pos+1;isize;++i)
{
list->base[i-1] = list->base[i];
}
list->size--;
printf("it is deleted!\n");
return true;
}
//查找某个元素是否存在
int find(SeqList *list,ElemType val)
{
for(int i=0;isize;++i)
{
if(val == list->base[i])
{
return i;
}
}
return -1;
}
//按值删除
bool Delete_val(SeqList *list,ElemType val)
{
if(Isempty(list))
{
return false;
}
int pos = find(list,val);
if(-1 != pos)
{
Delete_pos(list,pos);
}
else
printf("it's not exist!\n");
return true;
}
//求某个下标所对应的值
ElemType Getval(SeqList *list,int pos)
{
if(pos >= list->size)
{
printf("input error!\n");
return -1;
}
return list->base[pos];
}
//修改
bool Modify(SeqList *list,ElemType val)//bool Modify(SeqList *list,ElemType val);
{
int n = find(list,val);
int value;
if(-1 != n)
{
printf("please input a new val:\n");
scanf("%d",&value);
list->base[n] = value;
printf("it's modified!\n");
return true;
}
printf("it's not exist!\n");
return false;
}
//求后继
void next(SeqList *list,ElemType val)
{
int n = find(list,val);
if(-1 != n)
{
if(n == (list->size - 1))
{
printf("it don't have next!\n");
}
else
printf("it's next is %d\n",list->base[n+1]);
}
else
printf("the value you put is not exist!\n");
}
//求前驱
void prio(SeqList *list,ElemType val)
{
int n = find(list,val);
if(-1 != n)
{
if(n == 0)
{
printf("it don't have prio!\n");
}
else
printf("it's prio is %d\n",list->base[n-1]);
}
else
printf("the value you put is not exist!\n");
}
//反转数组元素
void reverse(SeqList *list)
{
int left = 0;
int right = list->size - 1;
ElemType temp;
while(left<=right)
{
temp = list->base[left];
list->base[left] = list->base[right];
list->base[right] = temp;
left++;
right--;
}
printf("it is reversed!\n");
}
//清除线性表
void clear(SeqList *list)
{
list->size = 0;
printf("the list is clear!\n");
}
//销毁线性表
void destory(SeqList *list)
{
free(list->base);
printf("the list is destoryed!\n");
}
//升序排列
void ascend_sort(SeqList *list)
{
int i;
int j;
int k;
ElemType temp;
for(i=0;isize -1;++i) //选择排序(升序排列)
{
k = i;
for(j=i;jsize;++j)
{
if(list->base[j] < list->base[k])
{
k = j;
}
}
if(k != i)
{
temp = list->base[i];
list->base[i] = list->base[k];
list->base[k] = temp;
}
}
printf("they are resorted!\n");
}
//降序排列
void descend_sort(SeqList *list)//冒泡排序(降序)
{
int i;
int j;
ElemType temp;
for(i=0;isize-1;++i)
{
for(j=0;jsize -1-i;++j)
{
if(list->base[j] > list->base[j+1])
{
temp = list->base[j];
list->base[j] = list->base[j+1];
list->base[j+1] = temp;
}
}
}
printf("they are resorted!\n");
}
main.cpp
#include"SeqList.h"
int main()
{
SeqList mylist;
ElemType item;
int choice;
int i = 1;
int pos;
int value;
syatem("mode con cols=100 lines=100");
while(i)
{
printf("*****************SeqList********************\n");
printf(" ---zyh_helen\n");
printf("*[1]InitSeqList [2]Push_back *\n");
printf("*[3]Push_front [4]Pop_back *\n");
printf("*[5]Pop_front [6]Delete_pos *\n");
printf("*[7]show [8]insert_pos *\n");
printf("*[9]Delete_val [10]find *\n");
printf("*[11]Getval [12]Modify *\n");
printf("*[13]next [14]prio *\n");
printf("*[15]reverse [16]clear *\n");
printf("*[17]destory [18]ascend_sort*\n");
printf("*[19]descend_sort [0]quit_system *\n");
printf("please input the num of the fuction :\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
InitSeqList(&mylist);
break;
case 2:
printf("please input the num you want to push_front: (-1 as a break)\n");
while(cin>>item,item != -1)
{
Push_back(&mylist,item);
}
break;
case 3:
printf("please input the num you want to push_front: (-1 as a break)\n");
while(cin>>item,item != -1)
{
Push_front(&mylist,item);
}
break;
case 4:
Pop_back(&mylist);
break;
case 5:
Pop_front(&mylist);
break;
case 6:
printf("please input the pos:\n");
scanf("%d",&pos);
Delete_pos(&mylist,pos);
break;
case 7:
show(&mylist);
break;
case 8:
printf("please input the pos:\n");
scanf("%d",&pos);
int num;
printf("please input the num:\n");
scanf("%d",&num);
Insert_pos(&mylist,pos,num);
break;
case 9:
// int val;
printf("please input the val you want to delete:\n");
scanf("%d",&value);
Delete_val(&mylist,value);
break;
case 10:
printf("please input the value you want to find:\n");
// int value;
scanf("%d",&value);
int m;
m = find(&mylist,value);//!!!!!!!!!!!int m= find(&mylist,value)
if( m != -1)
{
printf("the value is found ,it's pos is %d\n",m);
}
else
{
printf("it is not exist!\n");
}
break;
case 11:
printf("please input the pos:\n");
scanf("%d",&pos);
if(Getval(&mylist,pos) != -1)
{
printf("the val is %d\n",Getval(&mylist,pos));
}
break;
case 12:
printf("please input the value you want to modify:\n");
// int value2;
scanf("%d",&value);
Modify(&mylist,value);
break;
case 13:
printf("Please input a value:\n");
scanf("%d",&value);
next(&mylist,value);
break;
case 14:
printf("Please input a value:\n");
scanf("%d",&value);
prio(&mylist,value);
break;
case 15:
reverse(&mylist);
break;
case 16:
clear(&mylist);
break;
case 17:
destory(&mylist);
break;
case 18:
ascend_sort(&mylist);
break;
case 19:
descend_sort(&mylist);
break;
default:
i = 0;
break;
}
}
return 0;
}
以下显示部分测试:没有测试的,望读者自行验证:
Pop_back & show:
push_front & show:
Pop_back& show:
Pop_front & show:
Delete_pos:下标为1的5被删了
insert_pos
ascend_sort(升序):