C++ 段错误链表

标签 c++ linked-list segmentation-fault

我有一个编译得很好的程序,但是当我尝试通过我的菜单运行一个函数时,我只是收到了段错误的响应。我似乎也无法找到段错误发生的位置。

链表.cpp

 #include "linkedList.h"
#include <iostream>
#ifndef LINKEDLIST_CPP
#define LINKEDLIST_CPP
using namespace std;
//Destructor
template<class T>
linkedList<T>::~linkedList()
{
  node *temp=new node;
  while(head->!=NULL)
    {
      temp=head;
      head=head->next;
      delete temp;
    }
}

//Copy Constructor
template <class T>
linkedList<T>::linkedList(const linkedList &list)
{
  node *temp =new node;
  temp =list.head;
  delete this;
  while(temp->next!=NULL)
    {
      orderedInsert(temp->data);
      temp=temp->next;
    }
}

//Assignment Operator
template <class T>
linkedList<T> &linkedList<T>::operator=(const linkedList &list)
{
  if(this != &list)
    {
      current =head;
      while(current->next!=NULL)
        {
          head=head->next;
          delete  current;
          current=head;
        }
      current=list.head;
      while(current->!=NULL)
        {
          orderedInsert(current->data);
          current=current->next;
        }
    }
  return *this;
  }

template <class T>
bool linkedList<T>::empty() const
{
  current=head;
 if(current==NULL)
    {
      return true;
    }
  else
    return false;
}
template <class T>
void linkedList<T>::clear()
{
  delete *this;
}

template <class T>
bool linkedList<T>::search(const T &value)
{
  current=head;
  while(current->data!=value||current->!=NULL)
    {
      current=current->next;
    }
  if(current->data==value)
    {
      return true;
    }
  else
    return false;
}

template <class T>
void linkedList<T>::orderedInsert(const T &value)
{
  current=head;
  while(value>current->data)
    {
      trailCurrent=current;
      current=current->next;
    }
  trailCurrent->next=new node(value,current);

}

template <class T>
bool linkedList<T>::remove(const T &value)
{
  node *temp;
  temp =head;
  if(search(value)!=true)
    {
      return false;
    }
  else
    {
    trailCurrent->next=current->next;
    temp=current;
    current=current->next;
    delete temp;
    return true;
    }
}
template <class T>
bool linkedList<T>::replace(const T &oldData,const T &newData)
{
  if(search(oldData)==false)
    {
      return false;
    }
  else
    {
      current=head;
      while(current->data!=oldData)
        {
          current=current->next;
        }
      current->data=newData;
    }
  return true;
}

template <class T>
void linkedList<T>::insert(const T &value)
{
  if(trailCurrent==NULL)
    {
      trailCurrent=head;
      head =new node(value,trailCurrent);
      trailCurrent=head;
    }
  else if(trailCurrent->next->data!=current->data)
    {
 trailCurrent=head;
      while(trailCurrent->next->data!=current->data)
        {
          trailCurrent=trailCurrent->next;
        }
      node *temp;
      temp=trailCurrent;
      temp=new node(value,current);
      trailCurrent->next=temp;
    }
  return true;
}

template <class T>
bool linkedList<T>::retrieve(T &value)const
{
  if(current==NULL)
    {
      return false;
    }
  else
    {
      current->data =value;
      return true;
    }
}

template <class T>
void linkedList<T>::begin()
{
 current=head;
  trailCurrent=NULL;
}

template <class T>
linkedList linkedList<T>::operator++()
{
  if(current!=NULL)
    {
      current=current->next;
      trailCurrent=trailCurrent->next;
      return *this;
    }
}

template<class T>
linkedList linkedList<T>::operator++(int i)
{
  if(current!=NULL)
    {
      trailCurrent=current;
      current=current->next;
    }
  return that;
}

template <class T>
ostream &operator<<(ostream &outStream,linkedList<T> list)
{
 T element;
this.current=this.head;
 while(this.current->!=NULL)
   {
     element=this.retrieve(element);
     outStream<<"["<<element<<"]"<<endl;
     this.current++;
   }
 return outStream;
}
#endif

有很多节点是 .cpp,我似乎无法找到这个段错误的来源。

linkedListApp.cpp

#include "linkedList.h"
#include "myDate.h"
#include <iostream>
using namespace std;
#include <fstream>


//FillList
//Description: opens a file and fills the list from the file
//Parameters:  indexList
//Return:      none
void FillList(linkedList<myDate> &list);


//DisplayList
//Description: displays the list to the monitor
//Parameters: indexList
//Return:     none
void DisplayList(linkedList<myDate> list);

//AddToList
//Description: adds to the list by ordered insert
//Parameters: indexList,num
//Return:     none
void AddToList(linkedList<myDate> &list);

//GetTodaysAppointments
//Description: Asks the user for today's date, and looks for a matching date from the list
//Parameters: indexList,num
//Return: none
void GetTodaysAppointments(linkedList<myDate> list);

//ChangeAppointment
//Description: Lets the user choose an appointment to change, and changes as such
//Parameters: indexList
//Return: None
void ChangeAppointment(linkedList<myDate> &list);

//Menu
int menu();

int main()
{
        int choice;
        int num;
        linkedList<myDate> L;

        while ((choice = menu()) != 6)
        {
                switch (choice)
                {
                    case 1: FillList(L); break;
case 2: AddToList(L); break;
                    case 3: GetTodaysAppointments(L); break;
                    case 4: ChangeAppointment(L); break;
                    case 5: DisplayList(L); break;
                }
        }


        return 0;
}


//menu
int menu()
{
        int ch;
        cout << endl;
        cout << "1. Fill from file" << endl;
        cout << "2. Add to the list" << endl;
        cout << "3. Get Todays Appointments" << endl;
        cout << "4. Change an appointment" << endl;
        cout << "5. Display list" << endl;
        cout << "6. Quit"<<endl;
        cout << "Choice: ";
        cin >> ch;
        return ch;
}

//FillList
//Description: opens a file and fills the list from the file
//Parameters:  indexList
//Return:      none
void FillList(linkedList<myDate> &list)
{
  myDate aDate;
  myTime aTime;
  ifstream f;
  bool result =true;
  string fname;

  cout << "File: ";
  cin >> fname;
  f.open(fname.c_str());
  if (f.fail())
    {
      cout << "Failed to open" << endl;
      return;
    }
  list.begin();
  f >> aDate;
  while (result && !f.eof())
    {
      f>>aTime;
      aDate.setTime(aTime);
      list.orderedInsert(aDate);
      f>>aDate;

    }

  f.close();
}

每当我尝试“填充”时,我最终都会遇到段错误,我的其余代码是 here,因为我有更多文件。但我认为你可以从这两个中找到段错误我只是不能

最佳答案

这只是对代码的一些想法 - 它不是决定性的,它可能不是段错误的解决方案。

您似乎误解了如何使用指针。例如,在您的析构函数中,您为 temp 分配了内存,然后立即将其设置为 head。这基本上是一个泄漏。您可以简单地将其设置为 head。除非您计划使用它,否则没有理由为其分配内存。你的整个代码都有这个问题。我会首先尝试了解指针的工作原理,因为这将是许多其他事情的基础。

template<class T>
linkedList<T>::~linkedList()
{
  node *temp=new node;
  while(head->!=NULL)
    {
      temp=head;
      head=head->next;
      delete temp;
    }
}

搜索功能中的潜在问题:

while(current->data!=value||current->!=NULL)

这里你的想法是正确的,但顺序是错误的。如果 current 为空怎么办?看看short-circuit evaluation .

你对 current 的使用似乎是一个糟糕的设计,因为你多次使用它,但也许有一些我不知道的要求。

我注意到了一些其他事情,但如果您需要检查代码,还有其他地方可以问。

关于C++ 段错误链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29859646/

相关文章:

c++ - 公共(public)/ protected /私有(private)继承的问题

c++ - 删除链表中的节点函数

c++ - 使用 "delete this"删除当前对象是否可以?

java - 在输入参数是引用的情况下如何重用代码?

c++ - 如何正确编写尾随返回类型?

C++使用在另一个导致无限循环中定义的结构

c - C 递归组合 (nCr) 中的段错误

C++ 段错误在哪里?

c++ - 将 OpenMP 与 clang 一起使用

c++ - 奇怪的段错误 - 将指针指向 vector 的对象推回导致崩溃