栈的链式存储 c实现的几种基本操作
2011-11-27 15:41
489 查看
我们都知道单链表中的结尾有一个NULL,正好栈的链式实现中就是利用了这个特性,把NULL看成是栈的底部;然后呢,把进栈和出栈都是从栈顶指针开始的。正好利用了这样的特性,使得链式栈使用青来没有什么难度。
//
#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;
}
//
#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;
}
相关文章推荐
- 线性表链式存储结构下基本操作的实现(初始化、赋值、取值、插入、删除、归并等)
- 二叉树基本操作实现(二叉树的链式存储)
- 二叉树(二叉链式存储)8种基本操作的实现
- 重学数据结构003――栈的基本操作及实现(链式存储)
- 重学数据结构003——栈的基本操作及实现(链式存储)
- C语言——链式存储实现栈的基本运算算法
- 以邻接矩阵作为存储结构实现图的创建与基本操作
- 线性表链式存储实现各种操作
- 栈之链式存储基本操作
- 链表(链式存储)的基本操作
- 图的邻接矩阵存储结构基本操作的实现
- JDBC实现数据库的几种基本操作(查询,分页查询,根据关键字进行查询以及插入数据)
- 数据结构_线性表_链式存储_双向循环链表的基本操作
- 栈的链式存储结构及其基本运算实现
- 堆分配存储表示的串其基本操作的实现
- 数据结构编程笔记十九:第七章 图 图的邻接表存储表示及各基本操作的实现
- 使用C++数组实现二叉树的存储和基本操作
- 几种手势操作的最基本实现
- 采用链式存储实现,进栈、出栈操作
- 第六周:栈的链式存储结构及其基本运算实现