码迷,mamicode.com
首页 > 编程语言 > 详细

栈的C语言实现(链表和数组)

时间:2015-03-10 17:22:21      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:   数组栈   链表栈   

数组栈的实现

stack_array.h

#ifndef _stack_array_h

#define EmptyTOS (-1)
#define MinStackSize (5)
#define TRUE 1
#define FALSE 0

typedef int ElementType;

typedef struct StackArrayNode
{
    int Capacity;
    int TopOfStack;
    ElementType *Array;
}StackArrayNode,*Stack;

int IsEmpty(Stack S);
int IsFull(Stack S);
Stack CreatStack(int MaxStackSize);
void FreeStack(Stack S);
void Push(ElementType x, Stack S);
void Pop(Stack S);
ElementType Top(Stack S);
ElementType TopandPop(Stack S);

#endif

stack_array.c

#include <stdio.h>
#include <stdlib.h>
#include "stack_array.h"

int IsEmpty(Stack S)
{
    if(S->TopOfStack == EmptyTOS)
        return TRUE;
    else 
        return FALSE;
}

int IsFull(Stack S)
{
    if(S->TopOfStack == S->Capacity)
        return TRUE;
    else 
        return FALSE;
}



Stack CreatStack(int MaxStackSize)
{
    Stack S;

    if(MaxStackSize < MinStackSize)
        printf("Stack is too small\n");

    S = (Stack)malloc(sizeof(struct StackArrayNode));
    if(S == NULL)
        exit(FALSE);

    S->Array = (ElementType *)malloc(sizeof(ElementType)*MaxStackSize);
    if(S->Array == NULL)
        exit(FALSE);

    S->Capacity = MaxStackSize;
    S->TopOfStack = EmptyTOS;
}

void FreeStack(Stack S)
{
    if(S != NULL)
    {
        free(S->Array);
        free(S);
    }
}

void Push(ElementType x, Stack S)
{
    if(IsFull(S))
        printf("Stack is full\n");
    else
    {
        S->TopOfStack ++;
        S->Array[S->TopOfStack] = x;
    }
}

void Pop(Stack S)
{
    if(IsEmpty(S))
        printf("Stack is empty\n");
    else
    {
        S->TopOfStack --;
    }
}

ElementType Top(Stack S)
{
    if(!IsEmpty(S))
        return S->Array[S->TopOfStack];

    printf("Stack is Empty\n");
    return FALSE;

}

ElementType TopandPop(Stack S)
{
    if(!IsEmpty(S))
        return S->Array[S->TopOfStack--];
    printf("Stack is Empty\n");
    return FALSE;
}

int main()
{
    Stack S;
    S = CreatStack(10);
    Push(1,S);
    Push(2,S);
    Push(4,S);
    printf("%d\n",Top(S));
    Pop(S);
    printf("%d\n",Top(S));
    Pop(S);
    printf("%d\n",Top(S));
}

链表栈的实现

stack.h

#ifndef _Stack_h

#define TRUE 1
#define FALSE 0

typedef int ElementType;
typedef struct StackNode
{
    ElementType data;
    struct StackNode *next;
}StackNode,*Stack;

int isEmpty(Stack S);
Stack CreateStack(void);
void MakeEmpty(Stack S);
void Push(ElementType x, Stack S);
void Pop(Stack S);
ElementType Top(Stack S);

#endif   /*_Stack_h */

stack.c

#include <stdlib.h>
#include <stdio.h>
#include "stack.h"



int isEmpty(Stack S)
{
    if(S->next == NULL)
        return TRUE;
    else 
        return FALSE;
}

void MakeEmpty(Stack S)
{
    if(S == NULL)
        printf("fata error");
    else
    {
        while(!isEmpty(S))
            Pop(S);
    }
}

Stack CreateStack(void)
{
    Stack S = (Stack)malloc(sizeof(struct StackNode));
    if(S == NULL)
        printf("fata error");
    S->next = NULL;
    MakeEmpty(S);
    return S;
}

void DeleteStack(Stack S)
{
    MakeEmpty(S);
    free(S);
}

void Push(ElementType x, Stack S)
{

    Stack TmpStack = (Stack)malloc(sizeof(struct StackNode));
    if(TmpStack == NULL)
        printf("fata error");
    else
    {
        TmpStack->data = x;
        TmpStack->next = S->next;
        S->next = TmpStack;
    }

}

void Pop(Stack S)
{
    Stack TmpStack;
    if(isEmpty(S))
        printf("Stack is Empty");
    else
    {
        TmpStack = S->next;
        S->next = S->next->next;
        free(TmpStack);
    }

}

ElementType Top(Stack S)
{
    if(isEmpty(S))
        return FALSE;
    else
        return S->next->data;
}

int main()
{
    Stack S = CreateStack();
    Push(1,S);
    Push(2,S);
    Push(4,S);
    printf("%d\n",Top(S));
    Pop(S);
    printf("%d\n",Top(S));
    Pop(S);
    printf("%d\n",Top(S));
}

参考自《数据结构与算法分析》

栈的C语言实现(链表和数组)

标签:   数组栈   链表栈   

原文地址:http://blog.csdn.net/zwhlxl/article/details/44176655

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!