自己写的c++实现的链表类

#include<iostream>
using namespace std;
struct Node  //结点信息
{
    int Data;
    Node *Next;
};
class ListLink  //链表类
{
private:
Node *Head;  //头结点
int Length;
public:
    ListLink();
    ListLink(int length);
    void Clear();
    ~ListLink();
    bool AddNode(Node *in);
    bool InsertToFirst(Node *in);
    bool InsertInOrder1(Node *p);
    bool InsertInOrder2(Node *p);
    bool DelNode(int tar);
    bool DelNode(Node *tar);
    int GetLength();
    void sort();
    void show();
    Node * SearchData(int tar);
    void ReverseList();
};


ListLink::ListLink()
{
Length = 0;
Head=NULL;
}
void ListLink::Clear()
{
    Node *p1,*p2;
    for(p1=Head;p1!=NULL;)
    {
        p2=p1;
        p1=p1->Next;
        delete p2;
    }
    Head=NULL;
}
ListLink::ListLink(int length)
{
    int i;
    Length = length;
    Node *Current,*Last;
    Head=Current=new Node;
    Last = new Node;
    Last->Data=0;
    for(i=0;i<length;i++)
    {
        Current->Next=Last;
        Current=Last;
        Last=new Node;
        Last->Data=0;
    }
    Current->Next=NULL;
    delete Last;
}
ListLink::~ListLink()
{
    Node *p1=NULL,*p2=NULL;
    for(p1=Head;p1!=NULL;)
    {
        p2=p1;
        p1=p1->Next;
        delete p2;
    }
    Head=NULL;
}
bool ListLink::AddNode(Node *in)
{
    Node *p = Head;
    while(p->Next!=NULL)
    {
        p=p->Next;
    }
    p->Next=in;
    in->Next=NULL;
    Length++;
    return true;
}
bool ListLink::InsertToFirst(Node *in)
{
    Node *p = Head;
    Head->Next=in;
    in->Next=p->Next;
    Length++;
    return true;
}
bool ListLink::InsertInOrder1(Node *in)//降序
{
Node *p=Head;
while(p->Next!=NULL&&p->Next->Data>in->Data)
p=p->Next;
in->Next=p->Next;
p->Next=in;
Length++;
return true;
}
bool ListLink::InsertInOrder2(Node *in)//升序
{
Node *p=Head;
while(p->Next!=NULL&&p->Next->Data<in->Data)
p=p->Next;
in->Next=p->Next;
p->Next=in;
Length++;
return true;
}
bool ListLink::DelNode(int tar)
{
Node *p=Head,*temp;
while(p->Next!=NULL&&p->Next->Data!=tar)
p=p->Next;
temp=p->Next;
if(temp)
{
p->Next=temp->Next;
delete temp;
Length--;
return 1;
}
return 0;
}
bool ListLink::DelNode(Node *tar)
{
Node *p=Head,*temp;
while(p->Next!=NULL&&p->Next->Data!=tar->Data)
p=p->Next;
temp=p->Next;
if(temp)
{
p->Next=temp->Next;
delete temp;
Length--;
return 1;
}
return 0;
}
int ListLink::GetLength()
{
return Length;
}
void ListLink::sort()   //dubble sort升序
{
int i,j;
Node *p=Head;
for(i=0;i<Length-1;i++)
for(j=0,p=Head;j<Length-i-1;j++,p=p->Next)
if(p->Next->Data>p->Next->Next->Data)
{
Node *temp1,*temp2,*temp3;
temp1=p->Next;
temp2=p->Next->Next;
temp3=p->Next->Next->Next;
p->Next=temp2;
temp2->Next=temp1;
temp1->Next=temp3;
}
}
void ListLink::show()
{
Node *p = Head->Next;
while(p!=NULL)
{
cout<<p->Data<<endl;
p=p->Next;
}
}
Node * ListLink::SearchData(int tar)
{
Node *p=Head;
while(p->Next!=NULL&&p->Next->Data!=tar)
p=p->Next;
if(p->Next)
{
return p->Next;
}
return NULL;
}


void ListLink::ReverseList()
{
    if(Head->Next == NULL || Head->Next->Next == NULL)
    {
return ;               //if the list have one or two links,return
    }


    Node *t = NULL,
*p = Head->Next,
*q = Head->Next->Next;
    while(q != NULL)
    {
t = q->Next;
q->Next = p;
p = q;
q = t;
    }


    Head->Next->Next = NULL;
    Head->Next = p;
    return ;
}


int main()
{
/*List t(7);
Node *in=new Node;
t.show();
cout<<(t.SearchData(0))->Data<<endl;
t.~List();*/
return 0;
}
/*
List::List();
List::List(int l);
List::~List() ;
bool List::AddList(Node *in) ;//move to the last and insert the node
bool List::InsertToFirst(Node *in);//move to the first and insert the node
bool List::InsertInOrder1(Node *in);//move to the first and insert the node
bool List::InsertInOrder2(Node *in);//move to the first and insert the node
bool DelNode(int tar);
bool DelNode(Node *tar);
int List::GetLength();
void List::sort();   //dubble sort
void List::show();
void List::ReverseList();
Node *SearchData(int tar);
*/
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
面向对象程序设计课程作业 1. 请创建一个数据型为T的链表模板List,实现以下成员函数: 1) 默认构造函数List(),将该链表初始化为一个链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编main函数,测试该模板的正确性: 1) 用List模板定义一个List型的模板对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List型的模板对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List型的模板对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List型的模板对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值