数据结构之基本表的操作:
SeqList.h(声明操作的基本函数)
#ifndef _SEQLIST_H #define _SEQLISY_H #include<assert.h> #include<iostream> using namespace std; #define ElemType int #define SEQLIST_DEFAULT_SIZE 10 typedef struct SeqList { ElemType *base; int size; size_t capacity; }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, int p, ElemType x); bool insert_val(SeqList *list, ElemType key); bool delete_val(SeqList *list, ElemType key); bool delete_pos(SeqList *list, int p); void show(SeqList *list); int modify(SeqList *list,int p,ElemType x); void clear(SeqList *list); int sort(SeqList *list); int destory(SeqList *list); int reverse(SeqList * list); int find(SeqList * list,ElemType x); int length(SeqList * list); int prio(SeqList * list,int p); int next(SeqList * list,int p); #endifSeqList.cpp(基本操作的实现)
#include "SeqList.h" int begin() { return 0; } int end(SeqList *list) { return list->size; } void InitSeqList(SeqList *list) { list->base=(ElemType *)malloc(sizeof(ElemType)*list->capacity); assert(list->base != NULL); list->capacity=SEQLIST_DEFAULT_SIZE; list->size=0; } int destory(SeqList *list) { if(!list->base) return 0; free(list->base); list->base=NULL; list->capacity=0; list->size=0; return 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 0; list->base[list->size++]=x; return 0; } bool push_front(SeqList *list,ElemType x) { if(isfull(list)) return 0; for(int i=list->size;i!=0;--i) { list->base[i]=list->base[i-1]; } list->base[0]=x; list->size++; return 0; } bool pop_back(SeqList *list) { if(isempty(list)) return 0; list->size--; return 0; } bool pop_front(SeqList *list) { if(isempty(list)) return 0; for(int i=1;i<list->size;i++) { list->base[i-1]=list->base[i]; } list->size--; return 0; } bool insert_val(SeqList *list, ElemType key) { int i=0; if(isempty(list)) return 0; reverse(list); while(list->base[i]<=key) { i+=1; } for(int j=list->size-1;j>=i;j--) { list->base[j+1]=list->base[j]; } list->base[i]=key; list->size++; return 0; }//**** bool insert_pos(SeqList *list, int p, ElemType x) { if(isfull(list)) return 0; if(p>=0&&p<=list->size) for(int i=list->size;i!=p;--i) { list->base[i]=list->base[i-1]; } list->base[p]=x; list->size--; return 0; } bool delete_val(SeqList *list, ElemType key) { int i=0; for(i=0;i<list->size;i++) { list->base[i]=key; break; } for(int j=i;j<=list->size;j++) { list->base[j]=list->base[j+1]; } list->size--; return 0; } bool delete_pos(SeqList *list,int p) { if(isempty(list)) return 0; if(p>=0&&p<list->size) { for(int i=p;i<list->size;i++) { list->base[i]=list->base[i+1]; } } list->size--; return 0; } void show(SeqList *list) { for(int i=0;i<list->size;i++) { cout<< list->base[i]<<" "; } cout<<endl; } int modify(SeqList *list,int p,ElemType x) { if(isempty(list)) return 0; if(p>=0&&p<list->size) list->base[p]=x; return 0; } void clear(SeqList *list) { list->size=0; } int sort(SeqList *list)//按照升序顺序 { int temp; if(isempty(list)) return 0; for(int i=0;i<list->size;i++) for(int j=0;j<list->size-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; } } return 0; } int reverse(SeqList * list) { int num=list->size/2; int temp; if(isempty(list)) return 0; for(int i=0,j=list->size-1;i<=num,j>=num;j--,i++) { if(i<j) { temp=list->base[i]; list->base[i]=list->base[j]; list->base[j]=temp; } } return 0; } int find(SeqList * list,ElemType x) { int i=0; if(isempty(list)) return 0; while(list->base[i]!=x) { i+=1; } cout<<"这是第"<<i<<"个数"<<endl; return 0; } int length(SeqList * list) { cout<<"长度为"<<list->size<<endl; return 0; } int prio(SeqList * list,int p) { if(isempty(list)) return 0; if(p>0&&p<=list->size) cout<<"前驱为"<<list->base[p-1]<<endl; } int next(SeqList * list,int p) { if(isempty(list)) return 0; if(p>=0&&p<list->size-1) cout<<"前驱为"<<list->base[p+1]<<endl; }main.cpp(主函数)
#include"SeqList.h" void main() { SeqList mylist; InitSeqList(&mylist); int select = 1; ElemType item; 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] reverse [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: 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; find(&mylist,item); break; case 12: cout<<"请输入要修改的数的位置:"; cin>>pos; cout<<"请输入新的值:"; cin>>item; modify(&mylist,pos,item); break; case 13: clear(&mylist); break; case 14: destory(&mylist); break; case 15: sort(&mylist); break; case 16: reverse(&mylist); break; case 17: length(&mylist); break; case 18: cout<<"请输入要查询后继数字的位:"; cin>>pos; next(&mylist,pos); break; case 19: cout<<"请输入要查询前驱数字的位:"; cin>>pos; prio(&mylist,pos); break; default: break; } } }