In the afternoon, various queue operations were implemented using a linked list, including creating, inserting, deleting, reading, and deleting all elements. All elements were read and determined whether they were empty and cleared, the source code debugging has passed, as shown in the following figure:
[Cpp]
# Include "iostream"
Using namespace std;
Typedef struct student
{
Int data;
Struct student * next;
} Node; // defines the struct node
Typedef struct linkqueue
{
Node * first;
Node * rear;
} Queue; // defines the queue structure, first pointer and last pointer.
/*******************************
Function Name: void initqueue (queue * HQ)
Function: sets the frontend and backend pointers to null during initialization.
********************************/
Void initqueue (queue * HQ)
{
HQ-> first = HQ-> rear = NULL;
}
/**************************************
Function Name: queue * insert (queue * HQ, int x)
Function: adds a new element to the queue after the last node.
***************************************/
Queue * insert (queue * HQ, int x)
{
Node * s;
S = new node;
S-> data = x;
S-> next = NULL;
If (HQ-> rear = NULL)
{
HQ-> first = s;
HQ-> rear = s;
}
Else
{
HQ-> rear-> next = s;
HQ-> rear = s;
}
Return HQ;
}
/******************************
Function Name: int delqueue (queue * HQ)
Function: delete an element from the queue *
*******************************/
Int delqueue (queue * HQ)
{
Node * p;
Int temp;
/* Stop running if the blockchain team is empty */
If (HQ-> first = NULL)
{
Printf ("the queue is empty and cannot be deleted! ");
Exit (1 );
}
Temp = HQ-> first-> data;
/* Save the first element of the queue for returning */
P = HQ-> first;
/* Temporarily store the first pointer of the team to clear the team's tail node */
HQ-> first = p-> next;/* point the first pointer of the team to the next node */
/* If the deleted team is empty, the team tail pointer must be empty at the same time */
If (HQ-> first = NULL)
{
HQ-> rear = NULL;
}
Free (p);/* reclaim the first node of the original team */
Return temp;/* return the deleted first element value */
}
/*******************************
Function Name: int readqueue (queue * HQ)
Function: Read the first element of the queue *
********************************/
Int readqueue (queue * HQ)
{/* Stop running if the blockchain team is empty */
If (HQ-> first = NULL)
{
Cout <"the queue is empty and cannot be deleted! ";
Exit (1 );
}
Return HQ-> first-> data;/* return the first element of the Team */
}
/*************************************** **********
Function Name: int emptyqueue (queue * HQ)
Function: Check whether the team is empty. If it is empty, 1 is returned; otherwise, 0 is returned.
**************************************** ********/
Int emptyqueue (queue * HQ)
{
/* Determine whether any pointer at the beginning or end of the team is null */
If (HQ-> first = NULL)
{
Return 1;
}
Else
{
Return 0;
}
}
/***********************************
Function Name: void clearqueue (queue * HQ)
Function: Clear all elements in a chain *
***********************************/
Void clearqueue (queue * HQ)
{
Node * p = HQ-> first;/* assign the first pointer to p */
/* Delete each node in the queue in sequence, and leave the first pointer of the queue blank */
While (p! = NULL)
{
HQ-> first = HQ-> first-> next;
Free (p );
P = HQ-> first;
}
/* The first pointer of the team is empty after the loop ends */
HQ-> rear = NULL;/* leave the team's tail pointer blank */
}
/*******************************
Function Name: void readall (queue * HQ)
Function: all elements in the output chain
*********************************/
Void readall (queue * HQ)
{
Node * p = HQ-> first;
While (p! = NULL)
{
Cout <p-> data <endl;
P = p-> next;
}
}
Void main ()
{
Queue q;
Int a [5] = {1, 2, 3, 4, 5 };
Int I;
Initqueue (& q );
For (I = 0; I <5; I ++)
{
Insert (& q, a [I]);
} // Insert data 1, 2, 3, 4, 5 into the queue
Cout <endl <"read all data in the queue: \ n ";
Readall (& q); // read all data in the queue
Insert (& q, 60); // insert a data
Cout <"read first node:" <readqueue (& q) <endl; // read first element
Cout <endl;
While (! Emptyqueue (& q ))
{
Cout <"deleted node:" <delqueue (& q) <endl ;;
Cout <"remaining nodes" <endl;
Readall (& q );
}
Clearqueue (& q );
}
# Include "iostream"
Using namespace std;
Typedef struct student
{
Int data;
Struct student * next;
} Node; // defines the struct node
Typedef struct linkqueue
{
Node * first;
Node * rear;
} Queue; // defines the queue structure, first pointer and last pointer.
/*******************************
Function Name: void initqueue (queue * HQ)
Function: sets the frontend and backend pointers to null during initialization.
********************************/
Void initqueue (queue * HQ)
{
HQ-> first = HQ-> rear = NULL;
}
/**************************************
Function Name: queue * insert (queue * HQ, int x)
Function: adds a new element to the queue after the last node.
***************************************/
Queue * insert (queue * HQ, int x)
{
Node * s;
S = new node;
S-> data = x;
S-> next = NULL;
If (HQ-> rear = NULL)
{
HQ-> first = s;
HQ-> rear = s;
}
Else
{
HQ-> rear-> next = s;
HQ-> rear = s;
}
Return HQ;
}
/******************************
Function Name: int delqueue (queue * HQ)
Function: delete an element from the queue *
*******************************/
Int delqueue (queue * HQ)
{
Node * p;
Int temp;
/* Stop running if the blockchain team is empty */
If (HQ-> first = NULL)
{
Printf ("the queue is empty and cannot be deleted! ");
Exit (1 );
}
Temp = HQ-> first-> data;
/* Save the first element of the queue for returning */
P = HQ-> first;
/* Temporarily store the first pointer of the team to clear the team's tail node */
HQ-> first = p-> next;/* point the first pointer of the team to the next node */
/* If the deleted team is empty, the team tail pointer must be empty at the same time */
If (HQ-> first = NULL)
{
HQ-> rear = NULL;
}
Free (p);/* reclaim the first node of the original team */
Return temp;/* return the deleted first element value */
}
/*******************************
Function Name: int readqueue (queue * HQ)
Function: Read the first element of the queue *
********************************/
Int readqueue (queue * HQ)
{/* Stop running if the blockchain team is empty */
If (HQ-> first = NULL)
{
Cout <"the queue is empty and cannot be deleted! ";
Exit (1 );
}
Return HQ-> first-> data;/* return the first element of the Team */
}
/*************************************** **********
Function Name: int emptyqueue (queue * HQ)
Function: Check whether the team is empty. If it is empty, 1 is returned; otherwise, 0 is returned.
**************************************** ********/
Int emptyqueue (queue * HQ)
{
/* Determine whether any pointer at the beginning or end of the team is null */
If (HQ-> first = NULL)
{
Return 1;
}
Else
{
Return 0;
}
}
/***********************************
Function Name: void clearqueue (queue * HQ)
Function: Clear all elements in a chain *
***********************************/
Void clearqueue (queue * HQ)
{
Node * p = HQ-> first;/* assign the first pointer to p */
/* Delete each node in the queue in sequence, and leave the first pointer of the queue blank */
While (p! = NULL)
{
HQ-> first = HQ-> first-> next;
Free (p );
P = HQ-> first;
}
/* The first pointer of the team is empty after the loop ends */
HQ-> rear = NULL;/* leave the team's tail pointer blank */
}
/*******************************
Function Name: void readall (queue * HQ)
Function: all elements in the output chain
*********************************/
Void readall (queue * HQ)
{
Node * p = HQ-> first;
While (p! = NULL)
{
Cout <p-> data <endl;
P = p-> next;
}
}
Void main ()
{
Queue q;
Int a [5] = {1, 2, 3, 4, 5 };
Int I;
Initqueue (& q );
For (I = 0; I <5; I ++)
{
Insert (& q, a [I]);
} // Insert data 1, 2, 3, 4, 5 into the queue
Cout <endl <"read all data in the queue: \ n ";
Readall (& q); // read all data in the queue
Insert (& q, 60); // insert a data
Cout <"read first node:" <readqueue (& q) <endl; // read first element
Cout <endl;
While (! Emptyqueue (& q ))
{
Cout <"deleted node:" <delqueue (& q) <endl ;;
Cout <"remaining nodes" <endl;
Readall (& q );
}
Clearqueue (& q );
}
Running result:
[Cpp] <PRE class = cpp name = "code"> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>
<PRE> </PRE>