数据结构_栈
今天成功实现了栈,可能因为栈比较简单,没花太多时间,毕竟就是一种特殊的表,比上次链表好多了,这次写链栈快多了。
代码如下
顺序栈
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
template<class T>
class m_stack
{
public:
m_stack(int n)
{
size = n;
data = new T[n];
if (data == nullptr)
{
cerr << "开辟失败" << endl;
}
top = -1;//初始化栈顶指针
}
void m_push(T k)
{
if (this->top == this->size - 1)
{
cout << "栈满" << endl;
}
else
{
this->top++;
this->data[top] = k;
//若top初始值为0
/*
*if(s.top == s.size)
{
}
else
{
s.data[top] = k;
top++;
}
*/
}
}
void m_pop()
{
if (this->top == -1)
{
cout << "栈空" << endl;
}
else
{
this->top--;
}
}
void get_top()
{
if (this->top == -1)
{
cout << "栈空" << endl;
}
else
{
cout << this->data[top] << endl;
}
}
bool empty()
{
if (this->top == -1)
return true;
else
return false;
}
bool full()
{
if (this->top == size - 1)
return true;
else
return false;
}
public:
T* data; //指针模拟数组
int top; //栈顶指针(该顺序栈中并非严格意义上指针)
int size;
};
int main()
{
m_stack<int> s(10);
s.m_push(1);
s.m_push(2);
s.m_push(3);
s.get_top();
s.m_pop();
s.get_top();
return 0;
}
链栈
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
template<class T>
class Node
{
public:
T data;
Node<T>* next;
};
template<class T>
class m_stack
{
public:
m_stack()
{
head = new Node<T>;
if (head == nullptr)
{
cout << "开辟失败" << endl;
}
else
{
head->next = nullptr;
}
}
void m_push(T k)
{
Node<T>* s = new Node<T>;
s->data = k;
s->next = head->next;
head->next = s;
}
void m_pop()
{
if (head->next == nullptr)
{
cout << "栈空" << endl;
}
else
{
//p指向要删除首元节点
Node<T>* p = head->next;
head->next = p->next;
delete p;
p = nullptr;
}
}
void get_top()
{
if (head->next == nullptr)
{
cout << "栈空" << endl;
}
else
{
cout << head->next->data << endl;
}
}
bool empty()
{
if (head->next == nullptr)
return true;
else
return false;
}
public:
Node<T>* head;
};
int main()
{
m_stack<int> s;
s.m_push(1);
s.m_push(2);
s.m_push(3);
s.get_top();
s.m_pop();
s.get_top();
return 0;
}