使用C语言实现链栈以及initialize,push,pop,isEmpty,getlength,destory等操作
2013-11-13 23:26
351 查看
本文使用了链栈,相对于顺序栈,链栈具有通常情况下不会出现栈满的情况
链栈和链表很相似。
由于栈的先进后出特性,栈在很多地方都很适用,比如
括号匹配,算术表达式求职,路径判断(走迷宫游戏)高级点的有函数的调用等等
总之,经常要接触栈
尤其是现在作为学生,一定要把数据结构这一块给弄好
作为链栈,那么还是需要定义一下存储数据的节点(数据对象)
typedef struct node{
Elem data;
struct node *next;
}SNode;
那么对应的操作(数据操作)有:initialize(),pop,push,isEmpty(), getLength(),destory()等
在贴代码完代码后,将总结一下本次编程过程中积累的几个经验
stack.h
stack.c
testStack.c
在编写的时候,这几个问题需要注意
1、testStac中的的这个语句
SNode *top;//top就指向这个栈的指针,top指向栈顶,我一般也会将其看做是这个栈的名字
2、关于为什么在initialize,push,pop,destory这几个函数传入的参数是SNode **top,我在之前在一篇博客指针还是指针的指针中作了一些介绍,一句话来表示就是pass by value 和pass by reference之间的区别
3、关于对野指针的处理,养成良好的编程习惯,malloc之后判断一下指针是否为NULL,free后将指针是设为NULL,声明指针时,将其赋值为NULL。这样可以更好的使用指针
链栈和链表很相似。
由于栈的先进后出特性,栈在很多地方都很适用,比如
括号匹配,算术表达式求职,路径判断(走迷宫游戏)高级点的有函数的调用等等
总之,经常要接触栈
尤其是现在作为学生,一定要把数据结构这一块给弄好
作为链栈,那么还是需要定义一下存储数据的节点(数据对象)
typedef struct node{
Elem data;
struct node *next;
}SNode;
那么对应的操作(数据操作)有:initialize(),pop,push,isEmpty(), getLength(),destory()等
在贴代码完代码后,将总结一下本次编程过程中积累的几个经验
stack.h
/* * 用链表实现栈,实现操作push、pop、isEmpty、getLength等 * 栈的top指针始终指向top节点, * SCU liujinmou */ #include <stdio.h> #include <stdlib.h> #define Elem int //define the node of the stack //we still use a struct to store the node typedef struct node{ Elem data; struct node *next; }SNode; //define the op //initialize the stack,if success return 1,else return -1 int initStack(SNode **top); //push,success,return 1,else return -1; int push(SNode **top,Elem e); //pop,return the Elem,else return -1 Elem pop(SNode **top); //judge whether the stack is empty,true return 1,no return 0,fail return -1 int isEmpty(SNode *top); //getLength int getLength(SNode *top); //destory the stack int destoryStack(SNode **top);
stack.c
#include "stack.h" int initStack(SNode **top){ *top=(SNode *)malloc(sizeof(SNode)); if((*top)!=NULL){ (*top)->data=0; (*top)->next=NULL; return 1;} else{ perror("allocate memory error!"); return -1; } } //push,success,return 1,else return -1; int push(SNode **top,Elem e){ SNode *temp; //judge the stack is available if(!(*top)){ perror("no such a stack"); return -1; } //push,first,the L is the top, temp=(SNode*)malloc(sizeof(SNode)); if(temp==NULL){perror("allocate memory error!");exit(-1);} temp->data=e; //push the node to the stack temp->next=*top; *top=temp; return 1; } //pop,return the Elem,else return -1 Elem pop(SNode **top){ SNode *temp; Elem e; if(!(*top)){ perror("no such a stack"); exit(-1); } temp=(*top); *top=(*top)->next; e=temp->data; free(temp); temp=NULL; return e; } //judge whether the stack is empty,true return 1,no return 0,fail return -1 int isEmpty(SNode *top){ // SNode *temp; // Elem e; if(!top){ printf("no such a stack\n"); exit(-1); } if(top->next==NULL){return 1;} else return 0; } //getLength int getLength(SNode *top){ int num=0; SNode *temp; if(!top){ printf("no such a stack\n"); exit(-1); } temp=top; while(temp->next!=NULL){ temp=temp->next; num++; } return num; } //destory the stack int destoryStack(SNode **top){ SNode *temp; temp=*top; if(*top==NULL){ perror("no such a stack"); exit(-1); } while((*top)->next!=NULL){ *top=(*top)->next; free(temp); temp=*top; } free(*top); *top=NULL; return 1; }
testStack.c
#include "stack.h" int main(){ SNode *top=NULL;//keeping a good coding habit, make the value of a new point to NULL int flag=0; int i=0; int length; Elem e; // if((length=getLength(top))>=0){ printf("the length of the stack is %d\n",length);} // if(flag=isEmpty(top)){printf("stack is empty\n");} //initialing flag=initStack(&top); if(flag){ printf("initialize success\n");} if(flag=isEmpty(top)){printf("after initialized the stack,the stack is empty\n");} if((length=getLength(top))>=0) printf("after initializd the stack,the length of the stack is %d\n",length); for(;i<10;i++){ flag=push(&top,i); if(flag) printf("push %d success\n",i); else printf("push error\n"); } if((length=getLength(top))>=0) printf("after pushed the elem ,the length of the stack is %d\n",length); for(i=0;i<length;i++){ e=pop(&top); printf("%d ",e); } printf("\n"); if((length=getLength(top))>=0) printf("after poped the elem,the length of the stack is %d\n",length); if(flag=destoryStack(&top)){printf("destory the stack success!\n");} if((length=getLength(top))>=0) printf("after destoried the stack,the length of the stack is %d\n",length); if(flag=isEmpty(top)){printf("after destoried the stack,the stack is empty\n");} return 0; }
在编写的时候,这几个问题需要注意
1、testStac中的的这个语句
SNode *top;//top就指向这个栈的指针,top指向栈顶,我一般也会将其看做是这个栈的名字
2、关于为什么在initialize,push,pop,destory这几个函数传入的参数是SNode **top,我在之前在一篇博客指针还是指针的指针中作了一些介绍,一句话来表示就是pass by value 和pass by reference之间的区别
3、关于对野指针的处理,养成良好的编程习惯,malloc之后判断一下指针是否为NULL,free后将指针是设为NULL,声明指针时,将其赋值为NULL。这样可以更好的使用指针
相关文章推荐
- 一张图展示:用两个栈来实现一个队列,完成队列的Push和Pop操作
- 算法2_实现一个栈,除了push和pop操作,还要实现min函数以返回栈中的最小值,时间复杂度都为O(1)(keep it up)
- 数据结构之栈的push与pop操作(顺序存储结构的c实现)
- 实现一个栈Stack,要求实现Push(出栈)、Pop(入栈)、Min(返回最小值的操作)的时间复杂度为O(1)
- 《剑指Offer》用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。java
- c++ 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 实现一个栈Stack,要求实现Push(出栈)、Pop(入栈)、Min(返回最小值的操作)的时间复杂度为O(1) /查找一个字符串中第一个只出现两次的字符
- 实现一个栈,要求实现Push(出栈)、Pop(入栈)、Min(返回最小值的操作)的时间复杂度为O(1)
- 011实现一个栈,除了push和pop操作,还要实现min函数以返回栈中的最小值,时间复杂度都为O(1)(keep it up)
- 【C语言】单链表的所有操作的实现(包括PopBack、PushBack、PopFront、PushFront、Insert)
- 实现一个 能在O(1)时间复杂度 完成 Push、Pop、Min操作的 栈
- 实现一个栈, 其 Push,Pop,Min 操作的 时间复杂度 均为 O( 1 )
- cc150:实现一个栈,除了push和pop操作,还要实现min函数以返回栈中的最小值。
- 链栈的基本操作实现(c语言)
- JAVA实现用两个栈来实现一个队列,完成队列的Push和Pop操作(《剑指offer》)
- 10.1-2 说明如何用一个数组A[1..n]来实现两个栈,使得两个栈中的元素总数不到n时,两者都不会发生上溢,注意PUSH和POP操作的时间应为O(1)。
- 实现一个栈,要求push,pop,Min的操作时间复杂度为O(1)
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 实现一个栈,使push,pop,min操作只需要o(1)时间