#include <iostream>
#include <cmath>
#include<algorithm>
#include<cstring>
using namespace std;
struct node{
int data;
node *next;
};
class sqlist
{
private:
node *head;
int lenth;
public:
sqlist()
{
cout<<"您申请了一个sqlist型对象,所占的字节数为"<<sizeof(sqlist)<<",感谢您的使用。"<<endl;
cout<<"在测试过程中如有问题,请及时向我反馈,我的联系方式是QQ:64076241.O(∩_∩)O~"<<endl<<endl;
lenth=0;
head=NULL;
}
void initial();
void creat();
node* findpos(int pos);
int findnum(int num);
void insert(int num,int pos);
void deletenode(int pos);
int len();
bool empty();
void reverse();
void print();
};
void sqlist::initial()
{
int i=1;
if(lenth==0)
{
cout<<"表为空,无需初始化"<<endl;
return ;
}
int size=lenth;
for(i=1;i<=size;i++)
{
deletenode(1);
}
cout<<"初始化完毕"<<endl;
}
void sqlist::creat()
{
int temp;
node *p,*r;
head=r=NULL;
cout<<"请顺序输入链表的值,并以空格隔开,以-1做为输入结束"<<endl;
cin>>temp;
while(temp!=-1)
{
lenth++;
p=new node;
p->data=temp;
if(head==NULL)
head=p;
else
r->next=p;
r=p;
cin>>temp;
}
if(r!=NULL)
r->next=NULL;
}
node* sqlist::findpos(int pos)//返回值为NULL代表没有找到;
{
node *p;
int i=1;
p=head;
if(head==NULL)
{
cout<<"表为空"<<endl;
return NULL;
}
else if(pos>lenth||pos<1)
{
cout<<"您的输入超出链表的区间范围"<<endl;
return NULL;
}
while(i<pos)
{
p=p->next;
i++;
}
return p;
}
int sqlist::findnum(int num)//返回值为零代表没有找到或者输入错误;
{
node *p=head;
int i=1;
if(head==NULL)
{
cout<<"链表为空,操作错误"<<endl;
return 0;
}
while(i<=lenth)
{
if(p->data==num)
return i;
else
p=p->next;
i++;
}
if(i==lenth+1)
{
cout<<"o(╯□╰)o,没有查找到该元素"<<endl;
return 0;
}
}
void sqlist::insert(int num,int pos)
{
node *p,*q;
int i;
if(lenth==0)
{
cout<<"链表为空,请先建立链表"<<endl;
return;
}
if(pos>lenth+1||pos<1)
{
cout<<"抱歉,此位置无效,您的输入范围应是1-"<<lenth+1<<"."<<endl;
return;
}
if(pos==1)
{
q=new node;
q->data=num;
q->next=head;
head=q;
cout<<"成功的在"<<pos<<"位置处插入元素"<<num<<",插入成功"<<endl;
lenth++;
return ;
}
p=findpos(pos-1);
q=new node;
q->data=num;
q->next=p->next;
p->next=q;
lenth++;
cout<<"成功的在"<<pos<<"位置处插入元素"<<num<<",插入成功"<<endl;
}
void sqlist::deletenode(int pos)
{
node *p,*r;
if(pos==1)
{
p=head;
head=head->next;
delete p;
lenth--;
return ;
}
p=findpos(pos-1);
if(p==NULL)
{
cout<<"删除结点时出现了查找错误,O(∩_∩)O~,pos越界了吧"<<endl;
return ;
}
r=p->next;
p->next=r->next;
lenth--;
delete r;
}
int sqlist::len()
{
cout<<"链表的长度为"<<lenth<<endl;
return lenth;
}
bool sqlist::empty()
{
if(lenth==0)
{
cout<<"容器为空"<<endl;
return true;
}
else
{
cout<<"容器非空"<<endl;
return false;
}
}
void sqlist::print()
{
node *p;
int i;
p=head;
i=1;
if(lenth==0)
{
cout<<"表为空,请先建表"<<endl;
return ;
}
while(i<=lenth)
{
cout<<p->data<<' ';
p=p->next;
i++;
}
cout<<"链表打印完毕"<<endl;
}
void sqlist::reverse()
{
node *p;
node *q;
int i=1;
node *r;
if(lenth==0)
{
cout<<"链表为空,请先建表"<<endl;
return;
}
else if(lenth==1)
{
cout<<"链表长度为一,不需逆转"<<endl;
return ;
}
else
{
p=head;
q=p->next;
r=q->next;
while(i<lenth)
{
if(p==head)
p->next=NULL;
q->next=p;
p=q;
q=r;
if(r!=NULL)
r=r->next;
i++;
}
head=p;
}
}
int main ()
{
int i;
node *temp;
cout<<" 欢迎使用由sqlist线性链表类"<<endl;
cout<<" ——creator abilitytao ,received the guidance by Mr Zhang Hong "<<endl<<endl;
sqlist test;
cout<<"接下来将进行十分BT的数据测试."<<endl<<endl;
cout<<"首先验证建表函数和初始化函数的正确性,循环两次,第一次请输入1 2 3 4 5 -1,第二次直接输入-1"<<endl;
for(i=1;i<=2;i++)
{
test.creat();
test.print();
test.initial();
test.print();
}
test.initial();
cout<<endl;
system("pause");
system("cls");
cout<<"接下来验证findpos函数的正确性,此处我们输入1 2 3 4 -1,并搜索pos=-1,0,1,2,3,4,5处的值"<<endl;
test.creat();
for(i=-1;i<=5;i++)
{
temp=test.findpos(i);
if(temp==NULL)
cout<<"查找错误"<<endl;
else
cout<<temp->data<<endl;
}
cout<<endl;
test.initial();
system("pause");
system("cls");
cout<<"接下来将测试findnum函数,输入数据为1 2 3 4 5 -1 ,我们查找4和100"<<endl;
test.creat();
cout<<"4位于"<<test.findnum(4)<<"号位置"<<endl;
test.findnum(100);
test.initial();
cout<<endl;
system("pause");
system("cls");
cout<<"接下来测试insert函数"<<endl;
cout<<"我们输入1 2 3 4 5 6 -1,先在4前面加上77,然后在最前面添上0,最后面添上7"<<endl;
test.creat();
test.insert(77,4);
test.print();
test.insert(0,1);
test.print();
cout<<"注意此时容器中已经有九个数了"<<endl;
test.insert(7,9);
test.print();
test.initial();
cout<<endl;
system("pause");
system("cls");
cout<<"再让我们来测试deletenode这个函数"<<endl;
cout<<"我们输入1 2 3 4 5 6 -1,然后从前向后依次删除<<endl";
test.creat();
test.print();
int test_len=test.len();
for(i=1;i<=test_len;i++)
{
test.deletenode(1);
test.print();
}
test.initial();
cout<<endl;
system("pause");
system("cls");
cout<<"最后让我们来测试一下reverse(逆转函数)"<<endl;
cout<<"我们将分别输入-1,1 -1,1 2 -1 以及9 8 7 6 5 4 3 2 1 -1进行测试"<<endl;
for(i=1;i<=4;i++)
{
test.creat();
test.print();
test.reverse();
test.print();
test.initial();
}
cout<<endl;
system("pause");
system("cls");
cout<<"经过以上测试,可以认为这个类是基本正确的,如果您在使用过程中遇到问题可以及时与我取得联系,谢谢您的使用,再见!"<<endl;
system("pause");
return 0;
}