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

数据结构---链表

时间:2016-07-08 21:43:47      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

先上源码,其中包括链表的添加、删除、输出和反转:

#include <iostream>
using namespace std;

class Node
{
public:
    int data;    //用来保存数据
    Node *next;    //指向下一个结点
    //构造函数
    Node(int _data)
    {
        data = _data;
        next = NULL;
    }
};


class LinkList
{
private:
    Node *head;    //表头
public:
    //构造函数
    LinkList()
    {
        head = NULL;        
    }

    //插入元素
    void insert(Node *node,int index)
    {
        //如果head为空指针
        if(head == NULL)
        {
            head = node;
            return;
        }

        //如果插入结点后的位置是链表首位
        if(index == 0)
        {
            node->next = head;    //node指针指向当前表头head
            head = node;
            return;
        }

        Node *current_node = head;    //用来在链表里遍历,初始指头指针head
        int count = 0;    //用来计数遍历了多少个结点

        //寻找目标位置的前一个位置
        while(current_node->next != NULL && count < index-1)
        {
            current_node = current_node->next;    //指向下一个结点
            count++;
        }

        //找到了前一个节点
        if(count == index-1)
        {
            node->next = current_node->next;    //新结点node的指针指向目标位置的后一个位置
            current_node->next = node;//目标位置的前一个结点指向新结点
        }
    }


    //遍历链表输出
    void output()
    {
        if(head == NULL)
        {
            return;
        }

        Node *current_node = head;    //用来遍历链表
        while(current_node != NULL)
        {
            cout << current_node->data << " ";
            current_node = current_node->next;
        }
        cout << endl;

    }
    //删除结点
    void delete_node(int index)
    {
        if(head == NULL)
        {
            return;
        }

        Node *current_node = head;
        int count = 0;

        if(index == 0)
        {
            head = head->next;
            delete current_node;
            return;
        }

        //寻找目标位置的前一个位置
        while( current_node->next != NULL && count < index-1)
        {
            current_node = current_node->next;
            count++;
        }

        if(count == index-1 && current_node->next != NULL)
        {
            Node *delete_node = current_node->next;
            current_node->next = delete_node->next;
            delete delete_node;
        }

    }

    //反转链表
    void reverse()
    {
        if(head == NULL)
        {
            return;
        }

        Node *next_node;//遍历过程中的下一个结点
        Node *current_node;//当前结点

        current_node = head->next;//current_node指向头节点的后一个节点
        head->next = NULL;//头结点的下一个结点指向一个空地址

        while(current_node != NULL)
        {。
            next_node = current_node->next;    //next_node 记录current_node的下一个结点
            current_node->next = head;//current_node 的next指针指向头结点

            head = current_node;    //更新头结点 
            current_node = next_node;    //更新current_node成为下一个结点
        }

    }

};

int main()
{
    LinkList linklist;
    for(int i = 1; i <= 10; ++i)
    {
        Node *node = new Node(i);
        linklist.insert(node,i-1);        
    }
    linklist.output();
    linklist.delete_node(3);
    linklist.output();
    linklist.reverse();
    linklist.output();
    return 0;
}

 首先定义一个Node,由两部分组成: 一个是data,即本身存放的数据;一个是next指针,用于指向下一个结点。

构造函数用于初始化Node中的data和next指针。

 

然后是LinkList类,这才是链表类。首先定义一个head指向表头。

然后是 insert(Node *node,int index)函数:

1.先考虑几种特殊的情况:一种是头指针为空指针,我们就让node成为头指针

if(head == NULL)
{
    head = node;
    return;
}

 

2.第二种情况:插入结点后的位置是链表的首位,就是index=0时,先让node的指针指向当前的头结点head,然后让node成为头结点head。

if(index == 0)
{
    node->next = head;
    head = node;
    return;
}       

 

3.在插入元素前,先找到目标位置的前一个位置,然后将结点插入到链表中。

首先定义两个变量,一个Node类型的指针current_node,用来在链表遍历,初始指向头指针head;

另一个是int类型的变量count,用来统计遍历了多少个节点,初始值为0。

Node *current_node = head;
int count = 0;

 

4.使用while循环寻找到目标的前一个位置,需要满足两个条件:

current_node 的下一个结点不能为空,如果为空就说明已经到了表尾;

count 要小于 index-1;

在while循环中,首先让current_node 指针指向它的下一个节点,然后count+1,这样就可以找到目标位置的前一位。

但是存在一种可能:index远大于链表的长度,这样就不符合条件。

while(current_node->next != NULL && count < index - 1)
{
    current_node = current_node->next;
    count++;     
}

所以要在while循环语句里面加一个限定条件:count = index - 1。

找到了目标位置的前一位,现在把这个新结点插入到链表里:

1.首先让node指向目标位置的后一个位置,也就是current_node的下一个结点,

2.然后让目标位置的前一个结点指向新节点,也就是current_node指向新结点

if(count == index - 1)
{
    node->next = current_node->next;    //新结点node的指针指向目标位置的后一个位置
    current_node->next = node;//目标位置的前一个结点指向新结点    
}

 

最后,测试数据:

int main()
{
    LinkList linklist;
    for(int i = 1; i <= 10; ++i)
    {
      Node *node = new Node(i);
      linklist.insert(node,i-1);        
    }   
    return 0;
}

 

数据结构---链表

标签:

原文地址:http://www.cnblogs.com/weishuan/p/5654479.html

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