#include <iostream> #include <cstdio> #include<bits/stdc++.h> using namespace std; //栈 typedef struct{ int *base; int *top; int stacksize; }sqstack; void intistack(sqstack &s){//建栈 s.base=(int *)malloc(100*sizeof(int)); if(!s.base) cout<<"error"<<endl; s.top=s.base; s.stacksize=100; } void pushstack(sqstack &s,int m){//入栈 if(s.top-s.base>=s.stacksize){ s.base=(int *)realloc(s.base,(s.stacksize+10)*sizeof(int)); if(s.base) cout<<"error"<<endl; s.top=s.base+s.stacksize; } *s.top++=m; } int popstack(sqstack &s,int &m){//出栈 if(s.top==s.base) cout<<"error"<<endl; m=*--s.top; return m; } //链列队 typedef struct qnode{ int data; qnode *next; }qnode,*queueptr; typedef struct{ queueptr first; queueptr last; }linkqueue; int initqnode(linkqueue &s){//建空队 s.first=s.last=(queueptr)malloc(sizeof(qnode)); if(!s.first)exit(-1); s.first->next=null; return 1; } int enqueue(linkqueue &s,int e){//入 queueptr p=(queueptr)malloc(sizeof(qnode)); if(!p)exit(-1); p->data=e; p->next=null; s.last->next=p; s.last=p; return 1; } int dequeue(linkqueue &s,int &e){//出 if(s.first==s.last)return 0; queueptr p=s.first->next; e=p->data; s.first->next=p->next; if(s.last==p)s.last=s.first; free(p); return 1; } int destroyquenue(linkqueue &s){//毁队 while(s.first){ s.last=s.first->next; free(s.first); s.first=s.last; } return 1; } //循环列队 #define maxsize 100 typedef struct{ int *base; int front,rear; }sqquenue; int intiquenue(sqquenue &s){//建 s.base=(int *)malloc(maxsize*sizeof(int)); if(!s.base)exit(-1); s.front=s.rear=0; return 1; } int quenuelen(sqquenue &s){//长度 return(s.rear-s.front+maxsize)%maxsize; } int enqueue(sqquenue &s,int e){//插入 if((s.rear+1)%maxsize==s.front)return -1; s.base[s.rear]=e; s.rear=(s.rear+1)%maxsize; return 1; } int dequenue(sqquenue &s){//删 if(s.front==s.rear)return -1; s.front=(s.front+1)%maxsize; return 1; }
栈和列队的一些代码
0 条评论