DBLinkedList.cpp
上传用户:smdfuse
上传日期:2015-11-06
资源大小:98k
文件大小:4k
- #include "stdafx.h"
- #include "DBLinkedList.h"
- #include "stdlib.h"
- Node::Node():avai(false),next(NULL),previous(NULL){
- // 数据置空
- nodedata.data = NULL;
- nodedata.size = 0;
- }
- Node::Node(void *_info,int size, Node *_next,Node *_previous):
- next(_next),previous(_previous),avai(true){
- nodedata.data = malloc(size); // 数据内存空间
- memcpy(nodedata.data,_info,size);
- nodedata.size = size;
- }
- Node::~Node(){
- // 释放数据内存空间
- free(nodedata.data);
- }
- void * Node::GetInfo(){
- return nodedata.data;
- }
- void Node::SetInfo(void *info,int size){
- //this->info = info;
- if(this->nodedata.data != NULL)
- free(nodedata.data);
-
- // 数据拷贝
- nodedata.data = malloc(size);
- memcpy(nodedata.data,info,size);
- }
- void Node::SetNext(Node *next){
- this->next = next;
- }
- void Node::SetPrevious(Node *previous){
- this->previous = previous;
- }
- Node* Node::GetPrevious(){
- return this->previous;
- }
- Node* Node::GetNext(){
- return this->next;
- }
- bool Node::GetAvai(){
- return this->avai;
- }
- DBLinkedList::DBLinkedList():head(NULL),rear(NULL),currentPtr(NULL){
- head = NULL;
- rear = NULL;
-
- }
- DBLinkedList::~DBLinkedList(){
- Destory();
- }
- // 最加数据
- bool DBLinkedList::Append(void *info,int size){
- if(head == NULL){
- head = new Node(info,size,NULL,NULL);
- rear = head;
- currentPtr = head;
- }else{
- rear->SetNext(new Node(info,size,NULL,rear));
- rear = rear->GetNext();
- }
- return true;
- }
- // 插入数据
- // last是前一个指针
- bool DBLinkedList::Add(void *info,int size,Node* last){
- // 如果是最后一个元素
- if(last == NULL || last == rear || head == NULL){
- this->Append(info,size);
- return true;
- }
- Node* next = last->GetNext();
-
- last->SetNext(new Node(info,size,next,last));
- if(next != NULL)
- next->SetPrevious(last->GetNext());
- if(this->rear == last){
- rear = last->GetNext();
- }
- return true;
- }
- bool DBLinkedList::Delete(Node *curr){
- if(curr == NULL)
- return false;
- // 如果只有一个元素
- if(curr->GetPrevious() == NULL && curr->GetNext() == NULL){
- //如果是第一个元素删除,或者唯一一个元素
- head = NULL;
- rear = NULL;
-
- delete curr;
- return true;
- }
-
- // 如果删除的是第一个元素
- if(curr->GetPrevious() == NULL){
- // 取得下一个元素
- head = curr->GetNext();
- head->SetPrevious(NULL);
- delete curr;
-
- return true;
- }
- // 如果删除的是最后一个元素
- if(curr->GetNext() == NULL){
- // 如果删除的是最后一个元素
- rear = curr->GetPrevious();
-
- rear->SetNext(NULL);
- delete curr;
- return true;
- }
-
- // 如果删除的是中间元素
- if(curr->GetNext() != NULL && curr->GetPrevious() != NULL)
- {
-
- // 如果删除的是中间的元素
- Node * previous = curr->GetPrevious();
- Node * next = curr->GetNext();
- delete curr;
- previous->SetNext(next);
- next->SetPrevious(previous);
- return true;
- }
- return true;
- }
- void* DBLinkedList::GetInfo(Node *node){
- return node->GetInfo();
- }
- Node* DBLinkedList::GetNext(){
- return currentPtr->GetNext();
- }
- Node* DBLinkedList::GetPrevious(){
- return currentPtr->GetNext();
- }
- Node* DBLinkedList::GetCurrent(){
- return currentPtr;
- }
- void* DBLinkedList::GetCurrentInfo(){
- if(currentPtr == NULL) return NULL;
- return currentPtr->GetInfo();
- }
- void* DBLinkedList::GetNextInfo(){
- currentPtr = currentPtr->GetNext();
-
- if(currentPtr == NULL){
- currentPtr = head;
- return NULL;
- }
- return currentPtr->GetInfo();
- }
- void * DBLinkedList::GetPreviousInfo(){
- currentPtr = currentPtr->GetPrevious();
- if(currentPtr == NULL){
- currentPtr = rear;
- return NULL;
- }
- return currentPtr->GetInfo();
- }
- /*
- * 函数:Destory
- * 功能:销毁
- */
- void DBLinkedList::Destory(){
- //遍历
- Node *curr = head;
- Node *next = NULL;
- if(head != NULL)
- next = head->GetNext();
- else
- return;
- for(;curr != NULL;curr = next){
- next = curr->GetNext();
- delete curr;
- }
- head = NULL;
- rear = NULL;
- }
- /*
- * 函数: ResetCurrentPtr
- * 功能: 重新设置CurrentPtr的位置到头或者尾
- */
- void DBLinkedList::ResetCurrentPtr(bool head){
- if(head){
- currentPtr = this->head;
- }else{
- currentPtr = rear;
- }
- }
- /*
- * GetLastInfo
- * 功能:获得表末尾的数据
- */
- void *DBLinkedList::GetLastInfo(){
- if(rear != NULL)
- return rear->GetInfo();
-
- return NULL;
- }
- /*
- * DeleteLast
- * 功能:删除最后一个位置的节点
- */
- bool DBLinkedList::DeleteLast(){
- return this->Delete(this->rear);
- }