栈和队列的实现(附加oj题)

void StackInit(Stack* ps)
{
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = -1;
}
void StackPush(Stack* ps, STDataType data)
{
	assert(ps);
	if (ps->top + 1 == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDataType* new = (STDataType*)realloc(ps->a,sizeof(STDataType)*newcapacity);
		if (new == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->a = new;
		ps->capacity = newcapacity;
	}
	ps->a[++ps->top] = data;
}
void StackPop(Stack* ps)
{
	assert(ps);
	assert(ps->top > -1);
	ps->top--;
}
STDataType StackTop(Stack* ps)
{
	assert(ps);
	assert(ps->top > -1);
	return ps->a[ps->top];
}
int StackSize(Stack* ps)
{
	assert(ps);
	return ps->top+1;
}
int StackEmpty(Stack* ps)
{
	assert(ps);
	if (ps->top > -1)
		return 1;
	else
		return 0;
}
void StackDestroy(Stack* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = -1;
}

void QueueInit(Queue* q)
{
	assert(q);
	q->front = NULL;
	q->rear = NULL;
}
void QueuePush(Queue* q, QDataType data)
{
	assert(q);
	QNode* new = (QNode*)malloc(sizeof(QNode));
	if (new == NULL)
	{
		perror("malloc ");
		return;
	}
	new->data = data;
	new->next = NULL;
	if (q->front == NULL)
	{
		q->front = new;
		q->rear = new;
	}
	else
	{
		q->rear->next = new;
		q->rear = new;
	}
}
void QueuePop(Queue* q)
{
	assert(q);
	assert(q->front);
	QNode* cur = q->front->next;
	free(q->front);
	q->front = cur;
	if (q->front == NULL)
		q->rear = NULL;
}
QDataType QueueFront(Queue* q)
{
	assert(q);
	assert(q->front);
	return q->front->data;
}
QDataType QueueBack(Queue* q)
{
	assert(q);
	return q->rear->data;
}
int QueueSize(Queue* q)
{
	if (q == NULL)
	{
		return 0;
	}
	QNode* tail = q->front;
	int size = 1;
	while (tail != q->rear)
	{
		tail = tail->next;
		size++;
	}
	return size;
}
int QueueEmpty(Queue* q)
{
	return q->front == NULL;
}
void QueueDestroy(Queue* q)
{
	assert(q);
	QNode* cur = q->front;
	while (cur != NULL)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	q->front = q->rear = NULL;
}

 

char pairs(char a) {
    if (a == '}') return '{';
    if (a == ']') return '[';
    if (a == ')') return '(';
    return 0;
}//将右括号变成左括号,如果不是右括号则返回0。

bool isValid(char* s) 
{
    int n = strlen(s);
    if (n % 2 == 1) 
    {
        return false;//如果为奇数,则数量匹配不上
    }
    int stk[n + 1], top = 0;
    for (int i = 0; i < n; i++) 
    {
        char ch = pairs(s[i]);
        if (ch) 
        {
            if (top == 0 || stk[top - 1] != ch) 
            {
                return false;//位置匹配错误,位置不对称。
            }
            top--;
        } else 
        {
            stk[top++] = s[i];
        }
    }
    return top == 0;top为零,则成功,top不为零则左括号大于右括号。
}

typedef struct tagListNode 
{
    struct tagListNode* next;
    int val;
} ListNode;//队列基本操作

typedef struct 
{
    ListNode* top;
} MyStack;//模拟栈头进头出


MyStack* myStackCreate() 
{
    MyStack*new=calloc(1,sizeof(MyStack));
    return new;
}//使用malloc会报错,提示你未初始化

void myStackPush(MyStack* obj, int x) 
{
    ListNode* new=(ListNode*)malloc(sizeof(ListNode));
    new->val=x;
    new->next=obj->top;//从顶往底看,new是顶,obj->top是在new的下面
    obj->top=new;//更新顶
}

int myStackPop(MyStack* obj) 
{
    ListNode*cur=obj->top;
    int x=cur->val;
    obj->top=cur->next;
    free(cur);
    return x;
}

int myStackTop(MyStack* obj) 
{
    return obj->top->val;
}

bool myStackEmpty(MyStack* obj) 
{
    return obj->top==NULL;
}

void myStackFree(MyStack* obj) 
{
    while(obj->top!=NULL)
    {
        ListNode*cur=obj->top->next;
        free(obj->top);
        obj->top=cur;
    }
}

typedef struct 
{
    int* stk;
    int stkSize;
    int stkCapacity;
} Stack;

Stack* stackCreate(int cpacity) 
{
    Stack* ret = malloc(sizeof(Stack));
    ret->stk = malloc(sizeof(int) * cpacity);
    ret->stkSize = 0;
    ret->stkCapacity = cpacity;
    return ret;
}

void stackPush(Stack* obj, int x) 
{
    obj->stk[obj->stkSize++] = x;
}

void stackPop(Stack* obj) 
{
    obj->stkSize--;
}

int stackTop(Stack* obj) 
{
    return obj->stk[obj->stkSize - 1];
}

bool stackEmpty(Stack* obj) {

    return obj->stkSize == 0;
}

void stackFree(Stack* obj) 
{
    free(obj->stk);
}
//上面都是构建栈的函数,为下面使用两个栈做好准备
typedef struct 
{
    Stack* inStack;//入对
    Stack* outStack;//出对
} MyQueue;//构建两个栈一个负责入对,一个负责出对

MyQueue* myQueueCreate() 
{
    MyQueue* ret = malloc(sizeof(MyQueue));
    ret->inStack = stackCreate(100);
    ret->outStack = stackCreate(100);
    return ret;
}//对两个栈进行初始化

void in2out(MyQueue* obj) 
{
    while (!stackEmpty(obj->inStack)) 
    {
        stackPush(obj->outStack, stackTop(obj->inStack));
        stackPop(obj->inStack);
    }
}//将入对的元素转到出对

void myQueuePush(MyQueue* obj, int x) 
{
    stackPush(obj->inStack, x);
}

int myQueuePop(MyQueue* obj) 
{
    if (stackEmpty(obj->outStack)) 
    {
        in2out(obj);
    }
    int x = stackTop(obj->outStack);
    stackPop(obj->outStack);
    return x;
}

int myQueuePeek(MyQueue* obj) 
{
    if (stackEmpty(obj->outStack)) 
    {
        in2out(obj);
    }
    return stackTop(obj->outStack);
}

bool myQueueEmpty(MyQueue* obj) 
{
    return stackEmpty(obj->inStack) && stackEmpty(obj->outStack);
}//两个栈为空才算空,只有其中一个是不算的

void myQueueFree(MyQueue* obj) 
{
    stackFree(obj->inStack);
    stackFree(obj->outStack);
}

typedef struct 
{
    int*x;//用数组
    int capacity;//记录容量
    int front;//头指针
    int rear;//尾指针
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k) 
{
    MyCircularQueue*new=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    new->capacity=k+1;//数组的实际长度比有效长度大一
    new->rear=0;//实际长度等于rear-front,假设k等于10,则rear-front=11,front为0,rear为10,
    new->front=0;//从0到10一共是11个数不是10个数
    new->x=(int*)malloc(sizeof(int)*new->capacity);//有效长度为k
    return new;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) 
{
    if((obj->rear+1)%obj->capacity==obj->front)//%的作用一是让rear只在容量范围内,保证不越界
    return false;//二是通过+1能使rear与front相等
    obj->x[obj->rear]=value;//如果rear+1==front,表明数组已满
    obj->rear=(obj->rear+1)%obj->capacity;
    return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) 
{
   if (obj->rear == obj->front) //数组为空
    {
        return false;
    }
    obj->front = (obj->front + 1) % obj->capacity;
    return true;
}

int myCircularQueueFront(MyCircularQueue* obj) 
{
    if(obj->rear==obj->front)
    return -1;
    return obj->x[obj->front];
}

int myCircularQueueRear(MyCircularQueue* obj) 
{
    if(obj->rear==obj->front)
    return -1;
    return obj->x[(obj->rear - 1 + obj->capacity) % obj->capacity];
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) 
{
    return obj->rear == obj->front;//通过判断式来确定返回值
}

bool myCircularQueueIsFull(MyCircularQueue* obj) 
{
    return (obj->rear + 1) % obj->capacity == obj->front;
}

void myCircularQueueFree(MyCircularQueue* obj) 
{
    free(obj->x);
    free(obj);
}

  • 13
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值