//language c
// 剑指09
// https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/
// 方法一,用链表栈
// 先实现栈
typedef struct Node{
int val;
struct Node *next;
} Node;
typedef struct Stack{
struct Node *top;
int size;
} Stack;
Stack* StackCreate() {
Stack *A = (Stack *)malloc(sizeof(Stack));
A->top = NULL;
A->size = 0;
return A;
}
void StackPush(Stack* obj, int x) {
Node *newnode=(Node*)malloc(sizeof(Node));
newnode->val = x;
newnode->next = obj->top;
obj->top = newnode;
obj->size++;
}
int StackPop(Stack* obj) {
// if(obj == NULL)
// return;
// if(obj->top == NULL)
// return;
Node *willbepoped = obj->top;
obj->top = obj->top->next;
int ans = willbepoped->val;
free(willbepoped);
obj->size--;
return ans;
}
int Getsize(Stack* obj){
return obj->size;
}
void Stackchange(Stack * a,Stack * b){ // 把a所有的都给b
int size_a = Getsize(a);
int temp;
for(int i=0; i <size_a; i++){
temp = StackPop(a);
StackPush(b,temp);
}
}
void StackFree(Stack* obj) {
free(obj);
}
// ---------------------------------------------------------------
typedef struct {
struct Stack *INstack;
struct Stack *OUTstack;
int size; // 正的代表在INstack里,负的代表在OUTstack里
} CQueue;
CQueue* cQueueCreate() {
CQueue *A = (CQueue *)malloc(sizeof(CQueue));
A->INstack = StackCreate();
A->OUTstack = StackCreate();
A->size = 0;
return A;
}
void cQueueAppendTail(CQueue* obj, int value) {
if(obj->size < 0){
Stackchange(obj->OUTstack,obj->INstack);
obj->size = - obj->size;
}
StackPush(obj->INstack,value);
obj->size += 1;
}
int cQueueDeleteHead(CQueue* obj) {
if(obj->size == 0)
return -1;
if(obj->size >0){
Stackchange(obj->INstack,obj->OUTstack);
obj->size = - obj->size;
}
int ans = StackPop(obj->OUTstack);
obj->size += 1;
return ans;
}
void cQueueFree(CQueue* obj) {
free(obj);
}
/**
* Your CQueue struct will be instantiated and called as such:
* CQueue* obj = cQueueCreate();
* cQueueAppendTail(obj, value);
* int param_2 = cQueueDeleteHead(obj);
* cQueueFree(obj);
*/
// 剑指09
// https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/
// 方法二,数组栈
// 先实现数组栈
typedef struct Stack{
int value[10000];
int size;
} Stack;
Stack* StackCreate() {
Stack *A = (Stack *)malloc(sizeof(Stack));
A->size = 0;
return A;
}
void StackPush(Stack* obj, int x) {
obj->value[obj->size++] = x;
}
int StackPop(Stack* obj) {
return obj->value[--obj->size];
}
int Getsize(Stack* obj){
return obj->size;
}
void Stackchange(Stack * a,Stack * b){ // 把a所有的都给b
int size_a = Getsize(a);
int temp;
for(int i=0; i <size_a; i++){
temp = StackPop(a);
StackPush(b,temp);
}
}
void StackFree(Stack* obj) {
free(obj);
}
// ---------------------------------------------------------------
typedef struct {
struct Stack *INstack;
struct Stack *OUTstack;
int size; // 正的代表在INstack里,负的代表在OUTstack里
} CQueue;
CQueue* cQueueCreate() {
CQueue *A = (CQueue *)malloc(sizeof(CQueue));
A->INstack = StackCreate();
A->OUTstack = StackCreate();
A->size = 0;
return A;
}
void cQueueAppendTail(CQueue* obj, int value) {
if(obj->size < 0){
Stackchange(obj->OUTstack,obj->INstack);
obj->size = - obj->size;
}
StackPush(obj->INstack,value);
obj->size += 1;
}
int cQueueDeleteHead(CQueue* obj) {
if(obj->size == 0)
return -1;
if(obj->size >0){
Stackchange(obj->INstack,obj->OUTstack);
obj->size = - obj->size;
}
int ans = StackPop(obj->OUTstack);
obj->size += 1;
return ans;
}
void cQueueFree(CQueue* obj) {
free(obj);
}
/**
* Your CQueue struct will be instantiated and called as such:
* CQueue* obj = cQueueCreate();
* cQueueAppendTail(obj, value);
* int param_2 = cQueueDeleteHead(obj);
* cQueueFree(obj);
*/