记载目前遇到的数据结构,以及对他们的掌握情况。
一、栈(stack)
栈目前只遇到过朴素的栈,并没有那么多特别的属性。
普通的栈的性质:
①后入先出(FILO)。
项目 | 掌握情况 |
---|---|
stl应用 | 100% |
数组(array)实现 | 100% |
链表(list)实现 | 100% |
堆(heap)实现 | 100% |
1.stl应用
#include <iostream>
#include <stack>
using namespace std;
int main(){
stack<int> s;
//error 1 出栈时/引用栈顶
//if(s.size()==0)puts("Stack underflow!");
s.push(1);
cout<<"pushed "<<1<<endl;
cout<<"top:"<<s.top()<<endl;
cout<<"empty?"<<(s.empty()?"YES":"NO")<<endl;
cout<<"size:"<<s.size()<<endl;
s.push(2);
cout<<"pushed "<<2<<endl;
cout<<"top:"<<s.top()<<endl;
cout<<"empty?"<<(s.empty()?"YES":"NO")<<endl;
cout<<"size:"<<s.size()<<endl;
s.pop();
cout<<"poped "<<2<<endl;
cout<<"top:"<<s.top()<<endl;
cout<<"empty?"<<(s.empty()?"YES":"NO")<<endl;
cout<<"size:"<<s.size()<<endl;
s.pop();
cout<<"poped "<<1<<endl;
cout<<"empty?"<<(s.empty()?"YES":"NO")<<endl;
cout<<"size:"<<s.size()<<endl;
}
/*
pushed 1
top:1
empty?NO
size:1
pushed 2
top:2
empty?NO
size:2
poped 2
top:1
empty?NO
size:1
poped 1
empty?YES
size:0
*/
2.数组实现
#include <iostream>
using namespace std;
const int Stack_size=100000;
int stack[Stack_size],top;
int main(){
//error 1 入栈时
//if(top==Stack_size)puts("Stack overflow!");
//error 2 出栈时/引用栈顶
//if(top==0)puts("Stack underflow!");
//push
stack[top++]=1;
cout<<"pushed "<<stack[top-1]<<endl;
cout<<"top:"<<stack[top-1]<<endl;
cout<<"empty?"<<(top==0?"YES":"NO")<<endl;
cout<<"size:"<<top<<endl;
//push
stack[top++]=2;
cout<<"pushed "<<stack[top-1]<<endl;
cout<<"top:"<<stack[top-1]<<endl;
cout<<"empty?"<<(top==0?"YES":"NO")<<endl;
cout<<"size:"<<top<<endl;
//pop
top--;
cout<<"poped "<<2<<endl;
cout<<"top:"<<stack[top-1]<<endl;
cout<<"empty?"<<(top==0?"YES":"NO")<<endl;
cout<<"size:"<<top<<endl;
//pop
top--;
cout<<"poped "<<1<<endl;
cout<<"empty?"<<(top==0?"YES":"NO")<<endl;
cout<<"size:"<<top<<endl;
}
/*
pushed 1
top:1
empty?NO
size:1
pushed 2
top:2
empty?NO
size:2
poped 2
top:1
empty?NO
size:1
poped 1
empty?YES
size:0
*/