1 #include<iostream> 2 #include<string> 3 #include<iomanip> 4 #include<fstream> 5 using namespace std; 6 7 #define OK 1 8 #define ERROR -1 9 #define OVERFLOW -2 10 typedef int Status; //函数返回值类型,函数状态代码 11 #define MAXSIZE 100 //线性表可能达到的最大长度 12 int length=0; 13 string head_1,head_2,head_3; 14 typedef struct{ 15 string id; //图书编号 16 string name; //图书名称 17 double price; //图书价格 18 }Book; 19 typedef struct LNode{ 20 Book data; //数据域 21 struct LNode *next; //指针域 22 }LNode,*LinkList; 23 Status InitList_L(LinkList &L){ 24 L=new LNode; //开辟以 LNode 大小的内存空间 25 L->next=NULL; 26 return OK; 27 } 28 Status CreateList_L(LinkList &L){ 29 LinkList p,r; 30 r=L; 31 //string head_1,head_2,head_3; 32 fstream file; 33 file.open("d:\book.txt"); 34 if(!file){ 35 cout<<"没有找到该文件,文件打开失败!"<<endl; 36 exit(ERROR); 37 } 38 file>>head_1>>head_2>>head_3; 39 while(!file.eof()){ 40 p=new LNode; 41 file>>p->data.id>>p->data.name>>p->data.price; 42 p->next=NULL; 43 r->next=p; 44 r=p; 45 length++; 46 } 47 file.close(); 48 return OK; 49 } 50 Status GetElem(LinkList &L,int i){ 51 LinkList p; 52 Book data; 53 int j; 54 p=L->next; 55 j=1; 56 while(p&&j<i){ 57 p=p->next; 58 j++; 59 } 60 if(!p||j>i) 61 return ERROR; 62 cout<<p->data.id<<" "<<p->data.name<<" "<<p->data.price<<endl; 63 return OK; 64 } 65 Status ListInsert_L(LinkList &L,int i){ 66 int j; 67 LinkList p,s; 68 p=L; //申明一个节点p,指向头节点 69 j=1; 70 while(p&&j<i){ //寻找第i个节点 71 p=p->next; 72 j++; 73 } 74 if(!p||j>i) //第 i 个元素不存在 75 return ERROR; 76 s=new LNode; //在内存中生成新的节点 77 cin>>s->data.id>>s->data.name>>s->data.price; 78 s->next=p->next; //将p的后继赋值给s的后继 79 p->next=s; //将s的后继赋值给p 80 return OK; 81 } 82 Status ListDelete_L(LinkList &L,int i){//删除单链表的第 i 个数据元素,表长减一 83 int j; 84 LinkList p,r; 85 p=L; //申明一个节点p,指向第一个节点 86 j=1; 87 while(p&&j<i){ //寻找将要删除的第 i 个数据 88 p=p->next; 89 j++; 90 } 91 if(!p||j>i) //链表中第 i 个数据不存在 92 return ERROR; 93 r=p->next; 94 p->next=r->next; //将r的后继赋值给p的后继 95 return OK; 96 } 97 int main() 98 { 99 int choose,i; 100 LinkList L,p; //定义头结点 101 cout<<"1.建立"<<endl; 102 cout<<"2.输入"<<endl; 103 cout<<"3.查找"<<endl; 104 cout<<"4.插入"<<endl; 105 cout<<"5.删除"<<endl; 106 cout<<"6.输出"<<endl; 107 cout<<"0.退出"<<endl<<endl; 108 choose=-1; 109 while(choose!=0){ 110 cout<<"请选择:"<<endl; 111 cin>>choose; 112 switch(choose){ 113 case 0: //退出 114 cout<<"您已经成功退出系统,欢迎您的到来!"<<endl; 115 break; 116 case 1: //建立 117 if(InitList_L(L)) 118 cout<<"线性链式表已成功建立!"<<endl; 119 else 120 cout<<"线性链式表建立失败!"<<endl; 121 break; 122 case 2: //输入 123 if(CreateList_L(L)) 124 cout<<"图书信息内容已经成功输入!"<<endl; 125 else 126 cout<<"图书信息内容输入失败"<<endl; 127 break; 128 case 3: //查找 129 int i; 130 cout<<"请您输入将要查找的第几本书"<<endl; 131 cin>>i; 132 cout<<"您要查找的第"<<i<<"本书的信息如下:"<<endl<<endl; 133 GetElem(L,i); 134 cout<<"----------------------------------------------------------------------"<<endl; 135 break; 136 case 4: //插入 137 cout<<"请您输入在第几行插入新的图书:"<<endl; 138 cin>>i; 139 cout<<"请您为将要插入新的图书依次输入 图书编号、图书名称、图书价格:"<<endl; 140 ListInsert_L(L,i); 141 cout<<"您插入新的图书信息,如下所示:"<<endl<<endl; 142 cout<<left<<head_1<<left<<" "<<head_2<<left<<" "<<head_3<<endl; 143 p=L->next; 144 while(p){ 145 cout<<left<<setw(15)<<p->data.id<<" "<<left<<setw(35)<<p->data.name<<p->data.price<<endl; 146 p=p->next; 147 } 148 cout<<"----------------------------------------------------------------------"<<endl; 149 break; 150 case 5: //删除 151 cout<<"请您输入将要删除的第几个元素:"<<endl; 152 cin>>i; 153 ListDelete_L(L,i); 154 cout<<"您已经成功删除第"<<i<<"个数据"<<endl; 155 cout<<"新的图书信息如下所示:"<<endl<<endl; 156 cout<<left<<head_1<<left<<" "<<head_2<<left<<" "<<head_3<<endl; 157 p=L->next; 158 while(p){ 159 cout<<left<<setw(15)<<p->data.id<<" "<<left<<setw(35)<<p->data.name<<p->data.price<<endl; 160 p=p->next; 161 } 162 cout<<"----------------------------------------------------------------------"<<endl; 163 break; 164 case 6: //输出 165 LinkList p; 166 cout<<"图书信息如下:"<<endl<<endl; 167 cout<<left<<head_1<<left<<" "<<head_2<<left<<" "<<head_3<<endl; 168 p=L->next; 169 while(p){ 170 cout<<left<<setw(15)<<p->data.id<<" "<<left<<setw(35)<<p->data.name<<p->data.price<<endl; 171 p=p->next; 172 } 173 cout<<"----------------------------------------------------------------------"<<endl; 174 break; 175 } 176 } 177 return 0; 178 }