专栏申请成功了,挺高兴的,以后好好努力,将自己的数据结构与算法学习之路雕刻与此!
/*---------------------------------------------------------------------------------
* Project: Stack.h
* Name: zwp
* Date: 2014/3
*---------------------------------------------------------------------------------*/
#ifndef STACK_H_
#define STACK_H_
struct Node;
typedef struct Node *Stack;
typedef int ElementType;
/*
** 判断空
*/
int IsEmpty(Stack S);
/*
** 创建栈
*/
Stack CreateStack(void);
/*
** 创建空栈
*/
void MakeEmpty(Stack S);
/*
** 入栈
*/
void Push(ElementType X, Stack S);
/*
** 出栈
*/
ElementType Top(Stack S);
/*
** 销毁栈顶元素
*/
void Pop(Stack S);
#endif
/*-------------------------------------------------------------------------
* Project: Stack.cpp
* Name: zwp
* Date: 2014/3
*-------------------------------------------------------------------------*/
#include "Stack.h"
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct Node
{
ElementType Element;
Stack Next;
};
/*
** 判断栈是否为空
*/
int IsEmpty(Stack S)
{
return S->Next == NULL;
}
/*
** 创建栈
*/
Stack CreateStack(void)
{
Stack S;
S = (Stack)malloc(sizeof(struct Node));
if(S == NULL)
printf("Out of space...\n");
S->Next = NULL;
MakeEmpty(S);
return S;
}
/*
** 创建空栈
*/
void MakeEmpty(Stack S)
{
if(S == NULL)
printf("Must use Create Stack....\n");
else
while(!IsEmpty(S))
Pop(S);
}
/*
** 入栈
*/
void Push(ElementType X, Stack S)
{
Stack TmpCell;
TmpCell = (Node*)malloc(sizeof(Node));
if(TmpCell == NULL)
printf("Out of space....\n");
else
{
TmpCell->Element = X;
TmpCell->Next = S->Next;
S->Next = TmpCell;
}
}
/*
** 弹栈
*/
ElementType Top(Stack S)
{
if(!IsEmpty(S))
return S->Next->Element;
printf("Empty Stack.....\n");
return NULL;
}
/*
** 删除栈顶元素
*/
void Pop(Stack S)
{
Stack FirstCell;
if(IsEmpty(S))
printf("Empty Stack...\n");
else
{
FirstCell = S->Next;
S->Next = S->Next->Next;
free(FirstCell);
}
}
/*-------------------------------------------------------------------------------
* Project: Main.cpp
* Name: zwp
* Date: 2014/3
*-------------------------------------------------------------------------------*/
#include "Stack.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
int i = 0;
Stack s = CreateStack();
for(i = 0; i < 100; ++ i)
Push(i+1, s);
for(i = 0; i < 100; ++ i)
{
printf("%d \n", Top(s));
Pop(s);
}
system("pause");
return 0;
}
下面是数组实现:
/*-----------------------------------------------------------------------------
* Project: Stack.h
* Name: zwp
* Date: 2014/4
*------------------------------------------------------------------------------*/
#ifndef STACK_H_
#define STACK_H_
typedef int ElementType;
typedef struct Node
{
int Capacity;
int Top;
ElementType *Array;
}*Stack;
/*
** Initialize
*/
Stack CreateStack(int MaxElement);
/*
** DisposeStack
*/
void DisposeStack(Stack S);
/*
** IsEmpty
*/
int IsEmpty(Stack S);
/*
** MakeEmpty
*/
void MakeEmpty(Stack S);
/*
** Push
*/
void Push(ElementType X, Stack S);
/*
** Pop
*/
void Pop(Stack S);
/*
** Top
*/
ElementType Top(Stack S);
/*
** IsFull
*/
int IsFull(Stack S);
#endif
/*-----------------------------------------------------------------------------------
* Project: Stack.cpp
* Name: zwp
* Date: 2014/4
*-----------------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "Stack.h"
/*
** Initialize
*/
Stack CreateStack(int MaxElement)
{
Stack S;
S = (Stack)malloc(sizeof(struct Node));
if(S == NULL)
printf("Out of space....\n");
S->Array = (ElementType*)malloc(sizeof(ElementType) * 100);
if(S->Array == NULL)
printf("Out of space...\n");
S->Capacity = 100;
MakeEmpty(S);
return S;
}
/*
** DisposeStack
*/
void DisposeStack(Stack S)
{
if(S != NULL)
{
free(S->Array);
free(S);
}
}
/*
** IsEmpty
*/
int IsEmpty(Stack S)
{
return S->Array ? 0 : 1;
}
/*
** MakeEmpty
*/
void MakeEmpty(Stack S)
{
S->Top = 0;
}
/*
** Push
*/
void Push(ElementType X, Stack S)
{
if(IsFull(S))
printf("Full stack....\n");
else
S->Array[++S->Top] = X;
}
/*
** Pop
*/
void Pop(Stack S)
{
if(IsEmpty(S))
printf("Empty stack....\n");
else
S->Top--;
}
/*
** Top
*/
ElementType Top(Stack S)
{
if(!IsEmpty(S))
return S->Array[S->Top];
printf("Empty Stack....\n");
return 0;
}
/*
** IsFull
*/
int IsFull(Stack S)
{
return (S->Top == 100) ? true : false;
}
/*-------------------------------------------------------------------------------
* Project: Main.cpp
* Name: zwp
* Date: 2014/4
*--------------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include "Stack.h"
int main(int argc, char* argv[])
{
Stack S = CreateStack(100);
for(int index = 0; index < 100; ++ index)
Push(index, S);
printf("\n\n");
for(int index = 0; index < 100; ++ index)
{
printf("%d \n", Top(S));
Pop(S);
}
system("pause");
}