C++顺序栈

SeqStack:

//顺序栈
#include<iostream>
using namespace std;
typedef int elemType;
const int MAXSIZE = 50;

struct SqStack
{
    elemType data[MAXSIZE];
    elemType top;
};

//初始化栈
void InitStack(SqStack *S)
{
    for(int i=0;i<MAXSIZE;i++)
    {
        S->data[i] = 0;
    }
    S->top = -1;
}

//获取栈的长度
int LengthStack(SqStack *S)
{
    int length = 0;
    if(S->top = -1)
        return 0;
    int i = S->top;
    while(i>=0)
    {
        i--;
        length++;
    }
    return length;
}

//自顶向下遍历栈
void TraverseStack(SqStack *S)
{
    if(S->top == -1)
        cout<<"栈为空."<<endl;
    int num = S->top;
    int i = 1;
    while(num>=0)
    {
        cout << i <<":\t"<< S->data[num] <<endl;
        i++;
        num--;
    }
}

//获取栈顶元素
int GetTop(SqStack *S)
{
    if(S->top == -1)
    {
        cout <<"栈为空."<<endl;
        return false;
    }
    elemType e = S->data[S->top];
    return e;
}

//插入元素为e的新的栈顶元素
bool Push(SqStack *S, elemType e)
{
    if(S->top == MAXSIZE-1)
    {
        cout<<"栈已满."<<endl;
        return false;
    }
    S->top++;
    S->data[S->top] = e;
    return true;
}

//删除栈顶元素
bool Pop(SqStack *S, elemType e)
{
    if(S->top == -1)
    {
        cout <<"栈为空."<<endl;
        return false;
    }
    e = S->data[S->top];
    S->data[S->top] = 0;
    S->top--;
    return true;
}

//判断是否为空
void EmptyStack(SqStack *S)
{
    if(S->top == -1)
        cout <<"栈为空."<<endl;
    else
        cout<<"栈不为空."<<endl;
}

//清空栈
void ClearStack(SqStack *S)
{
    if(S->top == -1)
        cout <<"栈已为空."<<endl;
    for(int i=S->top;i>=0;i--)
        S->data[i] = 0;
    S->top = -1;
}

int main()
{
    SqStack s;
    InitStack(&s);
    for(int i=1;i<7;i++)
        Push(&s,i);
    TraverseStack(&s);
    cout<<endl;

    elemType x = GetTop(&s);
    cout<<"栈顶元素为: "<< x <<endl;
    elemType i=0;;
    Pop(&s, i);
    cout<<"删除栈顶元素后栈为:"<<endl;
    TraverseStack(&s);
    cout<<endl;

    ClearStack(&s);
    cout<<"清空栈..."<<endl;
    TraverseStack(&s);
    EmptyStack(&s);
    cout<<endl;

    return 0;
}

 

 1 #include<iostream>
 2 using namespace std;
 3 typedef int ElemType;
 4 #define MAXSIZE 20
 5 
 6 struct SqStack
 7 {
 8     ElemType data[MAXSIZE];
 9     int top;
10 };
11 
12 //初始化栈
13 void InitStack(SqStack *S)
14 {
15     S->top = -1;
16 }
17 
18 //判断是否为空
19 void IsEmpty(SqStack *S)
20 {
21     if(S->top == -1)
22         cout<<"栈为空!"<<endl;
23     else
24         cout<<"栈不为空!"<<endl;
25 }
26 
27 //把s置空
28 void ClearStack(SqStack *S)
29 {
30     S->top = -1;
31 }
32 
33 //栈的长度
34 int LengthStack(SqStack *S)
35 {
36     return S->top+1;
37 }
38 
39 //用e返回S的栈顶元素
40 void GetTop(SqStack *S, ElemType *e)
41 {
42     if(S->top==-1)
43         cout<<"出错,栈为空!"<<endl;
44     *e = S->data[S->top];
45 }
46 
47 //插入栈顶元素e
48 void Push(SqStack *S, ElemType *e)
49 {
50     if(S->top == MAXSIZE-1)
51         cout<<"栈已满!"<<endl;
52     S->top++;
53     S->data[S->top] = *e;
54     //cout<<*e<<endl;
55 }
56 
57 //删除栈顶的元素,用e返回
58 void Pop(SqStack *S, ElemType *e)
59 {
60     if(S->top==-1)
61         cout<<"栈已空!"<<endl;
62     *e = S->data[S->top];
63     S->top--;
64 }
65 
66 //从栈底到栈顶依次显示
67 void PrintStack(SqStack *S)
68 {
69     int i = -1;
70     while(i<S->top)
71     {
72         i++;
73         cout<<S->data[i]<<" ";
74     }
75     cout<<endl;
76 }
77 
78 int main()
79 {
80     int e = 0;
81     SqStack S;
82     InitStack(&S);
83     for(int i=1; i<=10; i++)
84         Push(&S,&i);
85     cout<<LengthStack(&S)<<endl;
86     PrintStack(&S);
87     Pop(&S,&e);
88     cout<<LengthStack(&S)<<endl;
89     PrintStack(&S);
90     cout<<"弹出的栈顶元素为:"<<e<<endl;
91     GetTop(&S,&e);
92     cout<<"当前栈顶元素为:"<<e<<endl;
93     ClearStack(&S);
94     cout<<LengthStack(&S)<<endl;
95     IsEmpty(&S);
96 
97     return 0;
98 }

 

posted @ 2015-10-06 19:45  jx_yangbo  阅读(293)  评论(0编辑  收藏  举报