永发信息网

C++删除链表产生错误

答案:2  悬赏:40  手机版
解决时间 2021-05-05 04:27
  • 提问者网友:鼻尖触碰
  • 2021-05-04 05:52

#include<iostream.h>
struct none{
 double a;
 none *next;
};

double ave(none *head) {
 if(head == NULL)
  return 0;
 int i = 0;
 double sum = 0;
 none *h = head;
 while(h){
  sum += h->a;
  h = h->next;
  i++;
 }
 return sum/i;
}

void showlist(none *head) {
 none *h = head;
 while(h) {
  cout << h->a << endl;
  h = h->next;
 }
}

void create(none *&head) {
 none *p;
 cout << "score\n";
 int score;
 cin >> score;
 while(score){
  p = new none;
  p->next = head;
  head = p;
  p->a = score;
  cout << "score:\n";
  cin >> score;
 }
}

void del_maxmin(none *head) {
 none *h,*max,*min;
 if(head == NULL) return;
 max = min = h = head;
 while(h){
  if(h->a > max->a) max = h;
  if(h->a < min->a) min = h;
  h = h->next;
 }
 if(max == head){
  head = head ->next;
  delete head;
 }
 else{
  none *p = head;
  while(p->next != max) p = p->next;
  p->next = max->next;
  delete max;
 }
 if(min == head){
  head = head ->next;
  delete head;
 }
 else{
  none *p = head;
  while(p->next != min) p = p->next;
  p->next = min ->next;
  delete min;
 }
}
 


int main() {
 none *head = NULL;
 create(head);
 showlist(head);
 del_maxmin(head);
 showlist(head);
 cout << endl << ave(head) << endl;
}

最佳答案
  • 五星知识达人网友:执傲
  • 2021-05-04 06:55
请描述错误~!
全部回答
  • 1楼网友:夜风逐马
  • 2021-05-04 07:53
没有指针库?
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯