Book Management (single-chain table C ++) and book management single-chain table c

Source: Internet
Author: User

Book Management (single-chain table C ++) and book management single-chain table 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; // function return value type, function Status Code 11 # define MAXSIZE 100 // maximum length possible for a linear table 12 int length = 0; 13 string head_1, head_2, head_3; 14 typedef struct {15 string id; // Book no. 16 string name; // Book name 17 double price; // Book price 18} Book; 19 typedef struc T LNode {20 Book data; // data domain 21 struct LNode * next; // pointer domain 22} LNode, * LinkList; 23 Status InitList_L (LinkList & L) {24 L = new LNode; // open up memory space in LNode size 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 <"the file is not found. An error occurred while opening the file! "<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" <p-> data. name <"\ 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; // declare a node p pointing to the header node 69 j = 1; 70 while (p & j <I) {// find the I-th node 71 p = p-> next; 72 j ++; 73} 74 if (! P | j> I) // the I-th element does not exist. 75 return ERROR; 76 s = new LNode; // generate a new node in the memory 77 cin> s-> data. id> s-> data. name> s-> data. price; 78 s-> next = p-> next; // assign the successor of p to 79 p-> next = s; // assign the value of s to p 80 return OK; 81} 82 Status ListDelete_L (LinkList & L, int I) {// Delete the I data element of a single-chain table, the table length is reduced by 83 int j; 84 LinkList p, r; 85 p = L; // declares a node p, pointing to the first node 86 j = 1; 87 while (p & j <I) {// search for the I data to be deleted 88 p = p-> next; 89 j ++; 90} 91 if (! P | j> I) // 92 return ERROR; 93 r = p-> next; 94 p-> next = r-> next; // assign the successor value of r to the successor 95 return OK of p; 96} 97 int main () 98 {99 int choose, I; 100 LinkList L, p; // define the header node 101 cout <"1. create "<endl; 102 cout <" 2. enter "<endl; 103 cout <" 3. search for "<endl; 104 cout <" 4. insert "<endl; 105 cout <" 5. delete "<endl; 106 cout <" 6. output "<endl; 107 cout <" 0. exit "<endl; 108 choose =-1; 109 while (choose! = 0) {110 cout <"select:" <endl; 111 cin> choose; 112 switch (choose) {113 case 0: // exit 114 cout <"you have successfully exited the system. Thank you! "<Endl; 115 break; 116 case 1: // create 117 if (InitList_L (L) 118 cout <" the linear chain table has been created successfully! "<Endl; 119 else120 cout <" An error occurred while creating the linear chain table! "<Endl; 121 break; 122 case 2: // enter 123 if (CreateList_L (L) 124 cout <" the book information is successfully entered! "<Endl; 125 else126 cout <" "<endl; 127 break; 128 case 3: // query 129 int I; 130 cout <"Enter the book to be searched" <endl; 131 cin> I; 132 cout <"the information in the <I <" book you are looking for "<endl; 133 GetElem (L, I ); 134 cout <"plugin" <endl; 135 break; 136 case 4: // insert 137 cout <"Enter the row to insert a new book: "<endl; 138 cin> I; 139 cout <" Enter the number, name, and price of the book to be inserted in sequence: "<endl; 140 ListInsert_L (L, I); 141 cout <"you insert new book information, as shown below:" <endl; 142 cout <left 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.