栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

单链表练习:方法实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

单链表练习:方法实现

#include
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<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; idata = 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; idata = a[i];
            tail->next = p;
            tail = p;
        }
        tail->next = NULL;
    }
}
void List::Printall()
{
    Node *p ;
    p = first->next;
    while(p != NULL)
    {
        cout<data<<" ";
        p = p->next;
    }
    cout<next;
    Node *q;
    int n = 1;
    while(p!=NULL && n插在索引值为index上(第index+1位)
    {
        //index-1:扫完索引值为0~6的结束->插在第index位
        p = p->next;            //两种情况在index<=8时,都为使p=p->next=NULL,所以可以在链的末尾插入
        n++;
        //cout<data<<" "<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 && nnext;
        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 && nnext;
        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 && nnext;
        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函数

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/330465.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号