数据结构估计都忘得差不多了,现在还有点时间,就总结下。
1--------stack:
---》栈最大特点是先进后出,可以直接调用STL中stack,其掉用形式如下:
#include<stck>//需要引入头文件
stack<int>s //定义一个整形的栈;
s.push(4);//向栈顶插入一个元素
s.pop();//从栈顶弹出一个元素
s.empty();判断栈是否为空,如果为空返回true,否则返回false
s.size();//返回栈大小
----》下面是自己写的栈:
#include<iostream>
#include<conio.h>
using namespace std;
struct Node{
int data;
struct Node *next;
};
Node * top =NULL;
void push(int newdata)
{
if(top==NULL){
Node * node = new Node;
node->data = newdata;
node->next=NULL;
top=node;
}
else{
Node * node = new Node;
node->data = newdata;
node->next = top;
top=node;
}
}
void pop(){
if(top->next==NULL){
Node * node = new Node;
node = top;
delete node;
top = NULL;
}
else
{
Node * node = new Node;
node = top;
top = top->next;
delete node;
}
}
int size(){
int count;
while(top==NULL){
count++;
top= top->next;
}
return count;
}
int topdata(){
int newdata;
newdata = top->data;
return newdata;
}
bool empty()
{
if(top==NULL)
return true;
else
return false;
}
int main(){
push(12);
cout<<topdata()<<endl;
system("pause");
return 0;
}
2--------queue:
队列与栈刚好相反,队列是先进先出,像队列一样可以直接调用STL库的队列,其调用办法如下:
#include<queue>//引入头文件
queue<int> q;//定义一个队列
q.push(4);//在队列最后插入一个数据
q.pop();//弹出一个数据
q.empty();//判断队列是否为空
q.front();//获取队列头值
q.size();//获取队列大小
下面是自己写队列源码:
#include "stdafx.h"
#include<iostream>
using namespace std;
struct Node{
int data;
struct Node *next;
};
struct Node *front=NULL;
struct Node *rear=NULL;
void push(int newdata){
if(front==NULL && rear ==NULL){
struct Node * node = new Node();
node->data = newdata;
node->next=NULL;
front=node;
rear=node;
node = NULL;
}
else{
struct Node *node = new Node();
node->data = newdata;
node->next= NULL;
rear->next = node;
rear = node;
node = NULL;
}
}
int pop(){
if(front == NULL){
return 0;
}
else if(front == rear){
struct Node *node = new Node();
node = front;
delete(node);
front=NULL;
rear=NULL;
node = NULL;
return 1;
}
else{
struct Node *node = new Node();
node = front;
front = front->next;
delete(node);
node = NULL;
return 1;
}
}
int main(){
push(2);
push(3);
cout<<front->data<<endl;
cout<<rear->data<<endl;
pop();
cout<<front->data<<endl;
cout<<rear->data<<endl;
return 0;
}
3--------list:
4--------map:
5--------set:
6--------vector:
7---------iterater: