/*
* queue.h
* * created on: 2011-10-7
* author: admin
*/#ifndef queue_h_
#define queue_h_
#include #include /* the data type that contained in the queue */
typedef int queuedata;
typedef struct _queuequeue, *pqueue;
/* initialize queue */
void queue_init(pqueue* ptr, size_t size);
/* free memory which the queue own*/
void queue_destroy(pqueue* ptr);
/* add data to queue */
int queue_push(pqueue ptr, queuedata val);
/* dequeue */
queuedata queue_pop(pqueue ptr);
/* get data at front of the queue */
queuedata queue_top(pqueue ptr);
/* return the size of queue */
size_t queue_size(pqueue ptr);
#endif /* queue_h_ */
/* * queue.c
* * created on: 2011-10-7
* author: admin
*/#include #include #include "queue.h"
/* initialize queue */
void queue_init(pqueue* ptr, size_t size)
(*ptr)->capacity = size;
(*ptr)->length = 0;
(*ptr)->index = 0;
(*ptr)->push_index = 0;
(*ptr)->data = (queuedata*)malloc(sizeof(queuedata) * size);
if ((*ptr)->data == null)
}/* free memory which the queue own*/
void queue_destroy(pqueue* ptr)
/* add data to queue */
int queue_push(pqueue ptr, queuedata val)
ptr->push_index = (ptr->push_index % ptr->capacity) + 1;
ptr->data[ptr->push_index - 1] = val;
ptr->length += 1;
return 1;
}/* dequeue */
queuedata queue_pop(pqueue ptr)
ptr->length -= 1;
ptr->index = ((ptr->index) % ptr->capacity) + 1;
return ptr->data[ptr->index - 1];
}/* get data at front of the queue */
queuedata queue_top(pqueue ptr)
/* return the size of queue */
size_t queue_size(pqueue ptr)
C語言動態陣列實現
環境 vs2015 1 標頭檔案 dynamicarray.h ifndef dynamicarray h 如果沒有定義 define dynamicarray h 則定義 include include include typedef struct dynamicarray dynamic arr...
C語言實現動態陣列
include include 要使用malloc是要包含此標頭檔案 include 要使用memset是要包含此標頭檔案 intmain for i 0 i 列印陣列 free p 釋放記憶體,malloc和free一定要記得成組使用,不然會導致程式吃記憶體 getchar 讓程式停頓,觀察輸出 ...
C語言 動態陣列的實現
動態陣列 dynamic array 顧名思義就是能改變大小的陣列。使用動態陣列可以有效的提高記憶體利用率。下面附上動態陣列的c語言實現 標頭檔案 dynamic array.h ifndef dynamic array h define dynamic array h ifdef cpluscpl...