数据结构(一)单向链表c++实现
单向链表c++实现
代码结构

Node.h
#ifndef _NODE_H
#define _NODE_H
template <class T>
class  Node
{
    public:
    T val;
    Node<T>* next;
    Node();
};
template<class T>
Node<T>::Node()
{
    next = NULL;
}
#endif
List.h

#ifndef _LIST_H
#define _LIST_H
#include"Node.h"
#include<iostream>
using namespace std;
template<class T>
class List
{
    public:
    List();
    ~List();
    void append(T data);//尾部增加一个结点
    bool earse(int index);//删除第index个结点,下标以1开始。
    bool insert(int index,T data);//在index位置插入结点
    bool change(int index,T data);//修改index位置的结点的值为data
    int find_num(T data);//查找第一个val==data的结点并返回index,如果不存在则返回-1
    int find_index(int index);//查询index位置的值
    int len();//返回链表的长度
    void show();//将链表按顺序输出
    void clear();//将链表清空
    private:
    Node<T> *head;//头结点,没有val
    int length;//链表长度
};
#endif
List.cpp
构造函数List()
template<class T>
List<T>::List()
{
    head = new Node<T>;
    length = 0;
}
析构函数~List()
template<class T>
List<T>::~List()
{
    Node<T>* cur=head;//通过cur遍历每一个结点
    while(cur->next != NULL)
    {
        head = cur;//通过head存储要删除的结点的指针
        cur = cur->next;
        delete head;
    }
    delete cur;//删除最后一个结点
    /*Node<T> cur=head;
    while(cur!=NULL)
    {
        head = cur;
        cur = cur->next;
        delete head;
    }*/
}
增加append()
template<class T>
void List<T>::append(T data)
{
    Node<T>*cur = head;
    while(cur->next!=NULL)//当cur->next=NULL时退出循环,此时cur为链表最后一个结点
    {
        cur = cur->next;
    }
    Node<T>* Node_add = new Node<T>;
    cur->next = Node_add;
    Node_add->val = data;
    Node_add->next = NULL;
    length++;
}
删除earse()

template<class T>
bool List<T>::earse(int index)
{
    if(index>length)//判断删除位置是否合法
    {
        return false;
    }
    Node<T>*cur = head;
    for(int i=0;i<index-1;i++)//此处如果i<index,则下标是以0为开始。循环结束cur为删除结点的前一个结点
    {
        cur = cur->next;
    }
    Node<T>*cur_del = cur->next;
    cur->next = cur_del->next;
    delete cur_del;
    length--;
    return true;
}
插入insert()

template<class T>
bool List<T>::insert(int index,T data)
{
    if(index>length)
    {
        return false;
    }
    Node<T>* cur = head;
    for(int i=0;i<index-1;i++)
    {
        cur = cur->next;
    }
    Node<T>* cur_insert = new Node<T>;
    cur_insert->val = data;
    cur_insert->next = cur->next;
    cur->next = cur_insert;
    return true;
}
修改change()
template<class T>
bool List<T>::change(int index,T data)
{
    if(index>length)
    {
        return false;
    }
    Node<T>* cur = head;
    for(int i=0;i<index;i++)
    {
        cur = cur->next;
    }
    cur->val = data;
    return false;
}
查找
find_num()
template<class T>
bool List<T>::change(int index,T data)
{
    if(index>length)
    {
        return false;
    }
    Node<T>* cur = head;
    for(int i=0;i<index;i++)
    {
        cur = cur->next;
    }
    cur->val = data;
    return false;
}
find_index()
template<class T>
int List<T>::find_index(int index)
{
    if(index>length)
    {
        return -1;
    }
    Node<T>* cur = head;
    for(int i=0;i<index;i++)
    {
        cur = cur->next;
    }
    return cur->val;
}
长度len()
template<class T>
int List<T>::len()
{
    return length;
}
输出show()
template<class T>
void List<T>::show()
{
    Node<T>* cur = head->next;
    while(cur!=NULL)
    {
        cout<<cur->val<<"  ";
        cur = cur->next;
    }
    cout<<endl;
}
清空clear()
template<class T>
void List<T>::clear()
{
    Node<T>* cur=head->next;//头结点不删除
    Node<T>* cur_del =NULL;
    head->next = NULL;
    length = 0;
    while(cur!=NULL)
    {
        cur_del =cur;
        cur = cur->next;
        delete cur_del;
    }
}
测试
#include"List.h"
#include"List.cpp"
#include<iostream>
using namespace std;
int main()
{
    List<int> list;
    for(int i=1;i<=10;i++)
    {
        list.append(i);
    }
    cout<<"初始化链表:";list.show();
    list.earse(5);
    cout<<"删除第五个:";list.show();
    list.insert(4,99);
    cout<<"第四个位置插入数字 99: ";list.show();
    cout<<"第四个数为:"<<list.find_index(4)<<endl;
    cout<<"数字8的位置:"<<list.find_num(8)<<endl;
    list.change(4,88);
    cout<<"修改第四个数为 88: ";list.show();
    return 0;
}

本人水平有限,如有错误欢迎指正。
                    
                
                
            
        
浙公网安备 33010602011771号