線性表類(lèi)——張宏數(shù)據(jù)結(jié)構(gòu)第一課
晚上花了2個(gè)多小時(shí)寫(xiě)的,感覺(jué)不是很難,下次嘗試下寫(xiě)成鏈表+模板的形式 O(∩_∩)O~#include<iostream>
#include<algorithm>
using namespace std;
#define LISTVOLUME 10000
class sqlist
{
private:
int a[10001];
int lenth;
int max;
public:
sqlist()
{
memset(a,0,sizeof(a));
lenth=0;
max=LISTVOLUME;
}
void initial();
void creat();
void print();
void inset(int num,int pos);
void deletenode(int pos);
void sortlist();
int size();
bool empty();
bool full();
void findnode(int num);
};
void sqlist::initial()
{
memset(a,0,sizeof(a));
lenth=0;
max=LISTVOLUME;
}
void sqlist::creat()
{
cout<<"請(qǐng)順序鍵入鏈表中的數(shù)值,用空格隔開(kāi),并以'-1'結(jié)束"<<endl;
int pos=1;
int temp;
while(cin>>temp)
{
if(temp==-1)
break;
if(lenth>=LISTVOLUME)
{
cout<<"抱歉,線性表已滿,無(wú)法輸入數(shù)據(jù),請(qǐng)重新初始化該數(shù)據(jù)表"<<endl;
cout<<"請(qǐng)問(wèn)需要重新初始化嗎?(Y/N)"<<endl;
char temp;
cin>>temp;
if(temp=='Y'||temp=='y')
{
initial();
creat();
break;
}
else
break;
}
a[pos]=temp;
pos++;
lenth++;
}
}
void sqlist::print()
{
int i;
for(i=1;i<=lenth;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
}
void sqlist::inset(int num, int pos)
{
if(lenth>=LISTVOLUME)
{
cout<<"數(shù)據(jù)表已滿,無(wú)法添加數(shù)據(jù)"<<endl;
return;
}
if(pos<1||pos>lenth+1)
{
cout<<"您輸入的位置不合法,請(qǐng)重新輸入(僅需要輸入插入的位置):";
cin>>pos;
}
int i;
for(i=lenth;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=num;
lenth++;
}
void sqlist::deletenode(int pos)
{
if(pos<1||pos>lenth)
{
cout<<"您輸入的位置不合法,請(qǐng)重新輸入";
cin>>pos;
}
int i;
for(i=pos+1;i<=lenth;i++)
{
a[i-1]=a[i];
}
a[lenth]=0;
lenth--;
cout<<"成功刪除"<<pos<<"號(hào)結(jié)點(diǎn)"<<endl;
}
void sqlist::sortlist()
{
int temp;
cout<<"請(qǐng)問(wèn)您需要從小到大排列(鍵入1)還是從大到小排列(鍵入-1)"<<endl;
cin>>temp;
if(temp==1)
sort(a+1,a+1+lenth);
else if(temp==-1)
{
sort(a+1,a+1+lenth);
reverse(a+1,a+1+lenth);
}
cout<<"排序完成"<<endl;
}
int sqlist::size()
{
return lenth;
}
bool sqlist::empty()
{
if(lenth==0)
return true;
else
return false;
}
bool sqlist::full()
{
if(lenth>=max)
return true;
else
return false;
}
void sqlist::findnode (int num)
{
int i;
for(i=1;i<=lenth;i++)
{
if(a[i]==num)
{
cout<<"該元素位于"<<i<<"號(hào)位置"<<endl;
return ;
}
}
cout<<"沒(méi)有搜索到改元素,請(qǐng)重新查找"<<endl;
}
int main ()
{
sqlist test;
int m,n;
cin>>m>>n;
test.creat();
test.print();
test.initial();
test.creat();
test.print();
test.inset(m,n);
test.sortlist ();
test.deletenode(3);
test.findnode(3);
return 0;
}
#include<algorithm>
using namespace std;
#define LISTVOLUME 10000
class sqlist
{
private:
int a[10001];
int lenth;
int max;
public:
sqlist()
{
memset(a,0,sizeof(a));
lenth=0;
max=LISTVOLUME;
}
void initial();
void creat();
void print();
void inset(int num,int pos);
void deletenode(int pos);
void sortlist();
int size();
bool empty();
bool full();
void findnode(int num);
};
void sqlist::initial()
{
memset(a,0,sizeof(a));
lenth=0;
max=LISTVOLUME;
}
void sqlist::creat()
{
cout<<"請(qǐng)順序鍵入鏈表中的數(shù)值,用空格隔開(kāi),并以'-1'結(jié)束"<<endl;
int pos=1;
int temp;
while(cin>>temp)
{
if(temp==-1)
break;
if(lenth>=LISTVOLUME)
{
cout<<"抱歉,線性表已滿,無(wú)法輸入數(shù)據(jù),請(qǐng)重新初始化該數(shù)據(jù)表"<<endl;
cout<<"請(qǐng)問(wèn)需要重新初始化嗎?(Y/N)"<<endl;
char temp;
cin>>temp;
if(temp=='Y'||temp=='y')
{
initial();
creat();
break;
}
else
break;
}
a[pos]=temp;
pos++;
lenth++;
}
}
void sqlist::print()
{
int i;
for(i=1;i<=lenth;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
}
void sqlist::inset(int num, int pos)
{
if(lenth>=LISTVOLUME)
{
cout<<"數(shù)據(jù)表已滿,無(wú)法添加數(shù)據(jù)"<<endl;
return;
}
if(pos<1||pos>lenth+1)
{
cout<<"您輸入的位置不合法,請(qǐng)重新輸入(僅需要輸入插入的位置):";
cin>>pos;
}
int i;
for(i=lenth;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=num;
lenth++;
}
void sqlist::deletenode(int pos)
{
if(pos<1||pos>lenth)
{
cout<<"您輸入的位置不合法,請(qǐng)重新輸入";
cin>>pos;
}
int i;
for(i=pos+1;i<=lenth;i++)
{
a[i-1]=a[i];
}
a[lenth]=0;
lenth--;
cout<<"成功刪除"<<pos<<"號(hào)結(jié)點(diǎn)"<<endl;
}
void sqlist::sortlist()
{
int temp;
cout<<"請(qǐng)問(wèn)您需要從小到大排列(鍵入1)還是從大到小排列(鍵入-1)"<<endl;
cin>>temp;
if(temp==1)
sort(a+1,a+1+lenth);
else if(temp==-1)
{
sort(a+1,a+1+lenth);
reverse(a+1,a+1+lenth);
}
cout<<"排序完成"<<endl;
}
int sqlist::size()
{
return lenth;
}
bool sqlist::empty()
{
if(lenth==0)
return true;
else
return false;
}
bool sqlist::full()
{
if(lenth>=max)
return true;
else
return false;
}
void sqlist::findnode (int num)
{
int i;
for(i=1;i<=lenth;i++)
{
if(a[i]==num)
{
cout<<"該元素位于"<<i<<"號(hào)位置"<<endl;
return ;
}
}
cout<<"沒(méi)有搜索到改元素,請(qǐng)重新查找"<<endl;
}
int main ()
{
sqlist test;
int m,n;
cin>>m>>n;
test.creat();
test.print();
test.initial();
test.creat();
test.print();
test.inset(m,n);
test.sortlist ();
test.deletenode(3);
test.findnode(3);
return 0;
}
posted on 2009-02-19 00:51 abilitytao 閱讀(1001) 評(píng)論(3) 編輯 收藏 引用