【发布时间】:2015-02-20 02:36:55
【问题描述】:
在课堂上研究这个程序已经有几天了,我一直遇到段错误。我可以注释掉我认为导致它的代码部分,并且我不再遇到错误,但是我的打印功能不起作用。我明天拿起 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
【问题讨论】:
-
我所有的编程都是在 linux 的终端中完成的
-
I can comment out the part of code that I think is causing it and I stop getting the fault but then my print function doesn't work.好吧,你应该修复代码中不能正常工作的部分,而不是像没有伤害一样继续下去。 -
if (list == NULL) list->next = newNode;现在有一些高质量的编码! -
@John3136 完全超出了我的想象。我现在明白了,我可能应该在发帖之前买并阅读这本书
-
是的,你需要一本好书和/或更好的课程,像
Node *tmp = new Node; tmp = list;这样的东西确实会在两短行中泄漏内存。这段代码散布有逻辑缺陷。其中大部分看起来像 C 代码,有人扔了一些 C++ 并希望它会坚持下去。对不起,可怕的课程。