Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. typedef bool Status;
  5.  
  6. typedef struct
  7. {
  8.     char no[20];
  9.     char name[50];
  10.     float price;
  11. }Book;
  12.  
  13. typedef struct LNode{
  14.     Book book;
  15.     struct LNode *next;}LNode,*LinkList;
  16.  
  17. Status InitList(LinkList &L){
  18.     L = new LNode;
  19.     L->next=NULL;
  20.     return true;
  21. }
  22.  
  23. LNode *LocateBook(LinkList L,Book book){
  24.     LNode *p = L->next;//p指向首元结点
  25.     while(p && strcmp(p->book.no,book.no))
  26.         p=p->next;
  27.     return p;
  28. }
  29.  
  30. Status ListInsert(LinkList &L,int i,Book book){
  31.         LNode *p =L; int j=0;
  32.     while(p && (j<i-1)){
  33.         p=p->next;
  34.         ++j;
  35.     }
  36.     if(!p||j>i-1) return false;
  37.     LNode *s = new LNode;
  38.     s->book = book;
  39.     s->next = p->next;
  40.     p->next = s;
  41.     return true;
  42. }
  43.  
  44. Status ListDelete(LinkList &L,int i){
  45.     LNode *p=L;int j=0;
  46.     while((p->next)&&(j<i-1)){
  47.         p=p->next;
  48.         ++j;
  49.     }
  50.     if(!(p->next)||(j>i-1)) return false;
  51.     LNode * q =p->next;
  52.     p->next = q->next;
  53.     delete q;
  54.     return true;
  55. }
  56.  
  57. Status ListModify(LinkList &L, Book book){
  58.     LNode *p = LocateBook(L,book);
  59.     if (p==NULL) return false;
  60.     p->book.price = book.price;
  61.     return true;
  62. }
  63.  
  64. int ListCount(LinkList L){
  65.      LNode *p = L->next;
  66.      int count = 0;
  67.      while (p)
  68.      {
  69.          p=p->next;
  70.          count++;
  71.      }
  72.      return count;
  73. }
  74. int main(){
  75.     Book bookA;
  76.     strcpy(bookA.no,"9787302257646");
  77.     strcpy(bookA.name,"Basic");
  78.     bookA.price = 25;
  79.  
  80.     LinkList L;
  81.     InitList(L);
  82.     ListInsert(L,1,bookA);
  83.  
  84.     cout<<"priceA"<<LocateBook(L,bookA)->book.price<<endl;
  85.     bookA.price =35;
  86.     ListModify(L, bookA);
  87.     cout<<"priceA"<<LocateBook(L,bookA)->book.price<<endl;
  88.  
  89.     Book bookB;
  90.     strcpy(bookB.no,"9787302219972");
  91.     strcpy(bookB.name,"Apply");
  92.     bookB.price = 32;
  93.  
  94.     cout<<"ListInsertB"<<ListInsert(L,2,bookB)<<endl;
  95.      cout<<"ListCount"<<ListCount(L)<<endl;
  96.  
  97.     cout<<"deleteA"<<ListDelete(L,1)<<endl;
  98.      cout<<"ListCount"<<ListCount(L)<<endl;
  99.  
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement