您的位置:首页 > 编程语言 > C语言/C++

使用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

/*
*	用链表实现栈,实现操作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。这样可以更好的使用指针
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐