当前位置:文档之家› C语言堆栈和队列函数大全

C语言堆栈和队列函数大全

3.基本函数
Linkstack Init_Linkstack() /*初始化栈函数 1.先决条件:无 2.函数作用:初始化链栈,返回 top 地址*/ {
Linkstack top; top=(Linkstack)malloc(sizeof(Stacknode)); top->next=NULL; return top; } int Empty_Linkstack(Linkstack top) /*判栈空函数 1.先决条件:初始化链栈 2.函数作用:判断栈是否为空,空返回 1,不空返回 0*/ { if(top->next==NULL)
printf("****************************************************************\n"); printf("请输入选择(1~7):");
scanf("%d",&choice); getchar(); switch(choice) { case 1:top=Init_Linkstack();
Push_Seqstack(s,N%r); N=N/r;
}while(N); while(Pop_Seqstack(s,&x)) {
if(x>=10)/*为了能转为十进制以上的*/ printf("%c",x+55);
else printf("%d",x);
} free(s)Байду номын сангаас/*释放顺序栈*/ printf("\n"); return 1; }
return 0; *x=s->data[s->top]; return 1; } int Printf_Seqstack(Seqstack *s) /*遍历顺序栈函数 1.先决条件:初始化顺序栈 2.函数作用:遍历顺序栈,成功返回 1*/ { int i,j=0; for(i=s->top;i>=0;i--) {
if(top) printf("置空栈成功!\n");break;
case 2:if(Empty_Linkstack(top)) printf("此为空栈.\n");
else printf("此不为空栈.\n");;break;
case 3:printf("请输入一个整数:"); scanf("%d",&x); if(Push_Linkstack(top,x)) printf("入栈成功.\n"); else printf("栈已满,无法入栈.\n");;break;
else printf("取栈顶元素失败,因栈为空.\n");
break; case 6:Printf_Linkstack(top);break; case 7:printf("谢谢使用!\n");break; default :printf("输入错误,请重新输入!\n");break; } }while(choice!=7); return 0; } 二.队列 1.宏定义 2.结构体 3.基本函数 4.主函数
printf("%c",x+55); else
printf("%d",x); } printf("\n"); free(top);/*释放栈顶空间*/ return 1; }
4.主函数
int main() {
Linkstack top; int choice; datatype x; do{
printf("****************************************************************\n"); printf("1.置空栈 2.判栈空 3.入栈 4.出栈 5.取栈顶元素 6.遍历 7.退出\n");
if(s->top==-1) return 0;
*x=s->data[s->top]; s->top--; return 1; } int Top_Seqstack(Seqstack *s,datatype *x) /*取栈顶元素函数 1.先决条件:初始化顺序栈 2.函数作用:取栈顶元素,并把其存放到 x 中,成 功返回 1,因栈空失败返回 0*/ { if(s->top==-1)
return 0; s->top=s->top+1; s->data[s->top]=x; return 1; } int Pop_Seqstack(Seqstack *s,datatype *x)
/*出栈函数 1.先决条件:初始化顺序栈 2.函数作用:从栈中出一个数据,并将其存放到 x 中,成 功返回 1,因栈空失败返回 0*/ {
return 0; *x=top->next->data; return 1; } int Printf_Linkstack(Linkstack top) /*遍历链栈函数 1.先决条件:初始化链栈 2.函数作用:遍历链栈,成功返回 1*/ { Stacknode *p=top->next; int j=0; while(p) {
printf("%d ",p->data);/*因 datatype 不同而不同*/ j++; if(j%10==0)
printf("\n"); p=p->next; } printf("\n"); return 1; } int Conversation_Linkstack(int N,int r) /*数制转换函数(链栈)1.先决条件:具有置空栈,入栈,出栈函数 2.函数作用:将 N 转换为 r 进制 的数*/ { Linkstack top; datatype x; printf("%d 转为%d 进制的数为:",N,r);/*以后可以删除去*/ top=Init_Linkstack(); do { Push_Linkstack(top,N%r); N=N/r; }while(N); while(Pop_Linkstack(top,&x)) { if(x>=10)/*为了能转为十进制以上的*/
case 4:if(Pop_Linkstack(top,&x)) printf("出栈成功,出栈元素为:%d\n",x);
else printf("出栈失败,因栈为空.\n");break;
case 5:if(Top_Linkstack(top,&x)) printf("取栈顶元素成功,栈顶元素为:%d\n",x);
printf("%d ",s->data[i]);/*因 datatype 不同而不同*/ j++; if(j%10==0)
printf("\n"); } printf("\n"); return 1; } int Conversation_Seqstack(int N,int r) /*数制转换函数(顺序栈)1.先决条件:具有置空栈,入栈,出栈函数 2.函数作用:将 N 转换为 r 进 制的数*/ { Seqstack *s; datatype x; printf("%d 转为%d 进制的数为:",N,r);/*以后可以删除去*/ s=Init_Seqstack(); do {
一.顺序栈
1.宏定义
#include<stdio.h> #include<stdlib.h> #define MAXSIZE **** #define datatype ****
2.结构体
typedef struct {
datatype data[MAXSIZE]; int top; }Seqstack;
return 0; Stacknode *p=top->next; *x=p->data; top->next=p->next; free(p); return 1; } int Top_Linkstack(Linkstack top,datatype *x) /*取栈顶元素函数 1.先决条件:初始化链栈 2.函数作用:取栈顶元素并放到 x 中,成功返回 1, 因栈空失败返回 0*/ { if(top->next==NULL)
3.基本函数
Seqstack *Init_Seqstack() /*置空栈函数(初始化)1.先决条件:无;2.函数作用:首先建立栈空间,然后初始化栈顶指针,返 回栈 s 的地址*/ {
Seqstack *s; s=(Seqstack *)malloc(sizeof(Seqstack)); s->top=-1; return s; } int Empty_Seqstack(Seqstack *s) /*判栈空函数 1.先决条件:初始化顺序栈;2.函数作用:判断栈是否为空,空返回 1,不空返回 0*/ { if(s->top==-1) return 1; else return 0; } int Push_Seqstack(Seqstack *s,datatype x) /*入栈函数 1.先决条件:初始化顺序栈 2.函数作用:将数据 x 入栈,栈满则不能,成功返回 1,因 栈满失败返回 0*/ { if(s->top==MAXSIZE-1)
return 1; else return 0; } int Push_Linkstack(Linkstack top,datatype x) /*入栈函数 1.先决条件:初始化链栈 2.函数作用:将数据 x 入栈,成功返回 1,失败返回 0*/ { Stacknode *p; p=(Stacknode *)malloc(sizeof(Stacknode)); p->data=x; p->next=top->next; top->next=p; return 1; } int Pop_Linkstack(Linkstack top,datatype *x) /*出栈函数 1.先决条件:初始化链栈 2.函数作用:若栈空退出,若没空则将数据出栈,并将其存 放到 x 中,成功返回 1,因栈空失败返回 0*/ { if(top->next==NULL)
相关主题