#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
using namespace std;
typedef struct node
{
    int sum;
    struct node *next;
}Node;
void welcome();
void menu(); 
void insert(Node *head);// 1
void deletes(Node *head);// 2
void empty(Node *head);// 3
void counts(Node *head);// 4
void judge1(Node *head);// 5
void judge2(Node *head);// 6
int main()
{
    welcome();
    int flag;
    Node *head,*pre,*current,*next;
    pre=(Node *)malloc(sizeof(Node));
    head=(Node *)malloc(sizeof(Node));
    next=(Node *)malloc(sizeof(Node));
    head->next=NULL;
    while (1)
    {
        //输出菜单 
        menu();
        //进行选择 
        cin>>flag;
        cout<<endl;
        if (flag==2||flag==3||flag==4||flag==6)
        system("CLS");
        switch (flag)
        {
            case 1:
                insert(head);
                break;
            case 2:
                deletes(head);
                break;
            case 3:
                empty(head);
                head->next=NULL;
                break;
            case 4:
                counts(head);
                break;
            case 5:
                judge1(head);
                break;
            case 6:
                judge2(head);
                break;
            case 0:
                return 0;
        }
        //输出
        cout<<"进行操作后栈中元素如下\n";
        pre=head->next;
        while (pre!=NULL)
        {
            cout<<pre->sum<<endl;
            pre=pre->next; 
        }
        cout<<endl;
    }
}
void welcome()
{
    system("title 栈模拟程序");
    system("color f0");
    system("date /T");
    system("TIME /T");
    printf("\n\n\n\n\n\n");
    printf("\t\t\t\t\t~********* 8.栈模拟程序 ***********~\n");
    printf("\n\n\n\n");
    printf("\t\t\t\t\t~**** 制作者: Keven 2018年2月 *****~\n");
    printf("\n\n\n\n");
    printf("\t\t\t\t\t~******** 按任意键进入程序 ********~\n");
    printf("\n\n\n\n\n\n\n");
    system("pause");
    system("CLS");
}
void menu()
{
    cout<<"----------菜单-----------"<<endl;
    cout<<"输入1存入数据"<<endl<<"输入2取出数据"<<endl;
    cout<<"输入3清空栈"<<endl<<"输入4统计栈中数据个数"<<endl;
    cout<<"输入5判断给定数据是否存在"<<endl<<"输入6判断栈是否为空"<<endl;
    cout<<"输入0结束程序"<<endl<<"-------------------------";
    cout<<endl<<"请输入指令:";
}
void insert(Node *head)
{
    int s=0;
    string flag;
    cout<<"(输入任意非数字字符即返回上一步)"<<endl<<"请输入需要存入的数据:";
    cin>>flag;
    system("CLS");
    for (int i=0;i<flag.size();i++)
    {
        if (!isdigit(flag[i]))
        return ;
        s+=(flag[i]-'0')*pow(10,flag.size()-1-i);
    }
    while (head->next!=NULL)
    head=head->next;
    Node *current;
    current=(Node *)malloc(sizeof(Node));
    head->next=current;
    current->next=NULL;
    current->sum=s;
}
void deletes(Node *head)
{
    //判断特殊情况
    if (head->next==NULL)
    {
        cout<<endl<<"此时栈中已经没有数据了,请重新选择命令:"<<endl;
        return ;
    }
    Node *pre,*current;
    pre=(Node *)malloc(sizeof(Node));
    current=(Node *)malloc(sizeof(Node));
    pre=head;
    current=pre->next;
    while (current->next!=NULL)
    {
        pre=current;
        current=current->next;
    }
    cout<<"取出栈中的数据为"<<current->sum<<endl<<endl;
    pre->next=NULL;
    free(current);
}
void empty(Node *head)
{
    Node *current;
    current=(Node *)malloc(sizeof(Node));
    head=head->next;
    //释放链表 
    while (head!=NULL)
    {
        current=head;
        head=current->next;
        free(current);
    }
}
void counts(Node *head)
{
    int count=0;
    head=head->next;
    while (head!=NULL)
    {
        head=head->next;
        count++;
    }
    cout<<"栈中数据有"<<count<<"个数据"<<endl<<endl;
}
void judge1(Node *head)
{
    cout<<endl;
    int s=0;
    string flag;
    cout<<"请输入需要判断的是否存在的数据:"; 
    cin>>flag;
    system("CLS");
    for (int i=0;i<flag.size();i++)
    {
        if (!isdigit(flag[i]))
        return ;
        s+=(flag[i]-'0')*pow(10,flag.size()-1-i);
    }
    head=head->next;
    while (head!=NULL)
    {
        if (head->sum==s)
        {
            cout<<"栈中存在数据"<<s<<endl<<endl;
            return ;
        }
        head=head->next;
    }
    cout<<"栈中不存在数据"<<s<<endl<<endl;
}
void judge2(Node *head)
{
    if (head->next==NULL)
    cout<<"栈是空的"<<endl<<endl;
    else
    cout<<"栈不是空的"<<endl<<endl;
}