Data Structure Learning 5-queue

Source: Internet
Author: User

Queue: a first-in-first-out linear table that can be inserted at one end (at the end of the Team) and deleted at the other end (at the head of the team. Unlike stack insertion and deletion, these operations are performed at the top of the stack.

Here we only talk about queue chain storage. International practice, first go to the source code

1 # include <stdio. h> 2 # include <stdlib. h> 3 # include <malloc. h> 4 5 // define the queue 6 typedef struct node {7 int data; 8 struct node * Next; 9} queue; 10 // define the opponent pointer and the team's tail pointer 11 typedef struct pointer {12 queue * front; // the team's first pointer. The opponent pointer does not store the queue element 13 queue * rear; // team end pointer, which stores the data element 14} qpointer; 15 16 // The queue initializes 17 void queueinit (qpointer * QP) 18 {19 queue * que; 20 que = (queue *) malloc (sizeof (Queue); 21 que-> next = NULL; 22 // the start and end of the team point to the same memory space, the pointer field is null 23 QP-> front = que; 24 QP-> rear = que; 25} 26 27 // determines whether the queue is empty. If it is null, 1 is returned, if not empty, 0 28 int isempty (qpointer * QP) 29 {30 // judgment method is returned: whether the opponent pointer and team end pointer are the same 31 if (QP-> front = QP-> rear) 32 {33 return 1; 34} 35 return 0; 36} 37 38 // insert data element: 1 is returned for successful insertion, and 0 39 is returned for failed insertion. Int queuepush (qpointer * Qp, int element) 40 {41 queue * que; 42 que = (queue *) malloc (sizeof (Queue); 43 If (que = NULL) 44 {45 return 0; 46} 47 que-> DATA = element; 48 que-> next = NULL; 49 QP-> rear-> next = que; // Insert the node to the end of the queue 50 QP-> rear = que; // adjust the team end pointer 51 return 0; 52} 53 54 // delete data element: return 1 If deletion is successful, return 0 if deletion fails 55 int queuepop (qpointer * Qp, int * element) 56 {57 queue * que; 58 If (isempty (qP) 59 {60 return 0; 61} 62 que = QP-> front-> next; // que points to the next node of the queue header node, that is, the real 63 * element = que-> data; // 64 QP-> front-> next = que-> next; 65 // determine whether the queue has only one element. 66 If (QP-> rear = que) 67 {68 QP-> rear = QP-> front; 69} 70 free (que); 71 return 1; 72} 73 74 int main () 75 {76 qpointer * QP; 77 int X; 78 // initialize the queue 79 QP = (qpointer *) malloc (sizeof (qpointer); 80 queueinit (qP); 81 printf ("input positive integers: \ n "); 82 scanf ("% d", & X); 83 while (x> 0) 84 {85 queuepush (qP, x); 86 scanf ("% d ", & X); 87} 88 // output queue: First-> end 89 queue * P = QP-> front-> next; 90 if (P = NULL) 91 return 0; 92 printf ("queue element: \ n"); 93 while (p) 94 {95 printf ("% d", p-> data ); 96 p = p-> next; 97} 98 printf ("\ n"); 99 // Delete queue 100 printf ("delete queue: \ n "); 101 while (queuepop (qP, & X) 102 {103 printf ("% d", x); 104} 105 printf ("\ n "); 106 // release memory space 107 P = QP-> front; 108 free (p); 109 free (qP); 110 111 return 0; 112}

1. queue definition: In addition to defining the data structure of nodes in the queue, it also defines the head and end of the queue to facilitate operations on the queue, the queue operation only needs to be performed on the finger truth and the team tail pointer in the pointer struct.

2. Determine if it is null
When the first and tail pointers of a team only need the same address, the queue is empty. If the queue is empty, there is no data element in the queue. Note that the frontend is only the first node of the queue and does not represent the actual first node of the queue. When the queue is not empty, the actual first node of the queue should be the next node of the first node.

3. insert data elements

Insert at the end of the team. After the insert, the newly inserted node becomes the end of the team.

4. delete data elements

Deleting is performed at the beginning of the team. When the actual team is at the end of the team, deleting a data in the queue becomes an empty Queue (Rear = Front ). Finally, do not forget to release the memory space of the deleted data.

5. Notes

When the memory is finally released, you must first release the memory space pointed to by the first pointer and the last pointer in the pointer, and then release the memory space pointed to by the pointer struct.

By using GDB debugging, we can see that the memory model of the pointer struct is as follows:

After the queue is initialized, no data is inserted. Pointer struct pointer variable QP points to 0x804b008. Because QP has two pointer variables, it occupies 8 bytes of memory space and stores the front (pointing to 0x804b018) pointer variables and rear (pointing to 0x804b018) pointer variable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.