每天都在头秃 2023-11-16 11:23 采纳率: 96.7%
浏览 8
已结题

C++Segmentation fault : 段错误

这个程序在oj上有些数据跑出来报Segmentation fault,但是我没看出来问题,希望能帮忙看一看。

程序的目的是实现优先级对列




#include <cassert>
#include <iostream>
#include <string>
 #include "lib.h"
 #include "PriorityQueue.h"
using namespace std;

void error(std::string msg) {
   std::cout << "ERROR: " << msg << std::endl;
}

template <typename ValueType>
PriorityQueue<ValueType>::PriorityQueue() {
   head = NULL;
   count = 0;
}


template <typename ValueType>
PriorityQueue<ValueType>::~PriorityQueue() {
   clear();
}


template <typename ValueType>
int PriorityQueue<ValueType>::size() const {
   return count;
}

template <typename ValueType>
bool PriorityQueue<ValueType>::isEmpty() const {
   return count == 0;
}

template <typename ValueType>
void PriorityQueue<ValueType>::clear() {
   while (count > 0) {
      dequeue();
   }
}


template <typename ValueType>
void PriorityQueue<ValueType>::enqueue(ValueType value, double priority) {
   //TODO
   //1,compare the priority find the position, then insert 
   //2,if it's the same,put it next to it.
   //ele to insert
   Cell *ele =new Cell();
   ele->data =value;
   ele->priority =priority;
   ele->link=NULL;

   //if it's empty
   if(isEmpty()){
      head=ele;
      //cout<<ele->data<<endl;
      count++;
   }else{//there exist ele in the queue
      while (head->link!=NULL)
      {
         if(head->priority<=ele->priority&&head->link->priority>ele->priority){
            ele->link=head->link;
            head->link=ele;
            count++;
            //cout<<ele->data<<endl;
            break;
         }
         head=head->link;
      }

      //when catch the end
      if(head->link==NULL){//catch the end
            head->link=ele;
            count++;
            //cout<<ele->data<<endl;
            
         }
      

   } 
   

}


template <typename ValueType>
ValueType PriorityQueue<ValueType>::dequeue() {
   if (isEmpty()) error("dequeue: Attempting to dequeue an empty queue");
   //TODO
   Cell *mid =head;
   ValueType value =mid->data;
   head=mid->link;
   count--;
   delete mid;
   return value;
}

template <typename ValueType>
ValueType PriorityQueue<ValueType>::peek() const {
   if (isEmpty()) error("peek: Attempting to peek at an empty queue");
   //TODO
   return head->data;
}


template <typename ValueType>
PriorityQueue<ValueType>::PriorityQueue(const PriorityQueue<ValueType> & src) {
   deepCopy(src);
}

template <typename ValueType>
PriorityQueue<ValueType> & PriorityQueue<ValueType>::operator=(const PriorityQueue<ValueType> & src) {
   if (this != &src) {
      clear();
      deepCopy(src);
   }
   return *this;
}



template <typename ValueType>
void PriorityQueue<ValueType>::deepCopy(const PriorityQueue<ValueType> & src) {
   head = NULL;
   Cell **target = &head;
   for (Cell *scp = src.head; scp != NULL; scp = scp->link) {
      Cell *cp = new Cell;
      cp->data = scp->data;
      cp->priority = scp->priority;
      *target = cp;
      target = &cp->link;
   }
   *target = NULL;
   count = src.count;
}


int main() {
   PriorityQueue<string> pq;
   string value;
   double priority;

   string in_pair;
   while(getline(cin,in_pair)){
      int sp = in_pair.find(' ');
      value = in_pair.substr(0, sp);
      priority = stod(in_pair.substr(sp+1, in_pair.size()));
      pq.enqueue(value, priority);
   }
   cout<<"pq.size() = "<<pq.size()<<endl;
   int init_len = pq.size();
   for (int i = 0; i<init_len; i++) {
      cout<<"i="<<i<<": pq.peek() = "<<pq.peek()<<endl;
      cout<<"i="<<i<<": pq.dequeue() = "<<pq.dequeue()<<endl;
   }
   cout<<"pq.isEmpty(): "<<boolalpha<<pq.isEmpty()<<endl;
   return 0;
}

  • 写回答

3条回答 默认 最新

  • 关注

    段错误一般是因为内存错误导致的,比如使用了空指针或者野指针,使用指针类型前最好判断一下是否为NULL,并且在释放内存后,将指针置NULL。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 11月24日
  • 已采纳回答 11月16日
  • 修改了问题 11月16日
  • 创建了问题 11月16日

悬赏问题

  • ¥15 求高通平台Softsim调试经验
  • ¥15 canal如何实现将mysql多张表(月表)采集入库到目标表中(一张表)?
  • ¥15 wpf ScrollViewer实现冻结左侧宽度w范围内的视图
  • ¥15 栅极驱动低侧烧毁MOSFET
  • ¥30 写segy数据时出错3
  • ¥100 linux下qt运行QCefView demo报错
  • ¥50 F1C100S下的红外解码IR_RX驱动问题
  • ¥20 基于matlab的航迹融合 航迹关联 航迹插补
  • ¥15 用Matlab实现图中的光线追迹
  • ¥15 联想笔记本开机出现系统更新界面