1. 程式人生 > >資料結構與演算法分析c語言描述(Mark Allen)--棧ADT連結串列實現

資料結構與演算法分析c語言描述(Mark Allen)--棧ADT連結串列實現

棧ADT連結串列實現

  • 使用連結串列儲存
  • 操作集合
    • 入棧push
    • 出棧pop
    • 清空
    • 初始化
    • 返回棧頂元素
    • 列印整個棧
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#ifndef _Stack_H
struct Node;
typedef int ElementType;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;

//列印整個棧
void PrintStack(Stack S);

//插入 n個隨機元素到站內
void
GetRandomStack(Stack S, int num); //判斷鏈棧是否為空 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 // !_Stack_H struct
Node { ElementType Element; PtrToNode Next; }; int main(int argc, char const *argv[]) { int choose; int num; Stack S = CreateStack(); printf("\t\t\t1.get a random stack.\n"); printf("\t\t\t2.is the stack empty?\n"); printf("\t\t\t3.get the top element of the stack.\n"
); printf("\t\t\t4.show a stack.\n"); printf("\t\t\t5.make empty a stack.\n"); printf("\t\t\t6.push a element.\n"); printf("\t\t\t7.pop a element.\n"); printf("\t\t\t0.exit.\n"); while (1) { scanf("%d", &choose); switch (choose) { case 1: printf("\tplease input a int K to get a random K-length stack.\n"); scanf("%d", &num); GetRandomStack(S, num); PrintStack(S); break; case 2: if (IsEmpty(S)) { printf("\tstack is empty.\n"); } else { printf("\tstack is not empty.\n"); } putchar('\n'); PrintStack(S); break; case 3: printf("the top element is %d.\n", Top(S)); break; case 4: printf("-------------------------------------\n"); PrintStack(S); break; case 5: MakeEmpty(S); PrintStack(S); printf("\t\t\tdone!\n"); break; case 6: printf("\t\t\tplease intput a number to push into stack.\n"); scanf("%d",&num); Push(num,S); PrintStack(S); break; case 7: Pop(S); printf("\t\t\tdone.\n"); PrintStack(S); break; case 0: return 0; break; } } system("pause"); return 0; } int IsEmpty(Stack S) { return S->Next == nullptr; } Stack CreateStack(void) { Stack S; S = (Stack)malloc(sizeof(struct Node)); if (S == nullptr) { printf("out of space!\n"); exit(1); } S->Next = nullptr; MakeEmpty(S); return S; } void MakeEmpty(Stack S) { if (S == nullptr) { printf("must use CreateStack first!\n"); } else { while (!IsEmpty(S)) { Pop(S); } } } void Pop(Stack S) { PtrToNode FirstCell; if (IsEmpty(S)) { printf("stack is already empty!.\n"); } else { FirstCell = S->Next; S->Next = S->Next->Next; free(FirstCell); } } void DisposeStack(Stack S) { if (S->Next == nullptr) { printf("stack is already empty.\n"); exit(2); } else { while (!IsEmpty(S)) { Pop(S); } free(S); } } void Push(ElementType X, Stack S) { PtrToNode TmpCell; TmpCell = (PtrToNode)malloc(sizeof(struct Node)); if (TmpCell == nullptr) { printf("out of space!\n"); exit(3); } else { TmpCell->Element = X; TmpCell->Next = S->Next; S->Next = TmpCell; } } ElementType Top(Stack S) { if (!IsEmpty(S)) { return S->Next->Element; } else { printf("empty stack.\n"); exit(10086); } } void PrintStack(Stack S) { PtrToNode tmp = S->Next; while (tmp != nullptr) { printf("\t\t%d\n", tmp->Element); tmp = tmp->Next; } } void GetRandomStack(Stack S, int num) { if (S == nullptr) { printf("create a stack first.\n"); exit(5); } else { MakeEmpty(S); ElementType TempElement; srand((unsigned)time(NULL)); for (int i = 0; i < num; i++) { TempElement = rand() % 51; Push(TempElement, S); } } }