[Data Structure - Supplementary Chapter 3] The linear form single chain (C ++ implementation)

First, the class definition

Single-chain class is defined as follows:

#ifndef SIGNALLIST_H
#define SIGNALLIST_H

typedef int  ElemType; /* "ElemType类型根据实际情况而定, 这里假设为int */

/* 线性表的单链表存储结构 */
typedef struct node
{
    ElemType data; // 数据域
    struct node *next; // 指针域
}Node, LinkList;

class SignalList
{
public:
    SignalList(int size = 0); // 构造函数
    ~SignalList(); // 析构函数

    void clearList(); // 清空顺序表操作
    bool isEmpty(); // 判断是否为空操作 
    int getLength(); // 获取顺序表长度操作
    bool insertList(int i, const ElemType e); // 插入元素操作
    bool deleteList(int i, ElemType *e); // 删除元素操作
    bool getElem(int i, ElemType *e); // 获取元素操作
    bool insertListHead(const ElemType e); // 头部后插入元素操作
    bool insertListTail(const ElemType e); // 尾部后插入元素操作
    void traverseList(); // 遍历顺序表   
    int locateElem(const ElemType e); // 查找元素位置操作

private:
    LinkList *m_pList; // 单链表指针
};

#endif


Second, a constructor

For the first node m_pListfor memory, the data field is set to 0, to null pointer field.

// 构造函数
SignalList::SignalList(int size)
{
    // 初始化单链表
    m_pList = new Node;
    m_pList->data = 0;
    m_pList->next = NULL;
}


Third, the destructor

Empty single chain method calls, and the destruction of the head node.

// 析构函数
SignalList::~SignalList()
{
    clearList(); // 清空单链表
    delete m_pList;
    m_pList = NULL;
}


Fourth, empty the list operation

Destruction cycle each node except the first node.

// 清空链表操作
void SignalList::clearList()
{
    Node *cur; // 当前结点
    Node *temp; // 事先保存下一结点,防止释放当前结点后导致“掉链”

    cur = m_pList->next; //指向头结点
    while (cur)
    {
        temp = cur->next; // 事先保存下一结点,防止释放当前结点后导致“掉链”
        delete cur; // 释放当前结点
        cur = temp; // 将下一结点赋给当前结点
    }

    cur->next = NULL; // 注意还要将头结点的指针域指向空
}

Clear distinction and destructor list : the list is empty the loop destroy each node except the first node, destructors destroy all nodes, including the head node.


Five empty sentence length of the operating table, and the order of acquisition

// 判断是否为空操作
bool SignalList::isEmpty()
{
    return m_pList->next == NULL ? true : false;
}

// 获取链表长度操作
int SignalList::getLength()
{
    Node *cur = m_pList;
    int length = 0;

    while (cur->next)
    {
        cur = cur->next;
        length++;
    }

    return length;
}


Six insertion operation element

Note that this is the first node, the first node as position 0, position 1 and can only be inserted in the rear, so that at least one i.

// 插入元素操作
bool SignalList::insertList(int i, const ElemType e)
{
    // 判断链表是否存在
    if (!m_pList)
    {
        cout << "list not exist!" << endl;
        return false;
    }
    // 只能在位置1以及后面插入,所以i至少为1
    if (i < 1)
    {
        cout << "i is invalid!" << endl;
        return false;
    }

    // 找到位置i的前一个结点
    Node *front = m_pList; // 这里是让front指向链表的第0个结点-头结点
    int j = 1; // j为计数器,赋值为1,对应front指向的下一个结点,即插入位置结点
    while (front != NULL && j < i)
    {
        front = front->next;
        j++;
    }
    // 未找到i位置所在的前一个结点
    if (front == NULL)
    {
        cout << "dont find front!" << endl;
        return false;
    }       

    // 创建一个空节点,存放要插入的新元素
    Node *temp = new Node;
    temp->data = e;
    temp->next = NULL;

    // 插入结点s
    temp->next = front->next;
    front->next = temp;

    return true;
}


Seven, to remove the element operation

Note in advance to save the node to be deleted to avoid losing after deleting nodes.

// 删除元素操作
bool SignalList::deleteList(int i, ElemType *e)
{
    // 判断链表是否存在
    if (!m_pList)
    {
        cout << "list not exist!" << endl;
        return false;
    }
    // 只能删除位置1以及后面的结点
    if (i < 1)
    {
        cout << "i is invalid!" << endl;
        return false;
    }

    // 找到位置i的前一个结点
    Node *front = m_pList; // 这里是让front指向链表的第0个结点-头结点
    int j = 1; // j为计数器,赋值为1,对应front指向的下一个结点,即插入位置结点
    while (front != NULL && j < i)
    {
        front = front->next;
        j++;
    }
    // 未找到i位置所在的前一个结点
    if (front == NULL)
    {
        cout << "dont find front!" << endl;
        return false;
    }

    // 提前保存要删除的结点
    Node *temp = front->next;
    *e = temp->data; // 将要删除结点的数据赋给e

    // 删除结点
    front->next = front->next->next;    

    // 销毁结点
    delete temp;
    temp = NULL;

    return true;
}


Eight, traversal operation

Before traversing the need to determine whether there is a linked list.

// 遍历链表
void SignalList::traverseList()
{
    // 判断链表是否存在
    if (!m_pList)
    {
        cout << "list not exist!" << endl;
        return;
    }

    Node *cur = m_pList->next;
    while (cur)
    {
        cout << cur->data << " ";
        cur = cur->next;
    }
}


Nine, the main function is executed

Code executed in the main function as follows:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "signalList.h"

using namespace std;

int main()
{
    // 初始化链表
    SignalList signleList(20);

    cout << "插入元素0-2到链表!" << endl;
    for (int i = 0; i<3; i++)
    {
        signleList.insertList(i+1, i);
    }
    cout << endl;

    // 在位置2插入元素9到链表
    cout << "在位置2插入元素9到链表!" << endl << endl;
    signleList.insertList(2, 9);

    // 在位置3删除元素
    int value1;
    if (signleList.deleteList(3, &value1) == false)
    {
        cout << "delete error!" << endl;
        return -1;
    }
    else
    {
        cout << "在位置3删除元素,删除的元素为:" << value1 << endl << endl;
    }

    // 查找元素位置
    int index = signleList.locateElem(9);
    if (index == -1)
    {
        cout << "locate error!" << endl;
        return -1;
    }
    else
    {
        cout << "查找到元素9的位置为:" << index << endl << endl;
    }

    // 遍历链表
    cout << "遍历链表: ";
    signleList.traverseList();
    cout << endl << endl;

    // 清空链表
    cout << "清空链表!" << endl << endl;
    signleList.clearList();

    return 0;
}


The output shown below (compiler VS2013):


Guess you like

Origin www.cnblogs.com/linuxAndMcu/p/11563121.html
Recommended