/*************************************************************************
> File Name: DList.h
> Author: Shaojie Kang
> Mail: kangshaojie@ict.ac.cn
> Created Time: 2015年09月15日 星期二 10时52分35秒
> Problem:
双向链表的实现
************************************************************************/
#ifndef _DLIST_H
#define _DLIST_H
typedef int Item;
typedef struct Node* PNode;
// define node structrue
struct Node
{
Item data;
PNode previous;
PNode next;
Node(Item x): data(x), previous(NULL), next(NULL){}
};
// define linked list structure
struct DList
{
PNode head;
PNode tail;
int size;
DList(): head(NULL),tail(NULL),size(0){}
};
// init a double linked list
DList* InitList();
// clear a double linked list
void ClearList(DList *plist);
// clear a double linked list
void DestroyList(DList *plist);
// get the head
PNode GetHead(DList *plist);
// get the tail
PNode GetTail(DList *plist);
// get the size
int GetSize(DList *plist);
// pop_front
PNode PopFront(DList *plist);
// pop_back
PNode PopBack(DList *plist);
// push front
void PushFront(DList *plist, Item x);
// push back
void PushBack(DList *plist, Item x);
// insert a new node in the front of a certain node
void InsertBefore(DList *plist, PNode *p, Item key);
// insert a new node in the behind of a certain node
void InsertBehind(DList *plist, PNode *p, Item key);
// visit each element in DList
void ListTraverse(DList *plist, void (*visit)());
#endif
<pre name="code" class="cpp">/*************************************************************************
> File Name: DList.cpp
> Author: Shaojie Kang
> Mail: kangshaojie@ict.ac.cn
> Created Time: 2015年09月15日 星期二 11时27分57秒
************************************************************************/
#include<iostream>
#include"DList.h"
using namespace std;
DList* InitList()
{
DList *list = new DList;
return list;
}
void ClearList(DList *plist)
{
PNode p = plist->head;
if(!p) return; // 如果双向链表是空的
PNode next;
while(p != plist->tail)
{
next = p->next;
delete p;
p = next;
}
delete p;
plist->head = NULL;
plist->tail = NULL;
plist->size = 0;
}
void DestroyList(DList *&plist)
{
ClearList(plist);
delete plist;
plist = NULL;
}
PNode GetHead(DList *plist)
{
return plist->head;
}
PNode GetTail(DList *plist)
{
return plist->tail;
}
int GetSize(DList *plist)
{
return plist->size;
}
PNode PopFront(DList *plist)
{
PNode p = plist->head;
PNode result;
if(!p) return NULL;
plist->head = p->next;
result = p;
delete p;
if(plist->head == NULL)
plist->tail = NULL;
else
plist->head->previous = NULL;
plist->size--;
return result;
}
PNode PopBack(DList *plist)
{
PNode p = plist->tail;
PNode result;
if(!p) return NULL;
plist->tail = p->previous;
result = p;
delete p;
if(plist->tail == NULL)
plist->head = NULL;
else plist->tail->next = NULL;
plist->size--;
return result;
}
void PushFront(DList *plist, Item x)
{
PNode newNode = new Node(x);
if(plist->head == NULL)
{
plist->head = newNode;
plist->tail = newNode;
}
else
{
newNode->next = plist->head;
plist->head->previous = newNode;
plist->head = newNode;
}
plist->size++;
}
void PushBehind(DList *plist, Item x)
{
PNode newNode = new Node(x);
if(plist->tail == NULL)
{
plist->head = newNode;
plist->tail = newNode;
}
else
{
newNode->previous = plist->tail;
plist->tail->next = newNode;
plist->tail = newNode;
}
plist->size++;
}
void InsertBefore(DList *plist, PNode p, Item key)
{
if(p == NULL) return;
PNode newNode = new Node(key);
if(plist->head == NULL)
{
plist->head = newNode;
plist->tail = newNode;
plist->size = 1;
return;
}
if(p == plist->head)
PushFront(plist, key);
else
{
PNode newNode = new Node(key);
newNode->next = p;
newNode->previous = p->previous;
p->previous->next = newNode;
p->previous = newNode;
plist->size++;
}
}
void InsertBehind(DList *plist, PNode p, Item key)
{
if(p == NULL) return;
PNode newNode = new Node(key);
if(plist->head == NULL)
{
plist->head = newNode;
plist->tail = newNode;
plist->size = 1;
return;
}
if(p == plist->tail)
PushBehind(plist, key);
else
{
PNode newNode = new Node(key);
newNode->previous = p;
newNode->next = p->next;
p->next->previous = newNode;
p->next = newNode;
plist->size++;
}
}
void traverse(PNode p)
{
cout<<p->data<<" ";
}
void ListTraverse(DList *plist, void(*visit)(PNode))
{
if(plist->head == NULL) return;
PNode p = plist->head;
while(p != plist->tail)
{
visit(p);
p = p->next;
}
visit(p);
cout<<endl;
}
int main()
{
int arr[] = {
2, 1, 9, 8, 3, 5, 7
};
DList *plist = InitList();
for(int i = 0; i < 4; ++i)
{
PushFront(plist, arr[i]);
}
for(int i = 4; i < sizeof(arr)/sizeof(int); ++i)
{
PushBehind(plist, arr[i]);
}
ListTraverse(plist, traverse);
return 0;
}