1. 程式人生 > >c語言模擬實現棧的模板化

c語言模擬實現棧的模板化

       對於寫過表示式解析的同學一定不會陌生,我們需要兩個棧,一個是符號棧(char),一個是運算元棧(int).  經典的資料結構書中的棧,型別是定死了的。

所以能夠實現一個棧,自己指定型別,想必編寫接下來程式碼的心情要愉快的多。本實現過程採用巨集定義,這樣然介面看來就好像實現了模板化。這裡有篇csdn的文章也是實現了模板化的,但是它採用的是預處理運算子## 實現的,及字串化(stringification),具體連結:http://blog.csdn.net/kingofmiaomiao/article/details/2011195   (注意:字串化也是屬於巨集的範疇)

下面給出,本人實現的編碼,參考了一個名叫glu的開源專案中的array實現,在此表示感謝。

stack.h (這裡的push, pop因為大家應該都是再熟悉不過了,就沒有寫日誌了哈)

/**
 *該檔案實現了對棧操作的模板化,在實現過程中參考了一個開源的glu專案
 *The file has template the operation about stack by referencing a opensource project 'glu'
 *@author daniel
 *@date 2011-09-19
 */
#ifndef _STACK_H
#define _STACK_H
#include <string.h>
#include <stdlib.h>

#define STACK_INIT_SIZE 50
#define STACK_ERROR -1

#define initStack(type,number) \
	stack_do_alloc(sizeof(type), number)
#define push(type,stack,data) \
	(stack_error_msg = stack_resize((stack), (stack)->num + 1), \
	stack_error_msg != STACK_ERROR ? \
	 *((type *)((stack)->space + (stack)->num++ * (stack)->obj_size)) = data : data)
#define getTop(type,stack) \
	((stack)->num ? *((type *)((stack)->space + ((stack)->num - 1) * (stack)->obj_size)) : *((type *)0))
#define pop(type,stack) \
	((stack)->num ? ((stack)->num--, *((type *)((stack)->space + (stack)->num * (stack)->obj_size))): *((type *)0))
#define getNum(stack)	\
	((stack)->num)
#define isEmpty(stack)	\
	(0 == (stack)->num)
#define clearStack(stack) \
	((stack)->num = 0, \
	 (void) memset((stack)->space, 0, (stack)->n_size * (stack)->obj_size))
#define freeStack(stack) \
	(FREE((stack)->space), \
	 FREE(stack))

#  define ALLOC(type, num)	\
    ((type *) malloc(sizeof(type) * (unsigned long) (num)))
#  define REALLOC(type, obj, num)	\
    ((type *) realloc((void *) (obj), sizeof(type) * (unsigned long) (num)))
#  define FREE(obj)		\
    ((obj) ? (free((void *) (obj)), (obj) = 0) : 0)
#  define NIL(type)		((type *) 0)

#ifndef MAX
#  define MAX(a,b)		((a) > (b) ? (a) : (b))
#endif

int stack_error_msg;

typedef struct{
	char *space; // the head point
	int num; // number of stack elements
	int n_size; // size of stack or says capability
 	int obj_size; // size of each stack object
} stack_t;

stack_t * stack_do_alloc(int, int);
int stack_resize(stack_t *stack, int newnum);

#endif

stack.c

#include "stack.h"

stack_t * stack_do_alloc(int size, int number){
	stack_t *stack;

	stack = ALLOC(stack_t, 1);
	if(NIL(stack_t) == stack)
		return NIL(stack_t);
	
	stack->num = 0;
	stack->n_size = MAX(number, STACK_INIT_SIZE);
	stack->obj_size = size;
	stack->space = ALLOC(char, stack->n_size * stack->obj_size);
	if(NIL(char) == stack->space)
		return NIL(stack_t);

	(void) memset(stack->space,0,stack->n_size * stack->obj_size);
	return stack;	
}

int stack_resize(stack_t *stack, int newsize){
	int old_size;
	char *newspace, *pos;

	if(stack->n_size > newsize)
		return 0;
	
	old_size = stack->n_size;
	stack->n_size = MAX(stack->n_size * 2, newsize);
	newspace = REALLOC(char, stack->space, stack->n_size * stack->obj_size);
	if(NIL(char) == newspace){
		stack->n_size = old_size;		
		return -1;
	}
	stack->space = newspace;
	
	pos = stack->space + old_size * stack->obj_size;
	(void) memset(pos, 0, (stack->n_size - old_size) * stack->obj_size);
	return 0;
}
測試程式碼:
#include "stack.h"
#include <stdio.h>

int main(){
	
	stack_t *a = initStack(int, 10);
	push(int, a, 123);
	printf("getTop:%d\n", getTop(int, a));
	pop(int, a);
	freeStack(a);
	return 0;
}

相關推薦

c語言模擬實現模板

       對於寫過表示式解析的同學一定不會陌生,我們需要兩個棧,一個是符號棧(char),一個是運算元棧(int).  經典的資料結構書中的棧,型別是定死了的。所以能夠實現一個棧,自己指定型別,想必編寫接下來程式碼的心情要愉快的多。本實現過程採用巨集定義,這樣然介面看來

C語言模擬實現strstr函數,strrstr 函數

c語言 模擬實現 strstr strstr和strrstr已經算是字符串中相對比較難的了,但是只要我們善於分析,解剖字符串,就會化難為易。其實學習代碼的過程中需要我們靜下心來分析,理解。 srtstr函數的功能及用法 原型:char *strstr(const char *dst, const

c語言模擬實現memcpy

在c語言的庫函式中,有一個函式實現了陣列中元素的選擇性拷貝———memcpy; 除了利用庫函式之外,還可以通過自己的模擬實現元素的拷貝,先通過傳到呼叫函式的值來選擇要拷貝多少個元素;然後將所得到的陣列利用迴圈列印在螢幕上,完成拷貝。 #include<stdio.h> int *

c語言模擬實現strncat

       在c語言的庫函式中,strcat負責將一個字串加在另一個字串的後面,但他只能將一個字串的所有字元加在另一字串的後面,而strncat則可以選擇性的在字串後加字串,使追加字串更靈活,更安全。        在

c語言模擬實現strcat

      在庫函式中,strcat負責將兩個字串相連在一起,而在不使用庫函式的情況下,自己模擬實現一個my_strcat也可以實現這樣的效果。        先定義兩個字串,如果將字串arr2連線在arr1後,則必須保證arr1

C語言模擬實現【strncpy】 【strncat】 【strncmp】

模擬實現【strncpy】 char *my_strncpy(char* dest,char *src,size_t count) { char *ret = dest; assert(dest); assert(src); while(count)

c語言模擬實現順序表的所有介面

順序表的c語言實現 此專案建立了3個原始檔,test.h用來建立結構體和函式的宣告,main.c用來實現函式,test.c用來實現順序表的總邏輯。 test.h #include<stdio.h> #include<assert.h> #include&

C語言模擬實現簡易計算器(深度介紹switch分支語句)

switch語句是多分支選擇語句; switch(條件) { case1:(statement); case2(statement); … } #include<stdio.h> #include<stdlib.h> void menu

c語言模擬實現strcpy

//模擬實現strcpy    概念:              //strcpy(str1,st2)將陣列str2的值複製到陣列str1   解題思路:整體比較簡單                    1> 將數組裡的值一個個拷貝到另個數組;       

C語言模擬實現庫函式strlen的三種方法

學習之路無止境,漫漫長夜,我愛程式設計。 今天練習的是用C語言來簡單實現庫函式strlen。 首先strlen 函式的作用是求出字串長度,眾所周知,在C語言中,字串是以字串陣列的形式儲存的,並且以’\0’為結束標誌。先讓我們看看strlen庫函式的引數即返回值

C語言陣列實現操作

#include <stdio.h> #include <stdlib.h> int * array=NULL;//存放棧元素的陣列 int count=0;//當前棧的元素個數 //初始化棧 void init_stack(int

C語言模擬實現strncpy、strncpy、strncat、strstr和strrstr函式實現

以下是我用C語言模擬實現的部分字串函式: 1、strncpy函式的實現 #include<stdio.h> #include<assert.h> #include<

C語言模擬實現一個通訊錄,要求實現其新增 刪除 修改 查詢 模糊搜尋姓名和號碼 顯示和排序聯絡人資訊的功能

                題目:用C語言模擬實現一個通訊錄,要求實現其新增、刪除、修改、查詢、模糊搜尋姓名和號碼、顯示和排序

[c語言]模擬實現strncpy,strncat,strncmp

1.模擬實現strncpy //1.模擬實現strncpy(拷貝前n個字元) #include<stdio.h> #include<assert.h> char * my_strncpy(char *dest,const char *sr

C語言模擬順序的建立、入、出操作

#include<stdio.h> #include<stdlib.h> #define true 1 #define false 0 #define ok 1 #define error 0

資料結構---C語言陣列實現

https://blog.csdn.net/morixinguan/article/details/51374184 資料結構---棧(C語言陣列實現)   棧的全名稱為堆疊,棧其實就是與佇列相反的過程,佇列是先進先出,而棧便是先進後出了,如下圖:  

資料結構(c語言)——鏈儲存結構及實現

鏈棧:就是一種特殊的單鏈表,FILO(先進後出) 通常對於連結串列來說: 1.是不需要頭節點的。 2.基本不存在滿棧的情況,除非記憶體已沒有可用的空間。   不多bibi你們都懂哈,直接上程式碼:  鏈棧結構: typedef struct Stock

[原始碼和文件分享]C語言的基於實現的表示式求值

一、目的 理解中綴表示式求值的過程 理解中綴轉字尾表示式求值的過程 掌握堆疊的應用 二、問題描述 綴表示式,其中包含括號,加減乘除,乘方等運算,利用中綴表示式,對錶達式分析並求值 入的中綴表示式轉換為字尾形式,顯示字尾形式,並通過後綴形

資料結構之(Stack)的關於C語言實現

C語言實現棧共有兩種方法,一種是使用陣列的包含最大Size的棧,另外一種是運用連結串列的方法實現 關於陣列方法實現的程式碼如下: #include<stdio.h> /** * Your MinStack struct will be instantiate

【作業系統】C語言模擬作業系統實現磁碟排程演算法——電梯排程演算法

#include<stdio.h> #include<stdlib.h> #include<time.h> int *Init(int arr[]) { int