代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
C栈
作者:
独孤影
/ 发布于
2013/12/26
/
833
/** * Azalea Chatting Server * * Copyright (C) 2013-2015 Rex Lee <duguying2008@gmail.com> * * This program is free and opensource software; * you can redistribute it and/or modify * it under the terms of the GNU General Public License */ #include "ichat.h" #include <stdarg.h> #include "stack.h" /** * @brief initial the stack * @details [long description] * */ Stack* stack_init(stype type){ Stack* stk; stk=(Stack*)malloc(sizeof(Stack)); memset(stk,0,sizeof(Stack)); stk->size=0; stk->top=NULL; stk->index=(StackNode**)malloc(sizeof(void*)); memset(stk->index,0,sizeof(void*)); if(type<0||type>4){ printf("%s\n", "stack type error!\n"); } if (type==ints) { stk->type=ints; }else if (type==floats) { stk->type=floats; }else if (type==strings) { stk->type=strings; }else if (type==doubles) { stk->type=doubles; }else { stk->type=autos; } return stk; } /** * basic push element into stack * @param stk the stack * @param elem the element:stack node */ void extern stack_push(Stack* stk,StackNode* elem){ ///temp node StackNode* ct; stk->index=(StackNode**)realloc(stk->index,sizeof(StackNode*)*(stk->size+1)); stk->index[stk->size]=elem; ct=stk->top; ///set top next node elem->next=ct; elem->prev=NULL; ///set this element as top stk->top=elem; ///if the top node hava next, set the prev of next as the top if(stk->top->next!=NULL){ stk->top->next->prev=stk->top; } ///increas the size stk->size++; } /** * push the element(which is int) into stack * @param stk [the stack] * @param value [the int element] */ int stack_push_int(Stack* stk,int value){ if (ints!=stk->type&&autos!=stk->type) { printf("%s\n", "error:the stack type error!need int."); return -1; } StackNode* elem=(StackNode*)malloc(sizeof(StackNode)); memset(elem,0,sizeof(StackNode)); elem->type=inte; elem->int_value=value; stack_push(stk,elem); } /** * @brief push the element(float) into stack * @details [long description] * * @param stk [description] * @param value [description] * @return [description] */ int stack_push_float(Stack* stk,float value){ if (floats!=stk->type&&autos!=stk->type) { printf("%s\n", "error:the stack type error!need float."); return -1; } StackNode* elem=(StackNode*)malloc(sizeof(StackNode)); memset(elem,0,sizeof(StackNode)); elem->type=floate; elem->float_value=value; stack_push(stk,elem); } /** * @brief push the element(double) into stack * @details [long description] * * @param stk [description] * @param value [description] * @return [description] */ int stack_push_double(Stack* stk,double value){ if (doubles!=stk->type&&autos!=stk->type) { printf("%s\n", "error:the stack type error!"); return -1; } StackNode* elem=(StackNode*)malloc(sizeof(StackNode)); memset(elem,0,sizeof(StackNode)); elem->type=doublee; elem->double_value=value; stack_push(stk,elem); } /** * @brief push the element(string:char*) into stack * @details [long description] * * @param stk [description] * @param value [description] * @return [description] */ int stack_push_string(Stack* stk,const char* value){ if (strings!=stk->type&&autos!=stk->type) { printf("%s\n", "error:the stack type error!"); return -1; } StackNode* elem=(StackNode*)malloc(sizeof(StackNode)); memset(elem,0,sizeof(StackNode)); elem->type=stringe; elem->string_value=(char*)value; stack_push(stk,elem); } /** * @brief pop the top element of stack * @details [long description] * * @param stk [description] */ void stack_pop(Stack* stk){ if(stk->size<=0){ return; } StackNode* next=stk->top->next; free(stk->top); if (NULL!=next) { next->prev=NULL; } stk->top=next; stk->size--; stk->index=(StackNode**)realloc(stk->index,sizeof(void*)*stk->size); } /** * @brief get the stack node by the index is reverse order(索引倒序) * @details [long description] * * @param stk [description] * @param i [description] * @return [description] */ StackNode* stack_get(Stack* stk,int i){ if(stk->size>=i){ return stk->index[i]; }else{ return NULL; } } /** * @brief set the node by index * @details [long description] * * @param stk sokect * @param i index * @param node the node * @return [description] */ void stack_set(Stack* stk,int i,StackNode* node){ StackNode* old_node; if(stk->size>=i){ old_node=stk->index[i]; node->next=old_node->next; old_node->next->prev=node; old_node->prev->next=node; node->prev=old_node->prev; free(old_node); old_node=NULL; stk->index[i]=node; } } /** * @brief create a int stack node * @details [long description] * * @param value [description] * @return [description] */ StackNode* stack_node_int(int value){ StackNode* sn=(StackNode*)malloc(sizeof(StackNode)); sn->type=inte; sn->int_value=value; return sn; } /** * @brief create a string stack node * @details [long description] * * @param value [description] * @return [description] */ StackNode* stack_node_float(float value){ StackNode* sn=(StackNode*)malloc(sizeof(StackNode)); sn->type=floate; sn->float_value=value; return sn; } /** * @brief create a double stack node * @details [long description] * * @param value [description] * @return [description] */ StackNode* stack_node_double(double value){ StackNode* sn=(StackNode*)malloc(sizeof(StackNode)); sn->type=doublee; sn->double_value=value; return sn; } /** * @brief create a string stack node * @details [long description] * * @param value [description] * @return [description] */ StackNode* stack_node_string(const char* value){ StackNode* sn=(StackNode*)malloc(sizeof(StackNode)); sn->type=stringe; sn->string_value=(char*)value; return sn; } /** * @brief reverse the stack * @details [long description] * * @param stk [description] * @return [description] */ Stack* stack_reverse(Stack* stk){ ; } /** * @brief destroy the stack * @details [long description] * */ void stack_destroy(Stack* stk){ int i; int node_num=stk->size; for (i = 0; i < node_num; ++i) { stack_pop(stk); } free(stk->index); free(stk); stk=NULL; } ////////////////////////////////////////////////////////////////// /** * @brief print the whole stack * @details show the elements of the stack * * @param stack the pointer of stack */ void stack_print(Stack* stack){ StackNode* psn=stack->top; printf("the stack(size:%d) content is:\n", stack->size); printf("%s\n", "-----------------------------"); printf("%s\n", "TOP"); while(psn!=NULL){ if(psn->type==inte){ printf("[int] \t\t%d\n",psn->int_value); }else if(psn->type==floate){ printf("[float] \t\t%f\n",psn->float_value); }else if(psn->type==stringe){ printf("[string]\t\t%s\n",psn->string_value); }else if(psn->type==doublee){ printf("[double]\t\t%lf\n",psn->double_value); } psn=psn->next; } printf("%s\n", "BOTTOM"); printf("%s\n", "-----------------------------"); } void stack_reverse_print(Stack* stack){ StackNode* psn=stack->index[0]; printf("the stack(size:%d) content is:\n", stack->size); printf("%s\n", "-----------------------------"); printf("%s\n", "BUTTOM"); while(psn!=NULL){ if(psn->type==inte){ printf("[int] \t\t%d\n",psn->int_value); }else if(psn->type==floate){ printf("[float] \t\t%f\n",psn->float_value); }else if(psn->type==stringe){ printf("[string]\t\t%s\n",psn->string_value); }else if(psn->type==doublee){ printf("[double]\t\t%lf\n",psn->double_value); } psn=psn->prev; } printf("%s\n", "TOP"); printf("%s\n", "-----------------------------"); }
试试其它关键字
C栈
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
独孤影
贡献的其它代码
(
3
)
.
C栈
.
目录操作类
.
php+js在线剪切图片
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3