首先是main.cpp让我们来看看都有什么函数
#include<iostream> #include"SeqList.h" using namespace std; int main() { SeqList mylist; InitSeqList(&mylist); int select = 1; ElemType item = 0; ElemType pos = 0; ElemType ch = 0; 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_val [13]modify_pos *"<<endl; cout<<"* [14] clear [15]destroy *"<<endl; cout<<"* [16] sort [17]resver *"<<endl; cout<<"* [18] length [19]next *"<<endl; cout<<"* [20] prio *"<<endl; cout<<"**********************************"<<endl; cout<<"plese chose:>"; cin>>select; switch(select) { case 0: cout<<"quit successful!"<<endl; break; case 1: cout<<"please enter data end with -1:>"; while(cin>>item,item!=-1) { push_back(&mylist,item); } break; case 2: cout<<"please enter data end with -1:>"; while(cin>>item,item!=-1) { push_front(&mylist,item); } break; case 3: show(&mylist); break; case 4: pop_back(&mylist); cout<<"pop_back successful!"<<endl; break; case 5: pop_front(&mylist); cout<<"pop_front successful!"<<endl; break; case 6: cout<<"please enter position:>"; cin>>pos; cout<<"please enter insert data:>"; cin>>item; insert_pos(&mylist,pos,item); break; case 7: cout<<"please enter position:>"; cin>>pos; cout<<"please enter insert data:>"; cin>>item; insert_val(&mylist,pos,item); break; case 8: cout<<"please enter position:>"; cin>>pos; delete_pos(&mylist,pos); break; case 9: cout<<"please enter delete data:>"; cin>>item; delete_val(&mylist,item); break; case 10: cout<<"please enter find data:"; cin>>item; cout<<"the position is :"<<find(&mylist,item)<<endl; break; case 11: cout<<"please enter position:>"; cin>>pos; cout<<"the data is:"<<getvalue(&mylist,pos)<<endl; break; case 12: cout<<"please enter modify data:"; cin>>item; cout<<"please enter change data:"; cin>>ch; modify_val(&mylist,item,ch); break; case 13: cout<<"please enter modify postion:"; cin>>pos; cout<<"please enter change data:"; cin>>ch; modify_pos(&mylist,pos,ch); break; case 14: clear(&mylist); cout<<"clear successful!"<<endl; break; case 15: destroy(&mylist); cout<<"destory successful!"; cout<<"Nothing operation can be used!Please quit!"; break; case 16: sort(&mylist); cout<<"sort successful!"<<endl; break; case 17: resver(&mylist); cout<<"resver successful!"<<endl; break; case 18: cout<<length(&mylist)<<endl; break; case 19: cout<<"please enter postion:"; cin>>pos; cout<<next(&mylist,pos)<<endl; break; case 20: cout<<"please enter postion:"; cin>>pos; cout<<prio(&mylist,pos)<<endl; break; default: break; return 0; } } }
#include "SeqList.h" void InitSeqList(SeqList *list) { list->capacity = SEQLIST_DEFAULT_SIZE; list->base = (ElemType*)malloc(sizeof(ElemType)*list->capacity); assert(list->base != NULL);//断言 list->size = 0; }//开辟一个容量为10,大小为0的顺序表 bool isempty(SeqList *list) { return (list->size == 0); }//判断是否为空 bool isfull(SeqList *list) { return list->size >= list->capacity; }//判断是否为满 bool push_back(SeqList *list,ElemType x) { if(isfull(list)) return false; else list->base[list->size++] = x; return true; }//后插 bool push_front(SeqList *list,ElemType x) { if(isfull(list)) return false; for(size_t i = list->size;i>0;i--) { list->base[i] = list->base[i-1]; } list->base[0] = x; list->size++; return true; }//前插//把前面腾出一个位置,从后向前 //bool push_back(SeqList *list,ElemType x) //{ // return insert_pos(list,end(list),x); //} //bool push_front(SeqList *list,ElemType x) //{ // return insert_pos(list,begin(),x); //} bool pop_back(SeqList *list) { if(isfull(list)) return false; else list->size--; return true; }//后删 bool pop_front(SeqList *list) { if(isfull(list)) return false; for(int i = 0;i<list->size;i++) { list->base[i] = list->base[i+1]; } list->size--; return true; }//前删//把后面的数放到前面,从前向后 int find(SeqList *list,ElemType x) { for(int i = 0;i<list->size;i++) { if(list->base[i] == x) return i; } return -1; }//查找 void sort(SeqList *list) { for(int i = 0;i<list->size;i++) { for(int j = 0;j < list->size-i-1;j++) { if(list->base[j] > list->base[j+1]) { list->base[j] ^=list->base[j+1]; list->base[j+1] ^=list->base[j]; list->base[j] ^=list->base[j+1]; } } } }//排序(冒泡) bool insert_pos(SeqList *list, ElemType p, ElemType x)//p是位置,x是数据 { if(isfull(list)) return false; for(size_t i = list->size ;i>0; i--) { list->base[i] = list->base[i-1]; } list->base[p] = x; list->size+=1; return true; }//按位置插入 bool insert_val(SeqList *list, ElemType p, ElemType x) { if(isfull(list)) return false; sort(list); insert_pos(list,p,x); return true; // if(isfull(list)) // return false; // if(q<0 ||q<100) // return false; // // for(int i = list->size;i > 0;i--) // { // if(list->base[i] == x) // list->base[i] = list->base[i-1]; // list->base[i] = q; // } // list->size++; // return true; }//按值插入(先排序再插入指定位置) bool delete_pos(SeqList *list, ElemType key) { if(isempty(list)|| key < 0 || key > list->size-1) 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 x) { if(isempty(list)) return false; delete_pos(list,find(list,x)); return true; }//按值删除 int getvalue (SeqList *list,ElemType x) { if(isempty(list) || x < 0 || x > list->size) return 0; for(int i = 0;i<list->size;i++) { if(x == i) return list->base[i]; } return 0; }//输入位置返回值 bool modify_val(SeqList *list,ElemType in,ElemType ch) { if(isempty(list) || find(list,in) < 0 || find(list,in) > list->size) return false; list->base[find(list,in)] = ch; return true; }//按值修改 bool modify_pos(SeqList *list,ElemType key,ElemType ch) { if(isempty(list)) return false; list->base[key] = ch; return true; }//按位置修改 void show(SeqList *list) { for(int i = 0;i<list->size;++i) { cout<<list->base[i]<<" "; } cout<<endl; }//打印 void clear(SeqList *list) { list->size = 0; }//清除 bool destroy(SeqList *list) { if(list->base) free(list->base); list->size = 0; list->capacity = 0; return true; }//销毁 void resver(SeqList *list) { int *p = &list->base[0]; int *q = &list->base[list->size-1]; for(;q > p;(q-- && p++)) { *p = *p ^ *q; *q = *p ^ *q; *p = *p ^ *q; } }//翻转 int next(SeqList *list,ElemType key) { if(isempty(list) || key < 0 ||key > list->size-1) return 0; return list->base[key + 1]; }//后继 int prio(SeqList *list,ElemType key) { if(isempty(list) || key < 1 ||key > list->size) return 0; return list->base[key - 1]; }//前驱 size_t length(SeqList *list) { if(list->capacity==0) return -1; return list->size; }//长度
#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(); // 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 x); int find(SeqList *list,ElemType x); void sort(SeqList *list); bool delete_val(SeqList *list, ElemType x); bool delete_pos(SeqList *list, ElemType key); int getvalue (SeqList *list,ElemType x); bool modify_val(SeqList *list,ElemType in,ElemType ch); bool modify_pos(SeqList *list,ElemType key,ElemType ch); void show(SeqList *list); void clear(SeqList *list); bool destroy(SeqList *list); size_t length(SeqList *list); void resver(SeqList *list); int next(SeqList *list,ElemType key); int prio(SeqList *list,ElemType key); #endif
下面是运行结果的截图
push_back
pop_back
pop_front
insert_pos
insert_val
delete_pos
delete_val
find
get value
modify_val
modify_pos
原文地址:http://blog.csdn.net/cherry_ermao/article/details/45268909