不带头结点的链表逆序,链表可以存放任意数据类型


#include <iostream>
#include <stdlib.h>
using namespace std;


template <typename T>  //类模板,链表可以存放任意数据类型
class linklist
{
private:
    typedef struct node
    {
        T data;
        struct node *next;
    } node;        
    node *head;
public:
    linklist(node *p):head(p){}  //构造函数
    ~linklist()   //析构函数
    {
        if(!head)
        {
            return;
        }
        while(head)
        {
            node *h=head;
            head=head->next;
            delete h;
        }
    }

    int getlen()
    {
        if (!head)
        {
            return 0;
        }
        node *p=head;
        int len=0;
        while(p)
        {
            len++;
            p=p->next;
        }
        return len;
    }

    bool IsEmpty()  //判断链表是否为空
    {
        if(!head)
        {
            return true;
        }
        else
            return false;
    }

    void HeadInsertNode(T date)  //头插法每次都在第一个结点的位置插入
    {
        node *p=new node;
        p->data=date;
        p->next=head;
        head=p;
    }

    
    void IndexInsertNode(int n,T date)  //指定位置插入
    {
        node *p1=head;
        int i=1;
        if (n<=0)
        {
            printf("插入位置不对\n");
        }
        node *p=new node;
        p->data=date;
        if (n==1)
        {
            p->next=head;
            head=p;
        }
        else
        {
            while(p1!=NULL&&i<n-1)
            {
                p1=p1->next;
                i++;
            }
            p->next=p1->next;
            p1->next=p;
        }
    }

    void DeleteIndexNode(int n)  //删除链表
    {
        node *p3=head;
        if (!p3)
        {
            return;
        }

        if (n==1)
        {
            head=head->next;
            delete p3;
        }

        else
        {
            for (int i = 1; i < n-1; ++i)
            {
                p3=p3->next;
            }
            node *tmp=p3->next;
            p3->next=p3->next->next;
            delete(tmp);
            tmp=NULL;
       }
    }

    void Display()  //显示链表
    {
        node *p=head;
        while(p)
        {
            cout<<p->data<<endl;
            p=p->next;
        }
    }

    void reserve()  //反转链表
    {
        if (!head)  //
        {
            cout<<"链表为空"<<endl;
            exit(1);
        }
        node *p1=head,*p2=p1->next,*p3;
        while(p2)
        {
            p3=p2->next;
            p2->next=p1;
            p1=p2;
            p2=p3;
        }    
        head->next=NULL;
        head=p1;
    }

};

 

int main()
{
    int sel,num,pos;
    linklist<int> L(NULL);
    while(1)
    {
    cout<<"请输入您想进行的操作的序号:"<<endl;
    cout<<"1.反转链表2.获取链表长度 3.判断链表是否为空 \n 4.头插法 5.从指定位置插入 6.删除指定位置元素 7.打印链表"<<endl;
    cin>>sel;
    getchar();
    switch(sel)
    {
        case 1:
            L.reserve();
            break;
        case 2:
            cout<<L.getlen()<<endl;
            break;
        case 3:
            if(L.IsEmpty()==true)
            {
                cout<<"链表为空"<<endl;
            }
            else 
                cout<<"链表不为空"<<endl;
            break;
        case 4:
            cout<<"输入要插入的数字"<<endl;
            cin>>num;
            L.HeadInsertNode(num);
            break;
        case 5:
            cout<<"输入要插入的位置,和数字"<<endl;
            cin>>pos>>num;
            L.IndexInsertNode(pos,num);
            break;
        case 6:
            cout<<"输入要删除的位置"<<endl;
            cin>>pos;
            L.DeleteIndexNode(pos);
            break;
        case 7:
            L.Display();
            break;
        default:
            break;
    }
}
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半岛铁盒.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值