一、232 用栈实现队列

typedef struct Node{
int stackintop,stackouttop;//指向栈顶
int stackin[100],stackout[100];//存放、输出
} MyQueue;
MyQueue* myQueueCreate() {
//初始化
MyQueue*queue = (MyQueue*)malloc(sizeof(MyQueue));
queue->stackintop = 0;
queue->stackouttop = 0;
return queue;
}
void myQueuePush(MyQueue* obj, int x) {
obj->stackin[obj->stackintop++] = x;
}
int myQueuePop(MyQueue* obj) {
int stackintop = obj->stackintop;
int stackouttop = obj->stackouttop;
//当输出栈为空且输入栈不为空
if(stackouttop == 0){
while(stackintop>0){
//把stackin的数据复制给stackout
//obj->stackout[stackouttop] = obj->stackin[stackintop];
//obj->stackouttop++,obj->stackintop--;
obj->stackout[stackouttop++] = obj->stackin[--stackintop];
}
}
int result = obj->stackout[--stackouttop];
//当输入站不为空时
while(stackouttop>0){
//obj->stackin[stackintop]=obj->stackout[stackouttop];
//obj->stackintop++,obj->stackouttop--;
obj->stackin[stackintop++]=obj->stackout[--stackouttop];
}
obj->stackintop = stackintop;
obj->stackouttop = stackouttop;
return result;
}
int myQueuePeek(MyQueue* obj) {
return obj->stackin[0];
}
bool myQueueEmpty(MyQueue* obj) {
return obj->stackintop==0&&obj->stackouttop==0;
}
void myQueueFree(MyQueue* obj) {
obj->stackintop=0;
obj->stackouttop=0;
}
二、225 用队列实现栈
typedef struct LPNode{
int data;//数据节点
struct LPNode *next;//指向下一个节点
}LPNode;
typedef struct {
struct LPNode*front;//队头指针
struct LPNode*rear;//队尾指针
} MyStack;
MyStack* myStackCreate() {
MyStack*stack = (MyStack*)malloc(sizeof(MyStack));
assert(stack!=NULL);
stack->front = 0;
stack->rear = 0;
return stack;
}
void myStackPush(MyStack* obj, int x) {
//申请节点
LPNode*p = (LPNode*)malloc(sizeof(LPNode));
assert(p!=NULL);
p->data = x;
p->next = NULL;
//插入
if(obj->front==0&&obj->rear==0){
obj->front = p;
obj->rear = p;
}
else{
obj->rear->next = p;
obj->rear = p;
}
}
int myStackPop(MyStack* obj) {
int result = obj->rear->data;
if(obj->front == obj->rear){//当只有一个元素时
free(obj->rear);
obj->front = NULL;
obj->rear = NULL;
}
else{//不只有一个元素时
LPNode*p = obj->front;//从头指针开始遍历到尾指针-1
while(p->next!=obj->rear)
p=p->next;
free(obj->rear);
//更新尾指针
obj->rear = p;
p->next=NULL;
}
return result;
}
int myStackTop(MyStack* obj) {
return obj->rear->data;
}
bool myStackEmpty(MyStack* obj) {
return obj->front == NULL&& obj->rear == NULL;
}
void myStackFree(MyStack* obj) {
LPNode*p = obj->front;
while(obj->front!=NULL){
p=obj->front;
obj->front = p->next;
free(p);
}
obj->rear = NULL;
}
三、20 有效的括号

int notMatch(char tmp,char*stack,int stacktop){
switch(tmp){
case')':
return stack[stacktop-1]!='(';
case']':
return stack[stacktop-1]!='[';
case'}':
return stack[stacktop-1]!='{';
}
return 0;
}
bool isValid(char* s) {
int len = strlen(s);
if(len%2!=0)
return false;
char stack[10002];
int stacktop = 0;
for(int i=0;i<len;i++){
char tmp = s[i];
if(tmp=='('||tmp=='{'||tmp=='[')
stack[stacktop++]=tmp;//是左括号就入栈
else if(stacktop==0||notMatch(tmp,stack,stacktop))//若当前字符为右括号,且栈中无元素或右括号与栈顶元素不符
return false;
else
stacktop--;//匹配就出栈
}
if(stacktop!=0)//遍历结束,若栈中有元素
return false;
else
return true;
}
四、1047 删除字符串中的所有相邻重复项

char* removeDuplicates(char* s) {
int len = strlen(s);
char *stack = (char*)malloc(sizeof(char)*len+1);//使用字符串代替栈的操作
int stacktop=0;//栈顶
int p =0;//指向字符串中字母的指针
while(p<len){
char tmp = s[p++];//把p指向的字母赋值给tmp
if(stacktop>0&&tmp==stack[stacktop-1])//如果栈里有元素且遍历的字母与栈顶元素相等
stacktop--;//弹出
else
stack[stacktop++]=tmp;//不同或栈内没有元素则入栈
}
//字符串末尾要加‘、0’;
stack[stacktop]='\0';
return stack;
}