数据结构——栈和队列

栈及其基本代码

栈一般用数组,把数组尾部当作栈顶。

队列一般用链表,头尾指针。

栈:顶部出顶部进。

在数组栈中,数组的后边作为栈顶进行进出操作。

基础代码

Stack.h

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>

typedef int STDataType;
typedef struct Stack
{
    STDataType* a;
    int top;
    int capacity;
}ST;

void STInit(ST* pst);
void STDestroy(ST* pst);
void STPush(ST* pst, int x);
void STPop(ST* pst);
STDataType STTop(ST* pst);
bool STEmpty(ST* pst);
int STSize(ST* pst);

Stack.c

#include "Stack.h"

void STInit(ST* pst)
{
    assert(pst);
    pst->a = NULL;
    pst->top = 0;         //top指向栈顶元素的下一个。
    pst->capacity = 0;
}
void STDestroy(ST* pst)
{
    assert(pst);
    free(pst->a);
    pst->a = NULL;
    pst->top = 0;       
    pst->capacity = 0;
}

void STPush(ST* pst, int x)                      //容量是能容纳的量,不是已经存储的多少。
{
    if (pst->top == pst->capacity)
    {
        int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
        STDataType *tmp = (STDataType*)realloc(pst->a, newcapacity*sizeof(STDataType));
        if (tmp == NULL)
        {
            perror("realloc fail");
            return;
        }
        pst->a = tmp;
        pst->capacity = newcapacity;
    }

    pst->a[pst->top] = x;
    pst->top++;
}

void STPop(ST* pst)
{
    assert(pst);
    assert(!STEmpty(pst));
    pst->top--;
}

STDataType STTop(ST* pst)
{
    assert(pst);
    return pst->a[pst->top - 1];
}
bool STEmpty(ST* pst)
{
    assert(pst);
    return pst->top == 0;
}
int STSize(ST* pst)
{
    assert(pst);
    return pst->top;
}

test.c

#include "Stack.h"

void TestStack1()
{
    ST st;
    STInit(&st);
    STPush(&st, 1);
    STPush(&st, 2);
    printf("%d ", STTop(&st));
    STPop(&st);
    STPush(&st, 3);
    STPush(&st, 4);
    while (!STEmpty(&st))
    {
        printf("%d ", STTop(&st));
        STPop(&st);
    }
    STDestroy(&st);

}


int main()
{
    TestStack1();
    return 0;
}

队列及其基本代码:

队列:

先进先出,队头出数据(删除数据),队尾进数据(插入数据);

基础代码:

Queue.h

#include <stdio.h>
#include <assert.h>
#include <stdbool.h>

typedef int QDataType;
typedef struct QueueNode
{
    struct QueueNode* next;            //这个指针指向下一个的地址。
    QDataType data;
}QNode;

typedef struct Queue
{
    QNode* phead;
    QNode* ptail;
    int size;
}Queue;

void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);
void QueuePush(Queue* pq, QDataType x);
void QueuePop(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
int QueueSize(Queue* pq);
bool QueueEmpty(Queue* pq);

Queue.c

#include "Queue.h"

void QueueInit(Queue* pq)
{
    assert(pq);
    pq->phead = NULL;
    pq->ptail = NULL;
    pq->size = 0;
}
void QueueDestroy(Queue* pq)
{
    assert(pq);
    QNode* cur = pq->ptail;
    while (cur)
    {
        QNode* next = cur->next;
        free(cur);
        cur = next;
    }
    pq->ptail = pq->phead=NULL;
    pq->size = 0;
}

void QueuePush(Queue* pq, QDataType x)
{
    assert(pq);
    QNode* newnode = (QNode*)malloc(sizeof(QNode));
    if (newnode == NULL)
    {
        perror("malloc fail\n");
        return;
    }
    newnode->data = x;
    newnode->next = NULL;

    if (pq->ptail == NULL)
    {
        assert(pq->phead == NULL);
        pq->phead = pq->ptail = newnode;
    }
    else
    {
        pq->ptail->next = newnode;
        pq->ptail = newnode;
    }
    pq->size++;
}
void QueuePop(Queue* pq)
{
    assert(pq);
    assert(!QueueEmpty(pq));

    //一个节点或者两个节点。
        if (pq->phead->next == NULL)
        {
            free(pq->phead);
            pq->phead = pq->ptail = NULL;
        }
        else
        {
            QNode* next = pq->phead->next;
            free(pq->phead);
            pq->phead = next;
        }
        pq->size--;
}


QDataType QueueFront(Queue* pq)
{
    assert(pq);
    assert(!QueueEmpty(pq));
        return  pq->phead->data;
}
QDataType QueueBack(Queue* pq)
{
    assert(pq);
    assert(!QueueEmpty(pq));
    return  pq->ptail->data;
}
int QueueSize(Queue* pq)
{
    assert(pq);
    return pq->size;
}
bool QueueEmpty(Queue* pq)
{
    assert(pq);
    return pq->size == 0;
}

Test.c

#include "Queue.h"

void TestQueue()
{
    Queue q;
    QueueInit(&q);
    QueuePush(&q, 1);
    QueuePush(&q, 2);
    QueuePush(&q, 3);
    QueuePush(&q, 4);
    QueuePush(&q, 5);
    QueuePop(&q);
    QueuePush(&q, 6);
    QueuePush(&q, 7);

    while (!QueueEmpty(&q))
    {
        printf("%d ", QueueFront(&q));
        QueuePop(&q);
    }
    QueueDestroy(&q);


}


int main()
{
    TestQueue();
    return 0;
}

代码

括号匹配问题:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>

typedef char STDataType;
typedef struct Stack
{
    STDataType* a;
    int top;
    int capacity;
}ST;

void STInit(ST* pst);
void STDestroy(ST* pst);
void STPush(ST* pst, STDataType x);
void STPop(ST* pst);
STDataType STTop(ST* pst);
bool STEmpty(ST* pst);
int STSize(ST* pst);

void STInit(ST* pst)
{
    assert(pst);
    pst->a = NULL;
    pst->top = 0;
    pst->capacity = 0;
}
void STDestroy(ST* pst)
{
    assert(pst);
    free(pst->a);
    pst->a = NULL;
    pst->top = 0;
    pst->capacity = 0;
}

void STPush(ST* pst, STDataType x)
{
    if (pst->top == pst->capacity)
    {
        int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
        STDataType* tmp = (STDataType*)realloc(pst->a, newcapacity * sizeof(STDataType));
        if (tmp == NULL)
        {
            perror("realloc fail");
            return;
        }
        pst->a = tmp;
        pst->capacity = newcapacity;
    }

    pst->a[pst->top] = x;
    pst->top++;
}

void STPop(ST* pst)
{
    assert(pst);
    assert(!STEmpty(pst));
    pst->top--;
}

STDataType STTop(ST* pst)
{
    assert(pst);
    return pst->a[pst->top - 1];
}
bool STEmpty(ST* pst)
{
    assert(pst);
    return pst->top == 0;
}
int STSize(ST* pst)
{
    assert(pst);
    return pst->top;
}

bool isValid(char* s) {
    ST st;
    STInit(&st);
    while (*s)
    {
        if (*s == '('
            || *s == '['
            || *s == '{')
        {
            STPush(&st, *s);
            s++;
        }
        else
        {
            if (STEmpty(&st))
            {
                STDestroy(&st);
                return false;
            }
            STDataType top = STTop(&st);
            STPop(&st);

            if (*s == ')' && top != '(' || *s == '}' && top != '{' || *s == ']' && top != '[')
            {
                STDestroy(&st);
                return false;
            }
            else { s++; }
        }


    }

    bool ret = STEmpty(&st);
    STDestroy(&st);
    return ret;


}

两个队列实现一个栈

typedef int QDataType;

typedef struct QueueNode

{

    struct QueueNode* next;

    QDataType data;

}QNode;

typedef struct Queue

{

    QNode* phead;

    QNode* ptail;

    int size;

}Queue;

void QueueInit(Queue* pq);

void QueueDestroy(Queue* pq);

void QueuePush(Queue* pq, QDataType x);

void QueuePop(Queue* pq);

QDataType QueueFront(Queue* pq);

QDataType QueueBack(Queue* pq);

int QueueSize(Queue* pq);

bool QueueEmpty(Queue* pq);

void QueueInit(Queue* pq)

{

    assert(pq);

    pq->phead = NULL;

    pq->ptail = NULL;

    pq->size = 0;

}

void QueueDestroy(Queue* pq)

{

    assert(pq);

    QNode* cur = pq->ptail;

    while (cur)

    {

        QNode* next = cur->next;

        free(cur);

        cur = next;

    }

    pq->ptail = pq->phead=NULL;

    pq->size = 0;

}

void QueuePush(Queue* pq, QDataType x)

{

    assert(pq);

    QNode* newnode = (QNode*)malloc(sizeof(QNode));

    if (newnode == NULL)

    {

        perror("malloc fail\n");

        return;

    }

    newnode->data = x;

    newnode->next = NULL;

    if (pq->ptail == NULL)

    {

        assert(pq->phead == NULL);

        pq->phead = pq->ptail = newnode;

    }

    else

    {

        pq->ptail->next = newnode;

        pq->ptail = newnode;

    }

    pq->size++;

}

void QueuePop(Queue* pq)

{

    assert(pq);

    assert(!QueueEmpty(pq));

    //一个节点或者两个节点。

        if (pq->phead->next == NULL)

        {

            free(pq->phead);

            pq->phead = pq->ptail = NULL;

        }

        else

        {

            QNode* next = pq->phead->next;

            free(pq->phead);

            pq->phead = next;

        }

        pq->size--;

}


 

QDataType QueueFront(Queue* pq)

{

    assert(pq);

    assert(!QueueEmpty(pq));

        return  pq->phead->data;

}

QDataType QueueBack(Queue* pq)

{

    assert(pq);

    assert(!QueueEmpty(pq));

    return  pq->ptail->data;

}

int QueueSize(Queue* pq)

{

    assert(pq);

    return pq->size;

}

bool QueueEmpty(Queue* pq)

{

    assert(pq);

    return pq->size == 0;

}

#include <stdio.h>

#include <stdlib.h>

#include <assert.h>

#include <stdbool.h>

typedef char STDataType;

typedef struct Stack

{

    STDataType* a;

    int top;

    int capacity;

}ST;

void STInit(ST* pst);

void STDestroy(ST* pst);

void STPush(ST* pst, STDataType x);

void STPop(ST* pst);

STDataType STTop(ST* pst);

bool STEmpty(ST* pst);

int STSize(ST* pst);

void STInit(ST* pst)

{

    assert(pst);

    pst->a = NULL;

    pst->top = 0;

    pst->capacity = 0;

}

void STDestroy(ST* pst)

{

    assert(pst);

    free(pst->a);

    pst->a = NULL;

    pst->top = 0;

    pst->capacity = 0;

}

void STPush(ST* pst, STDataType x)

{

    if (pst->top == pst->capacity)

    {

        int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;

        STDataType* tmp = (STDataType*)realloc(pst->a, newcapacity * sizeof(STDataType));

        if (tmp == NULL)

        {

            perror("realloc fail");

            return;

        }

        pst->a = tmp;

        pst->capacity = newcapacity;

    }

    pst->a[pst->top] = x;

    pst->top++;

}

void STPop(ST* pst)

{

    assert(pst);

    assert(!STEmpty(pst));

    pst->top--;

}

STDataType STTop(ST* pst)

{

    assert(pst);

    return pst->a[pst->top - 1];

}

bool STEmpty(ST* pst)

{

    assert(pst);

    return pst->top == 0;

}

int STSize(ST* pst)

{

    assert(pst);

    return pst->top;

}


 

typedef struct

{

    Queue q1;

    Queue q2;

} MyStack;


 

MyStack* myStackCreate()

{

    MyStack* obj=(MyStack*)malloc(sizeof(MyStack));

    if(obj==NULL)

    {

        perror("malloc fail");

        return NULL;

    }

    QueueInit(&obj->q1);

    QueueInit(&obj->q2);

    return obj;

}

void myStackPush(MyStack* obj, int x)

{

    if(!QueueEmpty(&obj->q1))

    {

        QueuePush(&obj->q1,x);

    }

    else

    {

        QueuePush(&obj->q2,x);

    }

}

int myStackPop(MyStack* obj)

{

    Queue*pEmpty=&obj->q1;

    Queue*pNonEmpty=&obj->q2;

    if(!QueueEmpty(pEmpty))

    {

        pEmpty=&obj->q2;

        pNonEmpty=&obj->q1;

    }

    while(QueueSize(pNonEmpty)>1)

    {

        QueuePush(pEmpty,QueueFront(pNonEmpty));

        QueuePop(pNonEmpty);

    }

    int top=QueueFront(pNonEmpty);

    QueuePop(pNonEmpty);

    return top;

}

int myStackTop(MyStack* obj)

{

    if(!QueueEmpty(&obj->q1))

    {

        return QueueBack(&obj->q1);

    }

    else{

        return QueueBack(&obj->q2);

    }

}

bool myStackEmpty(MyStack* obj) {

    return QueueEmpty(&obj->q1)&&QueueEmpty(&obj->q2);

}

void myStackFree(MyStack* obj) {

    free(obj);

}

两个栈实现一个队列

#include <stdio.h>

#include <stdlib.h>

#include <assert.h>

#include <stdbool.h>

typedef char STDataType;

typedef struct Stack

{

    STDataType* a;

    int top;

    int capacity;

}ST;

void STInit(ST* pst);

void STDestroy(ST* pst);

void STPush(ST* pst, STDataType x);

void STPop(ST* pst);

STDataType STTop(ST* pst);

bool STEmpty(ST* pst);

int STSize(ST* pst);

void STInit(ST* pst)

{

    assert(pst);

    pst->a = NULL;

    pst->top = 0;         //top指向栈顶元素的下一个。

    pst->capacity = 0;

}

void STDestroy(ST* pst)

{

    assert(pst);

    free(pst->a);

    pst->a = NULL;

    pst->top = 0;      

    pst->capacity = 0;

}

void STPush(ST* pst, STDataType x)

{

    if (pst->top == pst->capacity)

    {

        int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;

        STDataType *tmp = (STDataType*)realloc(pst->a, newcapacity*sizeof(STDataType));

        if (tmp == NULL)

        {

            perror("realloc fail");

            return;

        }

        pst->a = tmp;

        pst->capacity = newcapacity;

    }

    pst->a[pst->top] = x;

    pst->top++;

}

void STPop(ST* pst)

{

    assert(pst);

    assert(!STEmpty(pst));

    pst->top--;

}

STDataType STTop(ST* pst)

{

    assert(pst);

    return pst->a[pst->top - 1];

}

bool STEmpty(ST* pst)

{

    assert(pst);

    return pst->top == 0;

}

int STSize(ST* pst)

{

    assert(pst);

    return pst->top;

}

typedef int QDataType;

typedef struct QueueNode

{

    struct QueueNode* next;

    QDataType data;

}QNode;

typedef struct Queue

{

    QNode* phead;

    QNode* ptail;

    int size;

}Queue;

void QueueInit(Queue* pq);

void QueueDestroy(Queue* pq);

void QueuePush(Queue* pq, QDataType x);

void QueuePop(Queue* pq);

QDataType QueueFront(Queue* pq);

QDataType QueueBack(Queue* pq);

int QueueSize(Queue* pq);

bool QueueEmpty(Queue* pq);

void QueueInit(Queue* pq)

{

    assert(pq);

    pq->phead = NULL;

    pq->ptail = NULL;

    pq->size = 0;

}

void QueueDestroy(Queue* pq)

{

    assert(pq);

    QNode* cur = pq->ptail;

    while (cur)

    {

        QNode* next = cur->next;

        free(cur);

        cur = next;

    }

    pq->ptail = pq->phead=NULL;

    pq->size = 0;

}

void QueuePush(Queue* pq, QDataType x)

{

    assert(pq);

    QNode* newnode = (QNode*)malloc(sizeof(QNode));

    if (newnode == NULL)

    {

        perror("malloc fail\n");

        return;

    }

    newnode->data = x;

    newnode->next = NULL;

    if (pq->ptail == NULL)

    {

        assert(pq->phead == NULL);

        pq->phead = pq->ptail = newnode;

    }

    else

    {

        pq->ptail->next = newnode;

        pq->ptail = newnode;

    }

    pq->size++;

}

void QueuePop(Queue* pq)

{

    assert(pq);

    assert(!QueueEmpty(pq));

    //一个节点或者两个节点。

        if (pq->phead->next == NULL)

        {

            free(pq->phead);

            pq->phead = pq->ptail = NULL;

        }

        else

        {

            QNode* next = pq->phead->next;

            free(pq->phead);

            pq->phead = next;

        }

        pq->size--;

}


 

QDataType QueueFront(Queue* pq)

{

    assert(pq);

    assert(!QueueEmpty(pq));

        return  pq->phead->data;

}

QDataType QueueBack(Queue* pq)

{

    assert(pq);

    assert(!QueueEmpty(pq));

    return  pq->ptail->data;

}

int QueueSize(Queue* pq)

{

    assert(pq);

    return pq->size;

}

bool QueueEmpty(Queue* pq)

{

    assert(pq);

    return pq->size == 0;

}


 

typedef struct {

    ST pushst;

    ST popst;

} MyQueue;


 

MyQueue* myQueueCreate() {

    MyQueue*obj=(MyQueue*)malloc(sizeof(MyQueue));

    STInit(&obj->pushst);

     STInit(&obj->popst);

   

    return obj;

}

void myQueuePush(MyQueue* obj, int x) {

    STPush(&obj->pushst,x);

}

int myQueuePop(MyQueue* obj) {

    int front=myQueuePeek(obj);

    STPop(&obj->popst);

    return front;

}

int myQueuePeek(MyQueue* obj) {

    if(STEmpty(&obj->popst))

    {

        while(!STEmpty(&obj->pushst))

        {

            STPush(&obj->popst,STTop(&obj->pushst));

            STPop(&obj->pushst);

        }

    }

    return STTop(&obj->popst);

}

bool myQueueEmpty(MyQueue* obj) {

    return STEmpty(&obj->pushst)&&STEmpty(&obj->popst);

}

void myQueueFree(MyQueue* obj) {

    STDestroy(&obj->pushst);

    STDestroy(&obj->popst);

    free(obj);

}

循环队列



 

typedef struct {

    int front;

    int rear;

    int k;

    int *a;

} MyCircularQueue;

void myCircularQueueFree(MyCircularQueue* obj);

bool myCircularQueueIsFull(MyCircularQueue* obj);

bool myCircularQueueIsEmpty(MyCircularQueue* obj);

int myCircularQueueRear(MyCircularQueue* obj);

int myCircularQueueFront(MyCircularQueue* obj);

bool myCircularQueueDeQueue(MyCircularQueue* obj);

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) ;

MyCircularQueue* myCircularQueueCreate(int k) {

    MyCircularQueue *obj=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));

    obj->a=(int*)malloc(sizeof(int)*(k+1));

    obj->front=obj->rear=0;

    obj->k=k;

    return obj;

}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {

    if(myCircularQueueIsFull(obj))

    {

        return false;

    }

    obj->a[obj->rear]=value;

    obj->rear++;

    obj->rear=obj->rear%(obj->k+1);

    return true;

}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {

     if(myCircularQueueIsEmpty(obj))

    {

        return false;

    }

    obj->front++;

    obj->front=obj->front%(obj->k+1);

    return true;

}

int myCircularQueueFront(MyCircularQueue* obj) {

    if(myCircularQueueIsEmpty(obj))

    {

        return -1;

    }

    return obj->a[obj->front];

}

int myCircularQueueRear(MyCircularQueue* obj) {

     if(myCircularQueueIsEmpty(obj))

    {

        return -1;

    }

    return obj->a[(obj->rear+obj->k)%(obj->k+1)];

}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) {

    return obj->front==obj->rear;

}

bool myCircularQueueIsFull(MyCircularQueue* obj) {

    return (obj->rear+1)%(obj->k+1)==obj->front;

}

void myCircularQueueFree(MyCircularQueue* obj) {

    free(obj->a);

    free(obj);

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值