单链表的实现(不带头结点)

// LinkList.cpp : 定义控制台应用程序的入口点。 
//定义的链表不含头结点 
 
#include "stdafx.h" 
#include <iostream> 
using namespace std; 
 
template<class Type> 
//定义结点 
struct Node 

    Type data; 
    Node<Type> *next; 
}; 
//定义链表 
template<class Type> 
class LinkList 

public: 
    LinkList();//默认构造函数 
    LinkList(const LinkList<Type>& otherList);//拷贝构造函数 
    ~LinkList(); 
    void createInsertHead();//头插法 
    void createInsertRear();//尾插法 
    void initList();//将链表归为初始状态 
    bool isEmpty();//判断链表是否为空 
    int  length(); 
    void destoryList();//销毁链表 
    void getFirstData(Type& firstdata); 
    void getData(int pos,Type& data); 
    void insertFirst(Type newdata);//在表头插入新的元素 
    void insertLast(Type newdata);//在表尾插入新的元素 
    void deleteFirst(Type& data);//删除表头元素 
    void deleteLast(Type& data);//删除表尾元素 
    void reverse(); 
    const LinkList<Type>& operator=(const LinkList<Type>&otherList);//重载赋值运算符 
    friend ostream& operator<< <>(ostream& cout,const LinkList<Type>& list); 
private: 
    int m_length;//链表中结点个数 
    Node<Type>* head; //不使用头结点 
}; 
 
template<class Type> 
LinkList<Type>::LinkList()//默认构造函数 

    //head = new Node<Type>; 
    head = NULL; 
    m_length = 0; 

 
template<class Type> 
LinkList<Type>::LinkList(const LinkList<Type>& otherList)//拷贝构造函数 
{    
    head = NULL; 
    m_length = otherList.m_length; 
     
    Node<Type> *current; 
    Node<Type> *otherListcurrent = otherList.head; 
    while(otherListcurrent != NULL) 
    { 
        for (int i=1; i<=otherList.m_length; i++) 
        { 
            Node<Type> *newnode = new Node<Type>; 
            newnode->data = otherListcurrent->data; 
            newnode->next = NULL; 
 
            if (i == 1) 
            { 
                head = newnode; 
                current = head; 
            } 
            else 
            { 
                current->next = newnode;              
                current = current->next;              
            } 
            otherListcurrent = otherListcurrent->next; 
        }        
    } 

 
template<class Type> 
LinkList<Type>::~LinkList() 

    destoryList();   

 
template<class Type> 
void LinkList<Type>::createInsertHead()//头插法 

    Node<Type> *newnode; 
    cout<<"请输入链表的长度:"; 
    cin>>m_length; 
    cout<<"请输入链表的元素:"; 
    for (int i=0; i<m_length; i++) 
    { 
       newnode = new Node<Type>; 
       cin>>newnode->data; 
       newnode->next = head; 
       head = newnode; 
    } 

 
template<class Type> 
void LinkList<Type>::createInsertRear()//尾插法    尾插法和拷贝构造函数都有单独考虑第一个结点的情况 

    Node<Type> *newnode; 
    Node<Type> *current; 
    cout<<"请输入链表的长度:"; 
    cin>>m_length; 
    cout<<"请输入链表的元素:"; 
    for (int i=1; i<=m_length; i++) 
    { 
        newnode = new Node<Type>; 
        cin>>newnode->data; 
        newnode->next = NULL; 
        if (i==1) 
        { 
            head = newnode; 
            current = head; 
        } 
        else 
        { 
            current->next = newnode; 
            current = current->next; 
        } 
    } 

 
template<class Type> 
void LinkList<Type>::initList() 

    destoryList(); 
    head = NULL; 
    m_length = 0; 

 
template<class Type> 
bool LinkList<Type>::isEmpty() 

    if (head == NULL) 
    { 
        return true; 
    } 
    else  
    { 
        return false; 
    } 

 
template<class Type> 
int  LinkList<Type>::length() 

    return m_length; 

 
template<class Type> 
void LinkList<Type>::destoryList() 

    Node<Type> *current; 
    while(head != NULL) 
    { 
        current = head; 
        head = current->next; 
        delete current;          
    } 
    head = NULL; 
    m_length = 0; 

 
template<class Type> 
void LinkList<Type>::getFirstData(Type& firstdata) 

    if (head!=NULL) 
    { 
        firstdata = head->data; 
    } 
    else  
    {    
        cout<<"链表为空!"<<endl; 
    } 

 
template<class Type> 
void LinkList<Type>::getData(int pos,Type& data) 

   if (pos<1||pos>m_length) 
   { 
       cout<<"指定的位置不正确!"<<endl; 
   } 
   else 
   { 
       Node<Type> *current = head; 
       int i=0; 
       while(i<pos-1) 
       { 
           current = current->next; 
           i++; 
       } 
       data = current->data; 
   }  

 
template<class Type> 
void LinkList<Type>::insertFirst(Type newdata) 

    Node<Type> *newnode = new Node<Type>; 
    newnode->data = newdata; 
    newnode->next = head; 
    head = newnode; 
    m_length++; 

 
template<class Type> 
void LinkList<Type>::insertLast(Type newdata)//尾部插入元素 

    Node<Type> *current = head; 
    while(current != NULL && current->next != NULL) 
    { 
        current = current->next; 
    } 
    Node<Type> *newnode = new Node<Type>; 
    newnode->data = newdata; 
    if (current==NULL) 
    { 
        /*newnode->next = current;
        current = newnode;*/ 
        newnode->next = head; 
        head = newnode; 
    } 
    else 
    { 
        newnode->next = current->next; 
        current->next = newnode; 
    }    
    m_length++; 

 
template<class Type> 
void LinkList<Type>::deleteFirst(Type& data) 

    if (isEmpty()) 
    { 
        cout<<"链表为空!"<<endl; 
    } 
    else 
    { 
        Node<Type> *temp = head; 
        data = head->data; 
        head = head->next; 
        delete temp; 
    } 
    m_length--; 

 
template<class Type> 
void LinkList<Type>::deleteLast(Type& data) 

    if (isEmpty()) 
    { 
        cout<<"链表为空!"<<endl; 
    } 
    else 
    {        
        Node<Type> *current = head; 
        for (int i=1; i<m_length-1; i++) 
        { 
            current = current->next; 
        } 
        Node<Type> *temp = current->next; 
        data = temp->data; 
        current->next = temp->next; 
        delete temp; 
        m_length--; 
    }    

 
template<class Type> 
void LinkList<Type>::reverse() 

    Node<Type> *current = head; 
    head = NULL; 
    if (current == NULL) 
    { 
        cout<<"链表为空!"<<endl; 
    } 
    else 
    { 
        while(current!=NULL) 
        { 
            Node<Type> *nextcurrent = current->next; 
            current->next = head; 
            head = current; 
            current = nextcurrent; 
        } 
    }    

 
template<class Type> 
const LinkList<Type>& LinkList<Type>::operator=(const LinkList<Type>&otherList) 

    Node<Type> *current; 
    Node<Type> *otherListcurrent = otherList.head; 
    if (this!=&otherList) 
    { 
        if (!isEmpty()) 
        { 
            initList();          
        } 
        if (otherListcurrent!=NULL) 
        {            
            for (int i=1; i<=otherList.m_length; i++) //或用while 
            { 
                Node<Type> *newnode = new Node<Type>; 
                newnode->data = otherListcurrent->data; 
                newnode->next = NULL; 
                if (i==1) 
                { 
                    head = newnode; 
                    current = head;//current始终指向链表的尾元素 
                } 
                else 
                { 
                    current->next = newnode; 
                    current = current->next; 
                } 
                otherListcurrent = otherListcurrent->next; 
            } 
        } 
    } 
    return *this; 

 
template<class Type> 
ostream& operator<< <>(ostream& cout,const LinkList<Type>& list) 

    Node<Type> *current = list.head; 
    if (current==NULL) 
    { 
        cout<<"链表为空!"<<endl; 
    } 
    else 
    {            
        while(current!=NULL) 
        { 
            cout<<current->data<<" "; 
            current = current->next; 
        } 
        cout<<endl; 
    } 
    return cout; 

 
 
 
int test(int argc, char* argv[]) 

    LinkList<char> *list = new LinkList<char>; 
    list->createInsertHead(); 
    cout<<"list:"<<*list<<endl; 
    list->initList(); 
    list->createInsertRear(); 
    if (!list->isEmpty()) 
    { 
        cout<<"链表长度为:"<<list->length()<<endl; 
    } 
    char firstdata; 
    list->getFirstData(firstdata); 
    cout<<"第一个元素是:"<<firstdata<<endl; 
    char posdata; 
    list->getData(4,posdata); 
    cout<<"第四个元素是:"<<posdata<<endl; 
    list->insertFirst('a'); 
    list->insertLast('m'); 
    cout<<"list:"<<*list<<endl; 
    char headdata; 
    char reardata; 
    list->deleteFirst(headdata); 
    list->deleteLast(reardata); 
    cout<<"list:"<<*list<<endl; 
    LinkList<char> *newList = new LinkList<char>(*list); 
    //newList->LinkList(*list); 
    cout<<"newList:"<<*newList<<endl; 
    list->reverse(); 
    cout<<"list:"<<*list<<endl; 
    newList = list; 
    cout<<"newList:"<<*newList<<endl; 
    system("pause"); 
    return 0; 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值