双向链表

今天的内容是双向链

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';
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值