云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 数据结构 栈和队列基本操作

数据结构 栈和队列基本操作

  • 62 次阅读
  • 3 次下载
  • 2025/6/14 8:47:46

实验二 栈和队列

//栈的顺寻存储操作

#include \#include \

#define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef struct { int *base; int *top; int stacksize; }Sqstack;

void Initstack(Sqstack &s) {

s.base=s.top=(int*)malloc(STACK_INIT_SIZE*sizeof(int)); s.stacksize=STACK_INIT_SIZE; }

void Push(Sqstack &s,int e) {

*s.top++=e; }

int Pop(Sqstack &s) {

int e;

e=*--s.top; return e; } /*

void conversion()//数制转换 {

int N;

printf(\请输入十进制数N:\ scanf(\ Sqstack s; Initstack(s); while(N) { Push(s,N%8); N=N/8; }

printf(\转换的八进制为:\

while(s.top!=s.base) { printf(\ }

printf(\ }

void main() {

conversion(); } */

void main() {

Sqstack s; Initstack(s); int n; int temp;

printf(\请输入初始化栈的元素个数:\ scanf(\ for(int i=1;i<=n;i++) { printf(\请输入第%d个元素:\ scanf(\ Push(s,temp); }

printf(\初始化的栈出栈为:\ while(s.top!=s.base) { printf(\ }

printf(\ }

//链栈的基本操作(括号的匹配检查)

#include \#include \

typedef struct SNode {

char data;

struct SNode *next; }SNode,*LinkStack;

//初始化栈

void Init(LinkStack &top) {

top=(LinkStack)malloc(sizeof(SNode)); top->next=NULL; }

//创建栈

void creat(LinkStack &top,int n) {

int i;

SNode *p;

top=(LinkStack)malloc(sizeof(SNode)); top->next=NULL;

printf(\ for(i=n;i>0;i--) { printf(\输入%d个元素:\ p=(LinkStack)malloc(sizeof(SNode)); scanf(\ p->next=top->next; top->next=p; } }

//进栈操作

void Push(LinkStack &top,char e)

{

SNode *q;

q=(LinkStack)malloc(sizeof(SNode)); q->data=e;

q->next=top->next; top->next=q; }

//出栈操作

void Pop(LinkStack &top,char &e) {

SNode *q;

e=top->next->data; q=top->next;

top->next=q->next; free(q); }

void display(LinkStack &top) {

SNode *p; p=top;

while(p->next!=NULL) { printf(\ \ p=p->next;

}

printf(\}

//测试程序 void main() {

char e,a;

LinkStack stack1; Init(stack1);

printf(\请输入括号以#号结束!\\n\ while(1) { scanf(\ if(a=='#') break;

搜索更多关于: 数据结构 栈和队列基本操作 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

实验二 栈和队列 //栈的顺寻存储操作 #include \#include \#define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef struct { int *base; int *top; int stacksize; }Sqstack; void Initstack(Sqstack &s) { s.base=s.top=(int*)malloc(STACK_INIT_SIZE*sizeof(int)); s.stacksize=STACK_INIT_SIZE; } void Push(Sqstack &s,int e) { *s.top++=e; } int Pop(Sqstack &

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com