自己实现栈和队列
代码:
/*******************************************/
文件名:sq.h
/*******************************************/
#ifndef SQ_H
#define SQ_H
#include <iostream>
#include<cstring>using namespace std;
class Mystack
{
private:char *data;size_t size=0;size_t cap=10;void resize(){size_t newcap=2*cap;char *newdata=new char[newcap];for(size_t i=0;i<size;i++){newdata[i]=data[i];}delete[]data;data=newdata;cap=newcap;}
public:Mystack():size(0),cap(10){data=new char[cap];}~Mystack(){delete[]data;}Mystack &operator=(const Mystack &other);char top();bool empty()const;size_t getsize()const;void push(const char &value);void pop();void show();
};class Myqueue
{
private:char *data;int frontIndex; // 队列头部的索引int rearIndex; // 队列尾部的索引int capacity; // 队列的容量int count; // 队列中当前元素的数量void resize() {char* newData = new char[capacity * 2];int i = 0;for (int j = frontIndex; i < count; j = (j + 1) % capacity, ++i) {newData[i] = data[j];}delete[] data;data = newData;frontIndex = 0;rearIndex = count;capacity *= 2;}
public:Myqueue():frontIndex(0),rearIndex(0),capacity(10),count(0){data = new char[capacity];}~Myqueue(){delete[] data;}Myqueue &operator=(const Myqueue &other);char &front();char &back();bool empty()const;size_t size()const;void push(const char &value);void pop();void show();
};#endif // SQ_H
/*******************************************/
文件名:sq.cpp
/*******************************************/
#include"sq.h"
Mystack &Mystack::operator=(const Mystack &other)
{delete[] data;size = other.size;cap = other.cap;data = new char[cap];for (size_t i = 0; i < size; ++i) {data[i] = other.data[i];}return *this;
}
char Mystack::top()
{return data[size-1];
}
bool Mystack::empty()const
{return size==0;
}
size_t Mystack::getsize()const
{return cap;
}
void Mystack::push(const char &value)
{if (size == cap){resize();}data[size++] = value;
}
void Mystack::pop()
{data[size--]=0;
}
void Mystack::show()
{for(size_t i=0;i<size;i++){cout<<data[i];}cout<<endl;
}Myqueue &Myqueue::operator=(const Myqueue &other)
{delete[] data;count = other.count;capacity = other.capacity;data = new char[capacity];for (int i = 0; i < count; ++i) {data[i] = other.data[i];}return *this;
}
char &Myqueue::front()
{return data[frontIndex];
}
char &Myqueue::back()
{return data[(rearIndex - 1 + capacity) % capacity];
}
bool Myqueue::empty()const
{return count == 0;
}
size_t Myqueue::size()const
{return count;
}
void Myqueue::push(const char &value)
{if (count == capacity) {resize();}data[rearIndex] = value;rearIndex = (rearIndex + 1) % capacity;++count;
}
void Myqueue::pop()
{frontIndex = (frontIndex + 1) % capacity;--count;
}
void Myqueue::show()
{for(int i=0;i<count;i++){cout<<data[i];}cout<<endl;
}
/*******************************************/
文件名:main.cpp
/*******************************************/
#include"sq.h"int main()
{Mystack s1;s1.push('a');s1.push('a');s1.push('a');s1.push('a');s1.push('a');cout<<s1.top()<<endl;cout<<s1.getsize()<<endl;s1.show();Mystack s2;s2=s1;s2.show();s2.pop();s2.show();Myqueue q1;q1.push('b');q1.push('b');q1.push('b');q1.push('b');q1.push('c');cout<<q1.front()<<endl;cout<<q1.back()<<endl;cout<<q1.size()<<endl;q1.show();Myqueue q2;q2=q1;q2.show();q2.pop();q2.show();return 0;
}