Stack implementation in C language and C Language

Source: Internet
Author: User

Stack implementation in C language and C Language

In C ++, you can use std: priority_queue to use the heap.

Stack C language implementation:

Heap. c

1/*** @ file heap. c 2 * @ brief heap. The default value is a small root heap, that is, the minimum heap top. 3 */4 # include <stdlib. h>/* for malloc () */5 # include <string. h>/* for memcpy () */6 typedef int heap_elem_t; // element type 7 8/** 9 * @ struct 10 * @ brief heap struct 11 */12 typedef struct heap_t 13 {14 int size; /** actual number of elements */15 int capacity;/** capacity, in the unit of elements */16 heap_elem_t * elems; /** heap array */17 int (* cmp) (const heap_elem_t *, const heap_elem_t *); 18} heap_t; 19 20/** element comparison function */21/** basic types (such as int, long, float, double) comparison function */22 int cmp_int (const int * x, const int * y) 23 {24 const int sub = * x-* y; 25 if (sub> 0) 26 {27 return 1; 28} 29 else if (sub <0) 30 {31 return-1; 32} 33 else 34 {35 return 0; 36} 37} 38 39/** 40 * @ brief heap initialization. 41 * @ param [out] h heap Object Pointer 42 * @ param [out] capacity initial capacity 43 * @ param [in] cmp comparison function, less than-1 returned, equal to return 0 44 * greater than return 1, the reverse is the big root heap 45 * @ return success return 0, failure return Error Code 46 */47 int heap_init (heap_t * h, const int capacity, int (* cmp) (const heap_elem_t *, const heap_elem_t *) 48 {49 h-> size = 0; 50 h-> capacity = capacity; 51 h-> elems = (heap_elem_t *) malloc (capacity * sizeof (heap_elem_t); 52 h-> cmp = cmp; 53 return 0; 54} 55 56/** 57 * @ brief release the heap. 58 * @ param [inout] h heap Object Pointer 59 * @ return success return 0, failure return Error Code 60 */61 int heap_uninit (heap_t * h) 62 {63 h-> size = 0; 64 h-> capacity = 0; 65 free (h-> elems); 66 h-> elems = NULL; 67 h-> cmp = NULL; 68 return 0; 69} 70 71/** 72 * @ brief determine whether the heap is empty. 73 * @ param [in] h heap Object Pointer 74 * @ return is null, return 1, otherwise return 0 75 */76 int heap_empty (const heap_t * h) 77 {78 return h-> size = 0; 79} 80 81/** 82 * @ brief get the number of elements. 83 * @ param [in] s heap Object Pointer 84 * @ return number of elements 85 */86 int heap_size (const heap_t * h) 87 {88 return h-> size; 89} 90 91/* 92 * @ brief the top-down filtering algorithm of the small root heap. 93 * @ param [in] h heap Object Pointer 94 * @ param [in] start Node 95 * @ return No 96 */97 void heap_sift_down (const heap_t * h, const int start) 98 {99 int I = start; 100 int j; 101 const heap_elem_t tmp = h-> elems [start]; 102 for (j = 2 * I + 1; j 

 

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.