已知两个栈共享存储空间,试编写算法initstack(&S) push(&S, i, e) pop(&S, i, &e) StackLength(S,i) 并在main()中调用函数测试。
//顺序栈基本运算算法 #include <stdio.h> #include <malloc.h> #define MaxSize 100 typedef char ElemType; typedef struct { ElemType data[MaxSize]; int top; //栈指针 } SqStack; //顺序栈类型 void InitStack(SqStack *&s) { s=(SqStack *)malloc(sizeof(SqStack)); s->top=-1; } void DestroyStack(SqStack *&s) { free(s); } bool StackEmpty(SqStack *s) { return(s->top==-1); } bool Push(SqStack *&s,ElemType e) { if (s->top==MaxSize-1) //栈满的情况,即栈上溢出 return false; s->top++; s->data[s->top]=e; return true; } bool Pop(SqStack *&s,ElemType &e) { if (s->top==-1) //栈为空的情况,即栈下溢出 return false; e=s->data[s->top]; s->top--; return true; } bool GetTop(SqStack *s,ElemType &e) { if (s->top==-1) //栈为空的情况,即栈下溢出 return false; e=s->data[s->top]; return true; } int main() { ElemType e ; SqStack *s ; printf("顺序栈s的基本运算如下:\n"); printf("(1)初始化栈s\n") ; InitStack(s) ; printf("(2)栈为%s\n",(StackEmpty(s)?"空":"非空")) ; printf("(3)依次进栈元素a,b,c,d,e\n"); Push(s,'a') ; Push(s,'b') ; Push(s,'c') ; Push(s,'d') ; Push(s,'e') ; printf("(4)栈为%s\n",(StackEmpty(s)?"空":"非空")) ; printf("(5)出栈序列:"); while (!StackEmpty(s)) { Pop(s,e) ; printf("%c",e) ; } printf("\n") ; printf("(6)栈为%s\n",(StackEmpty(s)?"空":"非空")) ; printf("(7)释放栈\n") ; DestroyStack(s) ; return 1 ; } #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #define MAXSIZE 100 #define ERROR 0 #define OK 1 #define TRUE 1 #define FALSE 0 typedef int EleType; typedef int Status; //2栈共享存储空间 数据结构 typedef struct SeqDoubleStack { EleType data[MAXSIZE]; int top1; int top2; }SeqDoubleStack; /* 弹栈 */ Status pop(SeqDoubleStack* stack,EleType *e,int stackNum) { if (!stack || !e)//空指针 { return ERROR; } //栈不为空的情况,根据栈编号,pop相应的栈的元素。 //栈1空栈判断stack->top1!=-1 栈2空栈判断stack->top2 != MAXSIZE if (stackNum == 1&&stack->top1!=-1) { *e = stack->data[stack->top1]; stack -> top1--; return OK; } if (stackNum == 2 && stack->top2 != MAXSIZE) { *e = stack->data[stack->top2]; stack->top2++; return OK; } //栈为空或者编号不对 return ERROR; } /* 压栈操作 */ Status push(SeqDoubleStack* stack, EleType e, int stackNum) { if (!stack)//空指针 { return ERROR; } //栈没有满的情况,根据栈编号,push相应的栈的元素。 //栈未满的条件都是stack->top1+1 < stack->top2 if (stackNum == 1 && stack->top1+1 < stack->top2) { stack->top1++; stack->data[stack->top1] = e; return OK; } if (stackNum == 2 && stack->top1 + 1 < stack->top2) { stack->top2--; stack->data[stack->top2] = e; return OK; } //栈已满或者编号不对 return ERROR; } /* 展示2个栈的元素 */ void showStack(SeqDoubleStack* stack) { for (int i = 0; i <= stack->top1; i++) { printf("%d,",stack->data[i]); } for (int j = stack->top2; j <= MAXSIZE - 1; j++) { printf("%d,", stack->data[j]); } printf("\n"); } /* 清空栈元素 */ Status clearSeqDoubleStack(SeqDoubleStack* stack) { if (!stack) { return ERROR; } stack->top1 = -1; stack->top2 = MAXSIZE; return OK; } /* 初始化栈 */ Status initSeqDoubleStack(SeqDoubleStack* stack) { if (!stack) { return ERROR; } stack->top1 = -1; stack->top2 = MAXSIZE; return OK; } /* 栈元素个数 */ int getLengthSeqDoubleStack(SeqDoubleStack* stack) { if (!stack) { return ERROR; } //stack->top1+1;//栈1元素个数 //MAXSIZE - stack->top2;//栈2元素个数 return stack->top1 + 1 + MAXSIZE - stack->top2; } int main(int argc, char *argv[]) { SeqDoubleStack stack; //初始化 initSeqDoubleStack(&stack); //压栈 push(&stack, 1, 1); push(&stack, 2, 1); push(&stack, 3, 1); push(&stack, 4, 1); push(&stack, 5, 1); push(&stack, 9, 2); push(&stack, 8, 2); push(&stack, 7, 2); push(&stack, 6, 2); puts("展示元素:"); //显示元素 showStack(&stack); printf("元素个数:%d\n", getLengthSeqDoubleStack(&stack)); EleType e1; EleType e2; //弹栈 pop(&stack, &e1,1); printf("pop:%d,",e1); pop(&stack,&e2,2); printf("pop:%d\n", e2); puts("展示元素:"); showStack(&stack); //清空 clearSeqDoubleStack(&stack); printf("\n"); return 0; }试编写算法判断一个整数是否按值对称,如:12321为按值对称。
//法一 #include<stdio.h> void main() { int n,m =0,s,r; printf("Input data is: "); scanf("%d",&n); s = n; while(s != 0) { r = s %10; //从低位到高位逐一分离 m = 10*m+r; //重新组合一整数 s = s/10; //求其商 } if(m==n) printf("yes\n"); else printf("no\n"); } //法二 #include<stdio.h> #define N 10 void main() { int n,x,m,i,k,j; int a[N]; printf("Input x = "); scanf("%d",&x); m=x; //把输入值的各个数字存入到数组上 for(i=0;i<N,m!=0;i++) { //取余取最低位 n = m%10; a[i] = n; //通过除法舍弃最低位 m /=10; } //判断是否为回文 for(j=0,k=i-1;j<k;j++,k--) { if(a[j]==a[k]) continue; else break; } if(j>=k) printf("\t%d为按值对称! ",x); else printf("\t%d不是按值对称! ",x); } //法三:栈 #include <stdio.h> #include <malloc.h> #define MaxSize 50 typedef int ElemType; typedef struct { ElemType data[MaxSize]; int top; }SqStack; void InitStack(SqStack *&s) /*初始化栈*/ { s=(SqStack *)malloc(sizeof(SqStack)); s->top=-1; } int Push(SqStack *&s,ElemType e) /*进栈*/ { if(s->top==MaxSize-1) /*栈满的情况*/ return 0; s->top++; 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 symmetry(int n) /*判断正整数是否按值对称*/ { int i,j,k=0,a[10]; ElemType e=0; SqStack *sa; InitStack(sa); // do /*将整数所有位数进栈*/ { j=n%10; //取当前数最后一位 i=n/10; //取当前数除了最后一位,其他位数 a[k]=j; //最后一位存入数组 Push(sa,j); //最后一位入栈 n=i; k++; }while(i>0); for(i=0;i<k/2;i++) { Pop(sa,e); if(e!=a[i]) return 0; } return 1; } void main() /*主函数*/ { int n,i; printf("请输入一个整数:\n"); scanf("%d",&n); if(symmetry(n)) printf("该整数按值对称\n"); else printf("该整数不按值对称\n"); }迭代法
#include"stdlib.h" #include"math.h" #include<iostream> using namespace std; int main() { int i; int a[40]; //声明存放斐波那契数列的数组 a[0] = 0; a[1] = 1; for (i = 2; i < 40; ++i) { a[i] = a[i - 1] + a[i - 2]; //进行迭代运算 cout << a[i] << endl; } }递归法
#include"stdlib.h" #include"math.h" #include<iostream> using namespace std; int fbi(int i) { if (i < 2) return i == 0 ? 0 : 1; //判断i的范围 else return fbi(i - 1) + fbi(i - 2); //递归运算 } int main() { int i; for (i = 0; i < 40; ++i) { cout << fbi(i) << endl; } }