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

数据结构之链表

时间:2015-04-17 20:34:21      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:node   数据结构   算法   

头文件

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;
}

数据结构之链表

标签:node   数据结构   算法   

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

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