单链表

1.单链表##

对数据结构一直比较欠缺,所以准备i从头学习一下数据结构。
从单链表开始,链表的介绍和定义就省略了,我觉得学习单链表最重要的是掌握结点类和链表类的用法。掌握了这两个东西以后链表对象的各种方法都很容易实现。刚开始以为很难,后来研究之后发现仔细按照数据结构来写也不是很难。
实际上,下面的链表对象方法的框架记下来之后基本上就可以实现链表对象的所有方法了:

void List::method(int data)
{
    Node *p=head;
    if(p==NULL)
    {
        // do something...
    }
    while(p!=NULL && p->data!=data)
    {
        // do something...
    }
    if(p==NULL)
    {
        // do something...
    }
    else
    {
        // do something...
    }
    return;
}

直接上干货:

1. 链表结点类定义

class Node
{
public:
    int data;  //结点元素值
    Node *next;   //结点后继
    Node(int data=0,Node *next= NULL)  //构造函数,默认新结点值为null
    {
        this->data = data;
        this->next = next;
    }
};

2. 链表类的定义

class List
{
private:
    Node *head,*tail;  //链表对象需要定义头结点和尾结点
    int position;     
public:
    List(){head = tail = NULL;};  //构造函数,默认头尾结点都为空
    ~List(){delete head; delete tail;};  //析构函数,删除头结点和尾结点
    // void print();
    // 各种List对象的方法
};

3. 链表的ADT方法

void print();  //打印链表
void Insert(int data=0);  //在链表尾部插入数据
void Delete(int data=0);  //按照元素值删除结点
void Search(int data=0);  //按照元素值搜索,并打印索引
int getvalue(int position);   //按照索引返回元素值
void setvalue(int position,int data);  //按照索引更改结点的值
void Insertwithindex(int index,int data);  //按照索引插入结点
void Deletewithindex(int index);  //按照索引删除结点
int getindexofnumber(int data);   //按照元素值查找,返回索引
void Clearlist();   //清空链表
int Getlength();   //返回链表的长度

4. C++代码

#include <iostream>

using namespace std;
class Node
{
public:
    int data;
    Node *next;
    Node(int data=0,Node *next= NULL)
    {
        this->data = data;
        this->next = next;
    }
};
class List
{
private:
    Node *head,*tail;
    int possition;
public:
    List(){head = tail = NULL;};
    ~List(){delete head; delete tail;};
    void print();
    void Insert(int data=0);
    void Delete(int data=0);
    void Search(int data=0);
    int getvalue(int position);
    void setvalue(int position,int data);
    void Insertwithindex(int index,int data);
    void Deletewithindex(int index);
    int getindexofnumber(int data);
    void Clearlist();
    int Getlength();
};
//打印链表
void List::print()
{
    Node *p=head;
    cout << "List print: ";
    while(p!=NULL)
    {
        cout << p->data <<" " ;
        p=p->next;
    }
    cout <<endl;
}
//在尾部插入数据
void List::Insert(int data)
{
    if(head == NULL )
    {
        head=tail=new Node(data);
        head->next = NULL;
        tail->next = NULL;
    }
    else
    {
        Node *p=new Node(data);
        tail->next = p;
        tail=p;
        tail->next=NULL;
    }
}
//按照数据删除
void List::Delete(int data)
{
    Node *p=head,*pre;
    if(p==NULL)
    {   cout << "The LIST is empty!" <<endl;return;}
    while(p!=NULL && p->data!=data)
    {
        pre=p;
        p=p->next;
    }
    if(p==NULL)
    {cout << "There is no value "<<data<< " in this list." <<endl;}
    else
    {
        pre->next=p->next;
        delete(p);
    }
    return;
}
//按数据查找
void List::Search(int data)
{
    Node *p=head;
    if(p==NULL)
    {   cout << "The LIST is empty!" <<endl;return;}
    int count=0;
    while(p!=NULL && p->data!=data)
    {
        p=p->next;
        count++;
    }
    cout << "the value searched is at position " << count << endl;

}
//get某个位置的值
int List::getvalue(int postion)
{
    Node *p=head;
    if(p==NULL)
    {   cout << "The LIST is empty!" <<endl;}
    else
    {
        int pos =1;
        while(p!=NULL && pos!=postion)
        {   pos++;
            p=p->next;
        }
        if(p==NULL)
        {cout << "There is no value of this position!"<<endl;}
        else
        {cout << "In this position, the value is " << p->data<< endl;}
    }
    return p->data;
}
//set某个位置的值
void List::setvalue(int position, int data)
{
    Node *p=head;
    if(p==NULL)
    {cout << "The list is empty!"<< endl;}
    else
    {
        int pos=1;
        while(p!=NULL && pos!=position)
        {
            pos++;
            p=p->next;
        }
        if(p==NULL)
        {cout << "There is no position in this list!"<<endl;}
        else
        {
            p->data=data;
            cout << "The value in this position has been changed!" <<endl;
        }
    }
}
//按照索引插入数据
void List::Insertwithindex(int index, int data)
{
    Node *p=head,*pre;
    int count=1;
    while(p!=NULL && count!=index)
    {
        count++;
        pre=p;
        p=p->next;
    }
    if(p!=NULL)
    {
        Node *s=new Node;
        s->data=data;
        pre->next=s;
        s->next=p;
    }
    else
    {cout<< "the list has no index " <<index<<endl;return;}
}
//按照索引删除数据
void List::Deletewithindex(int index)
{
    Node *p=head,*pre;
    int count=1;
    while(p!=NULL && count!=index)
    {
        count++;
        pre=p;
        p=p->next;
    }
    if(p!=NULL)
    {
        pre->next=p->next;
        delete(p);
    }
    else
    {cout<< "the list has no index " <<index<<endl;return;}
    return;
}
//查找元素,返回索引
int List::getindexofnumber(int data)
{
    Node *p=head;
    int pos=1;
    while(p!=NULL && p->data!=data)
    {
        pos++;
        p=p->next;
    }
    if(p!=NULL)
    {
        cout << "the index of the data "  << pos << endl;
        return pos;
    }
    else
    {cout<< "the list has no data " <<data<<endl;return -1;}

}
//清空链表
void List::Clearlist()
{
    Node *p=head,*pre;
    while(p!=NULL)
    {
        pre=p;
        p=p->next;
        delete(pre);
    }
    head=NULL;
    tail=NULL;
}
//链表长度
int List::Getlength()
{
    Node *p=head;
    int count=1;
    while(p!=NULL)
    {
        count++;
        p=p->next;
    }
    cout << "the length of list is " << count << endl;
    return count;
}
//主函数
int main()
{
    List mylist;
    for(int i=1;i<10;++i)
    {
        mylist.Insert(i);
    }
    cout <<endl<< "********mylist.print();*********"<<endl;
    mylist.print();
    cout <<endl<< "********mylist.getvalue(3);*********"<<endl;
    mylist.getvalue(3);
    cout <<endl<< "********mylist.setvalue(3,9);*********"<<endl;
    mylist.setvalue(3,9);
    mylist.print();
    cout <<endl<< "********mylist.Delete(2);*********"<<endl;
    mylist.Delete(2);
    mylist.print();
    cout << endl<<"********mylist.Insertwithindex(3,999);*********"<<endl;
    mylist.Insertwithindex(3,999);
    mylist.print();
    cout << endl<<"********mylist.Deletewithindex(3);*********"<<endl;
    mylist.Deletewithindex(3);
    mylist.print();
    cout << endl<<"********mylist.getindexofnumber(4);*********"<<endl;
    mylist.getindexofnumber(4);
    mylist.print();
    cout << endl<<"********mylist.Getlength();*********"<<endl;
    mylist.Getlength();
    cout << endl<<"********mylist.Clearlist();*********"<<endl;
    mylist.Clearlist();
    mylist.print();
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,496评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,407评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,632评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,180评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,198评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,165评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,052评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,910评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,324评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,542评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,711评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,424评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,017评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,668评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,823评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,722评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,611评论 2 353

推荐阅读更多精彩内容