Stack是一种特殊的串行形式的数据结构。
特点:
1、只允许在栈顶压入新元素(push);
2、只允许先由栈顶元素输出,也就是后进先出(pop);
下面我打算用List来实现Stack最简单的功能。
我的思路:
根据List的知识,允许在List里面删除,插入元素,而stack不过是删除和插入的元素都是最后一个而已(top),所以,我想,stack不过是List的一个更具体的特殊功能的List。
首先,创建List。
<pre><code>
//Item
struct Itemtype
{
int num;
};
//-------------------
//List
struct List
{
Itemtype item;
List *next;
};
</code></pre>
Item是自定义数据类型,里面可以有很多基本数据类型,例如int,float之类的。目前我只定义了一个int类型的。
接下来,定义一个stack类。
里面的公共接口也就是功能,看函数名字可以意会。私有数据有stack的大小,还有头指针,尾指针。
<pre><code>
//Stack
class Stack
{
public:
void Stack_Init(int Init_size);
int Stack_Length();
void Stack_Destroy();
void Stack_Export();
void Stack_Push(int item);
int Stack_Pop();
private:
int List_Size;
List *head;
List *top;
};
</code></pre>
接下来的事情也就是List的事情了,不过其中的很多细节还是要注意的。比如,stack已经为NULL了,那么再执行pop弹出函数就应该输出警示stack为NULL。同样的,超出长度也应该提示超出信息。
源代码:
<pre><code>
include<iostream>
using namespace std;
//Item
struct Itemtype
{
int num;
};
//List
struct List
{
Itemtype item;
List *next;
};
//Stack
class Stack
{
public:
void Stack_Init(int Init_size);
int Stack_Length();
void Stack_Destroy();
void Stack_Export();
void Stack_Push(int item);
int Stack_Pop();
private:
int List_Size;
List *head;
List *top;
};
//----------------------
void Stack::Stack_Init(int Init_size)
{
List *temp;
int num;
top = new List;
head = temp = new List;
cout<<"Input "<<Init_size<<" Stack items:"<<endl;
List_Size = Init_size;
for(int i=1; i<=Init_size; ++i)
{
cin>>num;
temp->item.num = num;
if(i < Init_size)
{
temp->next = new List;
temp = temp->next;
}
if(i == Init_size)
{
top = temp;
break;
}
}
}
//---------------------------
void Stack::Stack_Push(int item)
{
List *temp = new List;
if(List_Size == 0)
{
head->item.num = item;
List_Size++;
return;
}
else
{
temp->item.num = item;
top->next = temp;
top = temp;
List_Size++;
}
}
//--------------------------
int Stack::Stack_Pop()
{
List *temp = new List;
int t;
temp = head;
if(List_Size == 0)
{
cout<<"Overflow!"<<endl;
return 0;
}
for(int i=0; i<List_Size-2; ++i)
{
temp = temp->next;
}
t = top->item.num;
delete(top);
top = temp;
List_Size--;
return t;
}
void Stack::Stack_Export()
{
List *temp;
temp = head;
if(List_Size <= 0)
{
cout<<"Stack is Empty!"<<endl;
return;
}
for(int i=0; i<List_Size; ++i)
{
cout<<temp->item.num<<" ";
temp = temp->next;
}
}
//----------------------------
void Stack::Stack_Destroy()
{
List *temp;
List *D_temp;
temp =head;
while(temp != top)
{
D_temp = temp;
temp = temp->next;
delete(D_temp);
}
delete(top);
head = top;
List_Size = 0;
}
//--------------------------------
int Stack::Stack_Length()
{
if(List_Size <= 0)
{
cout<<"Stack is Empty!"<<endl;
return 0;
}
return List_Size;
}
//--------------------------------
int main()
{
Stack s;
int sele_num;
int Init_size;
bool IF = false;
loop:
cout<<"-------------Stack menu-------------"<<endl;
cout<<"1.Stack_Init"<<endl;
cout<<"2.Stack_Push"<<endl;
cout<<"3.Stack_Pop"<<endl;
cout<<"4.Stack_Length"<<endl;
cout<<"5.Stack_Export"<<endl;
cout<<"6.Stack_Destroy"<<endl;
cout<<"Select:";
cin>>sele_num;
if(sele_num == 1)
{
IF = true;
}
if(IF == false)
{
cout<<"Sorry! you have to Init_Stack first!"<<endl;
goto loop;
}
switch(sele_num)
{
case 1:
{
cout<<"Input the Init size of Stack:"<<endl;
cin>>Init_size;
s.Stack_Init(Init_size);
goto loop;
}
case 2:
{
int num;
cout<<"push num: "<<endl;
cin>>num;
s.Stack_Push(num);
goto loop;
}
case 3:
{
cout<<"pop num: "<< s.Stack_Pop()<<endl;
goto loop;
}
case 4:
{
cout<<"size of Stack: "<<endl;
cout<<s.Stack_Length()<<endl;
goto loop;
}
case 5:
{
cout<<"Stack Export: ";
s.Stack_Export();
cout<<endl;
goto loop;
}
case 6:
{
s.Stack_Destroy();
cout<<"The Stack has been destroyed!"<<endl;
IF = false;
goto loop;
}
default:
{
cout<<"Not in range!"<<endl;
goto loop;
}
}
return 0;
}
</code></pre>