发布时间 : 星期三 文章数据结构 栈和队列基本操作更新完毕开始阅读9449fc282af90242a895e549
实验二 栈和队列
//栈的顺寻存储操作
#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;