fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define MAX 8
  4. struct HH{
  5. char mahang[20];
  6. char tenhang[30];
  7. char donvi[10];
  8. int dongia,soluong;
  9. };
  10. struct Node{
  11. HH infor;
  12. Node *next;
  13. };
  14. typedef Node *TRO;
  15. void creat(TRO& L){
  16. L=NULL;
  17. }
  18. int empty(TRO &L){
  19. return (L==NULL);
  20. }
  21. HH taohh(char mh[20],char th[30],char dv[10],int dg,int sl ){
  22. HH hh;
  23. strcpy(hh.mahang,mh);
  24. strcpy(hh.tenhang,th);
  25. strcpy(hh.donvi,dv);
  26. hh.dongia=dg;
  27. hh.soluong=sl;
  28. return hh;
  29. }
  30. void display(HH hh){
  31. cout<<fixed;
  32. cout<<setw(8)<<left<<hh.mahang;
  33. cout<<setw(15)<<hh.tenhang;
  34. cout<<setw(20)<<hh.donvi;
  35. cout<<setw(20)<<hh.dongia;
  36. cout<<setw(15)<<hh.soluong;
  37. cout<<setw(15)<<hh.soluong *hh.dongia;
  38. cout<<endl;
  39. }
  40. void add(TRO &L,HH hh){
  41. TRO P,Q=L;
  42. P=new Node;
  43. P->infor=hh;
  44. P->next=NULL;
  45. if(empty(L)){
  46. L=P;
  47. }
  48. else{
  49. while(Q->next!=NULL)
  50. Q=Q->next;
  51. Q->next=P;
  52. }
  53. }
  54. void create_List(TRO &L){
  55. add(L,taohh("H2001","vo","quyen",6500,20));
  56. add(L,taohh("H2002","but chi","cai",12000,50));
  57. add(L,taohh("H2003","hop but","chiec",35000,15));
  58. add(L,taohh("H2004","tay ","cai",10000,50));
  59. add(L,taohh("H2005","thuoc ke","cai",7000,55));
  60. add(L,taohh("H2001","muc","lo",15000,28));
  61. }
  62. void showList(TRO L){
  63. TRO Q;
  64. if(!empty(L)){
  65. cout<<setw(8)<<left<<"ma hang";
  66. cout<<setw(15)<<"ten hang";
  67. cout<<setw(20)<<"don vi";
  68. cout<<setw(20)<<"don gia";
  69. cout<<setw(15)<<"so luong";
  70. cout<<setw(15)<<"thanh tien";
  71. cout<<endl;
  72. Q=L;
  73. while(Q!=NULL){
  74. display(Q->infor);
  75. Q=Q->next;
  76.  
  77. }
  78. }
  79. else{
  80. cout<<"danh sach rong "<<endl;
  81. }
  82. }
  83. TRO search_k(TRO&L,int k){
  84. int d=1;
  85. TRO Q=L;
  86. while(d<k-1 && Q->next!=NULL){
  87. d++;
  88. Q=Q->next;
  89. }
  90. if(d<k-1){
  91. return NULL;
  92. }
  93. return Q;
  94. }
  95. void xoa_pt_thu3(TRO &L){
  96. if(empty(L)){
  97. cout<<"danh sach rong "<<endl;
  98. return;
  99. }
  100. TRO Q=search_k(L,3);
  101. if(Q==NULL || Q->next ==NULL){
  102. cout<<"khong the xoa phan tu thu 3"<<endl;
  103. return;
  104. }
  105. TRO temp=Q->next;
  106. Q->next=temp->next;
  107. delete temp;
  108. cout<<"=========================DANH SACH SAU KHI XOA PT THU 3================"<<endl;
  109. showList(L);
  110. }
  111. void insert(TRO &L,TRO Q,HH hh){
  112. TRO P=new Node;
  113. P->infor=hh;
  114. P->next=Q->next;
  115. Q->next=P;
  116. }
  117. void chen_vt_dau(TRO&L){
  118. TRO Q=search_k(L,1);
  119. if(Q==NULL){
  120. cout<<"khong the chen"<<endl;
  121. return;
  122. }
  123. insert(L,Q,taohh("2007","phan","hop",15,45000));
  124. cout<<"=========================DANH SACH SAU KHI CHEN ========================"<<endl;
  125. showList(L);
  126. }
  127. void sort(TRO &L){
  128. if(L==NULL || L->next==NULL)
  129. return ;
  130. bool swapped;
  131. TRO ptr1;
  132. TRO lptr=NULL;
  133. do{
  134. swapped=false;
  135. ptr1=L;
  136. while(ptr1->next !=lptr){
  137. if(strcmp(ptr1->infor.tenhang,ptr1->next->infor.tenhang)>0){
  138. swap(ptr1->infor,ptr1->next->infor);
  139. swapped=true;
  140. }
  141. ptr1=ptr1->next;
  142. }
  143. lptr=ptr1;
  144. }
  145. while(swapped);
  146. cout<<"=====================DANH SACH SAU KHI SAP XEP====================="<<endl;
  147. showList(L);
  148. }
  149. int main(int argc,char const*argv[]){
  150. TRO L;
  151. creat(L);
  152. create_List(L);
  153. cout<<"==========================DANH SACH BAN DAU================="<<endl;
  154. showList(L);
  155. xoa_pt_thu3(L);
  156. chen_vt_dau(L);
  157. sort(L);
  158. return 0;
  159. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
==========================DANH SACH BAN DAU=================
ma hang ten hang       don vi              don gia             so luong       thanh tien     
H2001   vo             quyen               6500                20             130000         
H2002   but chi        cai                 12000               50             600000         
H2003   hop but        chiec               35000               15             525000         
H2004   tay            cai                 10000               50             500000         
H2005   thuoc ke       cai                 7000                55             385000         
H2001   muc            lo                  15000               28             420000         
=========================DANH SACH SAU KHI XOA PT THU 3================
ma hang ten hang       don vi              don gia             so luong       thanh tien     
H2001   vo             quyen               6500                20             130000         
H2002   but chi        cai                 12000               50             600000         
H2004   tay            cai                 10000               50             500000         
H2005   thuoc ke       cai                 7000                55             385000         
H2001   muc            lo                  15000               28             420000         
=========================DANH SACH SAU KHI CHEN ========================
ma hang ten hang       don vi              don gia             so luong       thanh tien     
H2001   vo             quyen               6500                20             130000         
2007    phan           hop                 15                  45000          675000         
H2002   but chi        cai                 12000               50             600000         
H2004   tay            cai                 10000               50             500000         
H2005   thuoc ke       cai                 7000                55             385000         
H2001   muc            lo                  15000               28             420000         
=====================DANH SACH SAU KHI SAP XEP=====================
ma hang ten hang       don vi              don gia             so luong       thanh tien     
H2002   but chi        cai                 12000               50             600000         
H2001   muc            lo                  15000               28             420000         
2007    phan           hop                 15                  45000          675000         
H2004   tay            cai                 10000               50             500000         
H2005   thuoc ke       cai                 7000                55             385000         
H2001   vo             quyen               6500                20             130000