【數據結構】鏈棧

Lstack.h

#pragma once
#include<iostream>
#include <stdlib.h>
using namespace std;

#define  Elemtype int
typedef struct Nstack
{
    Elemtype data;
    Nstack* next;
}Node;//節點

typedef struct
{
    Node* top;
    int size;
}Lstack;//棧結構

void Init(Lstack *st)
{
    st->top = (Node*)malloc(sizeof(Node));
    if (!st->top)
        return;
    st->top->next= NULL;
    st->top = NULL;
    st->size = 0;
}


void Push(Lstack *st, Elemtype &x)
{
    Node* p = (Node*)malloc(sizeof(Node));
    if (!p)
        return ;
    p->data = x;
    p->next = st->top;
    st->top = p;
    st->size++;
}

bool Empty(Lstack *st)
{
    return st->size==0;
}

void Pop(Lstack *st, Elemtype&e)
{
    if (!Empty(st))
    {
        cout << "棧爲空,不能刪除!" << endl;
        return;
    }
    Node*cur = st->top;
    e = cur->data;
    st->top = st->top->next;
    free(cur);
    st->size--;
}



void Gettop(Lstack *st,Elemtype &e)
{
    if (!Empty(st))
    {
        cout << "棧爲空,沒有棧頂元素!" << endl;
        return;
    }
    Node* temp = st->top;
    e = st->top->data;
    cout << "棧頂元素:" << e << endl;
}


void Length(Lstack *st)
{
    cout << "棧長爲:" << st->size << endl;
}

void Clear(Lstack *st)
{
    if (Empty(st))
        return;
    st->size = 0;
    Node* p=NULL;
    while (st->top!=NULL)
    {
        p = st->top;
        st->top = p->next;
        free(p);
    }
}

void Destroy(Lstack *st)
{
    Clear(st);
    free(st);
    st = NULL;
}

void show(Lstack *st)
{
    Node* p = st->top;
    while (p)
    {
        cout << p->data;
        p = p->next;
        cout << "-->";
    }
    cout << "棧底"<< endl;
}

mian.cpp

#include"Lstack.h"


void main()
{
    Lstack q;
    Elemtype item;
    int select = 1;
    while (select)
    {
        cout << "**********************************" << endl;
        cout << "* [1] init        [2] push       *" << endl;
        cout << "* [3] gettop      [4] length     *" << endl;
        cout << "* [5] pop         [6] clear      *" << endl;
        cout << "* [7] destroy     [8] show       *" << endl;
        cout << "* [0] quit                       *" << endl;
        cout << "**********************************" << endl;
        cout << "please chose:>";
        cin >> select;
        switch (select)
        {
        case 1:
            Init(&q);
            break;
        case 2:
            cout << "請輸入要入棧的元素:";
            cin >> item;
            Push(&q, item);
            break;
        case 3:
            Gettop(&q, item);
            break;
        case 4:
            Length(&q);
            break;
        case 5:
            Pop(&q, item);
            cout << "出棧元素爲:" << item << endl;
            break;
        case 6:
            Clear(&q);
            break;
        case 7:
            Destroy(&q);
            cout<<"棧已經被銷燬!不能再插入數據!"<<endl;
            return;
        case 8:
            show(&q);
            break;
        case 0:
            cout << "退出成功!" << endl;
            break;
        default:
            break;
        }
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章