//SeqList.h
#ifndef _SEQLIST_H
#define _SEQLIST_H
#include<iostream>
#include<assert.h>
#include<stdlib.h>
using namespace std;
#define ElemType int
#define SEQLIST_DEFAULT_SIZE 10
typedef struct SeqList
{
ElemType *base;
size_t capacity;
size_t size;
}SeqList;
bool empty(SeqList *list);
bool full(SeqList *list);
void InitSeqList(SeqList *list);
bool push_front(SeqList *list,ElemType x);
bool push_back(SeqList *list,ElemType x);
bool pop_back(SeqList *list);
bool pop_front(SeqList *list);
bool insert_pos(SeqList *list,int pos,ElemType x);
bool insert_val(SeqList *list,ElemType x);
bool delete_pos(SeqList *list,int pos);
bool delete_val(SeqList *list,ElemType x);
int find(SeqList *list,ElemType x);
int getvalue(SeqList *list,int pos);
bool modify(SeqList *list,ElemType item,ElemType x);
bool clear(SeqList *list);
bool destroy(SeqList *lsit);
bool sort(SeqList *list);
int length(SeqList *list);
int next(SeqList *list,ElemType item);
int prio(SeqList *list,ElemType item);
void show_SeqList(SeqList *list);
#endif
//SeqList.cpp
#include"SeqList.h"
void InitSeqList(SeqList *list)
{
list->capacity = SEQLIST_DEFAULT_SIZE;
list->base =(ElemType *)malloc(sizeof(ElemType)*list->capacity);
assert(list->base != NULL); //用malloc申请内存后必须判断是否为空
list->size = 0;
}
bool empty(SeqList *list) //判断是否空
{ return list->size<=0;}
bool full(SeqList *list) //判断是否满
{ return list->size >=list->capacity ;}
int prio(SeqList *list,ElemType item) //查找前驱
{
int i;
if(empty(list))
return -1;
i = find(list,item);
if(i == -1||i<1)
{
cout<<"输入的值不存在!"<<endl;
return -1;
}
return list->base[i-1];
}
int next(SeqList *list,ElemType item) //查找后继
{
int i;
if(empty(list))
return -1;
i = find(list,item);
if(i == -1||i+1>list->size-1)
{
cout<<"输入的值不存在!"<<endl;
return -1;
}
return list->base[i+1];
}
int length(SeqList *list) //求长度
{
if(empty(list))
return -1;
return list->size;
}
bool sort(SeqList *list) //冒泡法排序
{
if(empty(list))
return false;
int i,j;
for(i=0;i<list->size ;i++)
{
for(j=0;j<list->size -1;j++)
{
if(list->base[j]>list->base[j+1])
swap(list->base[j],list->base[j+1]);
}
}
return true;
}
bool destroy(SeqList *list) //摧毁链表
{
if(empty(list))
return false;
free(list->base );
list->size = 0;
return true;
}
bool clear(SeqList *list) //清除
{
if(empty(list))
return false;
list->size=0;
return true;
}
bool modify(SeqList *list,ElemType item,ElemType x) //修改
{
int i;
if(empty(list))
return false;
i = find(list,item);
list->base [i] = x;
return true;
}
int getvalue(SeqList *list,int pos) //按位置查找值
{
if(empty(list))
return -1;
else return list->base[pos];
}
int find(SeqList *list,ElemType x) //按值查找位置
{
int i;
if(empty(list))
return -1;
else
for(i=0;i<=list->size;i++)
{
if(list->base[i]==x)
return i;
}
return -1;
}
bool delete_val(SeqList *list,ElemType x) //按值删除
{
int i;
if(full(list)||empty(list))
return false;
else
for(i=0;i<=list->size;i++)
{
if(list->base[i]==x)
{
delete_pos(list,i);
return true;
}
}
cout<<"要删除的值不存在"<<endl;
return false;
}
bool delete_pos(SeqList *list,int pos) //按位置删除
{
int i;
if(full(list)||empty(list))
return false;
if(pos>list->size)
{
cout<<"选择的位置出错!"<<endl;
return false;
}
else
for(i=pos;i<=list->size-1;++i)
{
list->base [i ]=list->base[i+1];
}
list->size--;
return true;
}
bool insert_val(SeqList *list,ElemType x) //按值插入
{
int i;
if(full(list)||empty(list))
return false;
for(i=0;i<=list->size ;i++)
{
if(list->base[i]<=x&&list->base[i+1]>=x )
{
for(int j=list->size;j>i;j--)
{
list->base [j]=list->base [j-1];
}
list->size++;
list->base [i+1]=x;
return true;
}
}
return false;
}
bool insert_pos(SeqList *list,int pos,ElemType x) //按位置插入
{
int i;
if(full(list)||empty(list))
return false;
if(pos>list->size)
{
cout<<"选择的位置出错!"<<endl;
return false;
}
if(pos == 0)
push_front(list,x);
else
for(i=list->size;i>=pos;i--)
{
list->base [i]=list->base [i-1];
}
list->base [pos]=x;
list->size++;
return true;
}
bool pop_front(SeqList *list) //头删法
{
if(empty(list))
return false;
for(int i=0; i<list->size ;i++)
{
list->base [i] =list->base[i+1];
}
list->size--;
return true;
}
bool pop_back(SeqList *list) //尾删法
{
if(empty(list))
return false;
list->size--;
return true;
}
bool push_back(SeqList *list,ElemType x) //尾插法
{
list->base [list->size++ ] = x;
return true;
}
bool push_front(SeqList *list,ElemType x) //头插法
{
for(int i=list->size ;i>=0;i--)
{
list->base[i] =list->base [i-1];
}
list->base[0]= x;
list->size++;
return true;
}
void show_SeqList(SeqList *list) //输出链表
{
for(int i=0; i<list->size;i++)
{cout<<list->base [i]<<' ';}
cout<<endl;
}
//main.cpp
#include<iostream>
#include"SeqList.h"
using namespace std;
void main()
{
SeqList mylist;
InitSeqList(&mylist);
int select = 1;
ElemType item,x;
int pos;
while(select)
{
cout<<"**********************************"<<endl;
cout<<"* [1] push_back [2] push_front *"<<endl;
cout<<"* [3] show_seqlist[0] quit_system*"<<endl;
cout<<"* [4] pop_back [5] pop_front *"<<endl;
cout<<"* [6] insert_pos [7] insert_val *"<<endl;
cout<<"* [8] delete_pos [9] delete_val *"<<endl;
cout<<"* [10] find [11]getvalue *"<<endl;
cout<<"* [12] modify [13]clear *"<<endl;
cout<<"* [14] destroy [15]sort *"<<endl;
cout<<"* [16] resver [17]length *"<<endl;
cout<<"* [18] next [19]prio *"<<endl;
cout<<"**********************************"<<endl;
cout<<"choose:>";
cin>>select;
switch(select)
{
case 1:
cout<<"请输入要插入的数据(-1结束):>";
while(cin>>item,item!=-1)
{push_back(&mylist,item);}
break;
case 2:
cout<<"请输入要插入的数据:>";
while(cin>>item,item!=-1)
{push_front(&mylist,item);}
break;
case 3:
show_SeqList(&mylist);
break;
case 4:
pop_back(&mylist);
break;
case 5:
pop_front(&mylist);
break;
case 6:
cout<<"输入要插入的位置:>";
cin>>pos;
cout<<"请输入要插入的数据:>";
cin>>item;
insert_pos(&mylist,pos,item);
break;
case 7:
cout<<"请输入要插入的数据:>";
cin>>item;
insert_val(&mylist,item);
break;
case 8:
cout<<"请输入要删除的位置:>";
cin>>pos;
delete_pos(&mylist,pos);
break;
case 9:
cout<<"请输入要删除的值:>";
cin>>item;
delete_val(&mylist,item);
break;
case 10:
cout<<"请输入要查找的值:>";
cin>>item;
pos = find(&mylist,item);
cout<<"位置为:>"<<pos<<endl;
break;
case 11:
cout<<"请输入要获得的值的位置:>";
cin>>pos;
item = getvalue(&mylist,pos);
cout<<"获得的值"<<item<<endl;
break;
case 12:
cout<<"输入要修改的值:>";
cin>>item;
cout<<"输入修改后的值:>";
cin>>x;
modify(&mylist,item,x);
break;
case 13:
clear(&mylist);
cout<<"清除完成"<<endl;
break;
case 14:
destroy(&mylist);
cout<<"摧毁成功"<<endl;
break;
case 15:
sort(&mylist);
cout<<"排序完成"<<endl;
break;
case 16:
break;
case 17:
pos = length(&mylist);
cout<<"长度为:>"<<pos<<endl;
break;
case 18:
cout<<"请输入要查找后继的值:>";
cin>>item;
pos = next(&mylist,item);
cout<<"后继为:>"<<pos<<endl;
break;
case 19:
cout<<"请输入要查找前驱的值:>";
cin>>item;
pos = prio(&mylist,item);
cout<<"前驱为:>"<<pos<<endl;
break;
default:
cout<<"你输入的是个什么啊!"<<endl;
break;
}
}
}