Seg Fault和Pointer问题

时间:2015-02-20 02:36:56

标签: c++ linux

在课堂上工作了几天这个程序,我不断遇到seg故障。我可以注释掉我认为导致它的代码部分,并且我停止了错误但是我的打印功能不起作用。我明天要拿起c ++书来帮忙。

//linkedList.cpp
//Declaration of main and menu functions
//Programmer: Ronnie Warden
//Date: 2.15.15

#include<iostream>
#include "link.h"
using namespace std;

int main()
{
    Node *list = new Node;
    int delNode;
    int findNode;
    int choice = 1;
    list->next = NULL;

    while (choice != 5)
    {
        choice = menu();

        if (choice == 1)
            *list = insertNode(list);

        if (choice == 2)
        {   
            cout << "Enter the ID you wish to delete: ";
            cin >> delNode;
            *list = deleteNode(list, delNode);
        }

        if (choice == 3)
            printNode(list);

        if (choice == 4)
        {
            cout << "Enter the ID you are searching for: ";
            cin >> findNode;
            *list = searchNode(list, findNode);
        }

    if (choice < 1 || choice > 5)
        cout << "Invalid choice! Please try again." << endl;

}

return 0;
}

int menu()
{
    int choice = 1;

         cout << "1. Insert Node" << endl;
         cout << "2. Delete Node" << endl;
         cout << "3. Print List" << endl;
         cout << "4. Search List" << endl;
         cout << "5. Quit" << endl;
         cin >> choice;

return choice;
}

以下部分是抛出seg错误的部分。

//linkFun.cpp
//Declaration of functions used to manage the linked list
//Programmer: Ronnie Warden
//Date: 2.10.15

#include<iostream>
#include "link.h"
using namespace std;


/*************************************************************************************************************
Function: createNode
Parameters: No parameters
Return Type: Pointer to new node
Task: Create a node with "new" operator. Get a student data from keyboard and assign to members of the node.
*************************************************************************************************************/

Node createNode()
{
Node *newNode = new Node;

    cout << "Last Name?" << endl;
    cin >> newNode->lastName;
    cout << "First Name?" << endl;
    cin >> newNode->firstName;
    cout << "ID?" << endl;
    cin >> newNode->idNumber;

return *newNode;
}

/**************************************************************************************************************
Function: insertNode
Parameters: Pointer to the linked list
Return Type: Pointer to the linked list
Task: insert a new node to the linked list sorted by student's ID number. If ID is already in list notify user 
***************************************************************************************************************/

Node insertNode(Node *list)
{
    Node *newNode = new Node;
    Node *tmp = new Node;
    *newNode = createNode();
    int id = newNode->idNumber;

    if (list == NULL)           //Insert in empty list
        list->next = newNode;
    else
    {
         *tmp = searchNode(list, id);

         if (tmp->idNumber == newNode->idNumber)
             {
                cout << "ID number already in list! Please try again with a different ID number." << endl;
                insertNode(list);
        }

    if (list != NULL)
    {
        Node *tmp = list;

         if (tmp->idNumber > newNode->idNumber)     //Insert as first
         {
             newNode->next = tmp;
            list = newNode;
        }
    while (tmp->idNumber < newNode->idNumber)   //Search for insertion point
    {   
                if (tmp->next == NULL)        //Insert at end
                            tmp->next == newNode;

        tmp = tmp->next;

        if (tmp->idNumber < newNode->idNumber && tmp->next->idNumber > newNode->idNumber && tmp->next != NULL)  //Insert in-between
        {
            newNode->next = tmp->next->next;
            tmp->next = newNode;
        }

        }
    }
}
return *list;
}

 /***************************************************************************************************************
Function: searchNode
Parameters: Pointer to the linked list, student ID number
Return Type: Pointer to the node with matched ID number
Task: Search the linked list by student Id number. Notify user if ID is not found
***************************************************************************************************************/

Node searchNode(Node *list, int id)
{
    Node *missing = new Node;

    if (list->idNumber == id)   //Checks if first node matches id number
         return *list;
    else 
    {
        Node *tmp = new Node;   //creates temporary pointer to scroll through list
        while (tmp->idNumber != id)
       {    
            if (tmp->next == NULL)  //checks if number is missing returns sentinel if not found
            {
                missing->idNumber = 9999;   
                return *missing;
            }

             tmp = tmp->next;
         }
     return *tmp;

     }
 }

/***************************************************************************************************************
Function: deleteNode
Parameters: Pointer to the linked list, student ID number
Return Type: Pointer to the list
Task: Delete a node from the list. Notify user if no node matches
***************************************************************************************************************/

Node deleteNode(Node *list, int id)
{   
    Node *tmp = new Node;
    *tmp = searchNode(list, id);    
return *list;

}

/***************************************************************************************************************
Function: printNode
Parameters: Pointer to the linked list
Return Type: None
Task: Display the linked list
***************************************************************************************************************/

void printNode(Node *list)
{
    Node *tmp = new Node;
    tmp = list;

    while (tmp != NULL)
    {
        cout << tmp->lastName << endl;
        cout << tmp->firstName << endl;
        cout << tmp->idNumber << endl;
        tmp = tmp->next;
    }
}

如果我在insertNode函数中注释掉整个else语句,我就会停止获取seg错误,但是当我打电话给它打印时,它会打印3个空行然后是0.任何帮助都会受到赞赏,如果它出现了,那么我没有使用类的原因是因为我不被允许,它必须以结构化数据类型完成。

#ifndef LINK_H
#define LINK_H

struct Node {
    char lastName[20];
    char firstName[20];
    int idNumber;
    Node *next;
};

int menu();
Node createNode();
Node insertNode(Node*);
Node searchNode(Node*, int);
Node deleteNode(Node*, int);
void printNode(Node*);

#endif

1 个答案:

答案 0 :(得分:0)

你的主要问题是你没有正确使用指针 - 你在指针和对象之间有一半的距离。 (我不打算解决这个事实,这是C ++中的C风格程序)

例如:您的createNode()方法返回Node个对象。它通过在堆上创建新的Node然后返回该节点的内容来实现此目的。这意味着你实际上有内存泄漏。没有人记得指向你放入堆中的节点的指针,因此它会泄漏。

我的第一步是让createNode()返回Node*

Node* createNode()
{
    Node *newNode = new Node;

    // All your cout/cin stuff

    return newNode;
}

我将使用与其他方法相同的模式 - 使用指针而不是对象。以searchNode()为例。因为您正在使用对象,所以您需要一个特殊的“未找到”节点。如果使用指针,则可以为“未找到的大小写”返回NULL(在C ++中优选为0)。您也不需要new和临时变量。

Node* searchNode(Node* list, int id)
{
    while(list != 0) {
        if (list->id == id) {
            return list;
        }
        // This only impacts the local list. It doesn't
        // permanently change the value of the list
        // passed in.
        list = list->next;
    }
    return 0;
}

对于searchNode()上的奖励积分:由于它是一个排序列表,您可以在找到大于您要查找的ID时立即停止搜索。

现在你已经完成了,你的插入方法有点简单:

  1. 从用户那里获取新节点
  2. 检查是否存在 - 如果是,请删除刚刚创建的节点并提供错误
  3. 如果没有,请搜索列表并找到插入新节点的位置。