链表-02_第一个链表

一、说明

用模版实现链表

ListNode
List

链表操作

Insert      插入
Delete     删除
Invert     反转
Concatenate     合并

二、代码

main.cpp
#include <iostream>
#include "MyList.h"

using namespace std;

int main()
{
    cout << "第一个链表" << endl;
    List<int> IntList;
    IntList.Insert(5);
    IntList.Insert(15);
    IntList.Insert(25);
    IntList.Insert(35);
    IntList.Show();

    IntList.Invert();
    IntList.Show();

    IntList.Delete(15);
    IntList.Show();

    List<char> CharList;
    CharList.Insert('A');
    CharList.Insert('B');
    CharList.Insert('C');
    CharList.Show();
    CharList.Invert();
    CharList.Show();

    List<char> CharList2;
    CharList2.Insert('D');
    CharList2.Insert('E');
    CharList2.Insert('F');
    CharList2.Show();
    CharList2.Invert();
    CharList2.Show();

    CharList.Concatenate(CharList2);
    CharList.Show();
    return 0;
}
MyList.h
#ifndef LIST_H
#define LIST_H

#include <iostream>

using namespace std;

template <class Type> class List;

/*节点类ListNode*/
template <class Type>
class ListNode
{
    friend class List<Type>;
private:
    Type data;
    ListNode *link;
    ListNode(Type);//私有构造函数,只有由友元类List调用
} ;

/*链表类List*/
template <class Type>
class List
{
public:
    List()//构造函数。建立一个空的链表
    {
        first = 0;//头指针为0
    };

    void Insert(Type);//链表插入节点函数
    void Delete(Type);//链表删除节点函数
    void Invert();//链表元素反转
    void Concatenate(List<Type>);//连接链表
    void Show();//输出链表里的元素。测试用
private:
    ListNode<Type> *first;//头指针
};

/*ListNode的构造函数*/
template <class Type>
ListNode<Type>::ListNode(Type element)
{
    data = element;
    link = 0;
}

/*插入函数*/
template <class Type>
void List<Type>::Insert(Type k){
    ListNode<Type> *newNode = new ListNode<Type>(k);
    newNode->link = first;
    first = newNode;
}

template <class Type>
void List<Type>::Delete(Type k)
{
    ListNode<Type> *previous = 0;//用来保存被删除节点的前一个节点
    ListNode<Type> *current;//用来遍历整个链表
    /*令current节点为first节点;
    当current存在且其值不为k时;
    将前一个节点(previous)指向current,其current指向后一个节点。
    以此实现对链表的整个遍历*/
    for(current = first;current&&current->data != k;previous =current,current = current->link)
    {
        //空循环。目的:找到要被删除的节点
    }
    if(current)//找到了其data值为k的节点
    {
        if(previous)//如果有前一个那就不是第一个节点(即不是头结点)
        {
            previous->link = current->link;
        }else//要删除的节点是头结点
        {
            first = first->link;
        }
        delete current;//current指向first,first是new出来的。所以current需要delete操作
    }
}

/*反转链表*/
template <class Type>
void List<Type>::Invert()
{
    ListNode<Type> *p = first;//指向链表的开头
    ListNode<Type> *q = 0;//指向链表的结尾
    //第一个节点与最后一个节点交换,第二个与倒数第二个交换
    while(p)
    {
        ListNode<Type> *r = q;
        q = p;
        p = p->link;
        q->link = r;
    }
    first = q;
}

/*合并链表*/
template <class Type>
void List<Type>::Concatenate(List<Type> b)
{
    if(!first)//如果第一个链表是空的
    {
        first = b.first;
        return;
    }
    if(b.first)//链表b不是空的
    {
       ListNode<Type> *p;
       for(p = first;p->link;p = p->link)
       {
           ;//空循环。找到first节点链表的最后一个节点
       }
       p->link = b.first;
    }
}

/*输出节点值*/
template <class Type>
void List<Type>::Show()
{
    //定义一个指向first节点的curNode节点,当 curNode节点存在时,就指向下一个节点
    for(ListNode<Type> *curNode = first;curNode;curNode = curNode->link)
    {
        cout<<curNode->data;
        if(curNode->link){
            cout<<"->";
        }
    }
    cout<<endl;
}

#endif // LIST_H
输出结果

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值