typedef int ElemType;
struct LNode
{
ElemType data;
LNode* next;
};
void InitStack(LNode* &HS)
{
HS=NULL;
}
void ClearStack(LNode* &HS)
{
LNode *cp,*np;
cp=HS;
while(cp!=NULL)
{
np=cp->next;
delete cp;
cp=np;
}
HS=NULL;
}
int StackEmpty(LNode* HS)
{
return HS==NULL;
}
ElemType Peek(LNode *HS)
{
if(HS==NULL){
cerr<<"Linked stack is empty!"<<endl;
exit(1);
}
return HS->data;
}
void Push(LNode* &HS,const ElemType& item)
{
LNode* newptr=new LNode;
if(newptr==NULL){
cerr<<"Memory allocation failare!"<<endl;
exit(1);
}
newptr->data=item;
newptr->next=HS;
HS=newptr;
}
ElemType Pop(LNode* &HS)
{
if(HS==NULL){
cerr<<"Linked stack is empty!"<<endl;
exit(1);
}
LNode* p=HS;
HS=HS->next;
ElemType temp=p->data;
delete p;
return temp;
}
//以下是stack.cpp
#include<iostream.h>//棧的簡單應用,從鍵盤上輸入一批整數,然後按照相反的次序打印出來
#include<stdlib.h>
#include"stack.h"
#include<conio.h>
void main()
{
LNode *a;
InitStack(a);
int x;cout<<"輸入整數,用-1作爲終止鍵盤輸入"<<endl;
cin>>x;
while(x!=-1){
//假定用-1作爲終止鍵盤輸入的標誌,輸入的整數個數不能超過StackMaxSize所規定的值
Push(a,x);
cin>>x;
}
while(!StackEmpty(a))
cout<<Pop(a)<<" ";
cout<<endl;
getch();
}
/*
http://f2.9612.org//vcpp/webinfo/WebInfoBata1.asp
QQ羣:
34409541 討論網頁
34409326 討論JAVA 已滿
34408784 討論VC++
34409699 討論VC++
9143041 討論MFC編程
10614204 討論C#
10613030 討論Win32編程
10613067 討論遊戲開發
18779860 討論JAVA
*/