数据结构之链表

头文件
using ElementType = int;
struct Node{
    ElementType data;
    Node* next;
};
using PrtNode = Node*;
using Position = PrtNode;
void InitLinkList(PrtNode p);
bool IsEmpyt(PrtNode p);
bool IsLast(PrtNode p,Position pos);
void Insert(PrtNode p, ElementType value, Position pos);
Position Find(PrtNode p, ElementType value);
Position FindPrevious(PrtNode p, ElementType value);
Position FindLatter(PrtNode p, ElementType value);
void DeleteList(PrtNode p);
ElementType GetElement(PrtNode p, Position pos);
void DeleteElement(PrtNode p, ElementType value);
void PrintList(PrtNode p);
void Or(PrtNode p1, PrtNode p2);
void And(PrtNode p1, PrtNode p2, PrtNode p_result);
void CreateListHead(PrtNode p);
void CreateListTail(PrtNode p);
Function body
#include <iostream>
#include "LinkList.h"
/*Assume use a head node */
bool IsEmpyt(PrtNode p){
    return p->next == nullptr;
}
bool IsLast(PrtNode p, Position pos){
    if (!IsEmpyt(p))
        return pos->next ==nullptr;
    return false;
}
/* create a empty link list */
void InitLinkList(PrtNode p){
    p->next = nullptr;
}
Position Find(PrtNode p, ElementType value){
    Position tmp = p->next;//tmp points to the first node.
    while (tmp != nullptr&&tmp->data != value)
        tmp = tmp->next;
    if (tmp == nullptr){
        std::cout << "Not found \n";
        exit(0);
    }
    return tmp;
}
Position FindPrevious(PrtNode p, ElementType value){
    Position tmp = p;
    while (tmp->next != nullptr&&tmp->next->data != value)
        tmp = tmp->next;
    if (tmp->next == nullptr){
        std::cout << "Not found \n";
        exit(0);
    }
    return tmp;
}
Position FindLatter(PrtNode p, ElementType value){
    Position tmp= Find(p, value);
    if (IsLast(p, tmp))
        return nullptr;
    return tmp->next;
}
void Insert(PrtNode p, ElementType value, Position pos){
    PrtNode new_node = new Node;
    new_node->data = value;
    new_node->next = pos->next;
    pos->next = new_node;
}
void DeleteElement(PrtNode p, ElementType value){
    Position tmp=FindPrevious(p, value);
    //tmp->next = tmp->next->next;
    Position tp = tmp->next;
    tmp->next = tp->next;
    delete tp;
}
void DeleteList(PrtNode p){
    Position tmp = p->next;
    Position Cell;
    while (tmp != nullptr){
        Cell = tmp->next;
        delete tmp;
        tmp = Cell;
    }
    p->next = nullptr;
}
ElementType GetElement(PrtNode p, Position pos){
    return pos->data;
}
bool FindValue(PrtNode p, ElementType value){
    Position tmp = p->next;
    while (tmp != nullptr&&tmp->data != value)
        tmp = tmp->next;
    if (tmp == nullptr)
        return false;
    return true;
}
void Or(PrtNode p1, PrtNode p2){
    Position tmp = p1->next;
    while (tmp != nullptr){
        if (!FindValue(p2, tmp->data))
            Insert(p2, tmp->data, p2);
        tmp = tmp->next;
    }
}
void And(PrtNode p1, PrtNode p2, PrtNode p_result){
    Position tmp = p1->next;
    while (tmp != nullptr){
        if (FindValue(p2, tmp->data))
            Insert(p_result, tmp->data, p_result);
        tmp = tmp->next;
    }
}
void PrintList(PrtNode p){
    Position tmp = p->next;
    while (tmp != nullptr){
        std::cout << tmp->data << " ";
        tmp = tmp->next;
    }
}
void CreateListHead(PrtNode p){
    /*Create a list with ten nodes */
    PrtNode new_node;
    for (int i = 0; i < 10; ++i){
        new_node = new Node;
        new_node->data = i + 1;
        new_node->next = p->next;
        p->next = new_node;
    }
}
void CreateListTail(PrtNode p){
    PrtNode new_node;
    for (int i = 0; i < 10; ++i){
        new_node = new Node;
        new_node->data = i + 1;
        new_node->next = p->next;
        p->next = new_node;
        p = new_node;
    }
}
Test
int main(){
    std::cout << "Create a list with ten nodes for test \n";
    PrtNode p = new Node;
    //p->next = nullptr;
    InitLinkList(p);
    CreateListHead(p);
    std::cout << "Check if the list is empty :";
    if (IsEmpyt(p))
        std::cout << "Empty \n";
    else
        std::cout << "Not Empty \n ";
    std::cout << "Print all the elements in the list :";
    PrintList(p);
    std::cout << "\n Insert 88 after the head node \n";
    Insert(p, 88, p->next);
    std::cout << "\n Print all the elements in the list :";
    PrintList(p);
    std::cout << "\n Insert 99 after position of 3 \n";
    Insert(p, 99, FindLatter(p, 3));
    std::cout << "\n Print all the elements in the list :";
    PrintList(p);
    std::cout << "\n Delete  10 from the list \n";
    DeleteElement(p, 10);
    std::cout << "\n Print all the elements in the list :";
    PrintList(p);
    std::cout << "\n Create another list with ten nodes \n";
    PrtNode p2 = new Node;
    InitLinkList(p2);
    CreateListTail(p2);
    std::cout << "\n Print all the elements in the list :";
    PrintList(p2);
    /*Or(p, p2);
    std::cout << "\n After call the or function ,output the result :";
    PrintList(p2);
    */
    PrtNode p_result = new Node;
    InitLinkList(p_result);
    And(p, p2, p_result);
    std::cout << "\n After call the and function ,output the result : ";
    PrintList(p_result);
    std::cout << "Delete the list \n";
    DeleteList(p);
    std::cout << "Check if the list is empty :";
    if (IsEmpyt(p))
        std::cout << "Empty \n";
    else
        std::cout << "Not empty \n";
    std::cout << std::endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值