实验之链栈
来源:互联网 发布:sql字符串中的单引号 编辑:程序博客网 时间:2024/06/05 18:05
实验目的:
熟练应用栈的链式存储结构——链栈,并了解其运作原理,编写代码实行。
实验要求:
应用链栈完成学生信息的入栈,出栈,编写代码并完成操作。
实验代码:
头文件:
#include <iostream>
template<class T>
class Node
{
public:
T data;
Node *next;
};
template<class T>
class Linkstack{
public:
Linkstack(){top=NULL;}
void push();
T pop();
T Gettop(){if (top!=NULL) return top->data;}
int empty(){if (top==NULL) return 1; else return 0;}
private:
Node <T> *top;
};
源文件:
#include "tou.h"
#include<iostream>
using namespace std;
template <class T>
void Linkstack<T>::push(){
T x; cin>>x;
Node<T> *s=new Node<T>;
s->data=x;
s->next=top;
top=s;
}
template <class T>
T Linkstack<T>::pop(){
T x; Node <T>*p;
if(top==NULL) throw"over";
x=top->data; p=top;
top=top->next;
delete p;
return x;
}
void main(){
Linkstack<int> x;
int i; int n;
cout<<"please the number"<<endl;
cin>>n;
for(i=1;i<=n;i++){
x.push();
}
cout<<x.Gettop()<<endl;
cout<<x.empty()<<endl;
for(i=1;i<=n;i++){
cout<<x.pop()<<endl;
}
system("pause");
}
实验结果:
实验总结:
应用链式存储结构,需要创造结点,链式存储结构特点是:灵活应用零散空间存储信息,可并不特别节省空间,且链栈操作也受限,除析构函数外,链栈的算法时间复杂度均为1。
- 实验之链栈
- 实验四之链栈
- 实验三之链栈的实现
- 实验之顺序栈
- 实验之链队列
- 实验:协议栈之串口基础实验
- 栈实验之数组逆序
- 实验三之顺序栈
- 数据结构实验之链表
- 数据结构实验之链表
- 协议栈相关实验之 SerialApp实验 详解
- 协议栈相关实验之 SerialApp实验 详解
- 实验二 线性表综合实验之静态链表
- 实验二 线性表综合实验之《静态链表》
- 实验二 线性表综合实验之《静态链表》
- 《数据结构》实验三:栈和队列实验之实验内容1
- 《数据结构》实验三: 栈和队列实验之实验内容2
- 栈实验之栈的基本操作
- Spring AOP实战讲解
- 基础练习 十六进制转八进制
- VMware连网
- 企业实战之切面编程《统一打印日志》
- C语言——该年的第几天
- 实验之链栈
- kmem_cache_init
- Linux学习-结构体数组和结构体指针
- Spring Boot 自定义favicon.ico
- JAVA中的移位运算
- C语言数据结构将链串里所有值为x的字符删除
- 线程的基础学习之创建线程
- eclipse对Tomcat应用进行远程调试
- JVM规范(五)特定方法