十万火急~在线等~堆栈在链式存储上的操作(包括头文件,功能函数文件,主函数文件)

4.堆栈在链式存储上的操作包括:
(1) 初始化空栈 INITSTACK(top)
(2) 判空栈操作 EMPTY(top)
(3) 进栈操作 PUSH(top,x)
(4) 出栈操作 POP(top)
(5) 取栈顶元素操作 GETTOP(top)
(6) 栈置空操作CLEAR(top)
(7) 求当前栈中元素个数的操作 CURRENT_SIZE(top)

重点:(包括头文件,功能函数文件,主函数文件)

谢谢各位大侠啦~~我全部分都豁出去了~<虽然才那么一点~嘻嘻>

第1个回答  2008-12-16
该有基本操作基本都有了
希望对你有帮助哈

Status InitStack(Sqstack &S){
//构建一个空栈S
S.base=(SElemType2*)malloc(STACK_INIT_SIZE*sizeof(SElemType2));
if(!S.base) exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}//InitStack
Status DestroyStack(Sqstack &S){
//销毁栈S,S不再存在
if(S.base){
free(S.base);
free(S.top);
S.base=S.top=NULL;
}
else cout<<"There is no stack now!"<<endl;
}
Status ClearStack(Sqstack &S){
//把S置为空栈
if(S.base) { S.top=S.base; return OK;}
else cout<<"There is no stack now!"<<endl;
return OK;
}
Status StackEmpty(Sqstack S){
//若S为空栈,则返回TRUE,否则返回FALSE
if(S.base==S.top) return YES;
else return NO;
}
int StackLength(Sqstack S){
//返回S的元素个数,即栈的长度
return S.top-S.base;
}
Status GetTop(Sqstack S,SElemType2 &e){
//若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR;
if(S.top==S.base) return ERROR;
e=*(S.top-1);
return OK;
}
Status Push(Sqstack &S,SElemType2 e){
//插入元素e为新的栈顶元素
SElemType2 *newbase;
if(S.top-S.base>=S.stacksize){//栈满,追加存储空间
newbase =(SElemType2*)realloc(S.top,(S.stacksize+STACKINCREMENT)*sizeof(SElemType2));
if(!newbase) exit(OVERFLOW);
S.top=newbase;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
}
Status Pop(Sqstack &S,SElemType2 &e){
//若栈不为空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROER
if(S.top==S.base) return ERROR;
e=*--S.top;
return OK;
}
Status visit(Sqstack S){
int i;
for(i=0;i<S.top-S.base;i++)
cout<<S.base[i]<<" ";
if(i==S.top-S.base) return YES;
return NO;
}
Status StackTraverse(Sqstack S,Status(*visit)(Sqstack)){
//从栈顶到栈底依次对栈中每个元素调用函数visit()。一旦visit()失败,则操作失败
if((visit)(S)) return YES;
else return NO;
}
链栈存储定义头文件:
typedef char SElemType;
typedef struct LNode{
SElemType data;
struct LNode *next;
}LNode,*StackPtr;
typedef struct{
StackPtr base;
StackPtr top;
}Linkstack;
链栈基本操作定义头文件:
Status Initstack(Linkstack &S){
//创建一个空的链栈,S作为栈底
//int i;
S.base=S.top=(StackPtr)malloc(sizeof(LNode));
if(!S.base) exit(OVERFLOW);
S.base->next=NULL;
return OK;
//if(n) cout<<"Please enter the initial elements in the stack!"<<endl;
//for(i=1;i<=n;i++){
// p=(Linkstack)malloc(sizeof(Lstack));
// cout<<"The number "<<i<<" element:";
// cin>>p->data;
// S->next=p;p->next=NULL;
// top=p;
// }
}
Status Destroystack_L(Linkstack S){
StackPtr p=NULL;
if(!S.base) return OK;
else{
for(p=S.base;p!=S.top;p=p->next)
free(p);
free(S.top);
return OK;
}
}
Status GetTop(Linkstack S,SElemType &e){
if(S.base==S.top) return ERROR;
else
e=S.top->data;
return OK;
}
Status Empty(Linkstack S){
//判断栈是否为空
// Linkstack base,top;
if(S.base==S.top) return YES;//cout<<"The stack is empty";
else return NO;
}
int Length(Linkstack S){
StackPtr p;
int i;
if(S.base==S.top) return 0;
for(p=S.base,i=0;p->next!=NULL;++i)
p=p->next;
return i;
}
Status Push(Linkstack &S,SElemType e){
//进栈,向栈中压入元素'e'
//压入后,栈的栈底指针S.base所指向的那个结点是空结点,其data域中没存有数据
// Linkstack newnode,top;
StackPtr newbase;
newbase=(StackPtr)malloc(sizeof(LNode));
if(!newbase) exit(OVERFLOW);
newbase->data=e;
newbase->next=NULL;
S.top->next=newbase;
S.top=newbase;//修改栈顶指针,将当前栈顶指针改为指向新插入的结点
//测试栈顶指针是否按要求改变:cout<<S.base<<" "<<S.top<<endl;
return OK;
}
Status Pop(Linkstack &S,SElemType &e){
//出栈,并且用'e'带出栈顶元素
// Linkstack top,base;
StackPtr p;
if(S.base==S.top) return ERROR;
e=S.top->data;
for(p=S.base;p->next!=S.top;)
p=p->next; //修改栈顶指针:找寻原始栈顶的前一个结点,将其变成弹出原来栈顶后的新栈顶
free(S.top);
S.top=p;
S.top->next=NULL;
//测试栈顶指针是否按要求改变:cout<<S.base<<" "<<S.top<<endl;
return OK;
}
Status visit(Linkstack S){
StackPtr p;
if(S.base==S.top) return ERROR;
else{
for(p=S.base;p->next!=NULL;){
p=p->next;
cout<<p->data<<" ";
}
return OK;
}
}
Status Traverse(Linkstack S,Status (*visit)(Linkstack)){
if(visit(S)) return YES;
else return NO;
}
顺序栈主调函数:
#include"comdef.h"
#include"Sqstackdef.h"
#include"Sqstackapp.h"
int main(){
Sqstack S;
S.base=NULL;
int choice,len;
SElemType e;
cout<<"Choice menu:"<<endl;
cout<<"'1':To create an empty stack!"<<endl;
cout<<"'2':To destroy the stack!"<<endl;
cout<<"'3':To clear the stack!"<<endl;
cout<<"'4':To judge if the stack is empty!"<<endl;
cout<<"'5':To get the length of the stack!"<<endl;
cout<<"'6':To get the top element in the stack!"<<endl;
cout<<"'7':To push 'e' into the stack!"<<endl;
cout<<"'8':To pop the top element in the stack!"<<endl;
cout<<"'9':To travel the stack!"<<endl;
cout<<"Please input your choice:(0 to end)";
cin>>choice;
while(choice){
switch(choice) {
case 1:
if(InitiStack(S))
cout<<"You have created an empty stack successfully!"<<endl<<"You can choose '7' to push an element into it!"<<endl;
break;
case 2:
if(!S.base){
cout<<"There is no stack now!So you can't choose this operate now!"<<endl;
break;
}
DestroyStack(S);
cout<<"You have destroyed the stack successfully!"<<endl;
break;
case 3:
if(!S.base){
cout<<"There is no stack now!So you can't choose this operate now!"<<endl;
break;
}
ClearStack(S);
cout<<"You have cleared the stack successfully!"<<endl;
break;
case 4:
if(!S.base){
cout<<"There is no stack now!So you can't choose this operate now!"<<endl;
break;
}
if(StackEmpty(S)) cout<<"The stack is empty now!"<<endl;
else cout<<"The stack isn't empty!"<<endl;
break;
case 5:
if(!S.base){
cout<<"There is no stack now!So you can't choose this operate now!"<<endl;
break;
}
else len=StackLength(S);
cout<<"The length is:"<<len<<endl;
break;
case 6:
if(!S.base){
cout<<"There is no stack now!So you can't choose this operate now!"<<endl;
break;
}
if(GetTop(S,e))
cout<<"The top element is:"<<e<<endl;
else cout<<"The stack is empty now!"<<endl;
break;
case 7:
if(!S.base){
cout<<"There is no stack now!So you can't choose this operate now!"<<endl;
break;
}
cout<<"Please input the element you want to push into the stack:";
cin>>e;
Push(S,e);
cout<<"You have pushed "<<e<<" at the top of the stack successfully!"<<endl;
break;
case 8:
if(!S.base){
cout<<"There is no stack now!So you can't choose this operate now!"<<endl;
break;
}
if(Pop(S,e)){
cout<<"Present top element is:"<<e<<endl;
cout<<"You have poped the top element successfully!"<<endl;
}
else cout<<"The stack is empty now!"<<endl;
break;
case 9:
if(!S.base){
cout<<"There is no stack now!So you can't choose this operate now!"<<endl;
break;
}
if(S.base==S.top) cout<<"The stack is empty now!";
else cout<<"Present stack is:";
StackTraverse(S,visit);
cout<<endl;
break;
}
cout<<"Please input your choice:(0 to end)";
cin>>choice;
}
system("PAUSE");
return 0;
}
链栈主调函数:
#include"comdef.h"
#include"Lstackdef.h"
#include"Lstackapp.h"
int main(){
Linkstack S;
S.base=S.top=NULL;
int choice;
SElemType e;
cout<<"Choice menu:"<<endl;
cout<<"'1':To create an empty stack!"<<endl;
cout<<"'2':To judge if the stack is empty!"<<endl;
cout<<"'3':To get the length of the present stack!"<<endl;
cout<<"'4':To push an element at the top of the stack!"<<endl;
cout<<"'5':To pop the element at the top of the stack!"<<endl;
cout<<"'6':To travel the stack!"<<endl;
cout<<"Please enter your choice:(0 to end)";
cin>>choice;
while(choice){
switch(choice){
case 1:
if(Initistack(S))
cout<<"You have created an empty stack successfully!"<<endl;
break;
case 2:
if(!S.base){
cout<<"There is no stack now!You can't choose this operate at the moment!"<<endl<<"You can choose '1' to created one!"<<endl;
break;
}
if(Empty(S)) cout<<"The stack is empty now!"<<endl;
else cout<<"The stack isn't empty now!"<<endl;
break;
case 3:
if(!S.base){
cout<<"There is no stack now!You can't choose this operate at the moment!"<<endl<<"You can choose '1' to created one!"<<endl;
break;
}
//测试栈顶与栈底指针是否按要求发生了改变:cout<<S.base<<" "<<S.top<<endl;
cout<<"The length of the stack is:"<<Length(S)<<endl;
break;
case 4:
if(!S.base){
cout<<"There is no stack now!You can't choose this operate at the moment!"<<endl;
break;
}
cout<<"Please enter the element that you want to push into the stack:";
cin>>e;
if(Push(S,e)) cout<<"You have push the element:"<<e<<" onto the top of the stack successfully!"<<endl;
else cout<<"Sorry!You have failed to push "<<e<<" onto the top of the stack!"<<endl;
break;
case 5:
if(!S.base){
cout<<"There is no stack now!You can't choose this operate at the moment!"<<endl;
break;
}
if(Pop(S,e)) cout<<"You have poped the top element:"<<e<<" successfully!"<<endl;
else cout<<"Sorry!The stack is empty now!So there isn't any element for you to pop!"<<endl;
break;
case 6:
if(!S.base){
cout<<"There is no stack now!You can't choose this operate at the moment!"<<endl;
break;
}
if(Empty(S)){
cout<<"The stack is empty now!"<<endl;
break;
}
cout<<"Present stack is:";
Traverse(S,visit);
cout<<endl;
break;
}
cout<<"Please enter your choice:(0 to end)";
cin>>choice;
}
system("PAUSE");
return 0;
}
相似回答