实验4:栈和队列的基本操作实现及其应用之《链栈》

实验4:栈和队列的基本操作实现及其应用之《链栈》

一、实验目的

1、   熟练掌栈和队列的结构特点,掌握栈和队列的顺序存储和链式存储结构和实现。

2、      学会使用栈和队列解决实际问题。

二、实验内容

建立一个链栈,实现栈的压栈和出栈操作。

#ifndef LinkStack_h
#define LinkStack_h
#include 
   
   
    
    
using namespace std;

template
    
    
     
     
struct Node 
{
	DataType data;
	Node
     
     
      
       *next;
	Node
      
      
       
       (DataType x) 
	{
		data = x;
		next = NULL;
	}
};
template
       
       
         class LinkStack { public: LinkStack() { top = NULL; } ~LinkStack() {}; void Push(DataType x); DataType Pop(); DataType GetTop(); int Empty() { if (top == NULL)return 1; else return 0; } private: Node 
        
          *top; }; #endif 
         
       
      
      
     
     
    
    
   
   
#include "LinkStack.h"
template
    
    
     
     
void LinkStack
     
     
      
      ::Push(DataType x)
{
	Node
      
      
       
        *s = new Node
       
       
        
        (x);
	s->next = top; top = s;
}

template
        
        
          DataType LinkStack 
         
           ::Pop() { if (top == NULL)throw"下溢"; DataType x = top->data; Node 
          
            *p = top; top = top->next; delete p; return x; } template 
           
             DataType LinkStack 
            
              ::GetTop() { if (top == NULL) throw "空栈"; return top->data; } 
             
            
           
          
        
       
       
      
      
     
     
    
    
#include
     
     
      
      
using namespace std;
#include "LinkStack.cpp"

void main()
{
	LinkStack
      
      
       
       s;
	if (s.Empty()==0)
		cout << "栈为空" << endl;
	else
		cout << "栈为非空" << endl;
	cout << "对1和2执行入栈操作" << endl;
	s.Push(1);
	s.Push(2);
	cout<<"栈顶元素为:"<< endl;
	cout <
       
       
        
        << endl;
	cout << "执行一次出栈操作" << endl;
	s.Pop();
	cout << "栈顶元素为:" << endl;
	cout << s.GetTop()<< endl;
	system("pause");
}
       
       
      
      
     
     


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值