栈的链式存储 c实现的几种基本操作
来源:互联网 发布:狸猫网络助手安卓 编辑:程序博客网 时间:2024/06/05 23:16
//
#include "stdafx.h"
#include <iostream>
using namespace std;
typedef struct Node
{
int iData;
Node* next;
}StackNode,*StackLink;
// 创建一个长度为n的链式栈
StackLink Creat(int n)
{
cout << "请输入" << n << "个整数" << endl;
int iNum = 0;
StackLink p,r,top = NULL;
for (int i = 0;i < n;++ i)
{
cin >> iNum;
p = (StackLink)malloc(sizeof(StackNode));
p->iData = iNum;
p->next = NULL;
if (top == NULL)
{
top = p;
}
else
{
r->next = p;
}
r = p;
}
return top;
}
// 出事化一个链式栈
void InitialStackList(StackLink& top)
{
top = NULL;
}
// 测试一个栈是否为空;
int IsEmpty(StackLink& top)
{
return top == NULL;
}
// 取当前栈顶元素;
void GetTopStack(StackLink& top,int& temp)
{
if (IsEmpty(top))
{
throw"栈空";
}
else
{
temp = top->iData;
}
}
// 栈中的进栈
int PushStack(StackLink& top,int item)
{
StackLink p = NULL;
if (!(p = (StackLink)malloc(sizeof(StackNode)) ))
{
return 0; // 开辟失败,不能创建成功;
}
else
{
p->iData = item;
p->next = top;
top = p;
return 1; // 插入成功;
}
}
// 出栈问题
int Pop(StackLink& top,int& iTemp)
{
StackLink p = NULL;
if (IsEmpty(top))
{
return 0;
}
else
{
p = top;
iTemp = p->iData;
top = p->next;
free(p);
return 1; // 操作成功;
}
return 1;
}
// 输出栈中的元素;
void Print(StackLink& top)
{
if (IsEmpty(top))
{
throw"栈空";
}
else
{
while(top)
{
cout << top->iData;
top = top->next;
}
}
cout << endl;
}
int main(int argc,char* argv[])
{
int iNumber = 5;
StackLink top = NULL;
top = Creat(iNumber);
Print(top);
InitialStackList(top);
if (IsEmpty(top))
{
cout << "栈是空的!" << endl;
}
else
{
cout << "栈不是空的!"<< endl;
}
return 0;
}
- 栈的链式存储 c实现的几种基本操作
- 单链表基本操作的C语言实现(链式存储结构)
- 链式栈的基本操作与实现
- 链式栈的基本操作实现c++
- 栈的链式存储实现c语言
- 串的链式存储的基本操作
- 数据结构 串(链式存储)的基本操作
- 串的链式存储基本操作
- 链表(链式存储)的基本操作
- 重学数据结构003——栈的基本操作及实现(链式存储)
- 二叉树(二叉链式存储)8种基本操作的实现
- 栈的基本操作(链式存储)链栈
- 栈的链式存储结构及其基本运算的实现
- 栈的链式存储结构及其基本运算的实现
- 栈的链式存储结构及其基本运算的实现
- 链式栈的基本操作
- 链式栈的基本操作
- 链式栈的基本操作
- web开发中的缓存问题的研究(三)
- ios UIImagePickerController 添加一个自定义的view。
- js刷新页面 方法大全
- 【漫谈】计算机专业找工作的一点感想
- USB详解
- 栈的链式存储 c实现的几种基本操作
- Your Performance Toolkit 读书笔记 -part1
- time_init
- htc a3380关于因使用google map api,导致apk程序安装时提示 程序未安装 的解决方法
- cap包分析
- linux tips
- late_time_init
- 成为一名JAVA程序员高手
- 错误的顺序链接 CRT 库和 MFC 库时出现 LNK2005 错误