【数据结构】链栈

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;
        }
    }

}

相关文章

【啊哈!算法】算法3:最常用的排序——快速排序       ...
匿名组 这里可能用到几个不同的分组构造。通过括号内围绕的正...
选择排序:从数组的起始位置处开始,把第一个元素与数组中其...
public struct Pqitem { public int priority; ...
在编写正则表达式的时候,经常会向要向正则表达式添加数量型...
来自:http://blog.csdn.net/morewindows/article/details/6...