//// Created by XXX on 2021/7/10.//#ifndefC11LEARN_STACK_H#defineC11LEARN_STACK_Htemplate<typenameT>classStack{private:int capacity;int top;T *array;public:Stack(int capacity =50);Stack(const Stack<T>& stack);Stack<T>&operator=(const Stack<T> stack);virtual~Stack();boolempty();voidpush(const T element);T pop();boolremove(const T key);boolcontain(const T &key);intsize()const;};template<typenameT>Stack<T>::Stack(int capacity):capacity(capacity){if(this->capacity<=0)capacity =50;array =new T[capacity];top =-1;}template<typenameT>Stack<T>::Stack(const Stack<T>& stack){capacity = stack.capacity;array =new T[capacity];for(int i =0; i < capacity;++i){array[i]=stack.array[i];}top = stack.top;}template<typenameT>
Stack<T>& Stack<T>::operator=(const Stack<T> stack){if(array!=nullptr)delete[] array;capacity = stack.capacity;array =new T[capacity];for(int i =0; i < capacity;++i){array[i]=stack.array[i];}top = stack.top;return*this;}template<typenameT>
Stack<T>::~Stack(){delete[] array;}template<typenameT>boolStack<T>::empty(){return top ==-1;}template<typenameT>voidStack<T>::push(const T element){top = top+1;if(top>= capacity){capacity *=2;T *array_temp =new T[capacity];for(int i =0; i < top;++i){array_temp[i]= array[i];}delete[] array;array = array_temp;}array[top]= element;}template<typenameT>boolStack<T>::remove(const T key){int index =-1;for(int i =0; i <= top;++i){if(array[i]== key){index = i;}}if(index ==-1)returnfalse;for(int i = index; i < top;++i){array[i]= array[i+1];}top--;returntrue;}template<typenameT>boolStack<T>::contain(const T &key){for(int i =0; i <= top;++i){if(array[i]== key)returntrue;}returnfalse;}template<typenameT>
T Stack<T>::pop(){if(empty()){throw"underflow";}else{top --;return array[top+1];}}template<typenameT>intStack<T>::size()const{return top+1;}#endif//C11LEARN_STACK_H
顺序表:
package seqTable;import java.util.ArrayList;
import java.util.Scanner;public class SeqList {private int data[];private int len;private int maxSize;public SeqList(){data new int[100];maxSize 100;len 0;}public SeqList(int n){data new int[n];maxS…
List(C模版实现的带哨兵的双向链表)
//
// Created by 许加权 on 2021/7/10.
//#ifndef C11LEARN_LIST_H
#define C11LEARN_LIST_H
template<typename T>
class List
{
protected:class Node{public:Node *pre;Node *next;T key;Node(){}Node(const T key):k…
顺序栈:
package SeqStack;public class Stack {private int top;private int base[];private int stackSize;public Stack(){stackSize 100;base new int[stackSize];top -1;}public Stack(int n){stackSize n;base new int[stackSize];top -1;}public bool…
循环队列:
package SeqQueue;public class Queue {private int data[];private int queueSize;private int front,rear;public Queue(){data new int[100];queueSize 100;front rear 0;}public Queue(int n){queueSize n;data new int[queueSize];front rear 0;}publi…