Implementation of the chain storage structure of the queue 2-calculate the number of elements at the beginning and end of the queue for viewing the queue

Source: Internet
Author: User

Implementation of the chain storage structure of the queue 2-calculate the number of elements at the beginning and end of the queue for viewing the queue

// Filename: list_queue.c // Author: LupingChen/Data: 2015.05.30 // Content: create \ destory \ full \ empty \ push # include <stadio. h> # include <stdlib. h> // define the Node data type typedef struct Node {int data; // The Node data struct Node * next; // record the next Node address} Node; // define the Queue data type typedef struct {Node * head; // header pointer} Queue; // output int pop (Queue * pq ); // view the first element int get_head (Queue * pq); // view the last element int get_tail (Queue * pq ); // calculate the int size (Queue * pq) of all elements in the Queue); Int main (void) {return 0;} // departure int pop (Queue * pq) {if (empty (pq) {return-1 ;} // find a replacement Node for the header pointer * p = pq-> head; p-> head = p-> next; // store the node data to be released int temp = p-> data; free (p); p = NULL; // return the released node data return temp ;} // view the first element int get_head (Queue * pq) {if (empty (pq) {return-1; // use-1 to indicate an error} return pq-> head-> data;} // view the team end element int get_tail (Queue * pq) {// The queue is empty. if (empty (pq) {return-1;} // The queue is not empty. // substitute Node * p = pq-> h Ead; // move the pointer cyclically and locate the last node while (p-> next! = NULL) {p = p-> next;} // return the end node data return p-> data;} // calculate the number of all elements in the Queue int size (Queue * pq) {int cnt = 0; Node * p = pq-> head; while (p! = NULL) {cnt ++; p = p-> next;} return cnt ;}

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.