C语言栈的各种基本运算代码

合集下载

数据结构(C语言)第3章 栈和队列

数据结构(C语言)第3章 栈和队列

Data Structure
2013-8-6
Page 13
栈的顺序存储(顺序栈)
利用一组地址连续的存储单元依次存放自栈底到栈顶的数 据元素。 结构定义: #define STACK_INIT_SIZE 100; // 存储空间初始分配量 #define STACKINCREMENT 10; // 存储空间分配增量 typedef struct { SElemType *base; // 存储空间基址 SElemType *top; // 栈顶指针 int stacksize; // 当前已分配的存储空间,以元素位单位 } SqStack;
解决方案2:
顺序栈单向延伸——使用一个数组来存储两个栈
Data Structure 2013-8-6 Page 21
两栈共享空间 两栈共享空间:使用一个数组来存储两个栈,让一个 栈的栈底为该数组的始端,另一个栈的栈底为该数组 的末端,两个栈从各自的端点向中间延伸。
Data Structure
2013-8-6
链栈需要加头结点吗? 链栈不需要附设头结点。
Data Structure
2013-8-6
Page 27
栈的链接存储结构及实现
Data Structure
2013-8-6
Page 11
GetTop(S, &e) 初始条件:栈 S 已存在且非空。 操作结果:用 e 返回S的栈顶元素。 Push(&S, e) 初始条件:栈 S 已存在。 操作结果:插入元素 e 为新的栈顶元素。 Pop(&S, &e) 初始条件:栈 S 已存在且非空。 操作结果:删除 S 的栈顶元素,并用 e 返回其值。
Data Structure

10个经典的C语言基础算法及代码

10个经典的C语言基础算法及代码

10个经典的C语言基础算法及代码1.冒泡排序算法冒泡排序是一种简单但效率较低的排序算法,在每一轮遍历中比较相邻的两个元素,如果顺序不正确则交换它们,直到整个数组有序为止。

```cvoid bubbleSort(int arr[], int n)for (int i = 0; i < n-1; i++)for (int j = 0; j < n-1-i; j++)if (arr[j] > arr[j+1])int temp = arr[j];arr[j] = arr[j+1];arr[j+1] = temp;}}}```2.选择排序算法选择排序是一种简单直观的排序算法,它每次从待排序的数组中选择最小(或最大)的元素,并放到已排序的数组末尾。

```cvoid selectionSort(int arr[], int n)for (int i = 0; i < n-1; i++)int min_index = i;for (int j = i+1; j < n; j++)if (arr[j] < arr[min_index])min_index = j;}}int temp = arr[i];arr[i] = arr[min_index];arr[min_index] = temp;}```3.插入排序算法插入排序的基本思想是将数组分为已排序和未排序两部分,每次将未排序的元素插入到已排序的合适位置。

```cvoid insertionSort(int arr[], int n)for (int i = 1; i < n; i++)int key = arr[i];int j = i - 1;while (j >= 0 && arr[j] > key)arr[j+1] = arr[j];j--;}arr[j+1] = key;}```4.快速排序算法快速排序使用分治法的思想,每次选择一个基准元素,将小于基准的元素放到左边,大于基准的元素放到右边,然后递归地对左右两个子数组进行排序。

c语言栈计算表达式

c语言栈计算表达式

c语言栈计算表达式在计算机科学中,栈是一种非常重要的数据结构,被广泛应用于编译器、操作系统、网络通信等领域。

在本文中,我们将探讨如何使用C语言实现栈来计算表达式。

表达式是由操作数、操作符和括号组成的数学式子,例如:3 + 4 * 2 / (1 - 5)。

在计算表达式时,我们需要遵循一定的计算规则,如乘除法优先于加减法,括号内的计算优先于括号外的计算等。

我们可以使用栈来实现对表达式的计算。

具体步骤如下:1. 定义两个栈:一个操作数栈和一个操作符栈。

2. 从左到右遍历表达式的每一个字符,如果是数字则将其压入操作数栈;如果是操作符则将其压入操作符栈,并根据运算规则进行计算。

3. 在遍历完成后,操作符栈中可能还有未计算的操作符,需要继续计算,直到操作符栈为空。

4. 最终操作数栈中只剩下一个数,即为表达式的计算结果。

下面是一段示例代码,用于计算简单的表达式:```#include <stdio.h>#include <stdlib.h>#include <ctype.h>#define MAX_SIZE 100typedef struct {int data[MAX_SIZE];int top;} Stack;void initStack(Stack *s) {s->top = -1;}void push(Stack *s, int item) { if (s->top == MAX_SIZE - 1) { printf('Stack Overflow');exit(1);}s->data[++s->top] = item;}int pop(Stack *s) {if (s->top == -1) {printf('Stack Underflow');exit(1);}return s->data[s->top--];}int isEmpty(Stack *s) {return s->top == -1;}int isFull(Stack *s) {return s->top == MAX_SIZE - 1;}int peek(Stack *s) {return s->data[s->top];}int evaluate(char *expr) {Stack operandStack, operatorStack; initStack(&operandStack);initStack(&operatorStack);int i = 0;while (expr[i] != '0') {if (isdigit(expr[i])) {int num = 0;while (isdigit(expr[i])) {num = num * 10 + (expr[i] - '0'); i++;}push(&operandStack, num);}else if (expr[i] == '(') {push(&operatorStack, expr[i]);i++;}else if (expr[i] == ')') {while (!isEmpty(&operatorStack) && peek(&operatorStack) != '(') {int op2 = pop(&operandStack);int op1 = pop(&operandStack);char op = pop(&operatorStack);int result;switch (op) {case '+':result = op1 + op2;break;case '-':result = op1 - op2;break;case '*':result = op1 * op2;break;case '/':result = op1 / op2;break;}push(&operandStack, result);}pop(&operatorStack);i++;}else if (expr[i] == '+' || expr[i] == '-' || expr[i] == '*' || expr[i] == '/') {while (!isEmpty(&operatorStack) &&peek(&operatorStack) != '(' &&((expr[i] == '*' || expr[i] == '/') || (expr[i] == '+' || expr[i] == '-') &&(peek(&operatorStack) == '*' || peek(&operatorStack) == '/'))) {int op2 = pop(&operandStack);int op1 = pop(&operandStack);char op = pop(&operatorStack);int result;switch (op) {case '+':result = op1 + op2;break;case '-':result = op1 - op2;break;case '*':result = op1 * op2;break;case '/':result = op1 / op2;break;}push(&operandStack, result); }push(&operatorStack, expr[i]); i++;}else {i++;}}while (!isEmpty(&operatorStack)) { int op2 = pop(&operandStack);int op1 = pop(&operandStack);char op = pop(&operatorStack);int result;switch (op) {case '+':result = op1 + op2;break;case '-':result = op1 - op2;break;case '*':result = op1 * op2;break;case '/':result = op1 / op2;break;}push(&operandStack, result);}return pop(&operandStack);}int main() {char expr[MAX_SIZE];printf('Enter an expression: ');fgets(expr, MAX_SIZE, stdin);int result = evaluate(expr);printf('Result = %d', result);return 0;}```在这段代码中,我们定义了一个栈结构体,包含了栈的数据和栈顶指针。

C语言中栈的基本操作

C语言中栈的基本操作

C语言中栈的基本操作栈(Stack)是一种遵循“后进先出”(LIFO)原则的数据结构,具有以下几个基本操作:入栈(Push)、出栈(Pop)、判断栈是否为空(Empty)以及获取栈顶元素(Top)。

下面将详细介绍这些基本操作。

1. 入栈(Push):将一个元素添加到栈的顶部。

入栈操作分为两个步骤:(1)判断栈是否已满,如果已满则无法再添加元素;(2)若栈不满,则将元素添加到栈的顶部,并更新栈顶指针。

具体实现代码如下:```void push(Stack *s, int item)if (is_full(s))printf("Stack is full, cannot push more elements.\n");return;}s->top++;s->data[s->top] = item;}```2. 出栈(Pop):将栈顶元素移除,并返回该元素的值。

出栈操作也有两个步骤:(1)判断栈是否为空,如果为空则无法进行出栈操作;(2)若栈不为空,则将栈顶元素移除,并更新栈顶指针。

具体实现代码如下:```int pop(Stack *s)int item;if (is_empty(s))printf("Stack is empty, cannot pop any elements.\n");return -1; // 指定一个特定的返回值来表示错误}item = s->data[s->top];s->top--;return item;}```3. 判断栈是否为空(Empty):判断栈是否为空分为两种情况,一种是根据栈顶指针进行判断,另一种是根据数据数量进行判断。

(1)判断栈顶指针是否为-1,若为-1则说明栈为空;(2)若栈内数据数量为0,则栈为空。

具体实现代码如下:```int is_empty(Stack *s)return s->top == -1; // 栈顶指针为-1表示栈为空}```4. 获取栈顶元素(Top):返回栈顶元素的值,但不对栈做任何修改。

c语言计算的代码

c语言计算的代码

c语言计算的代码使用C语言进行计算的代码在计算机编程中,C语言是一种非常常用的编程语言,它具有高效、灵活和可移植等特点。

在C语言中,我们可以使用各种算法和公式来进行各种数学计算,下面就来介绍一些常见的使用C语言进行计算的代码。

一、基本的四则运算在C语言中,我们可以使用加减乘除等基本算术运算符来进行四则运算。

例如,我们可以使用以下代码来实现两个数的相加运算:```#include <stdio.h>int main() {int a = 10;int b = 20;int sum = a + b;printf("两个数的和为:%d\n", sum);return 0;}```二、求平方根在数学中,我们经常需要求一个数的平方根。

在C语言中,我们可以使用math.h头文件中的sqrt()函数来实现求平方根的功能。

下面是一个简单的例子:```#include <stdio.h>#include <math.h>int main() {double num = 16.0;double result = sqrt(num);printf("16的平方根为:%f\n", result);return 0;}```三、求阶乘阶乘是数学中常见的一个概念,表示从1到某个数之间所有整数的乘积。

在C语言中,我们可以使用循环结构来计算阶乘。

下面是一个计算阶乘的例子:```#include <stdio.h>int main() {int n = 5;int result = 1;for (int i = 1; i <= n; i++) {result *= i;}printf("5的阶乘为:%d\n", result);return 0;}```四、求最大公约数最大公约数是两个数中最大的能够同时整除两个数的数。

C语言基础简单的数学运算的代码

C语言基础简单的数学运算的代码

C语言基础简单的数学运算的代码#include <stdio.h>int main() {// 定义并初始化变量int num1 = 10;int num2 = 5;// 加法运算int sum = num1 + num2;printf("加法运算结果:%d\n", sum);// 减法运算int difference = num1 - num2;printf("减法运算结果:%d\n", difference);// 乘法运算int product = num1 * num2;printf("乘法运算结果:%d\n", product);// 除法运算float quotient = (float)num1 / num2;printf("除法运算结果:%.2f\n", quotient);// 求余运算int remainder = num1 % num2;printf("求余运算结果:%d\n", remainder);return 0;}以上是一个简单的C语言程序,实现了基本的数学运算功能。

程序运行后,会输出每个数学运算的结果。

接下来我会逐行解释代码的含义和执行过程。

首先,在程序的开头我们使用了#include <stdio.h>这行代码,这是为了包含C语言标准库中的输入输出函数,以便后续可以使用printf()函数打印结果。

接着,在main()函数中,我们定义并初始化了两个整型变量num1和num2,分别赋值为10和5。

这两个变量代表了我们要进行数学运算的两个操作数。

然后,我们使用加法运算将num1和num2相加得到sum,并使用printf()函数打印出加法运算的结果。

接着,我们使用减法运算将num1减去num2得到difference,并使用printf()函数打印出减法运算的结果。

c语言五则运算代码

c语言五则运算代码

c语言五则运算代码C语言是一种广泛应用于计算机编程的编程语言,它具有灵活、高效的特点,常被用于进行各种数学计算和表达式求值。

本文将围绕C语言的五则运算(加法、减法、乘法、除法、取余)展开,探讨其在实际编程中的应用。

一、加法运算加法运算是最基本的数学运算之一,在C语言中使用加号(+)来表示。

在实际编程中,加法运算常用于计算两个数的和。

例如,我们可以编写一个程序,实现输入两个数并计算它们的和:```c#include <stdio.h>int main() {int a, b, sum;printf("请输入两个整数:\n");scanf("%d %d", &a, &b);sum = a + b;printf("它们的和为:%d\n", sum);return 0;}```二、减法运算减法运算是计算两个数之差的操作,在C语言中使用减号(-)来表示。

我们可以编写一个程序,实现输入两个数并计算它们的差:```c#include <stdio.h>int main() {int a, b, difference;printf("请输入两个整数:\n");scanf("%d %d", &a, &b);difference = a - b;printf("它们的差为:%d\n", difference);return 0;}```三、乘法运算乘法运算是计算两个数的积的操作,在C语言中使用星号(*)来表示。

我们可以编写一个程序,实现输入两个数并计算它们的积:```c#include <stdio.h>int main() {int a, b, product;printf("请输入两个整数:\n");scanf("%d %d", &a, &b);product = a * b;printf("它们的积为:%d\n", product);return 0;}```四、除法运算除法运算是计算两个数的商的操作,在C语言中使用斜杠(/)来表示。

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

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

C语言堆栈和队列函数大全一.顺序栈1.宏定义#include<stdio.h>#include<stdlib.h>#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 obtainedafter weight. Original sample residue weight of sample weight or weight expressed as a percentage of the dry weight of the sample. Samples ofash 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 effectivelyadequate 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 mustfirst 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);elseprintf("%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");elseprintf("此不为空栈.\n");;break;case 3:printf("请输入一个整数:");scanf("%d",&x);if(Push_Seqstack(s,x))printf("入栈成功.\n");elseprintf("栈已满,无法入栈.\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);elseprintf("出栈失败,因栈为空.\n");break;case 5:if(Top_Seqstack(s,&x))printf("取栈顶元素成功,栈顶元素为:%d\n",x);elseprintf("取栈顶元素失败,因栈为空.\n");break;case 6:Printf_Seqstack(s);break;case 7:printf("谢谢使用!\n");break;default :printf("输入错误,请重新输入!\n");break;}}while(choice!=7);return 0;}二.链栈1.宏定义#include<stdio.h>#include<stdlib.h>#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 fordetermination 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 mustfirst 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. Ashingfurnaces: 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 mustfirst 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);elseprintf("%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 mustfirst wash with boiling dilute hydrochloric acid, then wash with a lotof 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");elseprintf("此不为空栈.\n");;break;case 3:printf("请输入一个整数:");scanf("%d",&x);if(Push_Linkstack(top,x))printf("入栈成功.\n");elseprintf("栈已满,无法入栈.\n");;break;case 4:if(Pop_Linkstack(top,&x))printf("出栈成功,出栈元素为:%d\n",x);elseprintf("出栈失败,因栈为空.\n");break;case 5:if(Top_Linkstack(top,&x))printf("取栈顶元素成功,栈顶元素为:%d\n",x);elseprintf("取栈顶元素失败,因栈为空.\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. Thisvalue 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 mustfirst 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. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
}
int main()
{
ElemType e;
SqStack *s;
printf("初始化栈s\n ");
InitStack(s);
printf( "栈S为%s\n ", (StackEmpty(s)? "空": "非空"));
printf("一次进栈元素a,b,c,d,e;\n");
Push(s,'a');
DispStack(s);
GetTop(s,e);
printf("显示栈顶元素:%c",e);
printf("\n");
printf("输出出栈序列:");
while (!StackEmpty(s))
{
Pop(s,e);
printf("%c",e);
}
printf("\n");
printf("栈S为%s\n ",(StackEmpty(s)? "空": "非空"));
{
return(s->top+1);
}
//判断栈是否为空
int StackEmpty(SqStack *s)
{
return(s->top==-1);
}
//进栈
int Push(SqStack *&s,ElemType e)
{
if(s->top==MaxSize-1)
return 0;
s->top++;
Push(s,'b');
Push(s,'c');
Push(s,'d');
Push(s,'e');
printf("栈S为%s\n ", (StackEmpty(s)? "空": "非空"));
printf("输出栈长度S=%d:\n",StackLength(s));
printf("输出从栈顶到栈底的元素:");
s->data[s->top]=e;
return 1;
}
//出栈
int Pop(SqStack *&s,ElemType &e)
{
if(s->top==-1)
return 0;
e=s->data[s->top];
s->top--;
return 1;
}
//取出栈顶元素
int GetTop(SqStack *s, ElemType &e)
题目:
实现顺序栈的各种基本运算,并在此基础上设计一个主程序完成如下功能:
(1)初始化栈S;
(2)判断栈S是否为空;
(3)依次使元素a, b, c, d, e进栈;
(4)判断栈S是否为空;
(5)输出栈的长度;
(6)输出从栈顶到栈底元素;
(7)输出出栈序列;
(8)判断顺序栈S是否为空;
(9)释放栈
代码;
void InitStack(SqStack* &s)
{
s=(SqStack*)malloc(sizeof(SqStack));
s->top=-1;
}
//销毁栈
void ClearStack(SqStack *&s)
{
free(s);
}
//求栈的长度
int StackLength(SqStack *s)
printf("销毁栈\n");
ClearStack(s);
returnp==-1)
return 0;
e=s->data[s->top];
return 1;
}
//显示栈中元素
void DispStack(SqStack *s)
{
int i;
for(i=s->top;i>=0;i--)
printf("%c ",s->data[i]);
printf("\n");
#include<stdio.h>
#include<malloc.h>
#define MaxSize 50
typedef char ElemType;
typedef struct
{
ElemType data[MaxSize];
int top; //栈顶指针
}SqStack;
//顺序栈顶类型定义
//初始化栈
相关文档
最新文档