数据结构总结

 

 数据结构估计都忘得差不多了,现在还有点时间,就总结下。

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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值