标签:blog http io ar sp on 文件 数据 2014
今天给大家介绍栈的链式结构,用dev-c++4.9.9.2调试通过,少废话直接上代码:
数据结构体存放文件stacklist.h文件如下
#ifndef _STACKLIST_H_ #define _STACKLIST_H_ typedef struct _Node { int data; struct _Node *pre; struct _Node *next; }Node,*pNode; typedef struct _Stack_Header { struct _Node *botton; struct _Node *top; int size; }Stack_Header,*pStack_Header; pStack_Header init_stack_list(void); pNode push_node(pStack_Header plist,int data); pNode pop_node(pStack_Header plist); int print_stack_list(pStack_Header plist); #endif
函数存放文件stacklist.c
/******************************* 时间:2014.12.12 作者:XIAO_PING_PING 内容:栈的链式数据结构 功能:学习些数据结构 ********************************/ #include <string.h> #include <stdlib.h> #include <conio.h> #include "stacklist.h" /*初始化一个堆栈链表*/ pStack_Header init_stack_list(void) { pStack_Header plist; pNode p; plist = (Stack_Header *)malloc(sizeof(Stack_Header)); plist->botton = NULL; plist->top = NULL; plist->size = 0; return plist; } /*往堆栈里面添加节点,数据data*/ pNode push_node(pStack_Header plist,int data) { pNode p; p = (Node *)malloc(sizeof(Node)); p->data = data; p->next = NULL; p->pre = plist->top; if((plist->top == NULL) && (plist->botton == NULL)) { plist->top = p; plist->botton = p; plist->size = 1; printf("入栈第1个节点\n"); return p; } plist->top->next = p; plist->top = p; plist->size += 1; printf("入栈第%d个节点\n",plist->size); return plist->top; } /*出栈删除节点*/ pNode pop_node(pStack_Header plist) { pNode p; if(0 == plist->size) { printf("栈区没有节点,无法完成出栈\n"); return plist->top; } p = plist->top; plist->top = plist->top->pre; if(NULL != plist->top) { plist->top->next = NULL; } else { plist->botton = NULL; } free(p); plist->size -= 1; printf("出栈第%d个节点\n",plist->size + 1); return plist->top; } /*打印入栈数据*/ int print_stack_list(pStack_Header plist) { pNode p; if((plist->top == plist->botton) && (0 == plist->size)) { printf("栈区没有节点\n"); return -1; } p = plist->botton; printf("从栈底开始打印数据\n"); while(plist->top != p) { printf("%d ",p->data) ; p = p->next; } printf("%d ",p->data) ; printf("\n打印完毕\n"); return 0; }
测试文件test.c
#include <string.h> #include <stdlib.h> #include <conio.h> #include "stacklist.h" int main() { pStack_Header plist; plist = init_stack_list(); push_node(plist,13); push_node(plist,1); push_node(plist,232); push_node(plist,143); print_stack_list(plist); pop_node(plist); pop_node(plist); pop_node(plist); //pop_node(plist); //pop_node(plist); printf("\n"); print_stack_list(plist); getch(); }
运行结果如下图:
标签:blog http io ar sp on 文件 数据 2014
原文地址:http://blog.csdn.net/xiao_ping_ping/article/details/41896319