Queue operations using the C linked list

Source: Internet
Author: User

Operation queue, but this time it is changed to a linked list, but you should pay attention to the operations that come out of the queue.

The general idea is to delete the next node of the header node when leaving the queue. In this way, all nodes in the queue can be deleted, but if we delete the last node, our algorithm assigns the tail Pointer a NULL value, and a segment error will occur when the queue is added. Why? Because the value of the tail pointer is already NULL, and then the tail node is connected to the node, it will access illegal memory space. Therefore, when we leave the queue, we delete the header node directly, and then point the head pointer to the next node, so that the next node becomes the first node, and it is naturally not a member of the queue.

The source code is as follows:

/*************************************************************************> File Name: linkqueue.c> Author: Baniel Gao> Mail: createchance@163.com > Blog: blog.csdn.net/createchance > Created Time: Fri 20 Dec 2013 01:53:21 PM CST ************************************************************************/#include 
 
  #include 
  
   typedef struct _nodequeue_ {int data;struct _nodequeue_ *next;} nodequeue_st;typedef struct _linkqueue_ {int total;int current;nodequeue_st *head;nodequeue_st *tail;} linkqueue_st;linkqueue_st *linkqueue_init(int size);nodequeue_st *linkqueue_node_create(int value);int linkqueue_enqueue(linkqueue_st *queue, int value);int linkqueue_isfull(linkqueue_st *queue);int linkqueue_dequeue(linkqueue_st *queue, int *value);int linkqueue_isempty(linkqueue_st *queue);int linkqueue_free(linkqueue_st *queue);int main(void){linkqueue_st *queue = NULL;int value = 100;int size = 10;queue = linkqueue_init(size);while (-1 != linkqueue_enqueue(queue, value))value++;while (-1 != linkqueue_dequeue(queue, &value))printf("%5d", value);putchar('\n');linkqueue_free(queue);return 0;}linkqueue_st *linkqueue_init(int size){linkqueue_st *queue;queue = (linkqueue_st *)malloc(sizeof(linkqueue_st));queue->total = size;queue->current = 0;queue->head = linkqueue_node_create(0);queue->tail = queue->head;return queue;}nodequeue_st *linkqueue_node_create(int value){nodequeue_st *node = NULL;node = (nodequeue_st *)malloc(sizeof(nodequeue_st));node->data = value;node->next = NULL;return node;}int linkqueue_enqueue(linkqueue_st *queue, int value){nodequeue_st *node = NULL;if (linkqueue_isfull(queue))return -1;node = linkqueue_node_create(value);node->next = queue->tail->next;queue->tail->next = node;queue->tail = node;queue->current++;return 0;}int linkqueue_dequeue(linkqueue_st *queue, int *value){nodequeue_st *tmp = NULL;if (linkqueue_isempty(queue))return -1;tmp = queue->head;*value = tmp->next->data;queue->head = queue->head->next;free(tmp);queue->current--;return 0;}int linkqueue_isfull(linkqueue_st *queue){if (queue->current == queue->total)return 1;return 0;}int linkqueue_isempty(linkqueue_st *queue){if (queue->current == 0)return 1;return 0;}int linkqueue_free(linkqueue_st *queue){nodequeue_st *node = queue->head;nodequeue_st *tmp = NULL;while (node != NULL) {tmp = node;node = node->next;free(tmp);}free(queue);return 0;}
  
 



Related Article

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.