美文网首页
数据结构之线性表(下)

数据结构之线性表(下)

作者: dongwenbo | 来源:发表于2017-04-05 12:23 被阅读16次
单链表

单链表:通过指针连接的线性表

没有指针的语言如果表示链表?
答案是静态链表,静态链表用数组表示,使用元素的物理位序来替代地址

C++
Node

class Node {
public:
    int data;
    Node *next;
};

LinkedList

class LinkedList{
    Node *linkedList;
    int length;
public:
    LinkedList();
    ~LinkedList();
    bool listEmpty();
    int listLength();
    void clearList();
    void listInsertHead(int data);
    void listInsertTail(int data);
    void listInsert(int index,int data);
    void listDelete(int index);
    Node *getElem(int index);
    int locateElem(int data);
    Node* priorElem(Node *node);
    Node* nextElem(Node *node);
    void ListTraverse();
};


LinkedList::LinkedList(){
    linkedList = new Node();
    linkedList->data = 0;
    linkedList->next = nullptr;
    length = 0;
}

LinkedList::~LinkedList(){
    clearList();
    delete linkedList;
    linkedList = nullptr;
}

bool LinkedList::listEmpty(){
    return length == 0;
}

int LinkedList::listLength(){
    return length;
}

void LinkedList::clearList(){
    Node *currentNode = linkedList->next;
    while (currentNode) {
        Node *currentNextNode = currentNode->next;
        delete currentNode;
        currentNode = currentNextNode;
    }
    linkedList->next = nullptr;
    length = 0;
}

void LinkedList::listInsertHead(int data){
    Node *temp = new Node;
    temp->data = data;
    temp->next = linkedList->next;
    linkedList->next = temp;
    length++;
}

void LinkedList::listInsertTail(int data){
    Node *temp = new Node;
    temp->data = data;
    temp->next = nullptr;
    Node *tailNode = linkedList;
    while (tailNode->next) {
        tailNode = tailNode->next;
    }
    tailNode->next = temp;
    length++;
}

void LinkedList::listInsert(int index, int data){
    Node *temp = new Node;
    temp->data = data;
    Node *indexPreNode = linkedList;
    for (int k = 0; k < index; k++) {
        indexPreNode = indexPreNode->next;
    }
    temp->next = indexPreNode->next;
    indexPreNode->next = temp;
    length++;
}

void LinkedList::listDelete(int index){
    Node *indexPreNode = linkedList;
    for (int k = 0; k < index; k++) {
        indexPreNode = indexPreNode->next;
    }
    Node *waitDeleteNode = indexPreNode->next;
    indexPreNode->next = waitDeleteNode->next;
    delete waitDeleteNode;
    waitDeleteNode = nullptr;
    length--;
}

Node *LinkedList::getElem(int index){
    Node *indexNode = linkedList->next;
    for (int k = 0; k < index; k++) {
        indexNode = indexNode->next;
    }
    return indexNode;
}

int LinkedList::locateElem(int data){
    Node *currentNode = linkedList->next;
    for (int i = 0; i<length; i++) {
        if (currentNode->data == data) {
            return i;
        }else{
            currentNode = currentNode->next;
        }
    }
    return -1;
}

Node* LinkedList::priorElem(Node *node){
    Node *preNode = linkedList->next;
    for (int i = 0; i<length; i++) {
        if (preNode->next->data == node->data) {
            return preNode;
        }else{
            preNode = preNode->next;
        }
    }
    return nullptr;
}

Node* LinkedList::nextElem(Node *node){
    Node *currentNode = linkedList->next;
    for (int i = 0; i<length; i++) {
        if (currentNode->data == node->data) {
            return currentNode->next;
        }else{
            currentNode = currentNode->next;
        }
    }
    return nullptr;
}

void LinkedList::ListTraverse(){
    Node *currentNode = linkedList->next;
    while (currentNode) {
        std::cout<<currentNode->data<<std::endl;
        currentNode = currentNode->next;
    }
}

相关文章

  • 目录 - 数据结构

    总目录 数据结构 第01局:绪论 数据结构 第02局:线性表 上 数据结构 第03局:线性表 下 数据结构 第04...

  • 数据结构之线性表

    数据结构之线性表 1. 什么是线性表 线性表是一种最常用,最简单的一种数据结构。它是n个数据元素的有限序列。n 为...

  • 数据结构探险之线性表篇(上):顺序表

    数据结构探险之线性表篇 将要学到得: 线性表(链表) 什么是线性表? 线性表是n个数据元素的有限序列 排列之后成线...

  • 数据结构之线性表的链式存储结构

    之前写了线性表的顺序存储结构和有序线性表的顺序存储结构,今天接着写线性表的链式存储结构 数据结构之线性表的顺序存储...

  • 重温:数据结构与算法 - 03数组

    数据结构与算法之美 - 数组 数据结构与算法之美-学习大纲 什么数组? 数组是一种 线性表 数据结构。它用一组 连...

  • 【数据结构】线性表之单链表

    完整代码需结合前面一篇顺序表数据结构学习-线性表之顺序表各种操作网易云课堂小甲鱼课程链接:数据结构与算法 线性表的...

  • 数据结构之线性表(下)

    单链表:通过指针连接的线性表 没有指针的语言如果表示链表?答案是静态链表,静态链表用数组表示,使用元素的物理位序来...

  • 13-数据结构探险系列-线性表篇

    数据结构探险之线性表篇 将要学到得, 线性表(链表) 整体的路线图如上图所示,线性表要比队列和栈编码上难一点,起到...

  • iOS设计模式--迭代器

    学习迭代器之前,先看一种数据结构--线性表 线性表:线性表是最基本,最简单,也是最常用的一种数据结构。 线性表中数...

  • Java造轮子-数据结构-线性表

    数据结构-线性表 @(数据结构) 线性表是数据结构中的逻辑结构。可以存储在数组上,也可以存储在链表上。 顺序表(数...

网友评论

      本文标题:数据结构之线性表(下)

      本文链接:https://www.haomeiwen.com/subject/ysrtattx.html