【来吧~数据结构】之“栈”

栈—ADT

  栈的定义:

  栈是限制插入和删除只能在一个固定的位置上,称之为栈的顶(top)。

    栈的基本操作,最终要的是两个。

        1.Push(进栈)

        2.Pop(出栈)

众所周知,栈的特性是LIFO-后进先出,在我们日常生活中也能看到很多栈的例子,例如放薯片的圆桶

如图:

 

       如果我们不破坏这个桶的话,我们是不可能直接拿出最下面的薯片的,我们只能从最上面的薯片开始拿,直到上面的都拿出来我们才能拿出最后一个。而我们拿出来的最后一个其实是放进去的第一个。而拿出来的第一个其实是放进去的最后一个。这就是栈的结构特性。

栈的抽象结构:

在上面这个抽象结构中,栈顶元素是8

栈的实现

栈的实现方式一般有两种,一种是链表,一种是数组,都是依靠更简单的数据结构来实现的。

我主要利用链表实现栈

 

Filename:stack.h

 

 

#ifndef STACK_H

struct Node;

typedef struct Node *PtrNode;

typedef PtrNode Stack;

typedef int ElementType;

int IsEmpty(Stack S);

Stack CreateStack(void);

void DisposeStack(Stack S);

void MakeEmpty(Stack S);

void Push(ElementType x,Stack S);

ElementType Top(Stack S);

void Pop(Stack S);

#endif

 

  


 

FileName: stack.c

 

/*

 

* =====================================================================================

 

*

 

* Filename: stack.c

 

*

 

* Description: the implementation file of stack

 

*

 

* Version: 1.0

 

* Created: 2012年02月22日 18时43分01秒

 

* Revision: none

 

* Compiler: gcc

 

*

 

* Author: trexliu

* Organization:

 

*

 

* =====================================================================================

 

*/

 

#include <stdlib.h>

 

#include <stdio.h>

 

#include "stack.h"

 

typedef struct Node

 

{

 

ElementType Element;

 

PtrNode Next;

 

}Node;

 

 

 

 

 

/*

 

* === FUNCTION ======================================================================

 

* Name: IsEmpty

 

* Description:

判断栈是否为空

* =====================================================================================

 

*/

 

int

 

IsEmpty (Stack S )

 

{

 

 

 

return S->Next==NULL;

 

}	 /* ----- end of function IsEmpyt ----- */

 

 

 

/*

 

* === FUNCTION ======================================================================

 

* Name: CreateStack()

 

* Description: 创建一个空的栈,只有head。

* =====================================================================================

 

*/

 

Stack CreateStack()

 

{

 

Stack S;

 

S=malloc(sizeof(struct Node));

 

if(S==NULL){

 

perror("Out of space!!");

 

}

 

S->Next=NULL;

 

MakeEmpty(S);

 

return S;

 

 

 

}

 

 

 

 

 

/*

 

* === FUNCTION ======================================================================

 

* Name: MakeEmpty

 

* Description: 清空栈。通过遍历栈并不断弹出栈顶元素来实现。

 

* =====================================================================================

 

*/

 

void

 

MakeEmpty (Stack S)

 

{

 

if(S==NULL)

 

printf("Must use CreateStack first");

 

else

 

while(!IsEmpty(S))

 

Pop(S);

 

}	 /* ----- end of function MakeEmpty ----- */

 

 

 

/*

 

* === FUNCTION ======================================================================

 

* Name: Push

 

* Description:

把新的元素压入栈中,新元素位置在栈顶元素之前

* =====================================================================================

 

*/

 

void

 

Push (ElementType x,Stack S)

 

{

 

PtrNode TmpCell;

 

TmpCell=malloc(sizeof(struct Node));

 

if(TmpCell==NULL)

 

perror("Out of space");

 

else

 

{

 

TmpCell->Element=x;

 

TmpCell->Next=S->Next;

 

S->Next=TmpCell;

 

}

 

}	 /* ----- end of function Push ----- */

 

 

 

 

 

/*

 

* === FUNCTION ======================================================================

 

* Name: Pop

 

* Description: 弹出栈顶元素

* =====================================================================================

 

*/

 

void

 

Pop (Stack S )

 

{

 

PtrNode	FirstCell;

 

if(IsEmpty(S))

 

perror("Empty stack");

 

else

 

{

 

FirstCell=S->Next;

 

S->Next=S->Next->Next;

 

 

free(FirstCell);

 

}

 

}	 /* ----- end of function Pop ----- */

 

 

 

/*

 

* === FUNCTION ======================================================================

 

* Name: Top

 

* Description:

返回栈顶元素。

* =====================================================================================

 

*/

 

ElementType

 

Top (Stack S )

 

{

 

if(!IsEmpty(S))

 

return S->Next->Element;

 

perror("Empty stack");

 

return 0;

 

 

}	 /* ----- end of function Top ----- */

 

 

 

 

 

/*

 

* === FUNCTION ======================================================================

 

* Name: PrintStack

* Description: 打印栈中所有的元素。

* =====================================================================================

 

*/

 

void

 

PrintStack (Stack S)

 

{

 

 

if(S==NULL)

 

perror("Empty stack");

 

while(!IsEmpty(S))

 

{

 

printf(“%d”,Top(S));

Pop(S);

 

}

 

}	 /* ----- end of function PrintStack(Stack S) ----- */

 

 

 

 

 

 

 

int

 

main ( int argc, char *argv[] )

 

{

 

Stack SS;

 

SS=CreateStack();

 

Push(1,SS);	//first element

 

Push(2,SS);	//second

 

Push(3,SS);	//third

 

Push(4,SS);	//forth

 

PrintStack(SS);

 

return 0;

 

}	 /* ---------- end of function main ---------- */

 

main程序中,我们压栈的顺序是1->2->3->4,程序的运行结果如下

 所以,元素的出栈顺序和压栈顺序相反的,既4->3->2->1

OK~栈的内容也算看完了。下次就看队列啦~~

posted @ 2012-02-22 20:48  芬达·橙  阅读(219)  评论(1)    收藏  举报