Current location - Training Enrollment Network - Books and materials - Library management system program code? chain structure
Library management system program code? chain structure
I used to write it in c++ myself. You can refer to it

# include & ltiostream & gt

# include & ltfstream & gt

# include & lt string & gt

Use namespace std

Structured book information

{

char log in name[ 10];

char bookname[20];

Char author [20];

Number of characters [5];

char date[ 10];

Double the price;

};

Structural node

{

struct BookInfo book

Structure node * next

};

void option();

void select();

Node * header;

node * pt[ 10];

FILE * fp

//Create a linked list

/*

Node *CrtNode(int n)

{

Node * header; //Declare the head pointer head

Nodes *p, * s;;

Head = new node; //Create the Head node pointed by the head (empty head node)

S = head;

Cout & lt& lt "Please enter book information:"

for(int I = 0; I & ltn;; i++)

{

P = new node; //Create a node

Cout & lt& lt' login name:';

CIN . getline(p-& gt; book.loginname, 10);

strcat(p-& gt; book.loginname," \ n ");

Cout & lt& lt "title:";

CIN . getline(p-& gt; book.bookname,20);

strcat(p-& gt; book.bookname," \ n ");

Cout & lt& lt Author's name:;

CIN . getline(p-& gt; book.author,20);

strcat(p-& gt; book.author," \ n ");

Cout & lt& lt "classification number:";

CIN . getline(p-& gt; book.number,5);

strcat(p-& gt; book.number," \ n ");

Cout & lt& lt "Release date:";

CIN & gt; & gtp->; book.date

strcat(p-& gt; book.date," \ n ");

Cout & lt& lt price: ";

CIN & gt; & gtp->; book.price

s-& gt; next = p; //Point the created node to the next one of S.

s = p; //The pointer S moves backward by one node.

CIN . clear();

CIN . sync();

}

p->; Next = NULL// The next of the last node points to null.

Return head; //Returns the head pointer

}

*/

/*

strcat(p-& gt; book.loginname," \ n ");

strcat(p-& gt; book.bookname," \ n ");

strcat(p-& gt; book.author," \ n ");

strcat(p-& gt; book.number," \ n ");

strcat(p-& gt; book.date," \ n ");

*/

// 1. Insert

Null Insert (Node * Header)

{

Nodes *p, * s;;

S = head;

Cout & lt& lt "Please enter book information:"

P = new node;

CIN . clear();

Cout & lt& lt' login name:';

CIN . getline(p-& gt; book.loginname, 10);

strcat(p-& gt; book.loginname," \ n ");

CIN . clear();

CIN . sync();

Cout & lt& lt "title:";

CIN . getline(p-& gt; book.bookname,20);

strcat(p-& gt; book.bookname," \ n ");

CIN . clear();

CIN . sync();

Cout & lt& lt Author's name:;

CIN . getline(p-& gt; book.author,20);

strcat(p-& gt; book.author," \ n ");

CIN . clear();

CIN . sync();

Cout & lt& lt "classification number:";

CIN . getline(p-& gt; book.number,5);

strcat(p-& gt; book.number," \ n ");

CIN . clear();

CIN . sync();

Cout & lt& lt "Publication date (yyyy-mm-dd):";

CIN & gt; & gtp->; book.date

strcat(p-& gt; book.date," \ n ");

Cout & lt& lt price: ";

CIN & gt; & gtp->; book.price

while(s->; Next)

s = s-& gt; Next;

s-& gt; next = p;

p->; next = NULL

}

//initialization

Node * Initial ()

{

Node * header;

Head = new node;

head->; next = NULL

Return head;

}

//2. Show all information

Empty display (node * header)

{

int I = 1;

node * p;

//Display all nodes except the head node (because the head node is empty when it is created)

p = head-& gt; Next;

if(p == NULL)

{

Cout & lt& lt "system does not store any book information. Please enter the book information before other operations! " & lt& ltendl

}

other

{

Cout & lt& lt“* * * * * * * * * Here are all the books and materials * * * * * * * ".

And (p! = empty)

{

Cout & lt& lt "book"

Cout & lt& lt "Login name:"

Cout & lt& lt "Title:"

Cout & lt& lt "Author's name:"

Cout & lt& lt "Classification number:"

Cout & lt& lt "Publication date:"

Cout & lt& lt "Price:"

cout & lt& ltendl

p = p-& gt; Next;

i++;

cout & lt& ltendl

}

}

Cout & lt& lt "Please press Enter to return to the menu." & lt& ltendl

CIN . get();

}

//3. Discover

Int findauthor (constant node * header)

{

Node * ps

Char author [20];

int count = 0;

PS = head-& gt; Next;

Cout & lt& lt "Please enter the author's name:";

Cin.getline (author, 20);

Strcat (author, "\ n");

while(ps)

{

if(strcmp(PS-& gt; book.author,author) == 0)

{

pt[0]= PS;

count++;

}

PS = PS-& gt; Next;

}

if(count == 0)

Cout & lt& lt "The book you are looking for does not exist!" & lt& ltendl

Returns the count;

}

Int findbookname name (constant node * header)

{

Node * ps

char bookname[20];

int count = 0;

int I = 0;

PS = head-& gt; Next;

Cout & lt& lt Please enter a title: ";

cin.getline(bookname,20);

Strcat (book name, "\ n");

while(ps)

{

if(strcmp(PS-& gt; book.bookname,bookname) == 0)

{

pt[I]= PS;

count++;

i++;

}

If (PS->; next ==NULL)

Break;

PS = PS-& gt; Next;

}

if(count == 0)

Cout & lt& lt "The book you are looking for does not exist!" & lt& ltendl

Returns the count;

}

void Showarray(int n)

{

Cout & lt& lt“* * * * * * * * * * This is the book you are looking for * * * * * * ".

for(int I = 0; I & ltn;; i++)

{

Cout & lt& lt "book"

Cout & lt& lt "Login name:"

Cout & lt& lt "Title:"

Cout & lt& lt "Author's name:"

Cout & lt& lt "Classification number:"

Cout & lt& lt "Publication date:"

Cout & lt& lt "Price:"

cout & lt& ltendl

}

CIN . get();

}

Int Find (node * header)

{

int n,num

System ("CLS");

if(head-& gt; next == NULL)

{

Cout & lt& lt "system does not store any book information. Please enter the book information before other operations! " & lt& ltendl

CIN . get();

}

other

{

Cout & lt& lt Please select a search method (1. Search by author name 2. Search by title):;

CIN & gt; & gtn;

CIN . clear();

CIN . sync();

Switch (n)

{

Case 1:

num = find author(head);

If (num! = 0)

show array(num);

Break;

Case 2:

num = findbookname(head);

If (num! =0)

show array(num);

Break;

Default value:

Cout & lt& lt "Wrong input, please re-enter!" & lt& ltendl

CIN . get();

System ("CLS");

option();

select();

}

}

Quantity returned;

}

//4. Modify the book information

Invalid modification (node * header)

{

Nodes *p, *q, * s;;

p = head-& gt; Next;

int i,n;

if(p == NULL)

Cout & lt& lt "system does not store any book information. Please enter the book information before other operations! " & lt& ltendl

other

{

Cout & lt& lt "Please enter the name of the book whose information needs to be corrected:"

n = findbookname(head);

showarray(n);

Cycle:

{

Cout & lt& lt "Please select the book information to modify:";

}

CIN & gt; & gt me;

CIN . clear();

CIN . sync();

If (i>0& < = n)

{

Cout & lt& lt“* * * * * First Amendment "

Cout & lt& lt' login name:';

CIN . getline(pt[I- 1]-& gt; book.loginname, 10);

strcat(pt[I- 1]-& gt; book.loginname," \ n ");

CIN . clear();

CIN . sync();

Cout & lt& lt Author's name:;

CIN . getline(pt[I- 1]-& gt; book.author,20);

strcat(pt[I- 1]-& gt; book.author," \ n ");

CIN . clear();

CIN . sync();

Cout & lt& lt "classification number:";

CIN . getline(pt[I- 1]-& gt; book.number,5);

strcat(pt[I- 1]-& gt; book.number," \ n ");

CIN . clear();

CIN . sync();

Cout & lt& lt "Publication date (yyyy-mm-dd):";

CIN & gt; & gtpt[I- 1]-& gt; book.date

strcat(pt[I- 1]-& gt; book.date," \ n ");

Cout & lt& lt price: ";

CIN & gt; & gtpt[I- 1]-& gt; book.price

}

other

{

Cout & lt& lt "The serial number of the selected book is wrong!" ;

Go to loop;

}

}

CIN . get();

}

//5. Delete book information

void Deletebook(Node *head)

{

Nodes *p, * q;;

char bookname[20];

int count = 0;

int I = 0;

P = head;

q = p->; Next;

if(q == NULL)

Cout & lt& lt "There is no book information in the system!" & lt& ltendl

other

{

Cout & lt& lt Please enter a title: ";

cin.getline(bookname,20);

Strcat (book name, "\ n");

And (q! = empty)

{

q = p->; Next;

if(strcmp(q-& gt; book.bookname,bookname) == 0)

{

p->; next = q-& gt; Next;

Delete q;

count++;

}

p = q;

q = q-& gt; Next;

}

if(count == 0)

Cout & lt& lt "No book information found!" & lt& ltendl

other

Cout & lt& lt "Book information has been deleted!" & lt& ltendl

}

CIN . get();

}

//Linked list length

Int Len (constant node * header)

{

Node * ps

int count = 0;

PS = head-& gt; Next;

while(ps)

{

count++;

PS = PS-& gt; Next;

}

Returns the count;

}

//6. Save the file

Invalid save (node * header)

{

node * p;

char ST[20];

p = head-& gt; Next;

if((fp=fopen("book.dat "," w ")= = NULL)

{

Cout & lt& lt "Unable to open file"

Return;

}

char t[255];

//Assign L 1.listlen () to the number in the string.

int lenth = Len(head);

fwrite(& amp; lenth,sizeof(int), 1,FP);

while(p)

{

fwrite(p,sizeof(struct Node), 1,FP);

p = p-& gt; Next;

}

fclose(FP);

Cout & lt& lt "file has been saved, please press Enter to exit!" & lt& ltendl

CIN . get();

}

//8. Delete all book information.

Void Free_List (node * header)

{

node * p;

p = head-& gt; Next;

while(p)

{

Delete p;

p = head-& gt; Next;

}

}

/*

void readstr(FILE *f,char *string)

{

do

{

//①: Read a line first.

fgets(string,255,f); //fgets (): read a string with the length of 255- 1 from the file F.

//and store it in a string.

} while((string[0]= = '/')| |(string[0]= = ' \ n ');

Return;

}

*/

//Read the file

Node * Load ()

{

char c[255];

int lenth

Nodes *p, * q;;

Head = new node;

P = head;

if((fp=fopen("book.dat "," r ")= = NULL)

{

Cout & lt& lt "Unable to open file"

Return head;

}

other

Fred (& amplenth, sizeof(int), 1, fp);

for(int I = 0; I & ltlenthi++)

{

Q = new node;

fread(q,sizeof(struct Node), 1,FP);

p->; next = q;

p = q;

}

p->; next = NULL

fclose(FP);

Return head;

}

//9. Menu options

Invalid selection ()

{

int m,n;

Cout & lt& lt Please enter the following numbers to select the required operation (1~ 8):;

CIN . clear();

CIN . sync();

CIN & gt; & gtn;

CIN . clear();

CIN . sync();

Switch (n)

{

Case 1:

System ("CLS");

To insert (a head);

Break;

Case 2:

System ("CLS");

Show (head);

Break;

Case 3:

System ("CLS");

Find (the head);

Break;

Case 4:

System ("CLS");

Modify (header);

Break;

Case 5:

System ("CLS");

delete book(head);

Break;

Case 6:

Save (header);

Break;

Case 7:

Exit (0);

Break;

Case 8:

Free_List (header);

Break;

Default value:

Break;

}

}

// 10. Menu interface

Void option ()

{

Cout & lt& lt "Book Information Management System"

cout & lt& lt“* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *”& lt; & ltendl

Cout & lt& lt" 1. Enter the book information 2. Output book information "

Cout & lt& lt"3. Find book information 4. Modify the book information "

Cout & lt& lt"5. Delete book information 6. Save book information "

Cout & lt& lt"7. Exit the system "

cout & lt& lt“* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *”& lt; & ltendl

}

int main()

{

//head = Initial();

Header = load ();

while( 1)

{

System ("CLS");

option();

select();

}

Returns 0;

}