码迷,mamicode.com
首页 > 其他好文 > 详细

双向链表

时间:2015-04-20 15:04:38      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:position   node   using   数据结构   算法   

今天的内容是双向链

1. Head File

/*Double linked list */
using ElementType = int;
struct Node{
    ElementType data;
    Node* prior;
    Node* next;
};
using PtrNode = Node*;
using Position = Node*;
//Operation .
void InitList(PtrNode p);
bool IsEmpty(PtrNode p);
void Insert(PtrNode p, ElementType value, Position pos);
void Delete(PtrNode p, ElementType value);
Position Find(PtrNode p, ElementType value);
Position FindPrevious(PtrNode p, ElementType value);
void ClearList(PtrNode p);
void PrintList(PtrNode p);
bool IsLast(PtrNode p, Position pos);
void CreateList(PtrNode p);

2.Function body

/*Implemenation of double linkde list */
#include<iostream>
#include "标头.h"
bool IsEmpty(PtrNode p){
    return p->prior == p&&p->next==p;
}
void InitList(PtrNode p){
    p->next = p; 
    p->prior = p;
    std::cout << "Initialize successfully \n";
}
bool IsLast(PtrNode p, Position pos){
    if (!IsEmpty(p))
        return pos->next == p&&p->prior==pos;
    return false;
}
void Insert(PtrNode p, ElementType value, Position pos){
    PtrNode new_node = new Node;
    new_node->data = value;
    PtrNode temp = pos->next;
    new_node->next = pos->next;
    temp->prior = new_node;
    pos->next = new_node;
    new_node->prior = pos;
}
void Delete(PtrNode p, ElementType value){
    Position temp_1 = Find(p, value);
    Position temp_2 = FindPrevious(p, value);
    temp_2->next = temp_1->next;
    temp_1->next->prior = temp_2;
    delete temp_1;
}
Position Find(PtrNode p, ElementType value){
    Position temp = p->next;
    while (temp != p&&temp->data != value)
        temp = temp->next;
    if (temp == p)
        return nullptr;
    return temp;
}
Position FindPrevious(PtrNode p, ElementType value){
    return Find(p, value)->prior;
}
void PrintList(PtrNode p){
    Position temp = p->next;
    while (temp != p){
        std::cout << temp->data << " ";
        temp = temp->next;
    }
    std::cout << std::endl;
}
void ClearList(PtrNode p){
    Position temp = p->next;
    Position tmp;
    while (temp != p){
        tmp = temp->next;
        delete temp;
        temp = tmp;
    }
    p->next = p;
    p->prior = p;
}
/* Create a double linked list with ten node for the purpose of test */
void CreateList(PtrNode p){
    PtrNode new_node;
    for (int i = 0; i < 10; ++i){
        new_node = new Node;
        new_node->data = rand() % 10 + 1;
        Insert(p, new_node->data, p);
    }
}

3.Test

int main(){
    PtrNode p = new Node;
    std::cout << "Call the InitList function \n";
    InitList(p);
    std::cout << "Create a list with ten nodes for test \n";
    CreateList(p);
    std::cout << "Print all the elements in the list : ";
    PrintList(p);
    std::cout << "Inset 88 into the list after the head node \n";
    Insert(p, 88, p);
    std::cout << "Print all the elements in the list : ";
    PrintList(p);
    std::cout << "Delete 88 from the list \n";
    Delete(p, 88);
    std::cout << "Print all the elements in the list : ";
    PrintList(p);
    std::cout << "Clear the list \n";
    ClearList(p);
    std::cout << "Check if the list is empty : ";
    if (IsEmpty(p))
        std::cout << "Empty ";
    else
        std::cout << "Not empty ";
    std::cout << ‘\n‘;
}

双向链表

标签:position   node   using   数据结构   算法   

原文地址:http://blog.csdn.net/u014343243/article/details/45149049

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!