几类数据结构

Stack And Simple queue

Stack

A stack is a Last-In-First-Out (LIFO) collection
  1. Start: 
    Stack
  2. push(1) 
    Stack
  3. push(2) 
    Stack
  4. pop: returns 2 
    Stack
  5. push(3) 
    Stack
  6. push(4) 
    Stack
  7. push(5) 
    Stack

C++ Stack Usage

1:  
// cppStackExample.cpp - download  here

2:  

3:  
#include
 
<
stack>

4:  
#include
 
<
iostream>

5:  

6:  
int
 
main(
int
 
argc, 
char
 
argv[
]
)
{

7:  

8:  
 
 
std:
:
stack<
int
>
 
int_stack;

9:  
 
 
int_stack.push(
1)
;

10: 
 
 
int_stack.push(
2)
;

11: 

12: 
 
 
//in the std library top and pop are two seperate methods

13: 
 
 
std:
:
cout 
<
<
 
int_stack.top(
)
 
<
<
 
std:
:
endl;

14: 
 
 
int_stack.pop(
)
;

15: 

16: 
 
 
int_stack.push(
3)
;

17: 
 
 
int_stack.push(
4)
;

18: 
 
 
int_stack.push(
5)
;

19: 

20: 
 
 
return
 
0;

21: 
}


Simple Queue

Array Based Circular Buffers are sometimes useful in limited resource environments such as a network card integrated circuit.

Push

  1. Start: 
    ArrayCircularBufferInsertStart
  2. Push 
    ArrayCircularBufferInsertStart
  3. enqueue 
    ArrayCircularBufferInsertStart
  4. enqueue 
    ArrayCircularBufferInsertStart
  5. dequeue
    ArrayCircularBufferInsertStart
  6. dequeue 
    ArrayCircularBufferInsertStart
  7. After more pushes and pops 
    ArrayCircularBufferInsertStart
  8. enqueue 
    ArrayCircularBufferInsertStart
  9. enqueue 
    ArrayCircularBufferInsertStart

Priority_queue

包含priority_queue 的头文件是 <queue>

 

priority_queue类的主要成员:

priority_queue();    //默认构造函数,生成一个空的排序队列

priority_queue(const queue&);    //拷贝构造函数

priority_queue& operator=(const priority_queue &);    //赋值运算符重载

priority_queue 的私有成员:

value_type;   //priority_queue中存放的对象类型,它和priority_queue中的T类型相同

priority_queue(const Compare& comp);    //构造生成一个空的priority_queue对象,使用comp作为priority_queue的comparison

priority_queue(const value_type* first, const value_type* last);    //带有两个参数的构造 函数,使用默认的Comparison作为第三个参数

size_type;    //正整数类型,和Sequence::size_type类型一样。

bool empty() const;    //判断优先级队列是否为空,为空返回true,否则返回false

size_type size() const;    //返回优先级队列中的元素个数

const value_type& top() const();    //返回优先级队列中第一个元素的参考值。

void push(const value_type& x);    //把元素x插入到优先级队列的尾部,队列的长度加1

void pop();    //删除优先级队列的第一个值,前提是队列非空,删除后队列长度减1

 

priority_queue<Type, Container, Functional>

 如果我们把后面俩个参数缺省的话,优先队列就是大顶堆,队头元素最大。(这点由上面的程序可以看出)

Parameter

Description

Default

T

The type of object stored in the priority queue.

 

Sequence

The type of the underlying container used to implement the priority queue.

vector<T>

Compare

The comparison function used to determine whether one element is smaller than

another element. If Comparex,y) is true, then x is smaller than y. The element

returned by Q.top) is the largest element in the priority queue. That is, it has the

 property that, for every other element x in the priority queue, Compare(Q.top(), x) is false.

less<T>

 

自定义类型重载 operator< 后,声明对象时就可以只带一个模板参数。

但此时不能像基本类型这样声明

priority_queue<Node, vector<Node>, greater<Node> >;

原因是 greater<Node> 没有定义,如果想用这种方法定义

则可以按如下方式:

 

   
   
#include <iostream> #include <queue> using namespace std; struct Node { int x,y; Node(int a = 0,int b = 0):x(a),y(b){} }; struct cmp { bool operator()(Node a,Node b){ if (a.x == b.x) return a.y>b.y; return a.x>b.x; } }; int main(){ priority_queue<Node,vector<Node>,cmp > q; for (int i = 0; i < 10;++i) { q.push(Node(rand(),rand())); } while (!q.empty()) { cout<<q.top().x<<" "<<q.top().y<<endl; q.pop(); } return EXIT_SUCCESS; }

其实就三种用法

第一种,直接使用默认的。

它的模板声明带有三个参数,priority_queue<Type, Container, Functional> Type 为数据类型, Container 为保存数据的容器,Functional 为元素比较方式。 Container 必须是用数组实现的容器,比如 vector, deque 但不能用 list. STL里面默认用的是 vector. 比较方式默认用 operator< , 所以如果你把后面俩个 参数缺省的话,优先队列就是大顶堆,队头元素最大。 看例子

priority_queue<int> qi;

    int a[len] = {3,5,9,6,2};     priority_queue<int> qi;     for(i = 0; i < len; i++)         qi.push(a[i]);

    for(i = 0; i < len; i++)     {         cout<<qi.top()<<" ";         qi.pop();     }

通过<操作符可知在整数中元素大的优先级高。 故例子中输出结果为:9 6 5 3 2

第二种:

第二种方法: 在示例1中,如果我们要把元素从小到大输出怎么办呢? 这时我们可以传入一个比较函数,使用functional.h函数对象作为比较函数。

如果要用到小顶堆,则一般要把模板的三个参数都带进去。 STL里面定义了一个仿函数 greater<>,对于基本类型可以用这个仿函数声明小顶堆 priority_queue<int, vector<int>, greater<int> >qi2;

对于自定义类型,则必须自己重载 operator< 或者自己写仿函数

#include <iostream> #include <queue>

using namespace std;

struct Node{     int x, y;     Node( int a= 0, int b= 0 ):         x(a), y(b) {} };

bool operator<( Node a, Node b ){     if( a.x== b.x ) return a.y> b.y;     return a.x> b.x;  }

int main(){     priority_queue<Node> q;          for( int i= 0; i< 10; ++i )     q.push( Node( rand(), rand() ) );          while( !q.empty() ){         cout << q.top().x << ' ' << q.top().y << endl;         q.pop();     }          getchar();     return 0; }

 

或者这样定义也是能达到效果的:

struct Node{     int x, y;     Node( int a= 0, int b= 0 ):         x(a), y(b) {}

    friend operator<( Node a, Node b ){     if( a.x== b.x ) return a.y> b.y;     return a.x> b.x;

    } };

自定义类型重载 operator< 后,声明对象时就可以只带一个模板参数。 但此时不能像基本类型这样声明 priority_queue<Node, vector<Node>, greater<Node> >; 原因是 greater<Node> 没有定义,如果想用这种方法定义 则可以按如下方式

例子:

#include <iostream> #include <queue>

using namespace std;

struct Node{     int x, y;     Node( int a= 0, int b= 0 ):         x(a), y(b) {} };

struct cmp{     bool operator() ( Node a, Node b ){         if( a.x== b.x ) return a.y> b.y;                  return a.x> b.x; } };

int main(){     priority_queue<Node, vector<Node>, cmp> q;          for( int i= 0; i< 10; ++i )     q.push( Node( rand(), rand() ) );          while( !q.empty() ){         cout << q.top().x << ' ' << q.top().y << endl;         q.pop();     }          getchar();     return 0; }

还有一点要注意的是priority_queue中的三个参数,后两个可以省去,因为有默认参数,不过如果,有第三个参数的话,必定要写第二个参数。


Xor Linked List

C语言示例(异或指针双向链表)[编辑]

//异或链表结点结构
typedef struct XorNode{
    char data;
    struct XorNode *LRPtr;
}XorNode,*XorPointer;
//异或指针双向链表结构
typedef struct  XorLinkedList{
    XorPointer left,right;
}XorLinkedList;
//异或操作
XorPointer xor(XorPointer a,XorPointer b){
    return (XorPointer)((unsigned long)(a) ^ (unsigned long)(b));
}


//创建异或双向链表
void createXorLinkedList(XorLinkedList *list){
    char ch;
    XorPointer lastNode=NULL;
    int isFirstNode=1;
    while ((ch=getchar())!='\n') {
        XorPointer newNode=(XorPointer)malloc(sizeof(XorNode));
        newNode->data=ch;
        newNode->LRPtr=NULL;
        if (lastNode) {
            newNode->LRPtr=xor(lastNode, NULL);
            lastNode->LRPtr=xor(xor(lastNode->LRPtr,NULL), newNode);
        }
        lastNode=newNode;
        if (isFirstNode) {
            isFirstNode=0;
            list->left=newNode;
        }
    }
    list->right=lastNode;
}
//按任意方向依次输出各结点值
void print(XorPointer a,XorPointer b){
    XorPointer nullFirst=a==NULL?a:b;
    XorPointer nonNullFirst=a!=NULL?a:b;
    XorPointer tmp=NULL;
    do{
        printf("%c ",nonNullFirst->data);
        tmp=nonNullFirst;
        nonNullFirst=xor(nullFirst, nonNullFirst->LRPtr);
        nullFirst=tmp;
    }while(nonNullFirst!=NULL);
}
//在第i个结点之前插入一个结点
void XorLinkedListInsert(XorLinkedList *list,int pos,char data){
    XorPointer node=list->left,tmp=NULL;
    XorPointer last=NULL;
    XorPointer newNode=NULL;
    int i=1;
    while (i<pos && node!=NULL) {
        tmp=node;
        node=xor(last, node->LRPtr);
        last=tmp;
        i++;
    }
    newNode=(XorPointer)malloc(sizeof(XorNode));
    newNode->data=data;
    newNode->LRPtr=xor(last, node);
    if (node!=NULL) {
        node->LRPtr=xor(newNode, xor(last, node->LRPtr));
    }
    if (last!=NULL) {
        last->LRPtr=xor(xor(last->LRPtr, node), newNode);
    }
    if (pos==1) {
        list->left=newNode;
    }
}

Binary Search

Binary Search requires that the collection has random access and is sorted. It has O(lgn) time complexity.
1:  
// binarySearch.cpp - download  here

2:  

3:  
int
 
binarySearch(
int
 
array, 
int
 
n, 
int
 
key)
{

4:  
 
 
int
 
low 
=
 
0;

5:  
 
 
int
 
mid;

6:  
 
 
int
 
high 
=
 
n-1;

7:  

8:  
 
 
while
(
low 
<
=
 
high)
{

9:  
 
 
 
 
mid 
=
 
(
low 
high)
 
2;

10: 
 
 
 
 
if
(
key 
<
 
array[
mid]
)
{

11: 
 
 
 
 
 
 
high 
=
 
mid 
1;

12: 
 
 
 
 
}
 
else
 
if
(
array[
mid]
 
<
 
key)
{

13: 
 
 
 
 
 
 
low 
=
 
mid 
1;

14: 
 
 
 
 
}
 
else
 
{

15: 
 
 
 
 
 
 
return
 
mid;

16: 
 
 
 
 
}

17: 
 
 
}

18: 
 
 
return
 
-1;

19: 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值