关于栈的问题

zhanghengbo 2003-12-27 12:48:44
关于栈的问题,我还是不太了解, 怎么样赋值和提取,?最好举个例子, 当然越详细越好,理论与实践一概不拒,谢谢!
...全文
34 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
liquanle 2003-12-29
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
#include "Stack.h"
template <class Type>
Stack<Type>::Stack():size(4),count(0){
item=new Type[size];
if(item==NULL){cout<<"Out of memory!"<<endl; exit(0);}
}
template <class Type>
Stack<Type>::~Stack(){
delete []item;
}
template <class Type>
void Stack<Type>::push(Type i){
if(count==size){
int *temp=new Type[size*2];
if(temp==NULL){cout<<"Out of memory!"<<endl; exit(0);}
for(int i=0; i<count; i++){
temp[i]=item[i];
}
size*=2;
delete []item;
item=temp;
cout<<"New size "<<size<<endl;
}
item[count++]=i;
}
template <class Type>
Type Stack<Type>::get(){
if(isEmpty()) {cout<<"The stack is empty!"<<endl; exit(0);}
return item[--count];
}
template <class Type>
bool Stack<Type>::isEmpty()const{
return count==0;
}
这是一个模板栈。
sharkhuang 2003-12-29
  • 打赏
  • 举报
回复
FILO
xiaocai365 2003-12-29
  • 打赏
  • 举报
回复
#include <stack>

using std::stack;

.... 尽情用吧
fierygnu 2003-12-28
  • 打赏
  • 举报
回复
栈在数据结构里就是一个后进先出的容器。在C语言里,栈用来实现参数传递、自动变量分配等等功能。一般情况下C语言里不需要考虑栈的问题。
goodluckyxl 2003-12-27
  • 打赏
  • 举报
回复
栈在数据结构中是一种结构
老早以前自己做的,代码不好但是正确,希望对楼主有用

#include <iostream.h>
#include <assert.h>
#include <stdlib.h>
#include <ctype.h>
#define null 0

struct type
{
int x;
};
class link
{
friend class stack;
private:
type *data;
link *next;
public:
link();
link(type *Data,link *Next);
~link();
};
link::link(){
data=null;
next=null;
}


link::link(type *Data,link *Next){
data=Data;
next=Next;
}
link::~link(){
delete data;
delete next;
}


class stack
{
private:
link *head;
public:
void pop();
void push(type *Data);
void operator =(const type *p);
stack();
~stack();
};

stack::stack()
{
head=null;
}
stack::~stack()
{
delete head;
}
void stack::operator =(const type *p)
{
type *m = new type;
m->x=p->x;

}

void stack::push( type *Data )
{
link *newlink;
newlink=new link;

assert(newlink);

newlink->data=Data;
newlink->next=head;
head=newlink;
}

void stack::pop()
{
link *p;

if(head)
{
p=head;
p=p->next ;
cout<<head->data->x<<endl;
head=p;
}
}



void main()
{
type *m,*q;
m=new type;
q=new type;
m->x=1;
q->x=2;
stack l;
l.push(m);
l.push(q);
l.pop ();
l.pop ();
delete m;
delete q;
}

24,860

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧