[C Language] Stack and Queue - Meow Team, rush, rush, rush

Baozi, don’t you like it? Don't you want to comment? Don't you want to collect it?

Finally, follow me, follow me, follow me, you will see more interesting blogs! ! !

Meow meow meow, you are really important to me.

Table of contents

Preface

stack

Stack implementation

queue

Queue implementation

Summarize


Preface

Practice, practice, practice, practice a few more times to get the best answers. Put it down to your feet.


stack

Stack: a special linear list that only allows insertion at a fixed end and delete element operations. The end where data insertion and deletion operations are performed is called top of stack, and the other end is called bottom of stack . The data elements in the stack follow the LIFO (Last In First Out) principle.

Push: The stack'sinsertion operation is called push/push/push. The data is on top of the stack.

Pop : The deletion operation of the stack is called popping. The outgoing data is also on the top of the stack.

 Push: in, Pop: out


Stack implementation

The stack can generally be implemented usingarray or linked list. Relatively speaking, array structure implementation is better. Because the cost of inserting data at the end of the array is relatively small.

#include "common.h"

typedef int STDataType;
typedef struct Satck
{
	STDataType* _a;
	int _top;
	int _capacity;
}Stack;

void StackInit(Stack* ps, int n);
void StackPush(Stack* ps, STDataType x);
void StackPop(Stack* ps);
STDataType StackTop(Stack* ps);
int StackSize(Stack* ps);
// 空 0 非空1
int StackEmpty(Stack* ps);

void StackTest();
#include "Stack.h"

void StackInit(Stack* ps, int n)
{
	assert(ps);
	ps->_a = (STDataType*)malloc(sizeof(STDataType)*n);
	ps->_top = 0;
	ps->_capacity = n;
}

void StackPush(Stack* ps, STDataType x)
{
	assert(ps);
	if (ps->_top == ps->_capacity)
	{
		ps->_a = (STDataType*)realloc(ps->_a, ps->_capacity * 2*sizeof(STDataType));
		ps->_capacity *= 2;
	}

	ps->_a[ps->_top] = x;
	ps->_top++;
}

void StackPop(Stack* ps)
{
	assert(ps);
	if (ps->_top > 0)
	{
		ps->_top--;
	}
}

STDataType StackTop(Stack* ps)
{
	assert(ps);

	return ps->_a[ps->_top - 1];
}

int StackSize(Stack* ps)
{
	assert(ps);

	return ps->_top;
}

int StackEmpty(Stack* ps)
{
	assert(ps);

	return ps->_top == 0 ? 0 : 1;
}

void StackTest()
{
	Stack s;
	StackInit(&s, 3);

	StackPush(&s, 1);
	StackPush(&s, 2);
	StackPush(&s, 3);
	StackPush(&s, 4);
	
	while (StackEmpty(&s))
	{
		printf("top:%d\n", StackTop(&s));
		StackPop(&s);
	}
}

queue

Queue: A special linear table that only allows data insertion operations at one end and deletion data operations at the other end. The queue has a first-in, first-out function.

FIFO (First In First Out) Queue: The end that performs the insertion operation is called the end of the queueDequeue: The end that performs the deletion operation is calledqueue head


Queue implementation

Queue can also be implemented with array and linked list structures. It is better to use linked list structure, because if you use array structure, the dequeue will output data at the head of the array group. The efficiency will be lower.

#include "common.h"

typedef int QUDataType;

typedef struct QueueNode
{
	QUDataType _data;
	struct QueueNode* _next;
}QueueNode;

typedef struct Queue
{
	QueueNode* _front;
	QueueNode* _rear;
}Queue;

void QueueInit(Queue* q);
void QueueDestory(Queue* q);

void QueuePush(Queue* q, QUDataType x);
void QueuePop(Queue* q);
int QueueSize(Queue* q);
int QueueEmpty(Queue* q);
QUDataType QueueFront(Queue* q);
QUDataType QueueBack(Queue* q);
void QueueTest();
#include "Queue.h"

void QueueInit(Queue* q)
{
	assert(q);
	q->_front = q->_rear = NULL;
}

void QueueDestory(Queue* q)
{
	assert(q);
	QueueNode* cur = q->_front;
	while (cur)
	{
		QueueNode* next = cur->_next;
		free(cur);
		cur = next;
	}

	q->_front = q->_rear = NULL;
}

QueueNode* BuyQueueNode(QUDataType x)
{
	QueueNode* node = (QueueNode*)malloc(sizeof(QueueNode));
	node->_data = x;
	node->_next = NULL;
	return node;
}

void QueuePush(Queue* q, QUDataType x)
{
	assert(q);
	if (q->_front == NULL)
	{
		q->_front = q->_rear = BuyQueueNode(x);
	}
	else
	{
		q->_rear->_next = BuyQueueNode(x);
		q->_rear = q->_rear->_next;
	}
}

void QueuePop(Queue* q)
{
	if (q->_front)
	{
		QueueNode* next = q->_front->_next;
		free(q->_front);
		q->_front = next;

		if (q->_front == NULL)
		{
			q->_rear = NULL;
		}
	}
}

int QueueSize(Queue* q)
{
	assert(q);
	int size = 0;
	QueueNode* cur = q->_front;
	while (cur)
	{
		++size;
		cur = cur->_next;
	}

	return size;
}

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

QUDataType QueueFront(Queue* q)
{
	assert(q);
	return q->_front->_data;
}

QUDataType QueueBack(Queue* q)
{
	assert(q);

	return q->_rear->_data;
}

void QueueTest()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	QueuePush(&q, 4);

	while (QueueEmpty(&q))
	{
		printf("fornt:%d\n", QueueFront(&q));
		QueuePop(&q);
	}
}

In addition, let’s expand on this. In practice, we sometimes use a queue called a circular queue. For example, when the operating system course explains the producer-consumer model, you can use circular queues. A circular queue can be implemented using an array or a circular linked list.


20. Valid parentheses

Given a program that only includes '(', ')', '{', '}', '[', ']' string s , determine whether the string is valid.

A valid string must satisfy:

  1. The opening bracket must be closed by a closing bracket of the same type.
  2. Opening brackets must be closed in the correct order.
  3. Every right bracket has a corresponding left bracket of the same type.

Example 1:

输入:s = "()"
输出:true

Example 2:

输入:s = "()[]{}"
输出:true

Example 3:

Import:s = "(]"
Output:false

Valid bracket direct link

char pairs(char a) {
    if (a == '}') return '{';
    if (a == ']') return '[';
    if (a == ')') return '(';
    return 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;
}


Summarize

As always, meow~


Baozi, don’t you like it? Don't you want to comment? Don't you want to collect it?

Finally, follow me, follow me, follow me, you will see more interesting blogs! ! !

Meow meow meow, you are really important to me.

Supongo que te gusta

Origin blog.csdn.net/ormstq/article/details/132372244
Recomendado
Clasificación