码迷,mamicode.com
首页 > 编程语言 > 详细

C++ 链表

时间:2015-09-13 02:06:12      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
using namespace std;

template <typename T>
struct Node
{
    T t; //
    struct Node<T> *next;
};


template <typename T>
class LinkList
{
public:
    LinkList();
    ~LinkList();

public:
    int clear();
    int insert(T &t, int pos);
    int get(int pos, T &t);
    int del(int pos, T &t);
    int getLen();

protected:
    Node<T> *m_header;
    int m_len;

};

template <typename T>
LinkList<T>::LinkList()
{
    m_header = new Node<T>;
    m_header->next = NULL;
    m_len = 0;
}

template <typename T>
LinkList<T>::~LinkList()
{
    Node<T> *tmp = NULL;

    while (m_header !=NULL)//加进去的节点都是new出来的,在堆中,所以这里要手动释放,因为程序不会自动释放。
    {
        tmp = m_header->next;
        delete m_header;
        m_header = tmp;
    }
}

template <typename T>
int LinkList<T>::clear()
{

    //把旧的结点 del掉
    Node<T> *tmp = NULL;

    while (m_header)
    {
        tmp = m_header->next;
        delete m_header;
        m_header = tmp;
    }

    //重新创建
    m_header = new Node<T>;
    m_len = 0;

    return 0;
}

template <typename T>
int LinkList<T>::insert(T &t, int pos)
{
    Node<T> *current = NULL;

    current = m_header;

    for (int i=0; i<pos; i++)
    {
        current = current->next;
    }
    
    //把上层应用的t结点 缓存到容器中
    
    Node<T> *node = new Node<T>;//此处会调用T也就是Teacher的无参构造函数实现拷贝,因为放到容器里面是调用了对象的构造函数实现拷贝的,因此Teacher里面要提供一个无参构造函数,如果Teacher里面提供了有参构造函数则就没有无参构造函数了,如果没有无参构造函数则此处写成new Node<T>(1,"name"),
    node->next = NULL;
    node->t = t; // 把t1缓存下来

    //把node结点 在pos位置 加入到链表中

    node->next = current->next;
    current->next = node;

    m_len ++;
    return 0;
}

template <typename T>
int LinkList<T>::get(int pos, T &t)
{
    Node<T> *current = NULL;

    current = m_header;

    for (int i=0; i<pos; i++)
    {
        current = current->next;
    }
    t = current->next->t;
    return 0;
}

template <typename T>
int LinkList<T>::del(int pos, T &t)
{
    Node<T> *current = NULL;
    Node<T> *ret = NULL;

    current = m_header;

    for (int i=0; i<pos; i++)
    {
        current = current->next;
    }
    ret = current->next;
    t = ret->t; //把缓存的结点 给上层应用t

    //删除操作
    current->next = ret->next; 
    m_len --;
    delete ret; //注意释放内存  因为insert的时候 new Node<T>

    return 0;
}

template <typename T>
int LinkList<T>::getLen()
{
    return m_len;
}
#include <iostream>
using namespace std;

#include "LinkList.h"

struct Teacher
{
    char name[32];
    int age;
};

void main01()
{
    Teacher t1, t2, t3;
    Teacher  tmp;
    t1.age = 31;
    t2.age = 32;
    t3.age = 33;


    LinkList<Teacher> list;

    //插入元素 
    list.insert(t1, 0);
    list.insert(t2, 0);
    list.insert(t3, 0);    

    //遍历链表

    for (int i=0; i<list.getLen(); i++)
    {
        list.get(i, tmp);
        printf("age:%d ", tmp.age);
    }

    //销毁链表
    while (list.getLen() > 0)
    {
        list.del(0, tmp);
        printf("age:%d ", tmp.age);
    }
    
}

void main()
{
    main01();
    cout<<"hello..."<<endl;
    system("pause");
    return ;
}

 

C++ 链表

标签:

原文地址:http://www.cnblogs.com/yaowen/p/4803973.html

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