首页 > 学院 > 开发设计 > 正文

线性表-链栈

2019-11-08 18:23:46
字体:
来源:转载
供稿:网友
 #include <iostream> #include <string> #include <vector> #include <algorithm> #define MAXSIZE 100 using namespace std;/*链栈 */typedef struct StackNode{	int data;	struct StackNode *next;}StackNode,*LinkStack;//初始化,不带头结点 int InitStack(LinkStack &S){	S = NULL;	return 1; }  //入栈(头插入法) int push(LinkStack &S,int e) { 	LinkStack p = new StackNode; 	p->data = e; 	p->next = S; 	S = p; 	return 1;  }   //取栈顶元素  int getTop(LinkStack S)  {  	if(S!=NULL)  	{  		return S->data;	  }	  else	  {	  	return -1;	  }   }    //出栈  int pop(LinkStack &S)  {  	if(S==NULL)  	{  		cout<<"int pop(LinkList &S) err:栈空error"<<endl;   		return 0;	  }	  else	  {	  	LinkStack p = S;	  	S = S->next;	  	delete p;	  	return 1;	  }   } int main(){	LinkStack stack;	InitStack(stack);	push(stack,1);	push(stack,2);	push(stack,3);	push(stack,4);	push(stack,5);	cout<<"getTop:"<<getTop(stack)<<endl; 	pop(stack);	cout<<"getTop:"<<getTop(stack)<<endl; }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表