C language implements classic data structure code---stack and queue

Table of contents

the stack

Introduction

Using an array to implement a stack

queue

Introduction

Implementing a Queue Using an Array


the stack

Introduction

A stack is a common data structure that follows the "first in, last out" principle, that is, the last element that enters the stack is accessed first. The implementation of the stack can use an array or a linked list to store data.

Using an array to implement a stack

Define the stack structure : First, you need to define a structure to represent the stack, which contains an array for storing data and a pointer representing the top position of the stack.

#define MAX_SIZE 100

typedef struct Stack {
    int data[MAX_SIZE];
    int top;
} Stack;

Initialize the stack : set the top pointer of the stack to -1, indicating that the stack is empty.

void initialize(Stack* stack) {
    stack->top = -1;
}

Push operation : To insert an element into the top position of the stack, it is necessary to increase the top pointer of the stack by 1 and store the element in the top position of the stack.

void push(Stack* stack, int item) {
    if (stack->top == MAX_SIZE - 1) {
        printf("Stack is full\n");
    } else {
        stack->data[++stack->top] = item;
    }
}

Pop operation : To remove an element from the top position of the stack, it is necessary to decrease the top pointer of the stack by 1 and return the top element of the stack.

int pop(Stack* stack) {
    if (stack->top == -1) {
        printf("Stack is empty\n");
        return -1;
    } else {
        return stack->data[stack->top--];
    }
}

Get the top element of the stack : Return the element at the top of the stack without removing it from the stack.

int peek(Stack* stack) {
    if (stack->top == -1) {
        printf("Stack is empty\n");
        return -1;
    } else {
        return stack->data[stack->top];
    }
}

Determine whether the stack is empty : judge whether the stack is empty according to whether the top pointer of the stack is -1.

int isEmpty(Stack* stack) {
    return stack->top == -1;
}

Determine whether the stack is full : judge whether the stack is full according to whether the top pointer of the stack reaches the maximum index of the array.

int isFull(Stack* stack) {
    return stack->top == MAX_SIZE - 1;
}

A stack implemented using an array has a fixed size, and no more elements can be inserted when the stack is full. If you need to implement a stack that can dynamically expand in size, you can use a linked list to implement stack operations.

queue

Introduction

A queue is a common data structure that follows the "first in, first out" principle, that is, the elements that enter the queue first are accessed first. Queue implementations can use arrays or linked lists to store data.

Implementing a Queue Using an Array

Define the queue structure : First, you need to define a structure to represent the queue, which contains an array for storing data, a front pointer indicates the first element position of the queue, and a rear pointer indicates the last element position of the queue.

#define MAX_SIZE 100

typedef struct Queue {
    int data[MAX_SIZE];
    int front;
    int rear;
} Queue;

Initialize the queue : set both the front and rear pointers to -1, indicating that the queue is empty.

void initialize(Queue* queue) {
    queue->front = -1;
    queue->rear = -1;
}

Enqueue operation : To insert an element at the end of the queue, the rear pointer needs to be increased by 1, and the element is stored at the rear pointer position.

void enqueue(Queue* queue, int item) {
    if (queue->rear == MAX_SIZE - 1) {
        printf("Queue is full\n");
    } else {
        if (queue->front == -1) {
            queue->front = 0;
        }
        queue->data[++queue->rear] = item;
    }
}

Dequeue operation : To remove an element from the first element position of the queue, it is necessary to increase the front pointer by 1 and return the removed element.

int dequeue(Queue* queue) {
    if (queue->front == -1 || queue->front > queue->rear) {
        printf("Queue is empty\n");
        return -1;
    } else {
        return queue->data[queue->front++];
    }
}

Determine whether the queue is empty : judge whether the queue is empty according to the positional relationship between the front and rear pointers.

int isEmpty(Queue* queue) {
    return queue->front == -1 || queue->front > queue->rear;
}

Judging whether the queue is full : judge whether the queue is full according to whether the rear pointer reaches the maximum index of the array.

int isFull(Queue* queue) {
    return queue->rear == MAX_SIZE - 1;
}

A queue implemented using an array has a fixed size and no more elements can be inserted when the queue is full. If you need to implement a queue that can dynamically expand the size, you can use a linked list to implement the operation of the queue.

Guess you like

Origin blog.csdn.net/L888666Q/article/details/131415726