第一范文网 - 专业文章范例文档资料分享平台

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

来源:用户分享 时间:2025/6/16 9:46:56 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

实验二 栈和队列

//栈的顺寻存储操作

#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;

搜索更多关于: 数据结构 栈和队列基本操作 的文档
数据结构 栈和队列基本操作.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c5sanw1kf7d036aw5ujwp_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top