圖書管理(單鏈表C++)


  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<<"\t\t"<<p->data.name<<"\t\t"<<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<<"\t\t\t\t"<<head_2<<left<<"\t\t\t"<<head_3<<endl;
143                 p=L->next;
144                 while(p){
145                     cout<<left<<setw(15)<<p->data.id<<"\t\t"<<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<<"\t\t\t\t"<<head_2<<left<<"\t\t\t"<<head_3<<endl;
157                 p=L->next;
158                 while(p){
159                     cout<<left<<setw(15)<<p->data.id<<"\t\t"<<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<<"\t\t\t\t"<<head_2<<left<<"\t\t\t"<<head_3<<endl;
168                 p=L->next;
169                 while(p){
170                     cout<<left<<setw(15)<<p->data.id<<"\t\t"<<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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM