写了点儿代码, 感觉指针真的不会
双栈共享空间:
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> const int Maxesize = 101; typedef struct { int data[Maxesize]; int top1; int top2; }SqDoubleStack, *Stack; void Push(Stack &s, int e, int n) { if(s->top1+1 == s->top2) printf("栈满"); if(n == 1) s->data[++s->top1] = e; if(n == 2) s->data[--s->top2] = e; // printf("%d %d\n", s->top1, s->data[s->top1]); // printf("%d %d\n", s->top2, s->data[s->top2]); } void DeleteStack(Stack &s, int n, int *e) { if(s->top1+1 == s->top2) printf("栈满"); if(n == 1) e = &(s->data[s->top1--]); if(n == 2) e = &(s->data[s->top2--]); printf("%d\n", *e); } void DoubleStackInit(Stack &s/* == SqDoubleStack * &s*/) { s=(SqDoubleStack *)malloc(sizeof(SqDoubleStack)); //先申请, 后使用: s->top1 = -1; s->top2 = Maxesize; /*int i = 0; //ClearStack; while(i < Maxesize) { s->data[i] = 0; i++; } */ printf("%d %d\n", s->top1, s->top2); } int main() { Stack Q; DoubleStackInit(Q); int c, n; scanf("%d%d", &n, &c); Push(Q, n, c); int e; scanf("%d", &n); DeleteStack(Q, n, &e); return 0; }
队列知识点:
循环队列判断条件满队列
rear: 头指针; front: 尾指针;
(rear+1)%Maxesize = front;
队列长度:
Length=(rear-front+Maxesize)%Maxesize;
浙公网安备 33010602011771号