ZBLOG

使用单链表的其他方法修改此代码:#ifndef NULL #define NULL 0 #endif #include #include #include using namespace std; struct LNode { //存储数据 int month; int day; string name; double price; LNode* next;//后继指针 LNode() {}; LNode(int a,...

下面是使用双向链表的修改代码:

”`cpp #ifndef NULL #define NULL 0 #endif

#include #include #include

using namespace std;

struct LNode {

  1. int month;
  2. int day;
  3. string name;
  4. double price;
  5. LNode* prev; // 前驱指针
  6. LNode* next; // 后继指针
  7. LNode() {};
  8. LNode(int a, int b, string c, double d, LNode* p = NULL, LNode* n = NULL)
  9. : month(a), day(b), name(c), price(d), prev(p), next(n) {};

};

class List { public:

  1. int _size;
  2. LNode* head;
  3. void init() {
  4. head = new LNode();
  5. head->prev = NULL;
  6. head->next = NULL;
  7. _size = 0;
  8. }

public:

  1. List() { init(); }
  2. ~List() {
  3. while (head != NULL) {
  4. LNode* p = head->next;
  5. delete head;
  6. head = p;
  7. }
  8. _size = 0;
  9. }

public:

  1. // 头插法
  2. void insert(int& a, int& b, string& c, double& d) {
  3. _size++;
  4. if (head == NULL) {
  5. return ;
  6. }
  7. LNode* p = new LNode(a, b, c, d);
  8. if (head->next != NULL) {
  9. p->next = head->next;
  10. head->next->prev= p;
  11. }
  12. p ->prev= head ;
  13. head -> next=p ;
  14. };

//在指定位置插入元素

  1. void insert(int index,int a, int b, string c, double d) {
  2. while (index <= 0 || index > _size + 1)
  3. {
  4. return;
  5. }
  6. _size++;
  7. LNode* p = head;
  8. for(int i=0; i<index-1; i++){
  9. p=p->next;
  10. }
  11. LNode* tmp = new LNode(a,b,c,d);
  12. if(p->next!=NULL){
  13. p -> next ->prev=tmp;
  14. tmp -> next=p->next;
  15. }else{
  16. // 尾部插入
  17. p->next=tmp;
  18. tmp->prev=p;
  19. }
  20. tmp ->prev=p ;
  21. };
  22. void print_L() {
  23. LNode* p=head -> next ;
  24. while (p != NULL) {
  25. cout << p->month << " " << p->day << " ";
  26. p = p->next;
  27. }
  28. cout << endl;
  29. };
  30. int located(int a,int b) {
  31. LNode* p = head -> next ;
  32. int e = 1 ;
  33. if(p == NULL ) return e ;
  34. while(p!=NULL){
  35. if(a <p -> month ){
  36. e++ ;
  37. p=p ->next ;
  38. if(p==NULL){
  39. return e ;
  40. }
  41. } else if (a>p -> month )
  42. {
  43. return e ;
  44. }else if (a ==p -> month ){
  45. if(b<p->day){
  46. e++;
  47. p=p->next ;
  48. if(p==NULL){
  49. return e ;
  50. }
  51. } else if(b>p->day ){
  52. return e ;
  53. }else if(b==p -> day ){
  54. return e ;
  55. }
  56. }
  57. }
  58. };

};

int main() {

  1. int m, n;
  2. string str;
  3. string c;
  4. double d;
  5. List L1;
  6. double ave1 = 0;
  7. double ave2 = 0;
  8. cin >> m >> n;
  9. for (int i = 0; i < m; i++) {
  10. cin >> str >> c >> d;
  11. int a = stoi(str, 0, 10);
  12. if (a >= 10)
  13. str.erase(0, 3);
  14. else
  15. str.erase(0, 2);
  16. int b = stoi(str, 0, 10);
  17. int e = L1.located(a,b) ;
  18. L1.insert(e,a,b,c,d) ;
  19. }
  20. //L1.print_L();
  21. double sum1=0 ;
  22. int count1=0 ;
  23. double sum2=0 ;
  24. int count2=0 ;
  25. LNode* p=L1.head->next ;
  26. for(int i=0;i<m;i++){
  27. if(count1>=n){
  28. break ;
  29. }else{
  30. if(p -> name=="keyboard"){
  31. sum1+=p ->price ;
  32. p=p -> next ;
  33. count1++ ;
  34. } else if(p-> next !=NULL ) p=p->next ;
  35. }
  36. }
  37. LNode* s=L1.head->next ;

for(int i=0;i;i++){

  1. if(count2>=n){
  2. break ;
  3. }else{
  4. if(s -> name=="mouse"){
  5. sum2+=s ->price ;
  6. s=s -> next ;
  7. count2++ ;
  8. }else if(s-> next !=NULL ) s=s->next ;
  9. }
  10. }
  11. cout << fixed << setprecision(1) <<sum1/n<<endl;
  12. cout <<fixed << setprecision(1)<<sum2/n<<endl;
  13. return 0;

}

”`


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=9587

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?