1 Star 0 Fork 0

macroan/stack

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
LinkStack.cpp 2.04 KB
Copy Edit Raw Blame History
macroan authored 2018-04-18 13:20 +08:00 . 新建 LinkStack.cpp
// vptr.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
template <typename T>
struct Node
{
T data;
Node<T> *next;
};
template <typename T>
class LinkStack
{
public:
LinkStack(int size);
~LinkStack();
bool isEmpty();
bool isFull();
int getCount();
int getSize();
T top();
T pop();
bool push(T data);
private:
Node<T> *ptop;
int size;
int count;
};
template <typename T>
LinkStack<T>::LinkStack(int size)
{
count = 0;
this->size = size;
ptop = NULL;
}
template <typename T>
LinkStack<T>::~LinkStack()
{
if(ptop)
delete ptop;
ptop = NULL;
}
template <typename T>
int LinkStack<T>::getCount()
{
return count;
}
template <typename T>
int LinkStack<T>::getSize()
{
return size;
}
template <typename T>
bool LinkStack<T>::isEmpty()
{
return count == 0;
}
template <typename T>
bool LinkStack<T>::isFull()
{
return count == size;
}
template <typename T>
T LinkStack<T>::top()
{
if(!ptop)
return (T)-1;
return ptop->data;
}
template <typename T>
T LinkStack<T>::pop()
{
if ((!ptop) || (count == 0))
return (T)-1;
count--;
T d = ptop->data;
Node<T> *tmp = ptop;
ptop = ptop->next;
return d;
}
template <typename T>
bool LinkStack<T>::push(T data)
{
if(count == size)
return false;
Node<T> *newnode = new Node<T>();
newnode->data = data;
newnode->next = NULL;
if(ptop)
{
newnode->next = ptop;
ptop = newnode;
}
else
{
ptop = newnode;
}
count++;
return true;
}
int main(void) {
LinkStack<int> linkstack(10); ;
for(int i=0; i<10; i++)
{
linkstack.push(i);
}
cout<<"size:"<<linkstack.getCount()<<endl;
while(!linkstack.isEmpty())
{
printf("item = %d\n", linkstack.pop());
}
system("pause");
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/macroan/stack.git
git@gitee.com:macroan/stack.git
macroan
stack
stack
master

Search