自学内容网 自学内容网

【无标题】

#ifndef MY_STACK_H
#define MY_STACK_H
#include <iostream>
#include <stdlib.h>
#include <string.h>

using namespace std;

class My_Stack
{
private:
    char *data;
    int len;
    int head;
    int fail;
public:
    My_Stack();
    ~My_Stack();
    My_Stack &operator=(const char *arr);
    char top();
    bool empty();
    int size();
    void push(const char c);
    void show();
    void pop();
};

#endif // MY_STACK_H
 

#include "my_stack.h"

My_Stack::My_Stack()
{
     len = 0;
     head = -1;
     fail = 0;
     data = new char[0];
}

My_Stack&My_Stack :: operator=(const char *arr)
{
    int LEN;
    LEN =strlen(arr);
    data = new char[LEN];
    for(int i=0;i<LEN;i++)
    {
        *(data+i) = *(arr+i);
        head++;
        len++;
    }
    return  *this;
}
My_Stack :: ~My_Stack()
{
    delete [] data;
}
char My_Stack :: top()
{
    return data[head];
}
bool My_Stack :: empty()
{
    return len == 0;
}
int My_Stack :: size()
{
    return len;
}
void My_Stack :: push(const char c)
{

    *(data+len) = c;
    head++;
    len++;
    cout<<"栈顶插入"<<endl;

}
void My_Stack :: pop()
{
    *(data + head) = NULL;
    head--;
    len--;
    cout<<"栈顶删除"<<endl;
}
void My_Stack :: show()
{
    for(int i = head;i>=0;i--)
    {
        cout<<data[i]<<"\t";
    }
    cout<<endl;
}
 

#include <iostream>
#include "my_stack.h"

using namespace std;

int main()
{
    My_Stack h1;
    h1 ="abcd";
    h1.show();
   // cout<<endl;
    h1.push('x');
    h1.push('L');
    h1.show();
    h1.pop();
    h1.show();

    //cout << "Hello World!" << endl;
    return 0;
}
--------------------------------------------------------------------------------------------------

#ifndef MY_QUEUE_H
#define MY_QUEUE_H
#include <iostream>
#include <string.h>
#include <stdlib.h>

using namespace std;

class My_queue
{
private:
    int head;
    char *data;
    int tail;
    int len;
public:
    My_queue();
    ~My_queue();
    My_queue &operator =(const char *arr);
    char front();
    char back();
    bool empty();
    int size();
    void push();
    void pop();
    void show();

};

#endif // MY_QUEUE_H
 

#include "my_queue.h"

My_queue::My_queue()
{
    len = 0;
    head = 0;
    tail = -1;
    data = new char [0];
}
My_queue &My_queue :: operator=(const char *arr)
{
    int Len = strlen(arr);
    data = new char[Len];
    for(int i=0;i<Len;i++)
    {
        *(data+i)=*(arr+i);
        tail++;
        len++;

    }
    return  *this;
}
My_queue :: ~My_queue()
{
    delete [] data;
}
char My_queue :: front()
{
    return data[head];
}
char My_queue :: back()
{
    return  data[tail];
}
bool My_queue :: empty()
{
    return len == 0;
}
int My_queue ::size()
{
    return tail-head;
}
void My_queue ::push()
{
    char c;
    cout<<"请输入要插入的值"<<endl;
    cin>>c;
    *(data+len) = c;
    len++;
    tail++;
    cout<<"入队成功"<<endl;

}
void My_queue ::pop()
{
    *(data+head)=NULL;
    head++;
}
void My_queue ::show()
{
    for(int i=head;i<len;i++)
    {
        cout<<"a=="<<data[i]<<"\t";
    }
    cout<<endl;
}
 

#include "my_queue.h"


int main()
{
    My_queue h1;
    h1 = "abcdef";
    h1.show();
    cout<<"---------------"<<endl;
    h1.push();
    h1.show();
    cout<<"---------------"<<endl;
    h1.push();
    h1.push();
    h1.show();
    cout<<"---------------"<<endl;
    h1.pop();
    h1.show();
    cout<<"---------------"<<endl;
    cout<<h1.front()<<"\t"<<h1.back()<<"\t"<<h1.size()<<endl;



   // cout << "Hello World!" << endl;
    return 0;
}
 


原文地址:https://blog.csdn.net/m0_69999095/article/details/142546349

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!