順序表的基本操作:
"seqlist.h"頭文件
#ifndef SEQLIST_H_INCLUDED #define SEQLIST_H_INCLUDED #include#include using namespace std; template class SeqList { public: SeqList(size_t sz = INIT_SIZE); bool IsFull() const { return size >= capacity; } bool IsEmpty() const { return size == 0; } void show_list(); void push_back(const Type& x); void push_front(const Type& x); void pop_back(); void pop_front(); void insert_pos(int pos,const Type& x); void insert_val(const Type& x); void delete_pos(int pos); void delete_val(const Type& x); int find(const Type& x); int length() const; void clear(); void destory(); void resver(); void sort(); private: enum{INIT_SIZE = 10}; Type *base; size_t capacity; size_t size; }; template SeqList ::SeqList(size_t sz) { capacity = sz > INIT_SIZE ? sz : INIT_SIZE; base = new Type[capacity]; size = 0; } template void SeqList ::show_list() { for(int i=0; i %3Cbase%5Bi%5D%3C%3C%22%20%22%3B%0A%09%7D%0A%09cout%3C%3Cendl%3B%0A%7D%0A%0Atemplate%3Cclass%20Type%3E void SeqList ::push_back(const Type &x) { if(IsFull()) { cout<<"順序表已滿,不能插入!"< void SeqList ::push_front(const Type &x) { if(IsFull()) { cout<<"順序表已滿,不能插入!"< 0; --i) { base[i] = base[i-1]; } base[0] = x; size++; } template void SeqList ::pop_back() { if(IsEmpty()) { cout<<"順序表為空,不能刪除!"< void SeqList ::pop_front() { if(IsEmpty()) { cout<<"順序表為空,不能刪除!"< void SeqList ::insert_pos(int pos,const Type &x) { if(pos<0 || pos>size) { cout<<"要插入的位置非法!"< pos; --i) { base[i] = base[i-1]; } base[pos] = x; size++; } template void SeqList ::insert_val(const Type& x) { int pos = find(x); if(pos == -1) { return; } for(int i=size; i>=pos; --i) { base[i+1] = base[i]; } base[pos] = x; size++; } template void SeqList ::delete_pos(int pos) { for(int i=pos; i void SeqList ::delete_val(const Type &x) { int pos = find(x); if(pos == -1) { return; } for(int i=pos; i int SeqList ::find(const Type& x) { for(int i=0; i int SeqList ::length() const { return size; } template void SeqList ::clear() { size = 0; } template void SeqList ::destory() { delete []base; base = NULL; } template void SeqList ::resver() { for(int i=0; i %3Cbase%5Bi%5D%3C%3C%22%20%22%3B%0A%20%20%20%20%7D%0A%09cout%3C%3Cendl%3B%0A%7D%0A%0Atemplate%3Cclass%20Type%3E void SeqList ::sort() { for(int j=0; j base[i+1]) { int temp = base[i]; base[i] = base[i+1]; base[i+1] = temp; } } } for(int i=0;i %3Cbase%5Bi%5D%3C%3C%22%20%22%3B%0A%20%20%20%20%7D%0A%09cout%3C%3Cendl%3B%0A%7D%0A%23endif%20%2F%2F%20SEQLIST_H_INCLUDED%0A%3C%2Fpre%3E主函數:
#include "SeqList.h" int main() { SeqListmylist; int select = 1; int Item; int pos; while(select) { system("CLS"); cout<<"*************************************"<