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

2026/1/27 7:55:39

实验二 栈和队列

//栈的顺寻存储操作

#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文档下载到电脑
搜索更多关于: 数据结构 栈和队列基本操作 的文档
相关推荐
相关阅读
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 10

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xuecool-com QQ:370150219