链表(随便写的练手的)

//lianbiao.h

#include<iostream>

#ifndef  WYZ_H_H
#define  WYZ_H_H
const int NSIZE=50;
const int XSIZE=30;
const int FSIZE=6;//6门课成绩

class Bkt    //为下面的结构提供数据结点
{
private:
 int nume;
 int numt;
 char *c_name;
 char *n_xh;
 float *f_chj;

public:
   Bkt();
 Bkt(const char*n,const char *x,const float *y);
   Bkt(const Bkt &b_fz);
   Bkt &operator=(const Bkt &b_fuzh);
   ~Bkt();
   friend std::ostream & operator<<(std::ostream &os,Bkt &kkgg);
 char* fun_fhsj();
 
};


class Qnt
{
private:
 struct Note
 {
  Bkt n_suz;
  Note *next;
 };
 Note *h;
 Note *r;
 static int s_n_jl;   //记录所有创建链表结点的个数
 int n_dg;
public:
   Qnt();
    void wyz(const Bkt &n_x);   // 循环创建结点
 bool wyz(const char *n_chzh)const;//查找

    void wyz(const char* n_chzh,const Bkt &n_chru);//为插入结点创键结构
 friend std::ostream &operator<<(std::ostream &os, Qnt qnt);

    void n_zong()const;    //所有链表中结点的个数
 void n_fundg()const;   //单个链表中结点的个数

 
 ~Qnt();
};

#endif

//..........................................................................................................................................................

//lianbiao.cpp
#include<iostream>
#include<cstdio>
#include<cstring>
#include"lianbiao.h"
int Qnt::s_n_jl=0;     //初始化

Bkt::Bkt()
{
 nume=1;
 numt=1;
 c_name=0;
 n_xh=0;
 f_chj=0;
 
}
Bkt::Bkt(const char*n,const char *x,const float *y)
{
  nume=strlen(n);
 c_name=new char[nume+1];
 std::strcpy(c_name,n);
 numt=strlen(x);
 n_xh=new char[numt+1];
 std::strcpy(n_xh,x);
 f_chj= new float[FSIZE];
 for(int i=0;i<FSIZE;++i)   //6门课和成绩
         f_chj[i]=y[i];

 
}
Bkt::~Bkt()
   {
    delete []c_name;
    delete []n_xh;
    delete []f_chj;
   }
std::ostream &operator<<(std::ostream &os,Bkt &kkgg)
{
 os<<kkgg.c_name<<'/t'<<kkgg.n_xh<<'/t';
 for(int i=0;i<FSIZE;++i)
  os<<kkgg.f_chj[i]<<" ";
 return os;
}
char * Bkt::fun_fhsj()
 {
  return c_name;
 }

Bkt::Bkt(const Bkt & b_fz)  //复制构造函数
   {

    nume=b_fz.nume;
    numt=b_fz.numt;
    c_name=new char[nume+1];
     if(c_name==NULL)
     {
      std::cout<<"审请空间失败";exit(EXIT_FAILURE);
     }
 std::strcpy(c_name,b_fz.c_name);
        n_xh=new char[numt+1];
     if(n_xh==NULL)
     {
      std::cout<<"审请空间失败";exit(EXIT_FAILURE);
     }
 std::strcpy(n_xh,b_fz.n_xh);
     f_chj=new float[FSIZE];
   if(f_chj==NULL)
     {
      std::cout<<"审请空间失败";exit(EXIT_FAILURE);
     }
    for(int i=0;i<FSIZE;++i)
     f_chj[i]=b_fz.f_chj[i];
   }
Bkt &Bkt::operator=(const Bkt &b_fuzh)//重载赋值操作符
{
 if(this==&b_fuzh)
  return *this;
 delete []c_name;
 delete []n_xh;
    delete []f_chj;
 nume=b_fuzh.nume;
 numt=b_fuzh.numt;
 c_name=new char[nume+1];
     if(c_name==NULL)
     {
      std::cout<<"审请空间失败";exit(EXIT_FAILURE);
     }
 std::strcpy(c_name,b_fuzh.c_name);
    n_xh=new char[numt+1];
       if(n_xh==NULL)
    {
    std::cout<<"审请空间失败";exit(EXIT_FAILURE);
    }
 std::strcpy(n_xh,b_fuzh.n_xh);
    f_chj=new float[FSIZE];
       if(f_chj==NULL)
     {
      std::cout<<"审请空间失败";exit(EXIT_FAILURE);
     }
 for(int i=0;i<FSIZE;++i)

  f_chj[i]=b_fuzh.f_chj[i];
  
 return *this;
}
Qnt::Qnt()
{
 h=NULL;
 r=NULL;
 n_dg=0;
}
Qnt::~Qnt()
{
 Note *temp=NULL;
 while(h!=NULL)
 {
  h=h->next;
  delete temp;
 }
}


void Qnt::n_zong()const
 {

 std::cout<<"所有链表中总共有:"<<s_n_jl<<"个结点"<<std::endl;
 }
void Qnt::n_fundg()const
{
 std::cout<<"些链表中共有:"<<n_dg<<"个结点"<<std::endl;
}

 

void Qnt::wyz(const Bkt &n_x)  //创建连续性链表
{
 Note *add=new Note;
 if(add==NULL)
 {
  std::cout<<"申请内存失败";
  exit(0);
 }
 add->n_suz=n_x;
 add->next=NULL;
    s_n_jl++;
 n_dg++;
 if(h==NULL)  h=add;
    else r->next=add;
 r=add;

}
bool Qnt::wyz(const char *n_chzh)const   //查找
{
 Note *p=h;
  if(p==NULL)
  {
   std::cout<<"链表为空";
      exit(EXIT_FAILURE);
  }
  while(p!=NULL)
 {
  if(!std::strcmp(p->n_suz.fun_fhsj(),n_chzh) )  return true;
  else    p=p->next;
 }
  return false;
}

void Qnt::wyz(const char *n_chzh,const Bkt &n_chru)  //为插入结点创键结构
{
  Note *p=h;
  if(p==NULL)
  {
   std::cout<<"链表为空";
      exit(EXIT_FAILURE);
  }

 Note *add=new Note;
 if(add==NULL)
 {
  std::cout<<"申请内存失败";
  exit(0);
 }
 add->n_suz=n_chru;
 add->next=NULL;
    while(p!=NULL)
 {
  if(!std::strcmp(p->n_suz.fun_fhsj(),n_chzh) )
 {
  add->next=p->next;
  p->next=add;
     break;
 }
         else p=p->next;
  }
    s_n_jl++;
     n_dg++;

}
std::ostream &operator<<(std::ostream &os, Qnt qnt) //输出链表中的数据 ///我没有定义这个类的复制构造涵数
{                                          //为什么 这里的qnt中的的h指向的是lb中的h那为什么没有移动h的指向呢


if(qnt.h==NULL)
{
os<<"链表为空";
    exit(0);
}
while(qnt.h!=NULL)
{
os<<qnt.h->n_suz<<std::endl;
qnt.h=qnt.h->next;
}
os<<std::endl;
return os;
}

/*Note *p=qnt.h; //上面的那点我本想这样写的但不知为什么,有问题最后就像上面那样写了。
if(p==NULL)
{
os<<"链表为空";
    exit(0);
}
while(p!=NULL)
{
os<<p->n_suz<<std::endl;
p=p->next;
}
os<<std::endl;
return os;
*/

//.......................................................................................................................................................

//链表.cpp
#include<iostream>
#include<cstdio>
#include"lianbiao.h"

int main()
{
 Qnt lb,lt;
 char c_n[NSIZE];
    char l_x[XSIZE];
 float f_y[FSIZE];
 std::cout.width(35);
 std::cout.fill('*');
 std::cout<<"学生管理系统";
 std::cout.width(24);
 std::cout<<' '<<std::endl<<std::endl;
 std::cout.width(59);
  std::cout<<' '<<std::endl;;
 std::cout.width(14);
 std::cout<<"姓名";
 std::cout.width(14);
 std::cout<<"学号";
 std::cout.width(27);
 std::cout<<"5门课和成绩和总成绩";
 std::cout.width(4);
 std::cout<<' '<<std::endl<<"依照下面的格式输入:"<<std::endl<<"yi mao"
  <<std::endl<<"1010101010"<<std::endl<<"100 100 100 100 100 500"<<std::endl;
 std::cout<<"按下ctrl+z结束(必须输入一个学生的完整成绩)"<<std::endl<<"姓名:";
 std::cin.getline(c_n,NSIZE);
 
 std::cout<<"学号:";
 std::cin.getline(l_x,XSIZE);
 std::cout<<"成绩:";
 for(int x=0;x<FSIZE;++x)
 {
  std::cin>>f_y[x];
 }
 if(!std::cin) std::cout<<"输入错误";
 Bkt b_shju(c_n,l_x,f_y);
 while(std::cin)
 { 
  
   lb.wyz(b_shju);
   std::cin.getline(c_n,NSIZE);
   std::cin.getline(l_x,XSIZE);
         for(int x=0;x<FSIZE;++x)
           std::cin>>f_y[x];
  b_shju=Bkt(c_n,l_x,f_y);
   
 }
 std::cout.setf(std::ios_base::fixed,std::ios_base::floatfield);
 std::cout.setf(std::ios_base::showpoint);
 std::cout.precision(2);
 std::cout<<lb;
 std::cin.clear();

    char l_chruq[NSIZE];
 std::cout<<"输入要插入位置的前一个姓名:";
 std::cin.getline(l_chruq,NSIZE);
 if(lb.wyz(l_chruq))                                                  //查找
 {
 
 std::cout<<"输入要插入的数:";
 std::cout<<"姓名"<<'/t'<<"学号"<<'/t'<<"5门课和成绩和总成绩"<<std::endl;
 std::cin.getline(c_n,NSIZE);
 std::cin.getline(l_x,XSIZE);
 for(int x=0;x<FSIZE;++x)
 {
  std::cin>>f_y[x];
 }
    b_shju=Bkt(c_n,l_x,f_y);
 lb.n_fundg();
 lb.n_zong();
 lb.wyz(l_chruq,b_shju);
 std::cout<<lb;
 }
 else std::cout<<"要插入的位置不存在";
  

 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值