#include<string>
#include<iostream>
using namespace std;
static int N;
struct Medicine //定義鏈表
{
int ID; //ID號
string name; //藥品名稱
string category; //藥品類別
int num;//藥品數量
/*
string ids; //藥品批準文號
string company; //生產企業
int month; //有效期
int date_year; //年
int date_month; //月
int date_day; //日
*/
Medicine *pNext;
};
Medicine* create(int n)
{
int ID; //ID號
string name; //藥品名稱
string category; //藥品類別
int num;//藥品數量
/*
string ids; //藥品批準文號
string company; //生產企業
int month; //有效期
int date_year; //年
int date_month; //月
int date_day;
*/
Medicine *h,*p,*s;
if ((h=new Medicine)==NULL)
{
cout<<"分配內存失敗"<<endl;
exit(1);
}
h->name=" ";
h->ID = 0;
h->pNext=NULL;
p=h;
for (int i=0;i!=n;++i)
{
if((s=new Medicine)==NULL)
{
cout<<"分配內存失敗"<<endl;
exit(1);
}
p->pNext=s;
cout<<"請輸入第"<<i+1<<"個藥品的ID:";
cin>>ID;
s->ID = ID;
cout<<"請輸入第"<<i+1<<"個藥品的名稱:";
cin>>name;
s->name = name;
cout<<"請輸入第"<<i+1<<"個藥品的類別:";
cin>>category;
s->category = category;
cout<<"請輸入第"<<i+1<<"個藥品的數量:";
cin>>num;
s->num = num;
s->pNext=NULL;
p=s;
}
//p->pNext=NULL;
return h;
}
//查詢結點
Medicine* search(const Medicine *h, string name, int N)
{
int temp = N;
Medicine *p=h->pNext;
for (int i=0;i!=temp;++i)
{
if (name.compare(p->name)==0)
{
return p;
}
p=p->pNext;
}
return NULL;
}
//後插入
//extern N;
void Insert(Medicine *p)
{
Medicine *s;
if ((s=new Medicine)==NULL)
{
cout<<"分配內存失敗"<<endl;
exit(1);
}
cout<<"請輸入妳要插入的藥品名稱:";
cin>>s->name;
s->pNext=p->pNext;
p->pNext=s;
++N;
}
//查詢前向結點
Medicine* searchPre(Medicine *head, string name)
{
Medicine *p=head;
Medicine *q=p->pNext;
for (int i=0;i!=N;++i)
{
if (q!=NULL)
{
if (name.compare(q->name)==0)
return p;
}
else
return NULL;
p = q;
q = q->pNext;
}
return NULL;
}
//刪除結點
void Delete(Medicine *p, Medicine *q) //p為刪除的結點指針,q為前壹個結點指針
{
Medicine *temp;
temp=p->pNext;
delete p;
q->pNext=temp;
--N;
};
//釋放內存空間
void display(Medicine *h)
{
/*
Medicine *p;
for (int i=0;i!=N;++i)
{
p = h;
while(p->pNext)
{
p=p->pNext;
}
if(p != h)
{
cout<<"[DEL:"<<p->name<<"]"<<endl;
delete p;
}
}
*/
Medicine *p=h->pNext;
Medicine *pTemp;
while(NULL!=p->pNext)
{
pTemp = p->pNext;
cout<<"[刪除:"<<p->name<<"]"<<endl;
delete(p);
p->pNext=NULL;
p=pTemp;
}
cout<<"[刪除:"<<pTemp->name<<"]"<<endl;
delete(pTemp);
/*
cout<<"[DEL:"<<h->name<<"]"<<endl;
delete(h);
h=NULL;
*/
}
void main()
{
Medicine *head,*p=0,*q=0;
//char str[20];
string str;
int flag=1;
int sum;
int k;
cout<<"總***藥品數量:";
cin>>N;
head=create(N);
cout<<"\r\n";
//display(head);
cout<<"所要查詢的藥品名是: ";
cin>>str;
//查詢當前結點
if(p=search(head,str,N))
{
cout<<"該藥品的ID為"<<p->ID;
}
else
{
cout<<"該藥品不存在";
}
cout<<"\r\n";
// Insert(p);
while(flag)
{
sum=0;
p=head->pNext;
cout<<"開始統計藥品,請輸入藥品類別"<<"\r\n";
cin>>str;
for(k=0;k!=N;++k)
{
if (str.compare(p->category)==0)
{
sum=sum+p->num;
}
p=p->pNext;
}
cout<<"屬於"<<str<<"的藥品有"<<sum<<"個庫存量"<<"\r\n";
cout<<"是否統計完畢?(1繼續統計/0下壹個項目)";
cin>>flag;
}
//查詢前驅結點並刪除當前結點.
cout<<"所要刪除的藥品名是: ";
cin>>str;
q = head->pNext;
if(str.compare(q->name)==0)
{
delete q;
}
else
{
if(q=searchPre(head,str))
{
p=search(head,str,N);
Delete(p,q);
}
else
{
cout<<"該藥品不存在";
}
}
cout<<"\r\n"<<"該藥品已經刪除完畢"<<"\r\n";
cout<<"程序將要推出,現在演示刪除全部鏈表"<<"\r\n";
display(head);
}
我只是做了藥品類的壹部分變量,其余的相似,妳自己添加吧,程序調試通過。