大二数据结构3(栈和队列)

it2026-03-30  7

数据结构4(栈和队列之林大锐格)

#include <iostream> #include <bits/stdc++.h> using namespace std; #define maxleng 100 typedef int ElemType; typedef struct link { ElemType elem; struct link *next; }* linkstack; void initstack (linkstack &s) { s=NULL; } void push_link(linkstack &s,ElemType e) { linkstack p; p=new link; p->elem=e; p->next=s; s=p; } void pop_link(linkstack &s) { linkstack p; while(s!=NULL) { p=s;printf("%d ",p->elem);s=s->next; } } int main() { linkstack s; initstack(s); ElemType m,i; while(~ scanf("%d",&m)&&m!=0) { push_link(s,m); } pop_link(s); return 0; }

#include <iostream> #include <bits/stdc++.h> using namespace std; #define maxleng 100//限定栈的最长长度 typedef int ElemType;//用ElemType代替int类型 typedef struct link//定义一个栈 { ElemType elem[maxleng];//栈中的数据 int top;//栈的头结点 }sqstack; void initstack(sqstack &s)//初始化栈,让top指向头 { s.top=0; } int seg_push(sqstack &s,ElemType e)//入栈 { if(s.top>maxleng) { return 1; } s.elem[s.top]=e; s.top++; return 1; } void seg_pop(sqstack &s)//出栈 { while(s.top!=0) { printf("%d ",s.elem[s.top-1]); s.top--; } } int main()//主函数 { int n,m,j,e; sqstack s; initstack(s); while(~scanf("%d",&e)&&e!=0) { seg_push(s,e); } /*if(s.top==0) printf("error!!"); else for(int i=0;i<n;i++) { printf("%d",s.elem[s.top-1]); s.top--; }*/ seg_pop(s); return 0; }
最新回复(0)