Competition Practice Issue 15: Electronic Society September 2021 Youth Software Programming (graphic) level test papers (level 3) …

2023-01-23   ES  

1, icon

1. The node icon in the queue

Includes the value domain (the value of the preservation node), the pointer domain (refers to the next node), each node also has a memory address xxxx.

2. Quest icon

Set two pointers, one team first, and one team.

When initialized (init), allocate a memory area, front and rear are pointing here

FRONT’s memory block, the entire life cycle of the stack is not saved with valid data

rear points to the end of the team. When Rear and Front are the same, it means that the queue is empty

a. Initialization operation is as shown in the figure:

b. The operation of the team is as shown in the figure:

FRONT pointer unchanged

rear pointer points to the node to the team, that is, the end of the team

When the second node enters the team, the queue is shown in the figure:

c.

rear pointer unchanged

FRONT points to the next node, indicating that this node is already invalid.

The node 1 in Figure 18 becomes the head of the team, indicating that the value domain 1 of node 1 is equal to invalid.

d. The queue is empty as shown in the figure:

FRONT and Rear point to the same memory, that is, the two are equal

2. Realization

twenty two# /*********************************************** ************************
*Function: Implement the basic operation of the queue
*Description: The chain storage of the queue, and its departure, team, team length, display and other operations
*Method: Allocate a piece of memory, the team’s first Front and the team end rear pointer are pointing to the memory,
But this piece of memory does not save valid data,
During the operation, the memory pointed by FRONT has never been saved.
Therefore, the queue is the condition of the aircraft is front == rear
It is equivalent to wasting the memory area of a queue element (node),
But it provides great convenience for program implementation
*Time: 2010-11-27
*************************************************** ******************

#include <stdio.h>
#include <malloc.h>

// —————————————————– ——————————————————————-

TypeDef int Elemtype;

TypeDef Struct Node {// Quest node structure definition
ELEMTYPE VALUE; // The value of the node
struct node* next; // Next node address
} Node;

TypeDef Struct {// queue structure definition
Node* front; // team first
Node* rear; // team tail
} Queue;

// ————————– Queen operation ———————————————————————— ———————————————————————-

// Initialize queue
void initQueue (queue* queue) {
queue-> front = queue-> rear = (node*) malloc (sizeof (node));
Queue-> Front-> Next = NULL; // is equivalent to rear-> next = null
}

// Clear queue
void Clean (queue* queue) {
Node* pNode,* tmp;
pNode = queue-> front-> Next;
While (pNode! = NULL) {// traversed the queue to release each node memory
tmp = pNode; // Record the memory address to be released
pNode = pNode-> Next; // The address of the next node
Free (TMP);
}
}
void destroy (queue* queue) {
Clean (queue); // clear the queue first, release memory
Free (queue-> front); // Release
}

// Destruction queue

// join the team
void enqueue (queue* queue, node* node) {
/*
*Explanation: There can be another better way here
* Allocate a Node size memory, and then copy the data in NODE to the memory place
* In this way, when the memory area pointed to the Node, after the value of the memory is modified by the NODE pointer outside the ENQUEUE function,
* The value saved in the queue will not change
* This function is simple, not using this approach
*/
node-> next = null;
queue-> rear-> next = node;
queue-> rear = queue-> rear-> Next;
}

// depart from the team
Node* dequeue (queue* queue) {
Node* del = null;
ifue-> Front == Queue-> Rear) // Air Team
Return null;
DEL = Queue-> Front-> Next; // Non-empty
queue-> Front = Queue-> Front-> Next;
Return del;
}

// queue length
Int length (queue* queue) {
int i = 0;
Node* pNode;

pNode = queue-> front-> next; // point to the first node of the queue
While (pNode! = Null) {
i ++;
pNode = pNode-> Next;
}
Return i;
}

// Display queue node
void nodedisplay (node* node) {
/*
*Description: When the data type of the node is not int type
* Rewrite this function
*/
Printf (“value = %d/n”, node-> value);
}

// Display queue
void queuedisplay (queue* queue) {
Node* pNode;
pNode = queue-> front-> Next; // The first event
While (pNode! = Null) {// empty table
Nodedisplay (pNode);
pNode = pNode-> Next;
}
}

// —————————————————- ———————————————————————-

void main () {
Queue* linkqueue;
Node*node1,*node2,*node3,*node4,*node5;

node1 = (node*) malloc (sizeof (node));
node2 = (node*) malloc (sizeof (node));
node3 = (node*) malloc (sizeof (node));
node4 = (node*) malloc (sizeof (node));

linkqueue = (queue*) malloc (sizeof (queue));

node1-> value = 1;
node2-> value = 2;
node3-> value = 3;
node4-> value = 4;

Initqueue (linkqueue);

Enqueue (linkqueue, node1);
Printf (“%D joins the team, the queue length is%d/n”, node1-> value, length (linkqueue));
Queuedisplay (linkqueue);

Enqueue (linkqueue, node2);
Printf (“%D joins the team, the queue length is%d/n”, node2-> value, length (linkqueue));
Queuedisplay (linkqueue);

node5 = dequeue (linkqueue);
Printf (“%D goes out, the queue length is%d/n”, node5-> value, length (linkqueue));
Printf (“”, length (linkqueue));
Queuedisplay (linkqueue);

Enqueue (linkqueue, node3);
Printf (“%D joins the team, the queue length is%d/n”, node3-> value, length (linkqueue));
Queuedisplay (linkqueue);

Enqueue (linkqueue, node4);
Printf (“%D joins the team, the queue length is%d/n”, node4-> value, length (linkqueue));
Queuedisplay (linkqueue);

}

source

Related Posts

java use EasyExcel to read the Excell table content

linux simply read and write a file

SpringBoot @Onetoone solves the two -way dead cycle of JPA/Return to JSON data dead loop

In the Vue project, realize the function of closing the browser before the browser, FELIX

Competition Practice Issue 15: Electronic Society September 2021 Youth Software Programming (graphic) level test papers (level 3) …

Random Posts

Mychrome Make a CHROME browser portable version

1st stage: java primary-primary

UVA10635 Prince and Princess (LCS to LIS)

1

[FPGA] The design of the single port RAA (synchronous reading, synchronous writing)