数据结构C 版链队和顺序栈的实验报告及其代码.
实 验 报 告
课程名称: 数据库结构与数据库应用基础教程
系部名称:
专业班级:
学生姓名:
学 号:
指导教师:
实验项目 顺序栈和链队 实验日期 2015-11-15 实验地点 实验楼606 同组人数 4
? 传统实验 ? 现代实验 ? 其 他 实验类型 ? 验证性 ? 综合性 ? 设计性 ? 其 他
? 自立式 ? 合作式 ? 研究式 ? 其 他 一、 实验目的
1. 用控制台应用程序和Windows窗体应用程序实现顺序栈的获取栈的长度,入
栈,出栈,获取栈顶元素的操作。
2. 用控制台应用程序和Windows窗体应用程序实现顺序栈的链队的长度,入栈,
出栈,获取栈顶元素的操作。
二、实验仪器设备
硬件:AcerE5-571G-56AJ
操作系统:Winsows10
软件:Microsoft Visual Studio 2010
二、 实验原理、内容及步骤
顺序栈:
1. 创建一个控制台应用,再添加接口和类。
2. 在主方法下面实现添加顺序栈的操作,用push实现,其实就是一次性的把全部数据
入栈的操作。
3. 然后用switch case语句实现“求长度,入栈,出栈,获取栈顶元素”的操作。 4. 在做窗体的时候就是根据控制台来做,但是在做窗体的时候没用“判满,判空”,
而是用messagebox,show()提示输入异常的方法。
(相当于判空的操作)
(相当于判满的操作)
链队:
1. 创建一个控制台应用,再添加接口和类。
2. 在主方法下面实例化创建一个新的链队,用的是一个一个入队的方法,再进行其
他的操作。
3. 同样的用switch case语句实现“求长度,入队,出队,获取头结点”的操作。 4. 链队的窗体和顺序栈的窗体基本一样,都是根据控制台来做,不过没用“判空。
判满”。也是用messagebox.show()来表示。
(相当于判空的操作)
(相当于判满的操作)
四、实验中存在的问
题
快递公司问题件快递公司问题件货款处理关于圆的周长面积重点题型关于解方程组的题及答案关于南海问题
、解决方法及进一步的想法等
1.顺序栈的控制台应用程序是借用了顺序栈的代码实现的,特别是用.PrintAllElem
实现打印输出工作。
2.在做窗体的时候发现每个button按钮下面都要用string d = textBox2.Text; char[] c =
{ ',' }; string[] b = new string[100]; b = d.Split(c); for (int i = 0; i < b.Length;
i++ .Push(Convert.ToInt32(b[i])); }
如果只用一个根本不能实现,而且因为是顺序栈,不能只写一个而来调用此段代码,
需要在每个按钮下面都书写。
3.在做链队控制台的时候,最开始是用把数字用追加进去的方法,后来总是实现不
了,就改用入队的方法添加数字的方法
4.因为知道链队可以用调用的方法,所以就打算写一个代码表示输入的链队,然后每个按钮再调用来实现相关的操作,但是没有找到相关的资料,所以没实现。最后还是使用了顺序栈的方法,在每个button按钮下面用string d = textBox2.Text; char[] c
= { ',' }; string[] b = new string[100]; b = d.Split(c); for (int i = 0; i < b.Length; i++ .Push(Convert.ToInt32(b[i])); }的方法。
五、教师评语
成 绩
指导教师签字:
年 月 日 注:1、此报告为参考格式,各栏项目可根据实际情况进行调整;
2、实验成绩以优(90~100)、良(80~89)、中(70~79)、及格(60~69)、不及格(60以下)
五个等级评定。
附录
(因为窗体和控制台中的类和接口一样,所以省略部分窗体部分的代码。) using System;
using System.Collections.Generic; using System.Linq;
using System.Text;
namespace ConsoleApplication7 {
public interface IStack
{
int GetLength(); //求栈的长度
bool IsEmpty(); //判断栈是否为空
void Clear(); //清空操作
void Push(T item); //入栈操作
T Pop(); //出栈操作
T GetTop(); //取栈顶元素
}
}
using System;
using System.Collections.Generic; using System.Linq;
using System.Text;
namespace ConsoleApplication7 {
public class SeqStack : IStack
{
private int maxsize; //顺序栈的容量
private T[] data; //数组,用于存储顺序栈中的数据元素
private int top; //指示顺序栈的栈顶
//索引器
public T this[int index]
{
get
{
return data[index];
}
set
{
data[index] = value;
}
}
//容量属性
public int Maxsize
{
get
{
return maxsize;
}
set
{
maxsize = value;
}
}
//栈顶属性
public int Top
{
get
{
return top;
}
}
//构造器
public SeqStack(int size)
{
data = new T[size];
maxsize = size;
top = -1;
}
//求栈的长度
public int GetLength()
{
return top + 1;
}
//清空顺序栈
public void Clear()
{
top = -1;
}
//判断顺序栈是否为空
public bool IsEmpty()
{
if (top == -1)
{
return true;
}
else
{
return false;
}
}
//判断顺序栈是否为满
public bool IsFull()
{
if (top == maxsize - 1)
{
return true;
}
else
{
return false;
}
}
//入栈
public void Push(T item)
{
if (IsFull())
{
Console.WriteLine("Stack is full");
return;
}
data[++top] = item;
}
//出栈
public T Pop()
{
T tmp = default(T);
if (IsEmpty())
{
Console.WriteLine("Stack is empty");
return tmp;
}
tmp = data[top];
--top;
return tmp;
}
//获取栈顶数据元素
public T GetTop()
{
if (IsEmpty())
{
Console.WriteLine("Stack is empty!");
return default(T);
}
return data[top];
}
public void PrintAllElem()
{
// 判断是否为空表
if (IsEmpty())
{
Console.WriteLine("顺序栈中不存在数据元素,无法执行打印
操作~");
}
// 执行打印操作
else
{
Console.WriteLine("打印顺序栈中的数据元素:\n");
for (int i = 0; i <= top ; i++)
{
Console.Write(data[i] + " ");
}
Console.WriteLine();
}
}
internal object Pop(int pos)
{
throw new NotImplementedException();
}
internal object[] boolIsEmpty()
{
throw new NotImplementedException();
}
}
}
using System;
using System.Collections.Generic; using System.Linq;
using System.Text;
namespace ConsoleApplication7 {
class Program
{
static void Main(string[] args)
{
Console.Write("请输入顺序栈的长度:");
int b = Convert.ToInt32(Console.ReadLine());
// 创建并实例化顺序表类,为了方便演示,此处使用整数
SeqStack SL = new SeqStack(b); // 为了方便演示,此处
直接使用5作为表最大容量
Console.Write("请输入序顺序栈La的第1个成员:");
//把输入的元素追加到序列表La中;
string n = null;
do
{
int m = Convert.ToInt32(Console.ReadLine());
if (SL.GetLength() + 1 > b)
{
Console.WriteLine("您输入的顺序栈La的元素个数大于
顺序栈La的容量!!!");
Console.ReadKey();
break;
}
SL.Push(m);
Console.Write("是否继续为顺序栈La加入成员:(是:y,否:
n)");
n = Console.ReadLine();
if (n == "y")
{
Console.Write("请输入顺序栈La新加入的成员:");
}
} while (n == "y");
// 保存用户输入的值的变量
string input;
int pos;
int num;
while (true)
{
Console.WriteLine("请输入要执行的操作");
Console.WriteLine("1.获取栈的长度");
Console.WriteLine("2.入栈");
Console.WriteLine("3.出栈");
Console.WriteLine("4.获取栈顶元素");
input = Console.ReadLine();
switch (input)
{
case "1":
Console.WriteLine("当前顺序栈的长度:{0}",
SL.GetLength());
break;
case "2":
Console.WriteLine("请输入一个数入栈:");
if (int.TryParse(Console.ReadLine(), out num))
{ SL.Push(num);
SL.PrintAllElem();
}
break;
case "3":
Console.WriteLine("在顺序栈的顶部出栈", SL.Pop());
{
SL.PrintAllElem();
}
break;
case "4":
Console.WriteLine("获取栈顶元素;{0}",
SL.GetTop());
break;
}
}
}
public static SeqStack Empty { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication9 {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//求长度
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请先输入栈的最大长度~~~", "提示");
return;
}
if (textBox2.Text == "")
{
MessageBox.Show("请先输入栈的元素~~~", "提示");
return;
}
int a = Convert.ToInt32(textBox1.Text);
SeqStack ee = new SeqStack(a);
string d = textBox2.Text;
char[] c = { ',' };
string[] b = new string[100];
b = d.Split(c);
for (int i = 0; i < b.Length; i++)
{
ee.Push(Convert.ToInt32(b[i]));
}
if (b.Length > a)
{
MessageBox.Show("输入的元素个数大于顺序栈的容量~~~", "
提示");
return;
}
textBox3.Text = Convert.ToString(ee.GetLength());
}
//入栈
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请先输入栈的最大长度~~~", "提示");
return;
}
if (textBox2.Text == "")
{
MessageBox.Show("请先输入栈的元素~~~", "提示");
return;
}
int a = Convert.ToInt32(textBox1.Text);
SeqStack ee = new SeqStack(a);
string d = textBox2.Text;
char[] c = { ',' };
string[] b = new string[100];
b = d.Split(c);
for (int i = 0; i < b.Length; i++)
{
ee.Push(Convert.ToInt32(b[i]));
}
if (b.Length > a)
{
MessageBox.Show("输入的元素个数大于顺序栈的容量~~~", "
提示");
return;
}
if (ee.GetLength() != 0)
{
MessageBox.Show("顺序栈生成成功~~~", "提示");
}
textBox3.Text = textBox2.Text;
}
//出栈
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请先输入栈的最大长度~~~", "提示");
return;
}
if (textBox2.Text == "")
{
MessageBox.Show("请先输入栈的元素~~~", "提示");
return;
}
int a = Convert.ToInt32(textBox1.Text);
SeqStack ee = new SeqStack(a);
string d = textBox2.Text;
char[] c = { ',' };
string[] b = new string[100];
b = d.Split(c);
for (int i = 0; i < b.Length; i++)
{
ee.Push(Convert.ToInt32(b[i]));
}
if (b.Length > a)
{
MessageBox.Show("输入的元素个数大于顺序栈的容量~~~", "
提示");
return;
}
textBox3.Clear();
int z = ee.GetLength();
for (int j = 0; j < z; j++)
{
textBox3.Text += Convert.ToString(ee.Pop()) + " ";
}
}
//获取栈顶元素
private void button4_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请先输入栈的最大长度~~~", "提示");
return;
}
if (textBox2.Text == "")
{
MessageBox.Show("请先输入栈的元素~~~", "提示");
return;
}
int a = Convert.ToInt32(textBox1.Text);
SeqStack ee = new SeqStack(a);
string d = textBox2.Text;
char[] c = { ',' };
string[] b = new string[100];
b = d.Split(c);
for (int i = 0; i < b.Length; i++)
{
ee.Push(Convert.ToInt32(b[i]));
}
if (b.Length > a)
{
MessageBox.Show("输入的元素个数大于顺序栈的容量~~~", "
提示");
return;
}
textBox3.Clear();
textBox3.Text = Convert.ToString(ee.GetTop());
private void button5_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
(因为窗体和控制台的接口和类一样,所以就省略窗体部分的代码。)
using System;
using System.Collections.Generic; using System.Linq;
using System.Text;
namespace ConsoleApplication8
{
public interface IQueue
{
int GetLength(); //求队列的长度
bool IsEmpty(); //判断对列是否为空
void Clear(); //清空队列
void In(T item); //入队
T Out(); //出队
T GetFront(); //取对头元素
}
} using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication8
{
public class Node
{
private T data; //数据域
private Node next; //引用域
//构造器
public Node(T val, Node p)
{
data = val;
next = p;
}
//构造器
public Node(Node p)
{
next = p;
}
//构造器
public Node(T val)
{
data = val;
next = null;
}
//构造器
public Node()
{
data = default(T);
next = null;
}
//数据域属性
public T Data
{
get
{
return data;
}
set
{
data = value;
}
}
//引用域属性
public Node Next
{
get
{
return next;
}
set
{
next = value;
}
}
}
} using System;
using System.Collections.Generic; using System.Linq;
using System.Text;
namespace ConsoleApplication8 {
public class LinkQueue : IQueue
{
private Node front; //队列头指示器
private Node rear; //队列尾指示器
private int num; //队列结点个数
//队头属性
public Node Front
{
get
{
return front;
}
set
{
front = value;
}
}
//队尾属性
public Node Rear
{
get
{
return rear;
}
set
{
rear = value;
}
}
//队列结点个数属性
public int Num
{
get
{
return num;
}
set
{
num = value;
}
}
//构造器
public LinkQueue()
{
front = rear = null;
num = 0;
}
public int GetLength()
{
return num;
}
//清空链队列
public void Clear()
{
front = rear = null;
num = 0;
}
//判断链队列是否为空
public bool IsEmpty()
{
if ((front == rear) && (num == 0))
{
return true;
}
else
{
return false;
}
}
//入队
public void In(T item)
{
Node q = new Node(item);
if (rear == null)
{
rear = q;
front = q;
}
else
{
rear.Next = q;
rear = q;
}
++num;
}
//出队
public T Out()
{
if (IsEmpty())
{
Console.WriteLine("Queue is empty!");
return default(T);
}
Node p = front;
front = front.Next;
if (front == null)
{
rear = null;
}
--num;
return p.Data;
}
//获取链队列头结点的值
public T GetFront()
{
if (IsEmpty())
{
Console.WriteLine("Queue is empty!");
return default(T);
}
return front.Data;
} using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
LinkQueue SL = new LinkQueue();
string input;
int num;
while (true)
{
Console.WriteLine("请输入您要执行的操作编号:");
Console.WriteLine("1.入队");
Console.WriteLine("2.求长度");
Console.WriteLine("3.出队");
Console.WriteLine("4.获取链队列头节点的值");
input = Console.ReadLine();
switch (input)
{
case "1":
Console.WriteLine("请输入一个数入链队:");
string b = Console.ReadLine();
char [] c = { ',' };
string[] d = new string[20000];
d = b.Split(c);
for (int j = 0; j < d.Length; j++)
{
SL.In(Convert.ToInt32 (d[j]));
}
break;
case "2":
Console.WriteLine("当前链队中的数据元素个数为:
{0}", SL.GetLength());
break;
case "3":
int a = SL.GetLength();
for (int i = 0; i < a; i++)
{
Console.WriteLine("出链队的第{0}个值:
{1}",i+1, SL.Out());
}
break;
case "4":
Console.WriteLine("获取队头元素;{0}",
SL.GetFront());
break;
}
}
}
}
} using System;
using System.Collections.Generic; using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication9 {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
//入队
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请先输入链队的最大长度~~~", "提示");
return;
}
if (textBox2.Text == "")
{
MessageBox.Show("请先输入链队的元素~~~", "提示");
return;
}
int a = Convert.ToInt32(textBox1.Text);
LinkQueue ee = new LinkQueue(a);
string d = textBox2.Text;
char[] c = { ',' };
string[] b = new string[100];
b = d.Split(c);
for (int i = 0; i < b.Length; i++)
{
ee.In(Convert.ToInt32(b[i]));
}
if (b.Length > a)
{
MessageBox.Show("输入的元素个数大于顺序栈的容量~~~", "
提示");
return;
}
if (ee.GetLength() != 0)
{
MessageBox.Show("链队生成成功~~~", "提示");
}
textBox3.Text = textBox2.Text;
}
//出队
private void button4_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请先输入链队的最大长度~~~", "提示");
return;
}
if (textBox2.Text == "")
{
MessageBox.Show("请先输入链队的元素~~~", "提示");
return;
}
int a = Convert.ToInt32(textBox1.Text);
LinkQueue ee = new LinkQueue(a);
string d = textBox2.Text;
char[] c = { ',' };
string[] b = new string[100];
b = d.Split(c);
for (int i = 0; i < b.Length; i++)
{
ee.In(Convert.ToInt32(b[i]));
}
if (b.Length > a)
{
MessageBox.Show("输入的元素个数大于顺序栈的容量~~~", "
提示");
return;
}
textBox3.Clear();
int z = ee.GetLength();
for (int j = 0; j < z; j++)
{
textBox3.Text += Convert.ToString(ee.Out()) + " ";
}
}
//获取头结点
private void button5_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请先输入链队的最大长度~~~", "提示");
return;
}
if (textBox2.Text == "")
{
MessageBox.Show("请先输入链队的元素~~~", "提示");
return;
}
int a = Convert.ToInt32(textBox1.Text);
LinkQueue ee = new LinkQueue(a);
string d = textBox2.Text;
char[] c = { ',' };
string[] b = new string[100];
b = d.Split(c);
for (int i = 0; i < b.Length; i++)
{
ee.In(Convert.ToInt32(b[i]));
}
if (b.Length > a)
{
MessageBox.Show("输入的元素个数大于顺序栈的容量~~~", "
提示");
return;
}
textBox3.Clear();
textBox3.Text = Convert.ToString(ee.GetFront());
}
//长度
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请先输入链队最大的长度~~~", "提示");
return;
}
if (textBox2.Text == "")
{
MessageBox.Show("请先输入链队的元素~~~", "提示");
return;
}
int a = Convert.ToInt32(textBox1.Text);
LinkQueue ee = new LinkQueue(a);
string d = textBox2.Text;
char[] c = { ',' };
string[] b = new string[100];
b = d.Split(c);
for (int i = 0; i < b.Length; i++)
{
ee.In(Convert.ToInt32(b[i]));
}
if (b.Length > a)
{
MessageBox.Show("输入的元素个数大于顺序栈的容量~~~", "
提示");
return;
}
textBox3.Text = Convert.ToString(ee.GetLength());
}
private void button1_Click_1(object sender, EventArgs e)
{
this.Close();
}
}
}
书中横卧着整个过去的灵魂——卡莱尔
人的影响短暂而微弱,书的影响则广泛而深远——普希金
人离开了书,如同离开空气一样不能生活——科洛廖夫
书不仅是生活,而且是现在、过去和未来文化生活的源泉 ——库法耶夫
书籍把我们引入最美好的社会,使我们认识各个时代的伟大智者———史美尔斯
书籍便是这种改造灵魂的工具。人类所需要的,是富有启发性的养料。而阅读,则正是这种养料———雨果