数据结构| |栈和队列

栈和队列


栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。不包含任何元素的栈称为空栈,栈又称为先进后出的线性表。

栈分为:顺序栈与链式栈

注意:顺序栈的所有操作的时间复杂度为O(1)

对于顺序栈的基本操作

第一部分:头文件Stack.h

#ifndef __STACK_H__
#define __STACK_H__
​
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
​
typedef int DataType;
​
typedef struct Stack
{
    DataType* stack;
    int capacity;
    int top;
}Stack;
​
void StackInit(Stack* ps);
void StackDestory(Stack* ps);
​
void StackPush(Stack* ps, DataType d);
void StackPop(Stack* ps);
DataType StackTop(Stack* ps);
int StackEmpty(Stack* ps);
int StackSize(Stack* ps);
​
​
​
#endif //__STACK_H__

第二部分:函数实现部分:Stack.c

#include "Stack.h"
​
void StackInit(Stack* ps)
{
    assert(ps);
​
    ps->stack = (DataType*)malloc(sizeof(DataType)*3);
    ps->top = -1;//栈顶指向当前位置
    ps->capacity = 3;
}
​
void StackDestory(Stack* ps)
{
    assert(ps);
​
    ps->stack = NULL;
    ps->top = -1;
    ps->capacity = 0;
}
​
void AddCapacity(Stack* ps)
{
    assert(ps);
​
    ps->stack = (DataType*)realloc(ps->stack, sizeof(DataType)*ps->capacity * 2);
    if (ps->stack == NULL)
    {
        perror("realloc size");
    }
    ps->capacity *= 2;
}
​
void StackPush(Stack* ps, DataType d)
{
    assert(ps);
​
    //判断栈的储存空间是否已满
    if (ps->top == ps->capacity - 1) //满了
    {
        //进行增容
        AddCapacity(ps);
        //进行存放
        ps->stack[++ps->top] = d;
    }
    else
    {
        //直接进行存放
        ps->stack[++ps->top] = d;
    }
}
​
void StackPop(Stack* ps)
{
    assert(ps);
    
    ps->top--;
}
DataType StackTop(Stack* ps)
{
    assert(ps);
​
    return ps->stack[ps->top];
}
​
//空 0
//非空 1
int StackEmpty(Stack* ps)
{
    assert(ps);
​
    return ps->top == -1 ? 0 : 1;
}
​
int StackSize(Stack* ps)
{
    assert(ps);
​
    int size = ps->top + 1;
    return size;
}

第三部分:测试部分test.c

#include "Stack.h"
​
void TestStack()
{
    Stack s = { NULL };
    StackInit(&s);
    StackPush(&s, 1);
    StackPush(&s, 2);
    StackPush(&s, 3);
    StackPush(&s, 4);
    printf("栈的容量:%d\n", StackSize(&s));
    while (StackEmpty(&s))
    {
        printf("%d ", StackTop(&s));
        StackPop(&s);
    }
    StackDestory(&s);
    printf("\n");
}
​
int main()
{
    TestStack();
    return 0;    
}

队列

队列:只允许在一端进行插入操作,在另一端进行删除操作的特殊线性表。进行插入操作的一端称为队尾,进行删除操作的一端称为队头。队列具有先进先出的特性。

对于链式队列的基本操作

第一部分:头文件Queue.h

#ifndef __QUEUE_H__
#define __QUEUE_H__
​
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
​
typedef int DataType;
​
typedef struct QueueNode
{
    DataType data;
    struct QueueNode* next;
}QueueNode;
​
typedef struct Queue
{
    QueueNode* front;
    QueueNode* rear;
}Queue;
​
void QueueInit(Queue* pq);
void QueueDestory(Queue* pq);
​
void QueuePush(Queue* pq, DataType d);
void QueuePop(Queue* pq);
DataType QueueFront(Queue* pq);
int QueueEmpty(Queue* pq);
int QueueSize(Queue* pq);
​
#endif //__QUEUE_H__

第二部分:函数实现部分Queue.c

#include "Queue.h"
​
void QueueInit(Queue* pq)
{
    assert(pq);
    
    pq->front = NULL;
    pq->rear = NULL;
}
​
void QueueDestory(Queue* pq)
{
    assert(pq);
​
    QueueNode* cur = pq->front;
    while (cur)
    {
        QueueNode* del = cur;
        cur = cur->next;
        free(del);
        del = NULL;
    }
    pq->front = NULL;
    pq->rear = NULL;
}
​
QueueNode* BuyNode(DataType d)
{
    QueueNode* newNode = (QueueNode*)malloc(sizeof(DataType));
    if (newNode == NULL)
    {
        perror("Buy newNode");
    }
​
    newNode->data = d;
    newNode->next = NULL;
    return newNode;
}
​
void QueuePush(Queue* pq, DataType d)
{
    assert(pq);
​
    //第一次入队
    if (pq->front == NULL)
    {
        QueueNode* newNode = BuyNode(d);
        pq->rear = newNode;
        pq->front = pq->rear;
    }
    else
    {
        QueueNode* newNode = BuyNode(d);
        pq->rear->next = newNode;
        pq->rear = newNode;
    }
}
​
void QueuePop(Queue* pq)
{
    assert(pq);
​
    pq->front = pq->front->next;
}
​
DataType QueueFront(Queue* pq)
{
    assert(pq);
​
    return pq->front->data;
}
​
//空 0
//非空 1
int QueueEmpty(Queue* pq)
{
    assert(pq);
​
    return pq->front == NULL ? 0 : 1;
}
​
int QueueSize(Queue* pq)
{
    int size = 0;
    assert(pq);
​
    QueueNode* cur = pq->front;
    while (cur)
    {
        cur = cur->next;
        size++;
    }
    return size;
}

第三部分:测试部分test.c

#include "Queue.h"
​
void TestQueue()
{
    Queue q = { NULL };
    QueueInit(&q);
    QueuePush(&q, 1);
    QueuePush(&q, 2);
    QueuePush(&q, 3);
    QueuePush(&q, 4);
    printf("队列的大小:%d\n", QueueSize(&q));
    while (QueueEmpty(&q))
    {
        printf("%d ", QueueFront(&q));
        QueuePop(&q);
    }
    QueueDestory(&q);
    printf("\n");
}
​
int main()
{
    TestQueue();
    return 0;    
}

猜你喜欢

转载自blog.csdn.net/qq_40399012/article/details/81665085