栈和队列

开门见山
【1】栈是限定在表的一端进行删除、插入等操作,允许操作的一端称为栈顶,另一端称为栈底,元素的进出遵循后进先出
【2】队列是在表的两端同时进行操作,允许插入元素的一端称为队尾,允许删除的一端称为队头,遵循先进先出


栈:
Stack.h

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

typedef int DataType;

typedef struct Stack
{
    DataType* _array;
    size_t top;
    size_t end;
}Stack;

void StackInit(Stack* s);
void StackPush(Stack* s, DataType x);
void StackPop(Stack* s);
DataType StackTop(Stack* s);
size_t StackSize(Stack* s);
int StackEmpty(Stack* s);

void StackInit(Stack* s)
{
    assert(s);
    s->end = s->top = 0;
    s->_array = NULL;
}
void StackPush(Stack* s, DataType x)
{
    assert(s);
    if (s->end == s->top)
    {
        size_t size = s->end > 0 ? s->end * 2 : 3;
        s->_array = (DataType*)realloc(s->_array, sizeof(DataType)*size);
        assert(s);
        s->end = size;
    }
    s->_array[s->top++] = x;    
}
void StackPop(Stack* s)
{
    assert(s);
    --s->top;
}
DataType StackTop(Stack* s)
{
    assert(s);
    return s->_array[s->top-1];
}
size_t StackSize(Stack* s)
{
    assert(s);
    return s->top;
}
//返回0就是空
int StackEmpty(Stack* s)
{
    assert(s);
    return s->top;
}

void test1()//栈的测试用例
{
    Stack s1;
    StackInit(&s1);
    StackPush(&s1, 1);
    StackPush(&s1, 2);
    StackPush(&s1, 3);
    StackPush(&s1, 4);
    StackPush(&s1, 5);
    while (StackEmpty(&s1))
    {
        printf("%d ", StackTop(&s1));
        StackPop(&s1);
    }
    printf("\n");
}

队列:

Queue.h
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>

typedef int DataType;

typedef struct QueueNode
{
    DataType data;
    struct QueueNode* _next;
}QueueNode;

typedef struct Queue
{
    QueueNode* _head;
    QueueNode* _tail;
}Queue;

//接口操作
void QueueInit(Queue* q);
void QueuePush(Queue* q, DataType x);
void QueuePop(Queue* q);
DataType QueueFront(Queue* q);
DataType QueueBack(Queue* q);
size_t QueueSize(Queue* q);
int QueueEmpty(Queue* q);

void QueueInit(Queue* q)
{
    assert(q);
    q->_head = q->_tail = NULL;
}

void QueuePush(Queue* q, DataType x)
{
    assert(q);
    QueueNode* node;
    node = (QueueNode*)malloc(sizeof(QueueNode));
    node->_next = NULL;
    node->data = x;
    assert(node);

    if (q->_tail == NULL)
    {
        q->_head = q->_tail = node;
    }
    else
    {
        q->_tail->_next = node;
        q->_tail = node;
    }
}

void QueuePop(Queue* q)
{
    assert(q);
    if (q->_head==q->_tail)
    {
        if (q->_head)
        {
            free(q->_head);
            q->_head = q->_tail = NULL;
        }
    }
    else
    {
        QueueNode* next = q->_head->_next;
        free(q->_head);
        q->_head = next;
    }
}

DataType QueueFront(Queue* q)
{
    assert(q);

    return q->_head->data;
}

DataType QueueBack(Queue* q)
{
    assert(q);
    return q->_tail->data;
}
size_t QueueSize(Queue* q)
{
    assert(q);
    size_t count = 0;
    while (q->_head)
    {
        q->_head = q->_head->_next;
        ++count;
    }
    return count;
}

int QueueEmpty(Queue* q)
{
    assert(q);
    return q->_head == NULL ? 0 : 1;
}

void test2()//队列的测试用例
{
    Queue q;
    QueueInit(&q);
    QueuePush(&q, 1);
    QueuePush(&q, 2);
    QueuePush(&q, 3);
    QueuePush(&q, 4);
    QueuePush(&q, 5);
    while (QueueEmpty(&q))
    {
        printf("%d ", QueueFront(&q));
        QueuePop(&q);
    }
    printf("\n");
}

#include"Stack.h"
#include"Queue.h"

int main()
{
    printf("栈>\n");
    test1();
    printf("队列>\n");
    test2();
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值