【链性栈】基本链性栈的实现

#include<iostream>

using namespace std;

typedef struct _NODE_
{
    int a;
    _NODE_* pNext;
}Node,*pNode;


typedef struct _TABLE_
{
    pNode pHead;
    int iNodeCount;
}Table,*pTable;

void InitStack(pTable pTableTemp);

bool PushStack(pTable pTableTemp, int a);

bool PopStack(pTable pTableTemp,int* a);

bool GetTop(pTable pTableTemp,int* a);

bool IsEmpty(pTable pTableTemp);

void DestroyStack(pTable pTableTemp);

void ClearStack(pTable pTableTemp);

int GetLength(pTable pTableTemp);


int main()
{
    pNode pNodeTemp = {0};

    Table TableTemp = {0};

    InitStack(&TableTemp);

    int i = 0;
    int a = 0;
    int Length = 0;
    int temp = 0;
    
    cout<<"当前状态:"<<endl;
    if(IsEmpty(&TableTemp))
    {
        cout<<"栈中没有元素"<<endl;
    }
    else
    {
        cout<<"栈中存在元素"<<endl;
    }

    cout<<"请输入元素,进行入栈操作."<<endl;
    for(i=0;i<10;i++)
    {
        cin>>a;
        PushStack(&TableTemp,a);
    }
    
    if(IsEmpty(&TableTemp))
    {
        cout<<"栈中没有元素"<<endl;
    }
    else
    {
        cout<<"栈中存在元素"<<endl;
    }


    GetTop(&TableTemp,&a);

    cout<<"栈顶元素为"<<a<<endl;

    cout<<"出栈顺序:"<<endl;
    for(i=0;i<10;i++)
    {
        PopStack(&TableTemp,&a);
        cout<<a<<ends;
    }
    cout<<endl;


    return 0;
}

void InitStack(pTable pTableTemp)
{
    if(!IsEmpty(pTableTemp))
    {
        DestroyStack(pTableTemp);
        pTableTemp->iNodeCount = 0;
        pTableTemp->pHead = NULL;
    }
}


bool PushStack(pTable pTableTemp, int a)
{
    pNode pNodeTemp = new Node;

    if(pNodeTemp != NULL)
    {
        pNodeTemp->a = a;

        pNodeTemp->pNext = NULL;

        pNodeTemp->pNext = pTableTemp->pHead;

        pTableTemp->pHead = pNodeTemp;

        pTableTemp->iNodeCount++;

        return true;
    }

    return false;
}


bool PopStack(pTable pTableTemp,int* a)
{
    if(pTableTemp->iNodeCount == NULL)
    {
        return false;
    }

    *a = pTableTemp->pHead->a;

    pNode pNodeTemp = pTableTemp->pHead;

    pTableTemp->pHead = pTableTemp->pHead->pNext;

    delete pNodeTemp;
    
    pTableTemp->iNodeCount--;

    return true;
}



bool GetTop(pTable pTableTemp,int* a)
{
    if(!IsEmpty(pTableTemp))
    {
        *a = pTableTemp->pHead->a;
        return true;
    }
    return false;
}


bool IsEmpty(pTable pTableTemp)
{
    if(pTableTemp->iNodeCount == 0)
    {
        return true;
    }

    return false;
}



void DestroyStack(pTable pTableTemp)
{
    pNode pNodeDel = pTableTemp->pHead;

    while(pNodeDel != NULL)
    {
        pTableTemp->pHead = pNodeDel->pNext;
        
        delete pNodeDel;

        pNodeDel = pTableTemp->pHead;

        pTableTemp->iNodeCount--;
    }
}


void ClearStack(pTable pTableTemp)
{
    if(!IsEmpty(pTableTemp))
    {
        pTableTemp->pHead = NULL;
        pTableTemp->iNodeCount = 0;
    }
}


int GetLength(pTable pTableTemp)
{
    return pTableTemp->iNodeCount;
}

 

 

//c++封类

#include<iostream>

using namespace std;

typedef struct _NODE_
{
    int a;
    _NODE_*pNext;
}Node,*pNode;


class CStack
{
    
private:

    pNode m_pHead;
    int m_iNodeCount;

public:

    CStack()
    {
        m_pHead = NULL;

        m_iNodeCount = 0;
    }
    ~CStack()
    {

    }

    void InitStack();
    
    bool PushStack(int a);

    bool PopStack(int& a);

    bool GetLength(int& nLen);

    bool IsEmpty();

    void DestroyStack();

    bool GetTop(int& a);

};


int main()
{

    return 0;
}





void CStack::InitStack()
{

    if(m_pHead != NULL)
    {
        DestroyStack();
    }

    m_pHead = NULL;

    m_iNodeCount = 0;
}

bool CStack::PushStack(int a)
{
    pNode pNodeTemp = new Node;

    if(pNodeTemp != NULL)
    {
        pNodeTemp->a = a;
        pNodeTemp->pNext = NULL;

        pNodeTemp->pNext = m_pHead;

        m_pHead = pNodeTemp;

        m_iNodeCount++;

        return true;
    }

    return false;
}

bool CStack::PopStack(int& a)
{
    if(IsEmpty())
    {
        return false;
    }

    a = m_pHead->a;
    pNode pNodeDel = m_pHead;
    m_pHead = pNodeDel->pNext;

    delete pNodeDel;

    pNodeDel = m_pHead;

    m_iNodeCount--;

    return true;
}
bool CStack::GetLength(int& nLen)
{
    nLen = m_iNodeCount;
    return true;
}

bool CStack::IsEmpty()
{
    if(m_iNodeCount == 0)
    {
        return true;
    }

    return false;
}
void CStack::DestroyStack()
{
    pNode pNodeDel = m_pHead;

    while(pNodeDel != NULL)
    {
        m_pHead = pNodeDel->pNext;

        delete pNodeDel;

        pNodeDel = m_pHead;

        m_iNodeCount--;
    }
}

bool CStack::GetTop(int& a)
{
    a = m_pHead->a;
    return true;
}

 

posted @ 2013-10-25 12:27  Geekers  阅读(224)  评论(0编辑  收藏  举报