#include "LList.h"
#include <iostream>
#include <stdio.h>
using namespace std;
void Lsave(LList<int> List )
{
FILE *fp;
if((fp=fopen("stu.txt","w"))==NULL)
{
printf("cannot open file\n");
return;
}
fprintf(fp," ");
for(int p=0;p<(List.length);p++)
{
fprintf(fp,"________");
cout<<"_______";
}
fprintf(fp,"\n|");
cout<<"\n"<<"|";
for(int q=0;q<(List.length);q++)
{
fprintf(fp," %d |",List.element[q]);
cout<<" "<<List.element[q]<<" |";
}
fprintf(fp,"\n ");
cout<<endl;
for(int p=0;p<List.length;p++)
{
fprintf(fp,"________");
cout<<"_______";
}
fprintf(fp,"\n");
cout<<endl;
fclose(fp);
}
void save(LList<int> List )
{
FILE *fp;
if((fp=fopen("stu.txt","a+"))==NULL)
{
printf("cannot open file\n");
return;
}
fprintf(fp," ");
for(int p=0;p<(List.length);p++)
{
fprintf(fp,"________");
cout<<"_______";
}
fprintf(fp,"\n|");
cout<<"\n"<<"|";
for(int q=0;q<(List.length);q++)
{
fprintf(fp," %d |",List.element[q]);
cout<<" "<<List.element[q]<<" |";
}
fprintf(fp,"\n ");
cout<<endl;
for(int p=0;p<List.length;p++)
{
fprintf(fp,"________");
cout<<"_______";
}
fprintf(fp,"\n");
cout<<endl;
fclose(fp);
}
void save3(LList<int> List )
{
FILE *fp;
if((fp=fopen("stu.txt","a+"))==NULL)
{
printf("cannot open file\n");
return;
}
fprintf(fp," ");
for(int p=0;p<(List.length);p++)
{
fprintf(fp,"________");
cout<<"_______";
}
fprintf(fp,"\n|");
cout<<"\n"<<"|";
fprintf(fp,"存取的第k个结点为:",List.num);
cout<<" "<<List.num<<" |";
fprintf(fp,"\n ");
cout<<endl;
for(int p=0;p<List.Length();p++)
{
fprintf(fp,"________");
cout<<"_______";
}
fprintf(fp,"\n");
cout<<endl;
fclose(fp);
}
void CreatList(LList<int> & List)
{
int n;
cout<<"先创建一个结点数n的顺序表:请输入n:"<<endl;
cin>>n;
if(n>List.MaxSize || n<=0)
{
cout<<"节点数输入有误!"<<endl;
return ;
}
cout<<"请输入每个节点:"<<endl;
for(int i=0;i<n;i++)
{
int j;
cin>>j;
List.element[i]=j;
}
List.length=n;
}
int main()
{
LList<int> List(64);
CreatList(List);
Lsave(List);
cout<<List.length;
int c;
cout<<"你是否想要改变这个线性表?(是:1;否:0)"<<endl;
cin>>c;
while (c)
{
int j;
int k;
int i_tem;
cout<<"请输入你想要改变的内容:"<<endl;
cout<<"1:在顺序表中第 k 个结点后插入一个新结点item;\n";
cout<<"2:存取顺序表中第 k 个结点的值;\n";
cout<<"3:删除顺序表中第 k 个结点;\n";
cout<<"4:顺序查找值为 x 的元素在顺序表中的位置(下标);\n";
cin>>j;
switch(j)
{
case 1:
cout<<"在顺序表中第 k 个结点后插入一个新结点;\n "<<endl;
cin>>k>>i_tem;
if(List.Insert(k,i_tem)==true)
{
cout<<"输出"<<endl;
save(List);
}
break;
case 2:
cout<<"存取顺序表中第 k 个结点的值;\n"<<endl;
cin>>k;
cout<<"输出"<<endl;
cout<<(List.Find(k,List.num))<<endl;;
save3(List);
break;
case 3:
cout<<"删除顺序表中第 k 个结点;\n"<<endl;
cin>>k;
if(List.Delete(k,i_tem)==true)
{
cout<<"输出"<<endl;
save(List);
}
break;
case 4:
cout<<"顺序查找值为 x 的元素在顺序表中的位置(下标);请输入你要查找的元素:\n"<<endl;
int a;
cin>>i_tem>>k;
a=(List.Search( i_tem, k));
cout<<"输出"<<endl;
save(List);
break;
}
cout<<"你是否想要改变这个线性表?(是:1;否:0)"<<endl;
cin>>c;
}
cout<<"OVER"<<endl;
return 0;
}
#ifndef _LList
#define _LList
#include <iostream>
using namespace std;
template <class T>
class LList
{
public:
T *element;
int MaxSize;
int length;
T num;
public:
LList(int MaxSize=64);
LList( T Value[],int n);
~LList();
bool IsEmpty() {return length==0;};
bool IsFull() {return length==MaxSize;}
int Length() {return length;}
int Find (int k,T item);
int Search ( T item,int n);
bool Delete(int k,T item);
bool Insert(int k,T item);
};
template<class T>
LList<T>::LList(int Size)
{
this->MaxSize=Size<64?64:Size;
this->element=new T[this->MaxSize];
this->length=0;
};
template <class T>
LList<T>::LList(T Value[],int n)
{
if(n>0)
{
this->element=new T[2*n];
this->MaxSize=2*n;
for(int i=0;i<=n;i++)
{
this->element[i]=Value[i];
}
this->length=n;
}
};
template <class T>
LList<T>::~LList()
{
delete[]this->element;
};
template<class T>
int LList<T>:: Find (int k,T item) //存取顺序表中第k个结点的值
{
if (k<0 || k>(this->length))
{
cout<<"存取结点是无效的!"<<endl;
return 0;
}
item=this->element[k-1];
return item;
};
template<class T>
int LList<T>::Search ( T item,int n) //查找元素为item的结点,并将它的下标值返回
{
for(int j=0; j<this->Length() ; j++)
{
if(item==element[j])
{
cout<<"已查找到该元素!"<<endl;
n=j;
return n;
}
if( j==this->Length())
{
throw "参数item是无效的";
}
}
};
template <class T>
bool LList <T>::Delete(int k,T item) //删除第k个结点
{
if(k<=0 || k>length || Length()==0)
{
cout<<"k值不合法!"<<endl;
return false;
}
for(int j=k;j<length;j++)
{
this->element[j-1]=this->element[j];
}
length=length-1;
return true;
};
template<class T>
bool LList <T>::Insert(int k,T item) //第k个节点后插入一个结点
{
if(k<=0 || k>length || length==MaxSize)
{
cout<<"插入不合法!"<<endl;
throw"error";
}
for(int j=(this->length);j>k;j--)
{
element[j]=element[j-1];
cout<<length<<" "<<j<<endl;
}
element[k]=item;
length=length+1;
return true;
};
#endif
#include <iostream>
#include <stdio.h>
using namespace std;
void Lsave(LList<int> List )
{
FILE *fp;
if((fp=fopen("stu.txt","w"))==NULL)
{
printf("cannot open file\n");
return;
}
fprintf(fp," ");
for(int p=0;p<(List.length);p++)
{
fprintf(fp,"________");
cout<<"_______";
}
fprintf(fp,"\n|");
cout<<"\n"<<"|";
for(int q=0;q<(List.length);q++)
{
fprintf(fp," %d |",List.element[q]);
cout<<" "<<List.element[q]<<" |";
}
fprintf(fp,"\n ");
cout<<endl;
for(int p=0;p<List.length;p++)
{
fprintf(fp,"________");
cout<<"_______";
}
fprintf(fp,"\n");
cout<<endl;
fclose(fp);
}
void save(LList<int> List )
{
FILE *fp;
if((fp=fopen("stu.txt","a+"))==NULL)
{
printf("cannot open file\n");
return;
}
fprintf(fp," ");
for(int p=0;p<(List.length);p++)
{
fprintf(fp,"________");
cout<<"_______";
}
fprintf(fp,"\n|");
cout<<"\n"<<"|";
for(int q=0;q<(List.length);q++)
{
fprintf(fp," %d |",List.element[q]);
cout<<" "<<List.element[q]<<" |";
}
fprintf(fp,"\n ");
cout<<endl;
for(int p=0;p<List.length;p++)
{
fprintf(fp,"________");
cout<<"_______";
}
fprintf(fp,"\n");
cout<<endl;
fclose(fp);
}
void save3(LList<int> List )
{
FILE *fp;
if((fp=fopen("stu.txt","a+"))==NULL)
{
printf("cannot open file\n");
return;
}
fprintf(fp," ");
for(int p=0;p<(List.length);p++)
{
fprintf(fp,"________");
cout<<"_______";
}
fprintf(fp,"\n|");
cout<<"\n"<<"|";
fprintf(fp,"存取的第k个结点为:",List.num);
cout<<" "<<List.num<<" |";
fprintf(fp,"\n ");
cout<<endl;
for(int p=0;p<List.Length();p++)
{
fprintf(fp,"________");
cout<<"_______";
}
fprintf(fp,"\n");
cout<<endl;
fclose(fp);
}
void CreatList(LList<int> & List)
{
int n;
cout<<"先创建一个结点数n的顺序表:请输入n:"<<endl;
cin>>n;
if(n>List.MaxSize || n<=0)
{
cout<<"节点数输入有误!"<<endl;
return ;
}
cout<<"请输入每个节点:"<<endl;
for(int i=0;i<n;i++)
{
int j;
cin>>j;
List.element[i]=j;
}
List.length=n;
}
int main()
{
LList<int> List(64);
CreatList(List);
Lsave(List);
cout<<List.length;
int c;
cout<<"你是否想要改变这个线性表?(是:1;否:0)"<<endl;
cin>>c;
while (c)
{
int j;
int k;
int i_tem;
cout<<"请输入你想要改变的内容:"<<endl;
cout<<"1:在顺序表中第 k 个结点后插入一个新结点item;\n";
cout<<"2:存取顺序表中第 k 个结点的值;\n";
cout<<"3:删除顺序表中第 k 个结点;\n";
cout<<"4:顺序查找值为 x 的元素在顺序表中的位置(下标);\n";
cin>>j;
switch(j)
{
case 1:
cout<<"在顺序表中第 k 个结点后插入一个新结点;\n "<<endl;
cin>>k>>i_tem;
if(List.Insert(k,i_tem)==true)
{
cout<<"输出"<<endl;
save(List);
}
break;
case 2:
cout<<"存取顺序表中第 k 个结点的值;\n"<<endl;
cin>>k;
cout<<"输出"<<endl;
cout<<(List.Find(k,List.num))<<endl;;
save3(List);
break;
case 3:
cout<<"删除顺序表中第 k 个结点;\n"<<endl;
cin>>k;
if(List.Delete(k,i_tem)==true)
{
cout<<"输出"<<endl;
save(List);
}
break;
case 4:
cout<<"顺序查找值为 x 的元素在顺序表中的位置(下标);请输入你要查找的元素:\n"<<endl;
int a;
cin>>i_tem>>k;
a=(List.Search( i_tem, k));
cout<<"输出"<<endl;
save(List);
break;
}
cout<<"你是否想要改变这个线性表?(是:1;否:0)"<<endl;
cin>>c;
}
cout<<"OVER"<<endl;
return 0;
}
#ifndef _LList
#define _LList
#include <iostream>
using namespace std;
template <class T>
class LList
{
public:
T *element;
int MaxSize;
int length;
T num;
public:
LList(int MaxSize=64);
LList( T Value[],int n);
~LList();
bool IsEmpty() {return length==0;};
bool IsFull() {return length==MaxSize;}
int Length() {return length;}
int Find (int k,T item);
int Search ( T item,int n);
bool Delete(int k,T item);
bool Insert(int k,T item);
};
template<class T>
LList<T>::LList(int Size)
{
this->MaxSize=Size<64?64:Size;
this->element=new T[this->MaxSize];
this->length=0;
};
template <class T>
LList<T>::LList(T Value[],int n)
{
if(n>0)
{
this->element=new T[2*n];
this->MaxSize=2*n;
for(int i=0;i<=n;i++)
{
this->element[i]=Value[i];
}
this->length=n;
}
};
template <class T>
LList<T>::~LList()
{
delete[]this->element;
};
template<class T>
int LList<T>:: Find (int k,T item) //存取顺序表中第k个结点的值
{
if (k<0 || k>(this->length))
{
cout<<"存取结点是无效的!"<<endl;
return 0;
}
item=this->element[k-1];
return item;
};
template<class T>
int LList<T>::Search ( T item,int n) //查找元素为item的结点,并将它的下标值返回
{
for(int j=0; j<this->Length() ; j++)
{
if(item==element[j])
{
cout<<"已查找到该元素!"<<endl;
n=j;
return n;
}
if( j==this->Length())
{
throw "参数item是无效的";
}
}
};
template <class T>
bool LList <T>::Delete(int k,T item) //删除第k个结点
{
if(k<=0 || k>length || Length()==0)
{
cout<<"k值不合法!"<<endl;
return false;
}
for(int j=k;j<length;j++)
{
this->element[j-1]=this->element[j];
}
length=length-1;
return true;
};
template<class T>
bool LList <T>::Insert(int k,T item) //第k个节点后插入一个结点
{
if(k<=0 || k>length || length==MaxSize)
{
cout<<"插入不合法!"<<endl;
throw"error";
}
for(int j=(this->length);j>k;j--)
{
element[j]=element[j-1];
cout<<length<<" "<<j<<endl;
}
element[k]=item;
length=length+1;
return true;
};
#endif