📄 链表.cpp
字号:
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<iomanip.h>
#include <fstream.h>
//#include <conio.h>
#include <stdlib.h>
#define false 0
#define true 1
//typedef bool;
//////////////////////////////student类
class student
{
friend ostream& operator<<(ostream &,const student&); //建立友元函数,以供访问
private:
char id[10]; //学号
char name[10]; //姓名
int score; //成绩
public:
student();
student(char *i,char *n,int s); //构造函数
input(); //输入函数
set_name(char *n);
set_id(char *i);
print(); //输出函数
operator = (student &s); //重载=
bool operator != (student &s); //重载!=
char *get_id(){return id;} //取学号
char *get_name(){return name;} //取姓名
};
student::student()
{
strcpy(id,""); //初始化
strcpy(name,"");
score=0;
}
student::student(char *i,char *n,int s)
{
strcpy(id,i);
strcpy(name,n);
score=s;
}
student::input() //输入函数
{
cout<<"请输入学号:";
cin>>id;
cout<<"请输入姓名:";
cin>>name;
cout<<"请输入成绩:";
cin>>score;
}
student::set_name(char *n) //姓名字符串拷贝
{
strcpy(name,n);
}
student::set_id(char *i) //学号字符串拷贝
{
strcpy(id,i);
}
student::print() //输出
{
cout<<setw(15)<<"学号"<<setw(15)<<"姓名"<<setw(15)<<"成绩"<<'\n';
// cout<<setw(15)<<id<<setw(15)<<name<<setw(15)<<score<<'\n';
printf("%-15.1s,%-15s,%-15d",id,name,score); //输出格式靠左对齐
}
student::operator = (student &s) //重载=
{
strcpy(id,s.id);
strcpy(name,s.name);
score=s.score;
}
bool student:: operator != (student &s) //重载!=
{
if(strcmp(name,s.name)!=0)
return true;
else
return false;
}
ostream& operator<<(ostream &os,const student &stu) //重载<<
{
os<<setw(15)<<"学号"<<setw(15)<<"姓名"<<setw(15)<<"成绩"<<'\n';
os<<setw(15)<<stu.id<<setw(15)<<stu.name<<setw(15)<<stu.score<<'\n';
return os;
}
/*template<class type>
class linearlist
{
public:
linearlist(){};
~linearlist(){};
virtual bool Isempty()=0;
virtual int length() =0;
virtual bool find(int k,type& x)=0;
virtual int search(const type& x)=0;
virtual bool ineart(int k,const type& x)=0;
virtual bool remove(int k)=0;
virtual bool updata(int k,const type& x)=0;
virtual bool Output(ostream& out)=0;
};
*/
/////////////////////////////////////////////////linklist类定义
template<class type> class linklist;
template<class type>
class node
{
private:
type data;
node<type> *link;
friend class linklist<type>;
};
template<class type>
class linklist
//:public linearlist<type>
{
public:
linklist(){first=NULL;length=0;}
~linklist();
int Isempty() const;
int get_length() const;
int search(type& x);
type get_i(int n);
bool insert(int k, type& x);
bool remove(int k);
void output(void);
private:
node<type> *first;
int length;
};
template<class type>
linklist<type>::~linklist()
{
node<type> *p;
while(first)
{
p=first->link;
delete first;
first=p;
}
}
template<class type>
int linklist<type>::get_length() const
{
return length;
}
template<class type>
int linklist<type>::Isempty()const
{
return length==0;
}
template<class type>
int linklist<type>::search(type& x)
{
node<type> *p=first;
for(int i=1;p!=NULL&&p->data!=x;i++)
p=p->link;
if(p==NULL||p->data!=x)
return 0;
else
return i;
}
template<class type>
type linklist<type>::get_i(int n)
{
node<type> *p=first;
//if(n<0||n>length)
// p=NULL;
//exit(1);
for(int i=1;i<n;i++)
p=p->link;
if(i!=n)
//p=first;
exit(1);
return p->data;
// else
// return first->data;
}
template<class type>
bool linklist<type>::insert(int k,type& x)
{
if(k<0||k>length)
{
cout<<"溢出!!!!!!!!!!!!!!!!"<<endl;
return false;
}
node<type> *p=first;
for(int i=1;i<k;i++)
p=p->link;
node<type> *q=new node<type>;
q->data=x;
if(k)
{
q->link=p->link;
p->link=q;
}
else
{
q->link=first;
first=q;
}
length++;
return true;
}
template<class type>
bool linklist<type>::remove(int k)
{
if(!length)
{
cout<<"没有可删除的内容!!!"<<endl;
return false;
}
if(k<1||k>length)
{
cout<<"溢出!!!!!!!!!!!!!!!!"<<endl;
return false;
}
node<type> *p=first, *q=first;
for(int i=1; i<k-1;i++)
q=q->link;
if(k==1)
first=first->link;
else
{
p=q->link;
q->link=p->link;
}
delete p;
length--;
return true;
}
template<class type>
void linklist<type>::output(void)
{
node<type> *p=first;
while(p)
{
cout<<p->data<<" ";
p=p->link;
}
}
///////////////////////////////////主函数////////////////////////////////
int showmenu();
int main(void)
{
int n,i;
cout<<"请初始化学生个数:"; //初始化
cin>>n;
student *s,s1;
s=new student[n];
linklist<student> stu;
showmenu();
char c;
cin>>c;
while(c!='0')
{
switch(c) //菜单
{
case '1':
for(i=0;i<n;i++)
{
cout<<"请输入第"<<i+1<<"位学生的信息:\n";
s[i].input();
stu.insert(i,s[i]);
}
break;
case '2':
stu.output();
break;
case '3':
cout<<"请输入插入学生信息:\n";
s1.input();
cout<<"请输入想要插入的位置:";
cin>>i;
if(stu.insert(i-1,s1))
cout<<"插入成功!!!\n";
else
cout<<"插入失败!!!\n";
break;
case '4':
char n[10];
cout<<"请输入要查找的姓名:";
cin>>n;
s1.set_name(n);
i=stu.search(s1);
cout<<"i="<<i<<endl;
// cout<<"链表长度:"<<stu.get_length()<<endl;
if(i<=0||i>stu.get_length())
cout<<"找不到!!!\n";
else
{
cout<<"i="<<i<<endl;
cout<<stu.get_i(i);
}
break;
case '5':
cout<<"请输入要删除的姓名:";
cin>>n;
s1.set_name(n);
i=stu.search(s1);
// cout<<"i="<<i<<endl;
// if(i)
// {
// cout<<"i="<<i<<endl;
if(i<=0||i>stu.get_length())
cout<<"找不到!!!\n";
else if(i&&stu.remove(i))
cout<<"删除成功!!!";
break;
case '0':break;
}
showmenu();
cin>>c;
}
return 0;
}
int showmenu(void)
{
cout<<setw(10)<<"*==学=生=信=息=管=理=系=统==*"<<endl;
cout<<"1---------->输入学生信息"<<endl;
cout<<"2---------->显示学生信息"<<endl;
cout<<"3---------->插入学生信息"<<endl;
cout<<"4---------->查找学生信息"<<endl;
cout<<"5---------->删除学生信息"<<endl;
cout<<"0---------->退出"<<endl;
cout<<"请选择(0~5):"<<endl;
return false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -