c語言之 有頭迴圈雙鏈表實現棧儲存

2021-06-22 21:04:24 字數 3440 閱讀 7329

棧的原理是先進的後出來

1、鍊錶的實現

dlist.h

/*

*dlist.h

*描述:

* 有頭迴圈雙表

*data:

* 2014-07-21

*/#pragma once

#include #include #include struct node_info

;struct list_info

;void list_init(struct list_info*);

void list_destroy(struct list_info*);

#define par(node,type) ((type*)node->par)

#define list_for_each(info, cur) \

for (cur = (info)->head.next; \

(cur) != &(info)->head; \

cur = (cur)->next)

#define list_for_each_safe(info, cur, tmp) \

for (cur = (info)->head.next, tmp = (cur)->next; \

(cur) != &(info)->head; \

cur = tmp, tmp = (tmp)->next)

dlist.c

/*

*dlist.c

* */

#include "dlist.h"

static void list_add(struct list_info *info,size_t data_size,const void *data_entry)

static void list_add_tail(struct list_info *info,size_t data_size,const void *data_entry)

static void list_get(struct list_info *info,size_t data_size,void *data_entry)

static void list_get_tail(struct list_info *info,size_t data_size,void *data_entry)

static void list_del(struct list_info *info,struct node_info *node)

static int list_isempty(struct list_info *info)

void list_init(struct list_info *info)

void list_destroy(struct list_info *info)

}

2、棧的實現

stack.h

/*

* stack.h

* */

#pragma once

#include "dlist.h"

struct stack_info

;struct stack_info* stack_init(struct stack_info *);

void stack_destroy(struct stack_info *);

stack.c

/*

* stack.c

* */

#include "stack.h"

static void stack_push(struct stack_info *info,size_t size,const void *data_entry)

static int stack_top(struct stack_info *info,size_t size,void *data_entry)

/* * 如果棧不為空,就獲得鍊錶的第乙個資料

*/info->list.get(&info->list,size,data_entry);

return 0;

}static int stack_pop(struct stack_info *info,size_t size,void *data_entry)

else }

static int stack_isempty(struct stack_info *info)

struct stack_info *stack_init(struct stack_info *info)

void stack_destroy(struct stack_info *info)

3、測試**

test.c

/*

* test.c

* */

#include "stack.h"

struct data_info

;int main()

, [1] = ,

[2] =

};stack_init(&stack);

size_t i = 0;

for(i = 0; i < sizeof(s)/sizeof(struct data_info);i ++)

if(stack.isempty(&stack))

else

struct data_info tmp;

if(stack.top(&stack,sizeof(struct data_info),&tmp))

printf("no top data!\n");

else

stack_destroy(&stack);

printf("after destroy stack ... \n");

if(stack.isempty(&stack))

else

return 0;

}

4、makfile

all:test

test:test.o dlist.o stack.o

gcc -o $@ $^

test.o:test.c

gcc -c test.c

dlist.o:dlist.c

gcc -c dlist.c

stack.o:stack.c

gcc -c stack.c

clean:

rm *.o test

5、執行結果

stack is not empty!

top data is : name peter , age is 21

after destroy stack ...

stack is empty!

C語言實現迴圈雙鏈表

include include include typedef int datatype typedef struct node linklist linklist init list 初始化迴圈雙鏈表 bool creat list linkl程式設計客棧ist l 建立鍊錶 int length...

C語言之單鏈表實現

鍊錶是一種物理儲存單元上非連續 非順序的儲存結構,資料元素的邏輯順序是通過鍊錶中的指標鏈結次序實現的。鍊錶由一系列結點 鍊錶中每乙個元素稱為結點 組成,結點可以在執行時動態生成。每個結點包括兩個部分 乙個是儲存資料元素的資料域,另乙個是儲存下乙個結點位址的指標域。相比於線性表順序結構,操作複雜。由於...

c語言之進製轉換(棧實現)

從上兩篇部落格中我們可以知道,棧具有後進先出的特性,而進製轉換的列印輸出剛好與計算過程相反,滿足棧這後進先出的特性,所以可以用棧很快的實現進製轉換,下面是用棧實現進製轉換的c函式 void conversion sqstack pstack,unsigned int n,const unsigned...