顺序表
SeqList.h
#ifndef SEQLIST_H
#define SEQLIST_H
#include <iostream>
#include<memory.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
template <typename T>
class SeqList
{
private:
T *ptr;
int size;
int len = 0;
public:
SeqList() : ptr(nullptr), size(0), len(0) {}
SeqList(T* arr, int n) : size(n), len(n) {
ptr = new T[n];
for (int i = 0; i < n; i++) {
ptr[i] = arr[i];
}
}
SeqList(const std::string& str) {
size = str.size();
len = size;
ptr = new T[size];
for (int i = 0; i < size; i++) {
ptr[i] = static_cast<T>(str[i] - '0');
}
}
~SeqList() {
delete[] ptr;
}
bool empty();
bool full();
void push_back(T e);
void show();
void insert(int index);
void delete_s(int index);
void pop_back();
T get_len();
T get_index(int index);
void sort(bool flag);
};
#endif
SeqList.cpp
#include "SeqList.h"
template <typename T>
bool SeqList<T>::empty()
{
return this->len == 0;
}
template <typename T>
bool SeqList<T>::full()
{
return this->len == this->size;
}
template <typename T>
void SeqList<T>::push_back(T e)
{
if (this->full())
{
cout << "顺序表已满,无法插入!" << endl;
return;
}
this->ptr[len++] = e;
}
template <typename T>
void SeqList<T>::show()
{
if (this->empty()) {
cout << "顺序表为空!" << endl;
return;
}
cout << "当前顺序表中的元素分别是:";
for (int i = 0; i < this->len; i++)
{
cout << this->ptr[i] << " ";
}
cout << endl;
}
template <typename T>
void SeqList<T>::insert(int index)
{
if (this->full())
{
cout << "表已经满了!" << endl;
return;
}
if (index > this->len + 1 || index <= 0)
{
cout << "插入位置不合理" << endl;
return;
}
T e;
cout << "请输入插入元素:";
cin >> e;
for (int i = len - 1; i >= index - 1; i--)
{
this->ptr[i + 1] = this->ptr[i];
}
this->ptr[index - 1] = e;
this->len++;
}
template <typename T>
void SeqList<T>::delete_s(int index)
{
if (index > this->len || index <= 0)
{
cout << "删除位置不合理" << endl;
return;
}
for (int i = index; i < this->len; i++)
{
this->ptr[i - 1] = this->ptr[i];
}
this->len--;
}
template <typename T>
void SeqList<T>::pop_back()
{
if (this->len == 0)
{
cout << "顺序表为空!" << endl;
return;
}
this->len--;
}
template <typename T>
T SeqList<T>::get_len()
{
return this->len;
}
template <typename T>
T SeqList<T>::get_index(int index)
{
if (index > this->len || index <= 0)
{
cout << "位置不合理" << endl;
return T();
}
return this->ptr[index - 1];
}
template <typename T>
void SeqList<T>::sort(bool flag)
{
for (int i = 0; i < this->len - 1; i++)
{
for (int j = 0; j < this->len - i - 1; j++)
{
if ((flag && this->ptr[j] > this->ptr[j + 1]) || (!flag && this->ptr[j] < this->ptr[j + 1]))
{
T t = this->ptr[j];
this->ptr[j] = this->ptr[j + 1];
this->ptr[j + 1] = t;
}
}
}
}
template class SeqList<int>;
main.cpp
#include "SeqList.h"
int main()
{
int arr[] = {1, 2, 3, 4, 5};
SeqList<int> sl(arr, 5);
sl.show();
int add;
cout << "请输入插入数据的位置:";
cin >> add;
sl.insert(add);
sl.show();
cout << "请输入删除数据的位置:";
cin >> add;
sl.delete_s(add);
sl.show();
int c;
cout << "是否尾删(1:YES/2:NO):";
cin >> c;
if (c == 1) {
sl.pop_back();
}
sl.show();
int l = sl.get_len();
cout << "顺序表当前长度为:" << l << endl;
int index;
cout << "请输入需要位置:";
cin >> index;
cout << index << "位置数据位:" << sl.get_index(index) << endl;
cout << "将顺序表进行排序(1:升序,0:降序):";
cin >> c;
sl.sort(c);
sl.show();
return 0;
}
栈
MyStack.h
#ifndef MYSTACK_H
#define MYSTACK_H
#include <iostream>
using namespace std;
template <typename T>
class My_stack {
private:
struct Node {
T data;
Node* next;
Node(T value) : data(value), next(nullptr) {}
};
Node* topNode;
int stackSize;
public:
My_stack();
My_stack(const My_stack &other);
My_stack& operator=(const My_stack &other);
~My_stack();
T& top();
bool empty() const;
int size() const;
void push(T value);
void pop();
void swap(My_stack &other);
};
template <typename T>
void swap(My_stack<T> &a, My_stack<T> &b);
#endif
MyStack.cpp
#include "MyStack.h"
template <typename T>
My_stack<T>::My_stack() : topNode(nullptr), stackSize(0) {}
template <typename T>
My_stack<T>::My_stack(const My_stack &other) : topNode(nullptr), stackSize(0) {
Node* current = other.topNode;
while (current) {
push(current->data);
current = current->next;
}
}
template <typename T>
My_stack<T>& My_stack<T>::operator=(const My_stack &other) {
if (this != &other) {
while (!empty()) {
pop();
}
Node* current = other.topNode;
while (current) {
push(current->data);
current = current->next;
}
}
return *this;
}
template <typename T>
My_stack<T>::~My_stack() {
while (!empty()) {
pop();
}
}
template <typename T>
T& My_stack<T>::top() {
if (empty()) {
cout << "栈空!" << endl;
exit(EXIT_FAILURE);
}
return topNode->data;
}
template <typename T>
bool My_stack<T>::empty() const {
return stackSize == 0;
}
template <typename T>
int My_stack<T>::size() const {
return stackSize;
}
template <typename T>
void My_stack<T>::push(T value) {
Node* newNode = new Node(value);
newNode->next = topNode;
topNode = newNode;
stackSize++;
}
template <typename T>
void My_stack<T>::pop() {
if (empty()) {
cout << "栈空!" << endl;
exit(EXIT_FAILURE);
}
Node* temp = topNode;
topNode = topNode->next;
delete temp;
stackSize--;
}
template <typename T>
void My_stack<T>::swap(My_stack &other) {
std::swap(topNode, other.topNode);
std::swap(stackSize, other.stackSize);
}
template <typename T>
void swap(My_stack<T> &a, My_stack<T> &b) {
a.swap(b);
}
template class My_stack<int>;
template void swap(My_stack<int> &a, My_stack<int> &b);
main.cpp
#include "MyStack.h"
int main() {
My_stack<int> s;
s.push(9);
s.push(2);
s.push(6);
s.push(7);
s.push(8);
cout << "栈顶元素:" << s.top() << endl;
cout << "栈的大小:" << s.size() << endl;
s.pop();
cout << "栈顶元素:" << s.top() << endl;
My_stack<int> s1;
s1.push(1);
s1.push(2);
My_stack<int> s2;
s2 = s;
swap(s2, s1);
cout << "交换后的栈顶元素:" << s2.top() << endl;
cout << "交换后另一个栈顶元素:" << s1.top() << endl;
return 0;
}
队列
MyQueue.h
#ifndef MYQUEUE_H
#define MYQUEUE_H
#include <iostream>
#include <cstring>
using namespace std;
template <typename T>
class Queue {
private:
T* data;
int len;
int size;
int front;
int rear;
public:
Queue();
Queue(const T* d, int size);
~Queue();
Queue(const Queue &other);
Queue& operator=(const Queue &other);
T MyFront();
T back();
bool empty();
int MySize();
void push(T e);
void emplace(T e);
T pop();
void swap(Queue &other);
};
#endif
MyQueue.cpp
#include "MyQueue.h"
template <typename T>
Queue<T>::Queue() : len(0), size(20), front(0), rear(0) {
data = new T[size];
}
template <typename T>
Queue<T>::Queue(const T* d, int size) : len(strlen(d)), size(size), front(0), rear(len) {
data = new T[size];
strcpy(data, d);
}
template <typename T>
Queue<T>::~Queue() {
delete[] data;
}
template <typename T>
Queue<T>::Queue(const Queue &other) : len(other.len), size(other.size), front(other.front), rear(other.rear) {
data = new T[size];
strcpy(data, other.data);
}
template <typename T>
Queue<T>& Queue<T>::operator=(const Queue &other) {
if (this != &other) {
delete[] data;
len = other.len;
size = other.size;
front = other.front;
rear = other.rear;
data = new T[size];
strcpy(data, other.data);
}
return *this;
}
template <typename T>
T Queue<T>::MyFront() {
if (empty()) {
cout << "队列为空" << endl;
exit(EXIT_FAILURE);
}
return data[front];
}
template <typename T>
T Queue<T>::back() {
if (empty()) {
cout << "队列为空" << endl;
exit(EXIT_FAILURE);
}
return data[rear - 1];
}
template <typename T>
bool Queue<T>::empty() {
return front == rear && len == 0;
}
template <typename T>
int Queue<T>::MySize() {
return len;
}
template <typename T>
void Queue<T>::push(T e) {
if (len >= size) {
cout << "队列已满" << endl;
return;
}
data[rear++] = e;
len++;
}
template <typename T>
void Queue<T>::emplace(T e) {
push(e);
}
template <typename T>
T Queue<T>::pop() {
if (empty()) {
cout << "队列为空" << endl;
exit(EXIT_FAILURE);
}
len--;
return data[front++];
}
template <typename T>
void Queue<T>::swap(Queue &other) {
std::swap(len, other.len);
std::swap(size, other.size);
std::swap(front, other.front);
std::swap(rear, other.rear);
std::swap(data, other.data);
}
template class Queue<char>;
main.cpp
#include "MyQueue.h"
int main() {
Queue<char> q("hello world", 20);
q.emplace('A');
q.emplace('B');
q.emplace('C');
cout << "q队首数据:" << q.MyFront() << endl;
cout << "q队尾数据:" << q.back() << endl;
cout << "q队列数据数量 " << q.MySize() << endl;
cout << "q尾删操作" << endl;
q.pop();
cout << "q队首数据:" << q.MyFront() << endl;
Queue<char> q1;
q1 = q;
cout << "q1队首数据:" << q1.MyFront() << endl;
cout << "q1队尾数据:" << q1.back() << endl;
Queue<char> q2("nihao", 20);
cout << "交换操作" << endl;
q.swap(q2);
cout << "q队首数据:" << q.MyFront() << endl;
cout << "q队尾数据:" << q.back() << endl;
cout << "q2队首数据:" << q2.MyFront() << endl;
cout << "q2队尾数据:" << q2.back() << endl;
return 0;
}
思维导图