如何实现栈的初始化,好难哦...书上讲的我头晕....小弟谢过啦...
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
struct Stack1
{ /*字符类结构体*/
char *base;
char *top;
int stacksize;
};
InitStack(struct Stack1 *S)
{ /*构造一个空栈*/
S->base=(char *)malloc(STACK_INIT_SIZE * sizeof(char));
if (!S->base) printf("Application is failed!");
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
}
这个是一个顺序栈!
#include<iostream>
using namespace std;
template <class T>
class Stack
{
private:
int top;
T *stacka;
int Max_size;
public:
Stack(int maxsize);
~Stack();
bool Empty();//
bool Full();//
void clear();//
T Pop();//
T Getpop();//
void push(T & x);
};
template <class T>
Stack<T>::Stack(int maxsize)
{top=-1;Max_size=maxsize;stacka=new T[Max_size];
}
template <class T>
Stack<T>::~Stack()
{ delete [] stacka;
}
template <class T>
bool Stack<T>::Empty()
{if(top==-1)return true;
else return false;
}
template <class T>
bool Stack<T>::Full()
{if(top==(Max_size-1))return true;
else return false;
}
template <class T>
void Stack<T>::clear()
{top=-1;}
template <class T>
T Stack<T>::Pop()
{ if(Empty()){cout<<"栈是空的"<<endl;exit(1);}
else T data=Stack[top];
top--;
return data;
}
template <class T>
T Stack<T>::Getpop()
{if(Empty()){cout<<"栈是空的"<<endl;exit(1);}
else return stacka[top];
}
template <class T>
void Stack<T>::push(T &x)
{if(Full()){cout<<"栈满"<<endl;exit(1);}
else top++;stacka[top]=x;
}
void main()
{ Stack<int> s1(10);
for(int i=0;i<10;i++)
{ s1.push(i);
cout <<s1.Getpop()<<" ";
}
int x=10;
s1.push(x);cout<<s1.Getpop();
}
但是栈内的数据不是输入进去的!直接传入了!