Node类构造函数实现

时间:2013-09-26 03:24:53

标签: c++ linked-list nodes

我目前有这个代码用于我的节点类,它存储两个名称,一个ID号,并具有Next功能:

class Node{
public:
  char LastName[20] ;
  char FirstName[20] ;
  int IDnumber ;
  Node *Next ;

 Node();
 void printNode();
};

这是我用来从键盘初始化节点变量的构造函数:

Node::Node(){

cout << "Enter ID number: " << endl;
cin >> IDnumber;
cout << "Enter last name: " << endl;
cin >> LastName;
cout << "Enter first name: " << endl;
cin >> FirstName;
Next=NULL;
}

void Node::printNode(){
cout << "ID number: " << IDnumber << endl;
cout << "Last name: "  << LastName <<endl;
cout << "First name: "  << FirstName << endl;
}

我遇到的问题是,无论何时我在代码中调用printNode()函数,我的代码都无法执行printNode()函数的第一行。 (未处理的异常)当我尝试使用单独的linkedlist类调用node-&gt; Next时,我也无法执行此代码。这让我相信我没有正确构建节点。关于我的代码中可能出错的任何想法?

链表是一个单独的类,它使用我在上面发布的节点类。

class LinkedList{

private:
Node* list;
Node* createNode();
Node* searchLocation(int);

public:

LinkedList();
~LinkedList();

void InsertNode();
void SearchNode();
void PrintList();
void DeleteNode(int);

};


LinkedList::LinkedList(){
Node* list = NULL;
}

Node* LinkedList::createNode(){
Node *NewNode = new Node();
return NewNode;
}

void LinkedList::InsertNode(){
Node* insert = createNode();
if (list == NULL){
    list = insert;}}

void LinkedList::PrintList(){
Node* temp = list;
while (temp != NULL){
temp->printNode();
temp = temp->Next;
}
}

我的LinkedList类的PrintList()函数在list-&gt; printNode()(cout&lt;&lt;&lt; IDnumber行)中断时失败,并且在list = list-&gt;下一行失败。

int main(){

int num = 0;
    LinkedList list;

  int menu=0;
  while(menu != 5){

  cout << endl << "Choose a menu option." <<endl
   << "1. Insert node " << endl << "2. Delete node " << endl
   << "3. Print list" << endl << "4. Search a node & print info" << endl
   << "5. Quit program  " << endl;


  cin >>  menu;
   menu = validate(menu);

 switch(menu){
 case 1: 
     list.InsertNode();

 break;

case 3: 
    list.PrintList();

 break;
 }}

return 0;

  }

1 个答案:

答案 0 :(得分:1)

您的代码中的错误很少。最重要的是,当您引用一些常见的list即静态变量时,您会引用始终为NULL的本地Node指针。

在这里你可以找到工作解决方案,请为正确的列表释放添加正文

~LinkedList(){}

你没事:

链表:

class LinkedList{

private:
static Node* list;
Node* createNode();
Node* searchLocation(int);

public:
LinkedList();
~LinkedList(){}
void InsertNode();
void SearchNode();
void PrintList();
void DeleteNode(int);
};

Node* LinkedList::list = NULL;
                 ^
     don't foget to initialize pointer to static object

LinkedList::LinkedList(){
Node* list = NULL;
}

Node* LinkedList::createNode(){
Node *NewNode = new Node();
return NewNode;
}

void LinkedList::InsertNode(){
Node* insert = createNode();
if(list==NULL)list=insert;
else
    list->Next = insert;
}

void LinkedList::PrintList(){
Node* temp = list;
while (temp != NULL){
temp->printNode();
temp = temp->Next;
}
}

和主要:

int main(){

int num = 0;
   LinkedList list;

   cout << endl << "Choose a menu option." <<endl
   << "1. Insert node " << endl << "2. Delete node " << endl
   << "3. Print list" << endl << "4. Search a node & print info" << endl
   << "5. Quit program  " << endl;

   list.InsertNode();
   list.InsertNode();

   list.PrintList();

return 0;

}

输出: 选择菜单选项。 1.插入节点 2.删除节点 3.打印列表 4.搜索节点&amp;打印信息 5.退出程序
输入身份证号码: 8 输入姓氏: 一世 输入名字: Ĵ 输入身份证号码: 9 输入姓氏: ķ 输入名字: 升

身份证号码:8

姓氏:我

名字:j

身份证号码:9

姓氏:k

名字:l

RUN SUCCESSFUL(总时间:14s)

相关问题