单链表练习:方法实现

#include<iostream>
using namespace std;
struct Node
{
    Node *next;
    int data;
};
//有空改善+继续补充:可以将遍历index的循环写进一个方法里吗?
//链表尾插初始化,是否考虑一下空数列的情况?
class List
{
public:
    List(); //初始化:头结点
    ~List();    //析构:销毁单链表
    List(int a[],int n,int flag=1);    //初始化:一个单链表,0头插,1尾插
    void Insert(int data,int index);    //插入元素
    void Delete(int index); //删除元素
    void Modify(int new_data,int index);  //修改元素
    void Printall();    //遍历:打印所有元素
    int Index(int data);    //按值查找:查找值为data的元素序号
    int Value(int index);   //按位查找:查找第i个结点的元素值
    int Length();   //求单链表长度
    bool Empty();   //判断单链表是否为空
private:
    Node *first;
};

int main()
{
    int a[8] = {};
    List li1(a,8);
    //index为索引值:扫过前index个
    //li1.Insert(100,8);
    //li1.Delete(7);
    //li1.Modify(100,7);
    //li1.Printall();
    //cout<<li1.Index(8)<<endl;  //不存在报错
    //cout<<li1.Value(8)<<endl;
    //List li1;
    //cout<<li1.Length()<<endl;
    //cout<<li1.Empty()<<endl;

    return 0;
}

List::List()
{
    first = new Node;
    first->next = NULL;
}
List::~List()
{
    Node *p=first;
    while(p!=NULL)
    {
        first = first->next;
        delete p;
        p = first;
    }
}
List::List(int a[],int n,int flag)
{
    int i;
    Node *p;
    if(flag==0)
    {
        first = new Node;
        first->next = NULL;
        for(i=0; i<n; i++)
        {
            p = new Node;
            p->data = a[i];
            p->next = first->next;
            first->next = p;
        }
    }
    else
    {
        first = new Node;
        Node *tail=new Node;
        tail->data = a[0];
        first->next = tail;
        for(i=1; i<n; i++)
        {
            p = new Node;
            p->data = a[i];
            tail->next = p;
            tail = p;
        }
        tail->next = NULL;
    }
}
void List::Printall()
{
    Node *p ;
    p = first->next;
    while(p != NULL)
    {
        cout<<p->data<<" ";
        p = p->next;
    }
    cout<<endl;
}
void List::Insert(int data,int index)
{
    Node *p = first->next;
    Node *q;
    int n = 1;
    while(p!=NULL && n<index)//index:扫完索引值为0~7的值结束->插在索引值为index上(第index+1位)
    {
        //index-1:扫完索引值为0~6的结束->插在第index位
        p = p->next;            //两种情况在index<=8时,都为使p=p->next=NULL,所以可以在链的末尾插入
        n++;
        //cout<<p->data<<" "<<n<<endl;
    }
    if(p==NULL)throw"输入索引值错误!!!";
    else
    {
        q = new Node;
        q->data = data;
        q->next = p->next;
        p->next = q;
    }
}
void List::Delete(int index)
{
    Node *p = first->next;
    Node *q;
    int n=1, x;
    while(p!=NULL && n<index)
    {
        p = p->next;
        n++;
    }

    if(p == NULL)throw"输入索引值错误!!!";
    else
    {
        q = p->next;
        x = q->data;
        p->next = q->next;
    }
    //return x; //如果要输出删除值的话
}
void List::Modify(int new_data,int index)
{
    Node *p = first->next;
    int n = 1;
    while(p!=NULL && n<index+1)
    {
        p = p->next;
        n++;
    }
    if(p == NULL)throw"输入索引值错误";
    else
        p->data = new_data;
}
int List::Index(int data)
{
    Node *p = first->next;
    int n=0;
    while(p!=NULL)
    {
        if(p->data==data)return n;
        p = p->next;
        n++;
    }
    if(p == NULL)throw"该值不存在";
}
int List::Value(int index)
{
    Node *p = first->next;
    int n = 0;
    while(p!=NULL && n<index)
    {
        p = p->next;
        n++;
    }
    if(p == NULL)throw"索引值超出范围!!!";
    else
        return p->data;
}
int List::Length()
{
    Node *p = first->next;
    int n = 0;
    while(p != NULL)
    {
        p = p->next;
        n++;
    }
    return n;
}
bool List::Empty()
{
    if(first->next)return 0;
    else return 1;
}

C++ throw关键字(抛出异常+异常规范)_C语言中文网 (biancheng.net)

(1条消息) bool 函数用法_lwwzyy-CSDN博客_bool函数

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值