#include<iostream>
#include<stdio.h>
#include<string>
#define OK 1
#define ERROR 2
using namespace std;
typedef struct LNode{
char data[10];
struct LNode *next;
}LNode,*LinkList;//链表格式
int CreateList_L(LinkList &L,char chinese[10],char english[10],char maths[10],LinkList one){
LinkList two,three;
strcpy(chinese,"语文");
strcpy(english,"英语");
strcpy(maths,"数学");
L=(LinkList)malloc(sizeof(LNode));
one=(LinkList)malloc(sizeof(LNode));
two=(LinkList)malloc(sizeof(LNode));
three=(LinkList)malloc(sizeof(LNode));
L->next=one;
one->next=two;
strcpy(one->data,chinese);
two->next=three;
strcpy(two->data,english);
three->next=NULL;
strcpy(three->data,maths);
return OK;
}//创建一个链表
int ListInsert_L(LinkList &L,int i,char w[]){
cout<<"请输入书名:"<<endl;
cin>>w;
int j;i=2;
LinkList p,s;
p=L;j=0;
while(p&&j<i-1){
p=p->next;++j;
}//找到第i个节点
if(!p||j>i-1)return ERROR;
s=(LinkList)malloc(sizeof(LNode));
strcpy(s->data,w);
s->next=p->next;
p->next=s;
cout<<"OK!"<<endl;
return OK;
}//借书时用来插入链表的
int look_after_L(LinkList &L,char e[10]){
cout<<"请输入书名:";
cin>>e;
LinkList p;
p=L->next;
do{
int t;
t=strcmp((p->data),e);
if(t==0)
{
cout<<"找到书籍!"<<endl;
return OK;
}
else{
if(strcmp(p->data,e)!=0&&p->next==NULL)
cout<<"没找到!"<<endl;
p=p->next;
}
}while(p!=NULL);
return OK;
}//查找函数
int delete_L(LinkList u,LinkList c,LinkList L,LinkList k){
u=(LinkList)malloc(sizeof(LNode));
c=(LinkList)malloc(sizeof(LNode));
k=(LinkList)malloc(sizeof(LNode));
int a,d,f,o,p,h=0;
char y[10];
cout<<"请输入要借的书籍:";
cin>>y;
c=L;
k=L;
u=L;
o=strcmp(y,c->data);
while(((c->next!=NULL)&&(o!=0)||o!=0)){
c=c->next;++h;o=strcmp(y,c->data);
if((c->next==NULL)&&(o!=0)){
break;
}
}
if((c->next==NULL)&&(o!=0)){
cout<<"没有这本书!"<<endl;
}
else{
if(o==0){
a=--h;
for(p=0;p<a;p++){
k=k->next;
}
d=h+2;
for(f=0;f<d;f++){
u=u->next;//cout<<"uu";
}
k->next=u;cout<<"成功借出!"<<endl;
}
}
return OK;
}//删除
int main(){
int n=4;
char chinese[10];char english[10];char maths[10];
LinkList L,one;
char e[10];char w[10];
CreateList_L(L,chinese,english,maths,one);
char step_back;
do{ cout<<endl<<"查询书籍请按1"<<endl<<endl<<"查看现有书籍请按2" <<endl<<endl<<"还书请按3"<<endl<<endl<<"借书请按4"<<endl<<endl<<"按5退出"<<endl;
cin>>step_back;
switch(step_back){
case'1':{
look_after_L(L,e);
break;
}
case'2':{
LinkList r,one;int t;
r=L;
for(t=0;r->next!=NULL&&t<=6;t++){
r=r->next;
cout<<r->data<<" ";
if(r->next==NULL){
cout<<endl;
}
}
break;
}
case'3':{
int i=4;
LinkList q;
ListInsert_L(L,i,w);
q=L;
if(q->next==NULL){
cout<<q->data;
}
else{
while(q->next!=NULL){
q=q->next;
}
if(q->next==NULL){
cout<<"已归还!"<<endl;
}
}
break;
}
case'4':{
LinkList u,c,k;
delete_L(u,c,L,k);
break;
}
case'5':{
exit(0);
break;}
case'6':{
cout<<" 曾逸飞处女作 谢谢各位支持!!!"<<endl;
}
}
}while(1);
system("pause");
return 1;
}
- 1
- 2
前往页