单链表——SList

来源:互联网 发布:c语言相关书籍 编辑:程序博客网 时间:2024/06/04 23:27

单链表:

 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。 如图显示:

这里写图片描述

这里写图片描述

DATA域中存放节点的数据,而NEXT域则存放节点下一个要指向的位置(后继)。
链表就如同现实生活中的火车一样,每个车厢都需要一个链子来进行链接,这“链子”就是指针了。
不多说了,直接上代码如何进行表示单链表。

首先我把链表分为以下几步:
1、创建链表
2、销毁链表
3、清空链表
4、获得链表的长度
5、链表的插入
6、链表头节点的插入
删除节点和插入操作相类似

//首先定义节点,如果数据不多,不具备多种属性,可以采用C语言中的Struct,使用C++中的class也是可以的typedef struct Node{    int data;    Node *next;    void print();};//或者class Node{ public:     int data;     Node *next;     void print();     };void Node::print(){    cout << data << endl;}//开始定义链表类#include <iostream>using namespace std;class List{public:     List();                                                  //创建链表    ~List();                                                  //销毁链表    void ClearList();                                         //清空链表    int ListLength();                                         //链表长度    void ListTraverse();                                      //遍历链表    bool ListInsert(int i, Node *pNode);                       //链表插入    bool ListInsertHead(Node *pNode);                         //插入在头结点之后private:    Node *m_plist;    int m_iLength;                                           //链表长度};List::List(){    m_plist = new Node;    m_plist->data = 0;    m_plist->next = NULL;    m_iLength = 0;}List::~List(){    ClearList();    delete m_plist;    m_plist = NULL;}int List::ListLength(){    return m_iLength;}void List::ClearList(){    Node *currentElem = m_plist->next;         //定义一个节点指向链表所指的下一位置    while (currentElem != NULL)    {        Node *temp = currentElem->next;        delete currentElem;        currentElem = temp;    }    m_plist->next = NULL;}bool List::ListInsertHead(Node *pNode){    Node *temp = m_plist->next;          //pNode节点将要插入,temp保存当前链表的下一个节点      Node *newNode = new Node;            //定义新节点    if (newNode == NULL)    {        return false;    }    newNode->data = pNode->data;         //将当前链表的值赋给新节点    m_plist->next = newNode;            //当前链表和新节点建立链接    newNode->next = temp;              //新节点插入完毕,链接temp    m_iLength++;    return true;}bool List::ListInsert(int i, Node *pNode){    if (i<0 || i>m_iLength)    {        return false;    }    Node *currentNode = m_plist;    for (int k = 0; k<i; k++)                //循环寻找插入点    {        currentNode = m_plist->next;    }    Node *newNode = new Node;               //定义新节点    if (newNode == NULL)    {        return false;    }    newNode->data = pNode->data;            //给新节点赋值    newNode->next = currentNode->next;      //新节点指向要插入位置节点的下一位置    currentNode->next = newNode;            //旧链表指向新来的节点成员    m_iLength++;    return true;}void List::ListTraverse(){    Node *currentNode = m_plist;              //头节点    while(currentNode->next != NULL)    {        currentNode = currentNode->next;        currentNode->printNode();    }}int main(){    Node node1;    node1.data = 3;    Node node2;    node2.data = 4;    Node node3;    node3.data=7;    int a;    List *pList = new List();    pList->ListInsertHead(&node1);    pList->ListInsertHead(&node2);    pList->ListInsert(1, &node3);    a = pList->ListLength();    cout << "Length: " << a << endl;    pList->ListTraverse();    delete pList;    pList = NULL;    system("pause");    return 0;}

大概就这样!

原创粉丝点击