c ++链表问题

时间:2016-03-16 19:24:37

标签: c++ linked-list nodes

#include "node.h"
#include <iostream>

// List class
class List 
{
    node *head; // head is an object that stores the address of the first node
  public:
    // constructor that initializes every list to null
    List() 
    { 
        head = NULL; 
    }
    // prtototype of the list member functions 
    void Print();
    void Insert(float sal, int en);
    void Delete(float sal, int en);
};

// linklist.h上面

#include "linklist.h"
#include <iostream>

using std::cout;
using std::cin;
using std::endl;


/**
 * Append a node to the linked list
 */
void List::Insert(float sal, int en) 
{

    // Create a new node
    node* newNode = new node();
    newNode->SetData(sal, en);
    newNode->setNext(NULL);

    // Create a temp pointer
    node *tmp = head;

    if ( tmp != NULL ) 
    {
        // Nodes already present in the list
        // Parse to end of list
        /*while ( tmp->Next() != NULL ) 
        {
            tmp = tmp->Next();
        }*/

        // Point the last node to the new node
        tmp->setNext(head);
    }
    else 
    {
        // First node in the list
        head = newNode;
    }
}

/**
 * Delete a node from the list
 */
void List::Delete(float salary, int data) 
{
    // Create a temp pointer
    node *tmp = head;

    // No nodes
    if ( tmp == NULL )
    return;

    // Last node of the list
    if ( tmp->Next() == NULL ) 
    {
        delete tmp;
        head = NULL;
    }
    else 
    {
        // Parse thru the nodes
        node *prev;
        do 
        {
            if ( tmp->Epnum() == data && tmp->Salary()== salary ) 
                break;
            prev = tmp;
            tmp = tmp->Next();
        } while ( tmp != NULL );

        // Adjust the pointers
        prev->setNext(tmp->Next());

        // Delete the current node
        delete tmp;
    }
}

/**
 * Print the contents of the list
 */
void List::Print()
{
    // Temp pointer
    node *tmp = head;

    // No nodes
    if ( tmp == NULL ) 
    {
        cout << "EMPTY" << endl;
        return;
    }

    // One node in the list
    if ( tmp->Next() == NULL ) 
    {
        cout << tmp->Salary() + tmp->Epnum();
        cout << " --> ";
        cout << "NULL" << endl;
    }
    else 
    {
        // Parse and print the list
        do 
        {
            cout << tmp->Epnum();
            cout << " --> ";
            tmp = tmp->Next();
        }
        while ( tmp != NULL );

        cout << "NULL" << endl;
    }
}

//上面的linlist.cpp

#include <iostream>
#include <cstdlib>
#include "linklist.h"

using namespace std;

void menu(List &);

int main()
{
    // New list
    List list;
    menu(list); 
    return 0;
}
void menu(List &list)
{   char choice;
    int item;
    float salary;
    do{ 
        system("CLS"); // use #include <cstdlib>
        cout << "\t\t\tMain Menu\n\n";
        cout << "\tInsert{A}\n";
        cout << "\tDelete\n";
        cout << "\tPrint{P}\n";
        cout << "\tExit\n";
        cout << "\t\t What? ";cin >>choice;
        choice = toupper(choice);
        cin.ignore();
        switch (choice)
        { case 'A':
            cout << "Enter Employee numbers to insert and salary : "; cin >> item; cin>>salary; 
            list.Insert(salary, item);
            cout << item<< " Inserted \n"; cin.get();
            break;
          /*case 'D':
            cout << "Enter Item to Delete : "; cin >> item; 
            list.Delete(item);
            cout << item<< " Deleted\n";cin.get();
            break;*/
          case 'P':
            list.Print();cin.get();
            break;
        } 
    }while (choice != 'E');
}

// main.cpp

//node.h
//#ifndef NODE_H
#define NODE_H

//node class
class node {
    int epnum;
    float salary;
    node* next;

    public:
    node()
    {} //null constructor

    //stores argument passed in func.
    void SetData(float _salary, int _epnum){
        salary = _salary;
        epnum = _epnum;
    }
    //stores in next the address of the next node
    void setNext (node* anext){
        next = anext;
    }
    //returns epnum stored
    int Epnum(){
        return epnum;
    }
    float Salary(){
        return salary;}
    //returns addres of next node 
    node* Next(){
        return next;
    }
};

//上面的node.h

我需要创建一个链接列表,将列表前面的节点作为程序插入,然后将其打印出来。由于某种原因,我无法在列表的前面插入节点,并且在尝试打印时遇到了infinte循环。它做了一些事情,但我不确切知道是什么。请帮忙。

3 个答案:

答案 0 :(得分:0)

在插入方法中,当您有1个元素时,tmp设置为head,然后tmp->setNext(head);将创建对自身的引用。这是打印方法中无限循环的原因。请尝试使用以下插入代码。

void List::Insert(float sal, int en) 
{
    // Create a new node
    node* newNode = new node();
    newNode->SetData(sal, en);
    newNode->setNext(head);
    head = newNode;
}

我还要注意,在你的打印方法中,列表中没有一个带有1个元素的边角情况。你的循环将完美地处理这种情况。如果省略列表分支中的一个节点,则会得到相同的结果。

void List::Print()
{
    // Temp pointer
    node *tmp = head;

    // No nodes
    if ( tmp == NULL ) 
    {
        cout << "EMPTY" << endl;
        return;
    }

    // Parse and print the list
    do 
    {
        cout << tmp->Epnum();
        cout << " --> ";
        tmp = tmp->Next();
    }
    while ( tmp != NULL );

    cout << "NULL" << endl;
}

答案 1 :(得分:0)

List::Insert中,您有:

node *tmp = head;

接着是

tmp->next = head;

因此,您在对象中有循环链接。它的next指向它自己。这导致打印功能无限循环。

您需要的是非常简单的:

void List::Insert(float sal, int en) 
{
    // Create a new node
    node* newNode = new node();
    newNode->SetData(sal, en);
    newNode->setNext(head);
    head = newNode;
}

答案 2 :(得分:0)

void List::Insert(float sal, int en) 
{

    // Create a new node
    node* newNode = new node();
    newNode->SetData(sal, en);
    newNode->setNext(NULL);

    //set the newNode next to point to head 
    newNode->setNext(head);
    //set the new head as the newNode
    head = newNode;


    // Create a temp pointer
    //node *tmp = head;

    /*if ( tmp != NULL ) 
    {
        // Nodes already present in the list
        // Parse to end of list
        /*while ( tmp->Next() != NULL ) 
        {
            tmp = tmp->Next();
        }

        // Point the last node to the new node
        tmp->setNext(head);
    }
    else 
    {
        // First node in the list
        head = newNode;
    }*/
}

/**
 * Delete a node from the list
 */
void List::Delete(float salary, int data) 
{
    // Create a temp pointer
    node *tmp = head;

    // No nodes
    if ( tmp == NULL )
    return;

    // Last node of the list
    if ( tmp->Next() == NULL ) 
    {
        delete tmp;
        head = NULL;
    }
    else 
    {
        // Parse thru the nodes
        node *prev;
        do 
        {
            if ( tmp->Epnum() == data && tmp->Salary()== salary ) 
                break;
            prev = tmp;
            tmp = tmp->Next();
        } while ( tmp != NULL );

        // Adjust the pointers
        prev->setNext(tmp->Next());

        // Delete the current node
        delete tmp;
    }
}

/**
 * Print the contents of the list
 */
void List::Print()
{
    // Temp pointer
    node *tmp = head;

    // No nodes
    if ( tmp == NULL ) 
    {
        cout << "EMPTY" << endl;
        return;
    }

    // One node in the list
    if ( tmp->Next() == NULL ) 
    {
        cout << tmp->Salary() + tmp->Epnum();
        cout << " --> ";
        cout << "NULL" << endl;
    }
    else 
    {
        // Parse and print the list
        do 
        {
            cout << tmp->Epnum();
            cout << " --> ";
            tmp = tmp->Next();
        }
        while ( tmp != NULL );

        cout << "NULL" << endl;
    }
}
当我写这段代码并且意识到我做错了什么时,我很累。