Deque C语言实现 && C++ STL基本操作
来源:互联网 发布:淘宝类似威廉的男装店 编辑:程序博客网 时间:2024/06/03 23:02
Deque
deque (usually pronounced like "deck") is an irregular acronym ofdouble-endedqueue. Double-ended queues are sequence containers with dynamic sizes that can be expanded or contracted on both ends (either its front or its back).
内部实现 example:
A "deque" is a data structure consisting of a list of items, on which the following operations are possible:
- Push(X,D): Insert item X on the front end of deque D.
- Pop(D): Remove the front item from deque D and return it.
- Inject(X,D): Insert item X on the rear end of deque D.
- Eject(D): Remove the rear item from deque D and return it.Write routines to support the deque that take O(1) time per operation.
Format of functions:
Deque CreateDeque();int Push( ElementType X, Deque D );ElementType Pop( Deque D );int Inject( ElementType X, Deque D );ElementType Eject( Deque D );
where Deque
is defined as the following:
typedef struct Node *PtrToNode;struct Node { ElementType Element; PtrToNode Next, Last;};typedef struct DequeRecord *Deque;struct DequeRecord { PtrToNode Front, Rear;};
Here the deque is implemented by a doubly linked list with a header. Front
andRear
point to the two ends of the deque respectively.Front
always points to the header. The deque is empty whenFront
andRear
both point to the same dummy header.Note:Push
and Inject
are supposed to return 1 if the operations can be done successfully, or 0 if fail. If the deque is empty,Pop
andEject
must returnERROR
which is defined by the judge program.
Sample program of judge:
#include <stdio.h>#include <stdlib.h>#define ElementType int#define ERROR 1e5typedef enum { push, pop, inject, eject, end } Operation;typedef struct Node *PtrToNode;struct Node { ElementType Element; PtrToNode Next, Last;};typedef struct DequeRecord *Deque;struct DequeRecord { PtrToNode Front, Rear;};Deque CreateDeque();int Push( ElementType X, Deque D );ElementType Pop( Deque D );int Inject( ElementType X, Deque D );ElementType Eject( Deque D );Operation GetOp(); /* details omitted */void PrintDeque( Deque D ); /* details omitted */int main(){ ElementType X; Deque D; int done = 0; D = CreateDeque(); while (!done) { switch(GetOp()) { case push: scanf("%d", &X); if (!Push(X, D)) printf("Memory is Full!\n"); break; case pop: X = Pop(D); if ( X==ERROR ) printf("Deque is Empty!\n"); break; case inject: scanf("%d", &X); if (!Inject(X, D)) printf("Memory is Full!\n"); break; case eject: X = Eject(D); if ( X==ERROR ) printf("Deque is Empty!\n"); break; case end: PrintDeque(D); done = 1; break; } } return 0;}/* Your function will be put here */
Sample Input:
PopInject 1PopEjectPush 1Push 2EjectInject 3End
Sample Output:
Deque is Empty!Deque is Empty!Inside Deque: 2 3
Deque CreateDeque(){ PtrToNode Temp_Node = (PtrToNode)malloc(sizeof(struct Node)); //创建节点 Deque Temp_Record = (Deque)malloc(sizeof(struct DequeRecord)); //创建队列 Temp_Node->Next = Temp_Node->Last = NULL; Temp_Record->Front = Temp_Record->Rear = Temp_Node; return Temp_Record; //返回队列首地址}int Push( ElementType X, Deque D ){ //Insert element at beginning PtrToNode Tnode = (PtrToNode)malloc(sizeof(struct Node)); if(!Tnode) return 0; //插入 Tnode->Element = X; Tnode->Last = D->Front; Tnode->Next = D->Front->Next; if(D->Front->Next) //Front原来所指的节点的前一个(Last)节点指向新的节点 D->Front->Next->Last = Tnode; D->Front->Next = Tnode; //Front的下一个指向新节点 //空队列 if(D->Front == D->Rear) D->Rear = Tnode;//更新Rear节点 return 1;}ElementType Pop( Deque D ){ //Delete first element if(D->Front == D->Rear) //队列为空 return ERROR; PtrToNode Tnode = D->Front->Next; //若pop之后为空队列,则Rear指向Front if(D->Front->Next == D->Rear) D->Rear = D->Front; //Front指向队列的第二个元素 D->Front->Next = D->Front->Next->Next; ElementType temp; temp = Tnode->Element; free(Tnode); return temp;}int Inject( ElementType X, Deque D ){ //Add element at the end PtrToNode Tnode = (PtrToNode)malloc(sizeof(struct Node)); //创建节点 if(!Tnode) return ERROR; //插入 Tnode->Element = X; Tnode->Next = NULL; D->Rear->Next = Tnode; Tnode->Last = D->Rear; if(D->Rear == D->Front) D->Front->Next = Tnode; D->Rear = Tnode;//更新Rear return 1;}ElementType Eject( Deque D ){ //Delete last element if(D->Front == D->Rear) return ERROR; PtrToNode Tnode = D->Rear; ElementType temp = D->Rear->Element; D->Rear = D->Rear->Last; free(Tnode); return temp;}
STL deque成员函数:
Member functions
- (constructor) Construct deque container
- (destructor) Deque destructor
- operator= Assign content
- begin Return iterator to beginning
- end Return iterator to end
- rbegin Return reverse iterator to reverse beginning
- rend Return reverse iterator to reverse end
- cbegin Return const_iterator to beginning
- cend Return const_iterator to end
- crbegin Return const_reverse_iterator to reverse beginning
- crend Return const_reverse_iterator to reverse end
- size Return size
- max_size Return maximum size
- resize Change size
- empty Test whether container is empty
- shrink_to_fit Shrink to fit
- operator[] Access element
- at Access element
- front Access first element
- back Access last element
- assign Assign container content
- push_back Add element at the end
- push_front Insert element at beginning
- pop_back Delete last element
- pop_front Delete first element
- insert Insert elements
- erase Erase elements
- swap Swap content
- clear Clear content
- emplace Construct and insert element
- emplace_front Construct and insert element at beginning
- emplace_back Construct and insert element at the end
- get_allocator Get allocator
常用方法 example:
#include <deque>#include <cstdio>#include <algorithm>using namespace std;int main(){deque<int> q(20); //创建dequedeque<int>::iterator pos; //创建构造器int i; for (i = 0; i < 20; ++i) q[i] = i;//输出dequeprintf("输出deque中数据:\n");for (i = 0; i < 20; ++i)printf("%d ", q[i]);putchar('\n');//在头尾加入新数据q.push_back(100);q.push_front(i);//输出dequeprintf("\n输出deque中数据:\n");for (pos = q.begin(); pos != q.end(); pos++)printf("%d ", *pos);putchar('\n');//查找printf("\n查找%d\n", 18);pos = find(q.begin(), q.end(), 18);if (pos != q.end())printf("find %d success\n", *pos);elseprintf("find failed\n");//在头尾删除数据q.pop_back();q.pop_front();//输出dequeprintf("\n输出deque中数据:\n");for (pos = q.begin(); pos != q.end(); pos++)printf("%d ", *pos);putchar('\n');return 0;}
- Deque C语言实现 && C++ STL基本操作
- C语言实现单链表的基本操作
- C语言实现无头链表的基本操作
- C语言实现栈的基本操作
- 字符串的基本操作,c语言实现
- 单链表的基本操作c语言实现
- C语言实现链表基本操作
- C语言实现单链表的基本操作
- 单链表的基本操作C语言实现
- 单链表基本操作C语言实现
- 【C++】 STL vector list deque
- 【C++】 STL vector list deque
- [C++][stl]vector、list、deque
- 【C++】 STL vector list deque
- C++ STL deque 内存布局
- [C++]STL vector list deque区别与实现
- hackerrank>Dashboard>C++>STL>Deque-STL
- C语言多线程-基本操作
- 二叉搜索树的广度优先遍历
- JS排序
- java 中hashset 与hashmap的区别
- 动态逆序对
- 洛谷 P1008 三连击
- Deque C语言实现 && C++ STL基本操作
- jiecaovideoplayer的简单使用
- 51Nod 1347 旋转字符串
- java和Js中的类型转换
- centos-6 如何改系统语言成中文简体
- 二进制转十进制
- makefile管理的项目转Cmake必看的一些资料
- java集合
- 功能独特的开源人工智能项目