1.
#include <stdio.h>
#include <stdlib.h>
struct Stack{
NODETYPE* arr;
int len;
int top;
};
struct Squeue {
NODETYPE* arr;
int front, rear;
};
struct DLink {
NODETYPE data;
DLink* next;
DLink* pre;
};
struct Link {
NODETYPE data;
Link* next;
};
bool matchBracket(LinkQueue *lq, Stack *s) {
char letterQ, letterS;
bool isEmpty(LinkQueue *);
bool deQueue(LinkQueue *, char *);
bool push(Stack *, char);
char top(Stack *);
bool pop(Stack *);
bool empty(Stack *);
while (!isEmpty(lq)) {
deQueue(lq, &letterQ);
if (letterQ == '(' || letterQ == '{' || letterQ == '[') {
push(s, letterQ);
}
else {
if (empty(s)) {
return false;
}
letterS = top(s);
pop(s);
switch (letterQ) {
case ')': if (letterS != '(')return false; break;
case ']': if (letterS != '[' ) return false; break;
case '}': if (letterS != '{' ) return false; break;
default:break;
}
}
}
if (empty(s)) {
return true;
}
else {
return false;
}
}
int main() {
int n;
char letter;
struct LinkQueue *lq;
struct Stack *s;
LinkQueue *create();
bool enQueue(LinkQueue *, char);
void printQ(LinkQueue *);
Stack *createStack(int);
lq = create();
printf("请输入栈的大小:n=");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("请输入第%d个括号:",i+1);
scanf("\n%c", &letter);
enQueue(lq, letter);
}
printQ(lq);
printf("\n");
s = createStack(n);
if (matchBracket(lq, s)) {
printf("该算术表达式配对");
}
else {
printf("该算术表达式不配对");
}
return 0;
}
2.
#include <stdio.h>
#include <stdlib.h>
struct Stack{
NODETYPE* arr;
int len;
int top;
};
void trainArrange(char *arrA,char *arrB,Stack *s) {
int i = 0, j = 0;
char c;
bool push(Stack *,char );
char top(Stack *);
bool pop(Stack *);
bool empty(Stack *);
while (i<10) {
if (arrA[i]=='H') {
push(s,arrA[i]);
}
else {
arrB[j++] = arrA[i];
}
i++;
}
while (!empty(s)) {
c = top(s);
pop(s);
arrB[j++] = c;
}
}
int main() {
char arrA[10] = {'H','S','S','H','H','S','S','S','H','H' };
char arrB[10] = { 0 };
Stack *createStack(int);
Stack *s;
s = createStack(10);
trainArrange(arrA,arrB,s);
for (int i = 0; i < 10;i++) {
printf("%c ",arrB[i]);
}
return 0;
}
3.
#include <stdio.h>
#include <stdlib.h>
struct Recursion {
int no;
int val;
};
struct Stack {
Recursion *arr;
int len;
int top;
};
int getP(Stack *s,int n,int x) {
if (n==0) {
return 1;
}
int fv1 = 1, fv2 = 2 * x;
for (int i = n; i >= 2;i--) {
s->top++;
s->arr[s->top].no = i;
}
while (s->top>=0) {
s->arr[s->top].val = 2 * x*fv2 - 2 * (s->arr[s->top].no - 1)*fv1;
fv1 = fv2;
fv2 = s->arr[s->top].val;
s->top--;
}
return fv2;
}
int main() {
Stack *s;
Stack *createStack(int);
bool push(Stack *,Recursion *);
bool full(Stack *);
bool empty(Stack *);
bool top(Stack *);
bool pop(Stack *);
int n, x;
int p;
printf("请输入n和x: \n");
printf("n=");
scanf("%d",&n);
printf("x=");
scanf("%d", &x);
s = createStack(n);
p=getP(s,n,x);
printf("%d",p);
return 0;
}
4.
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20
struct Squeue {
NODETYPE* arr;
int front, rear;
};
void manageCar(int* arrCar, int* arrArrange, Squeue* sq) {
int i = 0, passengerCar = 0, j = 0;
int c;
bool enQueue(Squeue*, int, int);
bool isEmpty(Squeue*);
bool deQueue(Squeue*, int*, int);
for (; arrCar[i] == 1 || arrCar[i] == 2; i++) {
if (arrCar[i] == 2 && passengerCar < 4) {
enQueue(sq, arrCar[i],MAXSIZE);
}
else {
if (passengerCar == 4) {
if (!isEmpty(sq)) {
deQueue(sq,&c,MAXSIZE);
arrArrange[j++] = c;
passengerCar = 0;
}
}
arrArrange[j++] = arrCar[i];
passengerCar++;
}
}
while (!isEmpty(sq)) {
deQueue(sq, &c, MAXSIZE);
arrArrange[j++] = c;
}
}
int main() {
int arrCar[] = { 2,1,2,1,1,1,1,2,2,2,1,1 };
int arrArrange[20] = { 0 };
Squeue* sq = (Squeue*)malloc(sizeof(Squeue));
Squeue* createQueue(int);
sq = createQueue(MAXSIZE);
manageCar(arrCar, arrArrange, sq);
for (int i = 0; i < 10; i++) {
printf("%d ", *(arrArrange + i));
}
return 0;
}