Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdlib.h>
- #include<iostream>
- using namespace std;
- class SeqList
- {
- private:
- Type *data;
- int size;
- int max;
- void Error(const char *s)const { cout << s; exit(1); }
- public:
- SeqList(int n = 100);
- SeqList(const SeqList& l);
- ~SeqList(){delete[]data;}
- SeqList& operator=(const SeqList& l);
- void InsertRear(const Type& item);
- void Insert(Type *itr, const Type& item);
- void Erase(Type *itr);
- void Clear() { size = 0; }
- Type& operator[](int id);
- const Type& operator[](int id) const;
- int Size()const { return size; }
- int Empty()const { return size == 0; }
- int Full()const { return size == max; }
- Type *Begin() { return data; }
- const Type *Begin()const { return data; }
- Type *End() { return data + size; }
- const Type *End()const { return data + size; }
- void Swap(int id1, int id2);
- Type *Print(const SeqList& l, int id) { cout << l.data[id]<<" "; return 0; }
- };
- SeqList::SeqList(int n)
- {
- data = new Type[n];
- if (data == NULL)
- Error("Memory allocation error!\n");
- size = 0;
- max = n;
- }
- void SeqList::InsertRear(const Type& item)
- {
- if (size == max)
- Error("InsertRear:list is full!\n");
- data[size] = item;
- size++;
- }
- SeqList::SeqList(const SeqList& l)
- {
- data = new Type[l.max];
- if (data == NULL)
- Error("Memoty allocation error!\n");
- for (int i = 0; i < l.size; ++i)
- data[i] = l.data[i];
- size = l.size;
- max = l.max;
- }
- SeqList& SeqList::operator=(const SeqList& l)
- {
- if (max != l.max)
- {
- delete[]data;
- data = new Type[l.max];
- if (data == NULL)
- Error("Memory allocation error!\n");
- }
- for (int i = 0; i < l.size; ++i)
- data[i] = l.data[i];
- size = l.size;
- max = l.max;
- return *this;
- }
- Type& SeqList::operator[](int id)
- {
- if (id<0 || id>size - 1)
- Error("id is illegal!\n");
- return data[id];
- }
- const Type& SeqList::operator[](int id)const
- {
- if (id<0 || id>size - 1)
- Error("id is illegal!\n");
- return data[id];
- }
- void SeqList::Insert(Type *itr, const Type& item)
- {
- for (Type *p = data + size, *q = data + size - 1; p != itr; --p, --q)
- *p = *q;
- *itr = item;
- size++;
- }
- void SeqList::Erase(Type *itr)
- {
- for (Type *p = itr, *q = itr + 1; q != data + size; ++p, ++q)
- *p = *q;
- size--;
- }
- void SeqList::Swap(int id1, int id2)
- {
- Type temp;
- if (id1<0 || id1>size || id2<0 || id2>size)
- Error("id is illegal!\n");
- temp = data[id1];
- data[id1] = data[id2];
- data[id2] = temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement