#include<iostream>
#include"SeqList.h"
using namespace std;
void main()
{
SeqList mylist;
InitSeqList(&mylist);
int select = 1;
ElemType item;
int pos;
while(select)
{
cout<<"**********************************"<<endl;
cout<<"* [0] quit_system [1] push_back *"<<endl;
cout<<"* [2] push_front [3]show_seqlist*"<<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<<"请选择:>";
cin>>select;
switch(select)
{
case 1:
cout<<"请输入要插入的数据(-1结束):>";
while(cin>>item,item!=-1)
{
push_back(&mylist,item);
}
break;
case 2:
cout<<"请输入要插入的数据(-1结束):>";
while(cin>>item,item!=-1)
{
push_front(&mylist,item);
}
break;
case 3:
show(&mylist);
break;
case 4:
cout<<"删除最后一个元素:>"<<endl;
pop_back(&mylist);
break;
case 5:
cout<<"删除第一个元素:>"<<endl;
pop_front(&mylist);
break;
case 6:
cout<<"请输入要插入的位置:>";
cin>>pos;
cout<<"请输入要插入的值:>";
cin>>item;
insert_pos(&mylist,pos,item);
break;
case 7:
cout<<"请输入要插入节点的下标:>";
cin>>pos;
insert_val(&mylist,pos,item);
break;
case 8:
cout<<"请输入要删除的下标:>";
cin>>item;
delete_pos(&mylist,item);
break;
case 9:
cout<<"请输入要删除的值:>";
cin>>item;
delete_val(&mylist,pos,item);
break;
case 10:
cout<<"请输入要查找的数据:"<<endl;
cin>>item;
cout<<"此数据的下标是:>" ;
find(&mylist,item,pos);
break;
case 11:
cout<<"请输入要查找的下标:"<<endl;
cin>>item;
cout<<" 此下标的值是:>" ;
getvalue(&mylist,pos,item);
break;
case 12:
cout<<"请输入要修改的下标:>" ;
cin>>pos;
cout<<"请输入要修改后的数字:>" ;
cin>>item;
modify(&mylist,pos,item);
break;
case 13:
cout<<"清处链表:"<<endl;
clear(&mylist);
break;
case 14:
cout<<"摧毁链表:"<<endl;
destroy(&mylist);
break;
case 15:
cout<<"按从小到大输出:"<<endl ;
sort(&mylist,pos,item);
break;
case 16:
cout<<"倒序输出:"<<endl ;
resver(&mylist,pos,item);
break;
case 17:
cout<<"统计链表长度:"<<endl;
length(&mylist,item);
break;
case 18:
cout<<"请输入要查找后继数的值:>" ;
cin>>item;
next(&mylist,pos,item);
break;
case 19:
cout<<"请输入要查找前驱数的值:>" ;
cin>>item;
prio(&mylist,pos,item);
break;
default:
break;
}
}
}
#include"SeqList.h"
void InitSeqList(SeqList *list)
{
list->capacity = SEQLIST_DEFAULT_SIZE;
list->base = (ElemType*)malloc(sizeof(ElemType));
assert(list->base != NULL);
list->size = 0;
}
int begin()
{
return 0;
}
int end(SeqList *list)
{
return list->size;
}
bool isempty(SeqList *list)
{
return list->size == 0;
}
bool isfull(SeqList *list)
{
return list->size >= list->capacity;
}
bool push_back(SeqList *list,ElemType x)//1
{
return insert_pos(list,end(list),x);
}
bool push_front(SeqList *list,ElemType x)//2
{
return insert_pos(list,begin(),x);
}
void show(SeqList *list)//3
{
for(int i=0; i<list->size; ++i)
{
cout<<list->base[i]<<" ";
}
cout<<endl;
}
bool pop_back(SeqList *list)//4
{
if(isempty(list))
return false;
list->size--;
return true;
}
bool pop_front(SeqList *list)//5
{
if(isempty(list))
return false;
for(int i=0; i<list->size-1; ++i)
{
list->base[i] = list->base[i+1];
}
list->size--;
return true;
}
bool insert_pos(SeqList *list, int p, ElemType x)//6
{
if(isfull(list))
return false;
if(p<0 || p>list->size)
return false;
for(int i=list->size; i>p; --i)
{
list->base[i] = list->base[i-1];
}
list->base[p] = x;
list->size++;
return true;
}
bool insert_val(SeqList *list, ElemType p, ElemType x)//7
{
if(isempty(list))
return false;
if(p<0 || p>list->size+1)
return false;
list->size++;
for(int i = list->size; i>p; --i)
{
list->base[i] = list->base[i-1];
}
list->base[p] = -1;
return true;
}
bool delete_pos(SeqList *list, ElemType key)//8
{
if(isempty(list))
return false;
if(key<0 || key>list->size)
return false;
{
for(int i=key; i<list->size; ++i)
list->base[i] = list->base[i+1];
}
list->size--;
return true;
}
bool delete_val(SeqList *list, ElemType pos,ElemType key)//9
{
if(isempty(list))
return false;
for(int i=0;i<=list->size;++i)
if(list->base[i] == key)
{
pos = i;
delete_pos(list,pos);
}
return true;
}
bool find(SeqList *list,ElemType key,ElemType p)//10
{
if(isempty(list))
return false;
{
for(int i=0; i<list->size; ++i)
if( list->base[i] == key)
{
int pos=i;
cout<<pos<<endl;
}
}
return true;
}
bool getvalue(SeqList *list,ElemType value,ElemType i)//11
{
if(isempty(list))
return false;
if(i<0 || i>list->size)
return false;
{
value = list->base[i];
cout<<value<<endl;
}
return true;
}
bool modify(SeqList *list,ElemType p,ElemType i)//12
{
if(isempty(list))
return false;
if(p<0 || p>list->size)
return false;
list->base[p] = i;
return true;
}
bool clear(SeqList *list )//13
{
if(isempty(list))
return false;
list->size = 0;
return true;
}
bool destroy(SeqList *list)//14
{
if(isempty(list))
return false;
if(list->base)
<span style="white-space:pre"> </span>free(list->base);
list->size = 0;
list->capacity = 0;
return true;
}
bool sort(SeqList *list,ElemType pos, ElemType item)//15
{
if(isempty(list))
return false;
for(int i=0;i < list->size;i++)
{
for(int j=0;j<list->size-1;j++)
{
pos = list->base[i];
if(pos<list->base[j])
{
list->base[i] = list->base[j];
list->base[j] = pos;
}
}
}
return true;
}
bool resver(SeqList *list,ElemType pos, ElemType item)//16
{
if(isempty(list))
return false;
int j = list->size-1;
for(int i=0;i < ((list->size)/2); i++)
{
pos = list->base[i];
list->base[i] = list->base[j];
list->base[j] = pos;
--j;
}
return true;
}
bool length(SeqList *list,ElemType count)//17
{
count = list->size;
cout<<count<<endl;
return true;
}
bool next(SeqList *list,ElemType pos, ElemType key)//18
{
if(isempty(list))
return false;
for(int i=0;i<=list->size;++i)
{
if(list->base[i] == key)
{
pos = i+1;
if(pos<list->size)
cout<<"此数的后继是"<<list->base[pos]<<endl;
}
}
return true;
}
bool prio(SeqList *list,ElemType pos, ElemType key)//19
{
if(isempty(list))
return false;
for(int i=0;i<=list->size;++i)
{ if(list->base[i] == key)
{
pos = i-1;
if(pos>=0)
cout<<"此数的前驱是"<<list->base[pos]<<endl;
}
}
return true;
}
#ifndef _SEQLIST_H
#define _SEQLIST_H
#include<iostream>
#include<assert.h>
using namespace std;
#define ElemType int
#define SEQLIST_DEFAULT_SIZE 10
typedef struct SeqList
{
ElemType *base;
size_t capacity;
size_t size;
}SeqList;
int begin();
int end(SeqList *list);
void InitSeqList(SeqList *list);
bool isempty(SeqList *list);
bool isfull(SeqList *list);
bool push_back(SeqList *list,ElemType x);
bool push_front(SeqList *list,ElemType x);
bool pop_back(SeqList *list);
bool pop_front(SeqList *list);
bool insert_pos(SeqList *list, ElemType p, ElemType x);
bool insert_val(SeqList *list, ElemType p, ElemType i);
bool delete_pos(SeqList *list, ElemType key);
bool delete_val(SeqList *list, ElemType pos,ElemType key);
bool find(SeqList *list,ElemType key,ElemType p );
bool getvalue(SeqList *list,ElemType value,ElemType i);
bool modify(SeqList *list,ElemType p, ElemType i);
bool clear(SeqList *list);
bool destroy(SeqList *list);
bool sort(SeqList *list,ElemType p, ElemType i);
bool resver(SeqList *list,ElemType p, ElemType i);
bool length(SeqList *list,ElemType count);
bool next(SeqList *list,ElemType p, ElemType i );
bool prio(SeqList *list,ElemType p, ElemType i);
void show(SeqList *list);
#endif