#include<bits/stdc++.h>
using namespace std;
typedef int ElemType;
#define MAXSIZE 8 //栈初始化大小
typedef struct SNode { //栈的顺序存储
ElemType* data;
int MaxSize;
int top; //第一个栈
int top2; //第二个栈
}*SeqStack;
typedef struct StackNode { //栈的链式存储
ElemType data;
struct StackNode* next;
}*LinkStack,StackNode;
typedef struct Queue{ //循环队列
ElemType * data;
int front;//front下一个位置开始存储第一个元素
int rear;
int MaxSize;
}*SqQueue,Queue;
/*链式队列结点*/
typedef struct LinkNode{
ElemType data;
struct LinkNode* next;
}LinkNode;
/*链式队列*/
typedef struct LinkQueue{
LinkNode* front, *rear;//队列的对头和队尾指针
}LinkQueue;
SeqStack InitStack();//栈的初始化
bool IsFull(SeqStack s);
bool IsEmpty(SeqStack s);
void Push(SeqStack s, ElemType e);
void Pop(SeqStack s, ElemType* e);
bool GetTop(SeqStack s, ElemType* e);
void Push_D(SeqStack s, ElemType e, int Tag); //共享栈Tag==1,插入第一个栈,2插入第二个栈
ElemType Pop_D(SeqStack s, int Tag);
LinkStack InitStack_2(LinkStack s);//栈的链式存储
void Push_2(LinkStack s, ElemType e);
bool IsEmpty_2(LinkStack s);
void Pop_2(LinkStack s);
//循环队列
SqQueue CreateQueue();
bool IsFull(SqQueue q);
bool AddQ(SqQueue q, ElemType e);
bool IsEmpty(SqQueue q);
ElemType DeleteQ(SqQueue q);
//队列的链式存储
void InitQueue(LinkQueue* Q);
void EnQueue(LinkQueue* Q, ElemType e);
void DeQueue(LinkQueue* Q, LinkNode* e);
bool IsEmptyQ(LinkQueue* Q);
// P92 ,03
double p(int n,int x);
double p1(int n,int x);
int main() {
cout<<p(4,3)<<endl<<p1(4,3);
//
// LinkQueue Q;
// InitQueue(&Q);
//
printf("fs");
// for(int i = 0; i < 5; i++){
// EnQueue(&Q,i);
// }
// printf("%d ",Q.front->next->data);
// LinkNode e;
// while(!IsEmptyQ(&Q)){
// DeQueue(&Q,&e);
// printf("%d ",e.data);
// }
// SqQueue Q = CreateQueue();
// for(int i = 0; i < 6; i++){
// AddQ(Q,i);
// }
// for(int i = 0; i < 6; i++){
// if(!IsEmpty(Q)){
// printf("%d ",DeleteQ(Q));
// }
// }
// LinkStack s = InitStack_2(s);
// for (int i = 0; i < 5; i++) {
// Push_2(s, i);
// }
// SeqStack s = InitStack();
// for(int i = 0; i < 5; i++){
// Push(s,i);
// }
// Push_D(s,6,2);
// for(int i = 0; i < 5; i++){
// printf("%d ",s->data[i]);
// }
printf("%d",s->data[7]);
// printf("%d",Pop_D(s,1));
// StackNode* p = s->next;
// while(p!=NULL){
// printf("%d ",p->data);
// p = p->next;
// }
// int e;
// Pop(s, &e);
// printf("\n%d", e);
return 0;
}
SeqStack InitStack() {
SeqStack s = (SeqStack)malloc(sizeof(struct SNode));
s->data = (ElemType*)malloc(sizeof(ElemType) * MAXSIZE);
s->MaxSize = MAXSIZE;
s->top = -1;
s->top2 = MAXSIZE;
}
bool IsFull(SeqStack s) {
return (s->top == s->MaxSize - 1);
}
bool IsEmpty(SeqStack s) {
return (s->top == -1);
}
void Push(SeqStack s, ElemType e) {
if (IsFull(s))return;
s->data[++(s->top)] = e;
}
void Pop(SeqStack s, ElemType* e) {
if (IsEmpty(s))return;
*e = s->data[(s->top)--];
}
bool GetTop(SeqStack s, ElemType* e) {
if (IsEmpty(s))return false;
*e = s->data[s->top];
return true;
}
void Push_D(SeqStack s, ElemType e, int Tag) {
if (s->top2 - s->top == 1) {
return;
}
else {
if (Tag == 1) {
s->data[++(s->top)] = e;
}
else if(Tag == 2){
s->data[--(s->top2)] = e;
}
}
}
ElemType Pop_D(SeqStack s, int Tag) {
if (Tag == 1) {
if (s->top == -1)return NULL;
else {
return s->data[(s->top)--];
}
}else if(Tag == 2){
if (s->top2 == MAXSIZE)return NULL;
else {
return s->data[(s->top2)++];
}
}
}
LinkStack InitStack_2(LinkStack s) {
s = (StackNode*)malloc(sizeof(struct StackNode));
s->next = NULL;
return s;
}
void Push_2(LinkStack s, ElemType e) {
StackNode* p = (StackNode*)malloc(sizeof(struct StackNode));
p->data = e;
p->next = s->next;
s->next = p;
}
bool IsEmpty_2(LinkStack s) {
return (s->next == NULL);
}
void Pop_2(LinkStack s) {
if (IsEmpty_2(s))return;
StackNode* p;
p = s->next;
s->next = p->next;
free(p);
}
SqQueue CreateQueue() {
SqQueue q = (Queue*)malloc(sizeof(struct Queue));
q->data = (ElemType*)malloc(MAXSIZE * sizeof(ElemType));
q->front = q->rear = 0;
q->MaxSize = MAXSIZE;
return q;
}
bool IsFull(SqQueue q) {
return ((q->rear + 1) % q->MaxSize == q->front); //队列长度:(q.rear+MaxSize-q.front)%MaxSize
}
bool AddQ(SqQueue q, ElemType e) {
if (IsFull(q))return false;
q->rear = (q->rear + 1) % q->MaxSize;
q->data[q->rear] = e;
return true;
}
bool IsEmpty(SqQueue q) {
return (q->front == q->rear);
}
ElemType DeleteQ(SqQueue q) {
if (IsEmpty(q))return false;
q->front = (q->front + 1) % q->MaxSize;
return q->data[q->front];
}
void InitQueue(LinkQueue* Q) {
LinkNode* p = (LinkNode*)malloc(sizeof(LinkNode));
Q->front = Q->rear = p;
Q->front->next = NULL;
}
void EnQueue(LinkQueue* Q, ElemType e) {
LinkNode* p = (LinkNode*)malloc(sizeof(LinkNode));
p->data = e;
p->next = NULL;
Q->rear->next = p;
Q->rear = p;
}
void DeQueue(LinkQueue* Q, LinkNode* e) {
LinkNode* p;
if (Q->front == Q->rear)return;
p = Q->front->next; //第一个有效结点
*e = *p;
Q->front->next = p->next;
if (Q->rear == p) {
Q->rear = Q->front; //尾结点指向头
}
free(p);
}
bool IsEmptyQ(LinkQueue* Q){
if(Q->front == Q->rear)return true;
else return false;
}
//void DeQueue(LinkQueue* Q, ElemType* e) {
// LinkNode* p;
// if (Q->front == Q->rear)return ;
// p = Q->front->next; //第一个有效结点
// *e = p->data;
// Q->front->next = p->next;
// if (Q->rear == p) {
// Q->front = Q->rear;
// }
// free(p);
//}
double p(int n,int x){
if(n == 0)return 1;
if(n == 1)return 2 * x;
struct stack{
int n;
double val;
}st[MAXSIZE];
int top = -1;
double fv1 = 1,fv2 = 2*x;
for(int i = n; i >= 2; i--){
st[++top].n = i; //n大的先入栈
}
while(top >= 0){//从栈顶逐一向栈底算
st[top].val = 2*x*fv2-2*(st[top].n-1)*fv1;
fv1 = fv2;
fv2 = st[top].val;
top--;
}
return fv2;
}
double p1(int n,int x){
if(n == 0)return 1;
if(n == 1)return 2*x;
return 2*x*p1(n-1,x)-2*(n-1)*p1(n-2,x);
}
队列与栈的基本操作
最新推荐文章于 2024-09-27 00:45:01 发布