【数据结构】栈和队列

目录

1.栈

栈的概念及结构

栈的实现

栈的实现(代码类似顺序表):Gitee栈的实现

栈的OJ链接:

2.队列

队列的概念及结构

队列的实现(代码类似单链表):Gitee队列的实现

扩展:环形队列OJ练习


1.栈

栈的概念及结构

栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈。出数据也在栈顶。

栈的实现

栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。

因为数组在尾上插入数据的代价比较小。

栈的实现(代码类似顺序表):Gitee栈的实现

void StackInit(ST*ps)//初始化栈
{
	assert(ps);
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}

void StackPush(ST* ps, STDatatype x)//入栈
{
	assert(ps);
	//检查容量够不够
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		ps->a = (STDatatype*)realloc(ps->a, sizeof(STDatatype)*newcapacity);
		if (ps->a == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}

void StackPop(ST* ps)//出栈
{
	assert(ps);
	assert(!StackEmpty(ps));
	ps->top--;
}

bool StackEmpty(ST* ps)//检测栈是否为空,如果为空返回非零结果,如果不为空返回0
{
	assert(ps);
	return ps->top == 0;
}

int StackSize(ST*ps)//获取栈中有效元素个数
{
	assert(ps);
	return ps->top;
}

STDatatype StackTop(ST*ps)//获取栈顶元素
{
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->a[ps->top-1];
}

void StackDestroy(ST*ps)//销毁栈
{
	assert(ps);
	if (ps->a)
	{
		free(ps->a);
	}
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}

栈的OJ链接:

给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。

有效字符串需满足:

左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。

//调用上述栈的实现的代码:

bool isValid(char * s)
{
    ST st;
    StackInit(&st);
    bool match = true;
    while(*s)
    {
        if(*s == '[' || *s == '(' || *s == '{')
        {
            StackPush(&st,*s);//如果等于 [,(,{ 就入栈
            s++;
        }
        else
        {
            if(StackEmpty(&st))
            {
                match = false;
                break;
            }
            
            char ch = StackTop(&st);//取栈顶的数据
            StackPop(&st);
            if(*s == ']' && ch != '['
            || *s == '}' && ch != '{'
            || *s == ')' && ch != '(')
            {
                match = false;
                break;
            }
            else
            {
                ++s;
            }
        }
    }
    if(match == true)
    {
        match = StackEmpty(&st);
    }
    StackDestroy(&st);
    return match;
}

2.队列

队列的概念及结构

队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out)

入队列:进行插入操作的一端称为队尾

出队列:进行删除操作的一端称为队头

队列的实现(代码类似单链表):Gitee队列的实现

这里采用结构体包一起的方法,因为队头head入数据,队尾tail出数据,要改变两者。

typedef int QDataType;

typedef struct QueueNode
{
	struct QueueNode* next;
	QDataType data;
}QueueNode;

//结构体包一起(结构体指针)
typedef struct Queue
{
	QueueNode* phead;
	QueueNode* ptail;
}Queue;

Queue.c文件: 

void QueueInit(Queue* pq)//队列的初始化
{
	assert(pq);
	pq->phead = NULL;
	pq->ptail = NULL;
}
void QueuePush(Queue* pq, QDataType x) //队尾入队列
{
	assert(pq);
	QueueNode*newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newnode == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;

	if (pq->ptail == NULL)
	{
		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}
}

void QueuePop(Queue* pq) //队头出队列
{
	assert(pq);
	assert(!QDataEmpty(pq));
	if (pq->phead->next == NULL)
	{
		free(pq->phead);
		pq->ptail = pq->phead = NULL;
	}
	else
	{
		QueueNode*next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}
	//思考这里会出现什么问题:
	//报错,野指针的问题
}

bool QDataEmpty(Queue* pq)
{
	assert(pq);
	return pq->ptail == NULL;
}

void QueueDestory(Queue* pq)
{
	assert(pq);
	QueueNode* cur = pq->phead;
	while (cur)
	{
		QueueNode* next = pq->phead->next;
		free(cur);
		cur = next;
	}
	pq->phead = pq->ptail = NULL;
}

int QueueSize(Queue* pq)
{
	assert(pq);
	QueueNode*cur = pq->phead;
	int size = 0;
	while (cur)
	{
		size++;
		cur = cur->next;
	}
	return size;
}

QDataType QueueFront(Queue* pq)//访问头的数据
{
	assert(pq);
	assert(!QDataEmpty(pq));
	return pq->phead->data;
}

QDataType QueueBack(Queue* pq)//访问尾的数据
{
	assert(pq);
	assert(!QDataEmpty(pq));
	return pq->ptail->data;
}

队头出队列的代码中,思考会出现什么问题?

void QueuePop(Queue* pq) //队头出队列
{
	assert(pq);
	assert(!QDataEmpty(pq));
	
		QueueNode*next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	//思考这里会出现什么问题:
	//报错,野指针的问题
}

扩展:环形队列OJ练习

实际中我们有时还会使用一种队列叫循环队列。如操作系统课程讲解生产者消费者模型
时可以就会使用循环队列。环形队列可以使用数组实现,也可以使用循环链表实现。

这里使用数组实现,不适合用单链表实现

第一,创建一个结点的结构,然后创建k+1个结点,链接起来

第二:单链表天生不适合取前一个

typedef struct 
{
    int*a;
    int front;
    int rear;
    int k;
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k) 
{
    MyCircularQueue*q = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    q->a = (int*)malloc(sizeof(int)*(k+1));
    q->front = 0;
    q->rear = 0;
    q->k = k;
    return q;
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) 
{
    assert(obj);
    return obj->front == obj->rear;
}

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

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) 
{
    assert(obj);
    if( myCircularQueueIsFull(obj))//如果满了则返回false
    return false;

    obj->a[obj->rear] = value;//如果为空,则放入value
    obj->rear++;
    //思考:边界问题?
    if(obj->rear == obj->k+1)
      obj->rear = 0;
        //或者
    //obj->rear %= (obj->k+1);
    return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) 
{
    assert(obj);
     if(myCircularQueueIsEmpty(obj))
     {
           return false;
     }
     ++obj->front;
    //同上
     obj->front %= (obj->k+1);
     return true;   
}

int myCircularQueueFront(MyCircularQueue* obj)
{
    assert(obj);
     if(myCircularQueueIsEmpty(obj))
     {
           return -1;
     }
     return obj->a[obj->front];
}

int myCircularQueueRear(MyCircularQueue* obj) 
{
     assert(obj);
     if(myCircularQueueIsEmpty(obj))
     {
           return -1;
     }
     int prevRear = obj->rear-1;
     //当rear为0的时候,返回第k个
     if(obj->rear == 0)
     prevRear = obj->k;
     return obj->a[prevRear];
}

void myCircularQueueFree(MyCircularQueue* obj) 
{
    assert(obj);
    free(obj->a);
    free(obj);

}
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值