1. 程式人生 > >棧———鏈表實現

棧———鏈表實現

鏈表實現 != sta ack clu empty reat top code

棧的鏈表實現細節主要在Push()和Pop()例程中鏈表的實現不一樣。

Stack.c:

#ifndef STACK_H
#define STACK_H

typedef char ElementType;
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;

int IsEmpty(Stack S);
Stack CreatStack();
void MakeEmpty(Stack S);
void DiseposeStack(Stack S);
void Push(Stack S, ElementType X);
void Pop(Stack S); ElementType Top(Stack S); ElementType PopAndTop(Stack S); #endif

LinkedStack.c:

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

struct Node{
    ElementType Element;
    PtrToNode Next;
};

Stack CreatStack()
{
    Stack S;
    S = (Stack)malloc(sizeof(struct
Node)); if(S == NULL) { printf("sorry! malloc is failed!"); return NULL; } else { S->Next = NULL; MakeEmpty(S);//confirm the stack is emptystack } return S; } int IsEmpty(Stack S) { return S->Next == NULL; } void MakeEmpty(Stack S) {
if(S == NULL) printf("Please creatstack first!"); while(!IsEmpty(S)) Pop(S); } void DiseposeStack(Stack S) { PtrToNode P, Tmp; P = S->Next; S->Next = NULL; while(P != NULL) { Tmp = P->Next; free(P); P = Tmp; } } void Push(Stack S, ElementType x) { PtrToNode TmpCell; TmpCell = (Stack)malloc(sizeof(struct Node)); if(TmpCell == NULL) printf("Sorry! malloc is failed!"); else { TmpCell->Element = x; TmpCell->Next = S->Next; S->Next = TmpCell; } } void Pop(Stack S) { PtrToNode FirstCell; if(IsEmpty(S)) printf("Sorry the stack is empty!"); else { FirstCell = S->Next; S->Next = FirstCell->Next; //S->Next = S->Next->Next;//這樣最安全 free(FirstCell); } } ElementType Top(Stack S) { if(!IsEmpty(S)) return S->Next->Element; printf("Sorry! the stack is empty"); return 0; } ElementType PopAndTop(Stack S) { PtrToNode FirstCell; ElementType FirstElement; if(IsEmpty(S)) printf("Sorry the stack is empty!"); else { FirstCell = S->Next; FirstElement = S->Next->Element; S->Next = S->Next->Next; free(FirstCell); return FirstElement; } }

棧———鏈表實現