#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;
}