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

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

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

一.顺序栈

1.宏定义

#include

#include

#define MAXSIZE ****

#define datatype ****

2.结构体

typedef struct

{

datatype data[MAXSIZE];

int top;

}Seqstack;

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 0;

s->top=s->top+1;

s->data[s->top]=x;

return 1;

}

int Pop_Seqstack(Seqstack *s,datatype *x) acidity, mL.; M--

calibration of the molar concentration of sodium hydroxide standard solution, moI/L; V--amount of the volume of sodium hydroxide standard solution, Ml; M--the weight of the sample, g. Such as poor meets the requirements, take the arithmetic mean of the second determination as a result. Results one decimal. 6, allowing differential analyst simultaneously or in quick succession for the second determination, the absolute value of the difference of the results. This value should be no more than 1.0. 1, definitions and principles for determination of ash in starches, starch and ash: starch samples of ash the residue obtained

after weight. Original sample residue weight of sample weight or weight expressed as a percentage of the dry weight of the sample. Samples of

ash at 900 ? high temperature until ashing sample ... The Crucible: determination of Platinum or other conditions of the affected material, capacity of 50mL. Dryer: has effectively adequate drying agent and-perforated metal plate or porcelain. Ashing furnaces: device for controlling and regulating temperature, offers 900 incineration temperature of 25 c. Analytical balance. Electric hot plate or Bunsen. 3, crucible of analysis steps preparation: Crucible must first wash with boiling dilute hydrochloric acid, then wash with a lot of water and then rinse with distilled water. Wash the Crucible within ashing furnace, heated at 900 to 25 ? 30min, and in the desiccator to cool to room temperature and then weighing,

/*出栈函数1.先决条件:初始化顺序栈2.函数作用:从栈中出一个数据,并将其

存放到x中,成

功返回1,因栈空失败返回0*/

{

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;

*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--)

{

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

{

Push_Seqstack(s,N%r);

N=N/r;

acidity, mL.; M--calibration of the molar concentration of sodium hydroxide standard solution, moI/L; V--amount of the volume of sodium hydroxide standard solution, Ml; M--the weight of the sample, g. Such as poor meets the requirements, take the arithmetic mean of the second determination as a result. Results one decimal. 6, allowing differential analyst simultaneously or in quick succession for the second determination, the absolute value of the difference of the results. This value should be no more than 1.0. 1, definitions and principles for determination of ash in starches, starch and ash: starch samples of ash the residue obtained after weight. Original sample residue weight of sample weight or weight expressed as a percentage of the dry weight of the sample. Samples of ash at 900 ? high temperature until ashing sample ... The Crucible: determination of Platinum or other conditions of the affected material, capacity of 50mL. Dryer: has effectively

adequate drying agent and-perforated metal plate or porcelain. Ashing furnaces: device for controlling and regulating temperature, offers 900 incineration temperature of 25 c. Analytical balance. Electric hot plate or Bunsen. 3, crucible of analysis steps preparation: Crucible must

first wash with boiling dilute hydrochloric acid, then wash with a lot of water and then rinse with distilled water. Wash the Crucible within ashing furnace, heated at 900 to 25 ? 30min, and in the desiccator to cool to room temperature and then weighing,

}while(N);

while(Pop_Seqstack(s,&x))

{

if(x>=10)/*为了能转为十进制以上的*/

printf("%c",x+55);

else

printf("%d",x);

}

free(s);/*释放顺序栈*/

printf("\n");

return 1;

}

4.主函数

int main()

{

Seqstack *s;

int choice;

datatype x;

do{

printf("************************************************************ ****\n");

printf("1.置空栈 2.判栈空 3.入栈 4.出栈 5.取栈顶元素 6.遍历 7.退出\n");

printf("************************************************************ ****\n");

printf("请输入选择(1~7):");

scanf("%d",&choice);

getchar();

switch(choice)

{

case 1:s=Init_Seqstack();

if(s)

printf("置空栈成功!\n");break;

case 2:if(Empty_Seqstack(s))

printf("此为空栈.\n");

else

printf("此不为空栈.\n");;break;

case 3:printf("请输入一个整数:");

scanf("%d",&x);

if(Push_Seqstack(s,x))

printf("入栈成功.\n");

else

printf("栈已满,无法入栈.\n");;break;

case 4:if(Pop_Seqstack(s,&x)) acidity, mL.; M--calibration of the molar concentration of sodium hydroxide standard solution, moI/L; V--amount of the volume of sodium hydroxide standard solution, Ml; M--the weight of the sample, g. Such as poor meets the requirements, take the arithmetic mean of the second determination as a result. Results one decimal. 6, allowing differential analyst simultaneously or in quick succession for the second determination, the absolute value of the difference of the results. This value should be no more than 1.0. 1, definitions and principles for determination of ash in starches, starch and ash: starch samples of ash the residue obtained after weight. Original sample residue weight of sample weight or weight expressed as a percentage of the dry weight of the sample. Samples of ash at 900 ? high temperature until ashing sample ... The Crucible: determination of Platinum or other conditions of the affected material, capacity of 50mL. Dryer: has effectively adequate drying agent and-perforated metal plate or porcelain. Ashing furnaces: device for controlling and regulating temperature, offers 900 incineration temperature of 25 c. Analytical balance. Electric hot plate or Bunsen. 3, crucible of analysis steps preparation: Crucible must first wash with boiling dilute hydrochloric acid, then wash with a lot of water and then rinse with distilled water.

Wash the Crucible within ashing furnace, heated at 900 to 25 ? 30min, and in the desiccator to cool to room temperature and then weighing, printf("出栈成功,出栈元素为:%d\n",x);

else

printf("出栈失败,因栈为空.\n");break;

case 5:if(Top_Seqstack(s,&x))

printf("取栈顶元素成功,栈顶元素为:%d\n",x);

else

printf("取栈顶元素失败,因栈为空.\n");

break;

case 6:Printf_Seqstack(s);break;

case 7:printf("谢谢使用!\n");break;

default :printf("输入错误,请重新输入!\n");break;

}

}while(choice!=7);

return 0;

}

二.链栈

1.宏定义

#include

#include

#define datatype ****

2.结构体

typedef struct snode

{

datatype data;

struct snode *next;

}Stacknode,*Linkstack;

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)

acidity, mL.; M--calibration of the molar concentration of sodium hydroxide standard solution, moI/L; V--amount of the volume of sodium hydroxide standard solution, Ml; M--the weight of the sample, g. Such as poor meets the requirements, take the arithmetic mean of the second determination as a result. Results one decimal. 6, allowing differential analyst simultaneously or in quick succession for the second determination, the absolute value of the difference of the results. This value should be no more than 1.0. 1, definitions and principles for

determination of ash in starches, starch and ash: starch samples of ash the residue obtained after weight. Original sample residue weight of sample weight or weight expressed as a percentage of the dry weight of the sample. Samples of ash at 900 ? high temperature until ashing sample ... The Crucible: determination of Platinum or other conditions of the affected material, capacity of 50mL. Dryer: has effectively adequate drying agent and-perforated metal plate or porcelain. Ashing furnaces: device for controlling and regulating temperature, offers 900 incineration temperature of 25 c. Analytical balance. Electric hot plate or Bunsen. 3, crucible of analysis steps preparation: Crucible must

first wash with boiling dilute hydrochloric acid, then wash with a lot of water and then rinse with distilled water. Wash the Crucible within ashing furnace, heated at 900 to 25 ? 30min, and in the desiccator to cool to room temperature and then weighing,

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)

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)

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)

acidity, mL.; M--calibration of the molar concentration of sodium hydroxide standard solution, moI/L; V--amount of the volume of sodium hydroxide standard solution, Ml; M--the weight of the sample, g. Such as poor meets the requirements, take the arithmetic mean of the second determination as a result. Results one decimal. 6, allowing differential analyst simultaneously or in quick succession for the second determination, the absolute value of the difference of the results. This value should be no more than 1.0. 1, definitions and principles for determination of ash in starches, starch and ash: starch samples of ash the residue obtained after weight. Original sample residue weight of sample weight or weight expressed as a percentage of the dry weight of the sample. Samples of ash at 900 ? high temperature until ashing sample ... The Crucible: determination of Platinum or other conditions of the affected material, capacity of 50mL. Dryer: has effectively adequate drying agent and-perforated metal plate or porcelain. Ashing

furnaces: device for controlling and regulating temperature, offers 900 incineration temperature of 25 c. Analytical balance. Electric hot plate or Bunsen. 3, crucible of analysis steps preparation: Crucible must

first wash with boiling dilute hydrochloric acid, then wash with a lot of water and then rinse with distilled water. Wash the Crucible within ashing furnace, heated at 900 to 25 ? 30min, and in the desiccator to cool to room temperature and then weighing,

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)/*为了能转为十进制以上的*/

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");

printf("************************************************************ ****\n");

printf("请输入选择(1~7):");

acidity, mL.; M--calibration of the molar concentration of sodium hydroxide standard solution, moI/L; V--amount of the volume of sodium hydroxide standard solution, Ml; M--the weight of the sample, g. Such as poor meets the requirements, take the arithmetic mean of the second determination as a result. Results one decimal. 6, allowing differential analyst simultaneously or in quick succession for the second determination, the absolute value of the difference of the results. This value should be no more than 1.0. 1, definitions and principles for determination of ash in starches, starch and ash: starch samples of ash the residue obtained after weight. Original sample residue weight of sample weight or weight expressed as a percentage of the dry weight of the sample. Samples of ash at 900 ? high temperature until ashing sample ... The Crucible: determination of Platinum or other conditions of the affected material, capacity of 50mL. Dryer: has effectively adequate drying agent and-perforated metal plate or porcelain. Ashing furnaces: device for controlling and regulating temperature, offers 900 incineration temperature of 25 c. Analytical balance. Electric hot plate or Bunsen. 3, crucible of analysis steps preparation: Crucible must

first wash with boiling dilute hydrochloric acid, then wash with a lot

of water and then rinse with distilled water. Wash the Crucible within ashing furnace, heated at 900 to 25 ? 30min, and in the desiccator to cool to room temperature and then weighing,

scanf("%d",&choice);

getchar();

switch(choice)

{

case 1:top=Init_Linkstack();

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;

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);

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.主函数

acidity, mL.; M--calibration of the molar concentration of sodium hydroxide standard solution, moI/L; V--amount of the volume of sodium hydroxide standard solution, Ml; M--the weight of the sample, g. Such as poor meets the requirements, take the arithmetic mean of the second determination as a result. Results one decimal. 6, allowing differential analyst simultaneously or in quick succession for the second determination, the absolute value of the difference of the results. This

value should be no more than 1.0. 1, definitions and principles for determination of ash in starches, starch and ash: starch samples of ash the residue obtained after weight. Original sample residue weight of sample weight or weight expressed as a percentage of the dry weight of the sample. Samples of ash at 900 ? high temperature until ashing sample ... The Crucible: determination of Platinum or other conditions of the affected material, capacity of 50mL. Dryer: has effectively adequate drying agent and-perforated metal plate or porcelain. Ashing furnaces: device for controlling and regulating temperature, offers 900 incineration temperature of 25 c. Analytical balance. Electric hot plate or Bunsen. 3, crucible of analysis steps preparation: Crucible must

first wash with boiling dilute hydrochloric acid, then wash with a lot of water and then rinse with distilled water. Wash the Crucible within ashing furnace, heated at 900 to 25 ? 30min, and in the desiccator to cool to room temperature and then weighing,

相关主题
相关文档
最新文档