2023/9/13 qt&c++

栈模板类

#include 
#define Max 100
using namespace std;
template 
//链式栈
class Stack
{
private:
    S *data;             //存放数据的指针
    int top;              //栈顶元素下标
public:
    Stack():top(-1),data(new S[Max])
    {
        cout<<"无参构造"<
//入栈
S Stack::stack_pop(S e,S *data)
{
    if(data == NULL||stack_full(data))
    {
        cout<<"入栈失败"<
//遍历栈
void Stack::stack_show(S *data)
{
    if(data == NULL||stack_empty(data))
    {
        cout<<"遍历失败"<=0;i--)
    {
        cout<
S  Stack::stack_push(S *data)
{
    if(data == NULL||stack_empty(data))
    {
        cout<<"出栈失败"<
int Stack::stack_empty(S *data)
{
    if(data == NULL)
    {
        cout<<"不合法"<
S Stack::stack_full(S *data)
{
    if(data ==NULL)
    {
        cout<<"不合法"<
S &Stack::Stack_get_top(S *data)
{
    if(data == NULL||stack_empty(data))
    {
        cout<<"获取栈顶元素失败"<
int Stack::stack_size(S*data)
{
    if(data == NULL||stack_empty(data))
    {
        cout<<"获取栈顶元素失败"<s1;
    double a[Max];
    s1.stack_pop(3.0,a);
    s1.stack_pop(3.1,a);
    s1.stack_pop(3.2,a);
    s1.stack_pop(3.3,a);
    s1.stack_show(a);
    int len = s1.stack_size(a);
    cout<<"栈的大小是:"<s2;
    int b[Max];
    s2.stack_pop(10,b);
    s2.stack_pop(20,b);
    s2.stack_pop(30,b);
    s2.stack_pop(40,b);
    s2.stack_pop(50,b);
    s2.stack_pop(60,b);
    s2.stack_show(b);
    s2.stack_push(b);
    s2.stack_push(b);
    s2.stack_push(b);
    s2.stack_push(b);
    s2.stack_show(b);
    int len1 = s2.stack_size(b);
    cout<<"栈的大小是:"<

队列模板类

#include 
#define Max 40
using namespace std;
template 
class Queue
{
private:
    Q *data; //存放队列的数组
    int head;   //队头下标
    int tail;   //队尾下标
public:
    Queue():data(new Q[Max]),head(tail=0),tail(head=0)
    {}
    ~Queue()
    {
        delete [] data;
    }
    //判空
    int  queue_empty(Q *data);
    //判满
    int  queue_full(Q *data);
    //入列
    Q queue_pop(Q e,Q *data);
    //遍历
    void queue_show(Q *data);
    //出列
    Q queue_push(Q *data);
    //求队列大小
    int queue_size(Q *data);

};

//判空
template 
int Queue::queue_empty(Q *data)
{
    if(nullptr == data)
    {
           cout<<"不合法!"<
int Queue::queue_full(Q *data)
{
    if(nullptr == data)
     {
         cout<<"不合法!"<
Q Queue::queue_pop(Q e,Q *data)
{
    if(NULL==data||queue_full(data))
    {
        cout<<"入队失败"<
void Queue::queue_show(Q *data)
{
    if(NULL==data||queue_empty(data))
    {
        cout<<"遍历失败"<
Q Queue::queue_push(Q *data)
{
    if(NULL==data||queue_empty(data))
    {
        cout<<"出列失败"<
int Queue::queue_size(Q *data)
{
    if(NULL==data||queue_empty(data))
    {
        cout<<"出列失败"<q1;
    int a[Max];
    q1.queue_pop(1,a);
    q1.queue_pop(2,a);
    q1.queue_pop(3,a);
    q1.queue_show(a);
    return 0;
}

你可能感兴趣的:(c++,qt)