📄 linklist.cpp
字号:
#include "stdafx.h"
#include <iostream>
#include <strstream>
#include <sstream>
#include <fstream>
#include <string>
#include "Linklist.h"
#include "Date.h"
#include "Books.h"
LinkList::LinkList( )
{
head=new Books;
head->next=NULL;
}
LinkList::~LinkList( ){}//要求自己写出析构函数的实现部分,释放动态申请的内存空间
void LinkList::AddBooksArray(Books a[],int n)
{
head= new Books;
Books *r,*s;
r=head;
for(int i=0;i<n;i++)
{
s=new Books;
*s=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
bool LinkList::IsEmpty()
{
return 0;
}
int LinkList::GetLength()
{
Books *p=head->next;
int i=0;
while(p)
{
p=p->next;
i++;
}
return i;
}
bool LinkList::AddBooks(Books *b)
{
Books *p,*s;
p=head->next;
while(p!=NULL)
{
if(p->m_ID!=b->m_ID)
{ s=p;
p=p->next;
}
else
{ cout<<"有这本书"<<endl;
return false;
}
}
if(p==NULL)
{ if(head->next==NULL)
{ head->next=b;
b->next=NULL;
}
else
{ s->next=b;
b->next=NULL;
}
}
return true;
}
Books * LinkList::SearchByID(string _id)
{
int i=0;
Books *p=head->next;
while(p!=NULL &&p->m_ID!=_id)
{
p=p->next;
}
if(p==NULL)
{
cout<<"NO Found "<<_id<<"的书籍"<<endl;
return NULL;
}
else
{
return p;
}
}
Books *LinkList::SearchByBookName(string _bookName)
{
int i=0;
Books *p=head->next;
while(p!=NULL &&p->m_BookName!=_bookName)
{
p=p->next;
}
if(p==NULL)
{
cout<<"NO Found "<<_bookName<<"的书籍"<<endl;
return NULL;
}
else
{
return p;
}
}
Books *LinkList:: SearchByAuthor(string _author)
{
int i=0;
Books *p=head->next;
while(p!=NULL &&p->m_Author!=_author)
{
p=p->next;
}
if(p==NULL)
{
cout<<"NO Found "<<_author<<"的书籍"<<endl;
return NULL;
}
else
{
return p;
}
}
bool LinkList::SaveList(char *fname)
{ ofstream fout(fname);
if(fout.fail())
{ cerr<<"error!";
return false;
}
Books * p;
p=head->next;
if(IsEmpty())
{ return false ;}
while(p)
{
fout<<*p;
p=p->next;
}
fout.close();
return true;
}
bool LinkList::ReadList(char *fname)
{ ifstream fin(fname);
if(fin.fail())
{
cerr<<"error"<<endl;
return false;
}
Books *s;
string temp;
while(!fin.eof())
{ s=new Books;
getline(fin,temp);
istringstream istr(temp);
istr>>*s;
AddBooks(s);
}
fin.close();
return true;
}
void LinkList::displayLinkList()
{
Books *p;
p=head;
do
{
cout<<p->next->m_ID<<" "<<p->next->m_BookName<<" "<<p->next->m_Author<<" "<<p->next->m_press<<" "<<p->next->m_publicDate<<" "<<p->next->m_price<<endl;
p=p->next;
}while (p->next!=NULL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -