带有对象类的C ++链表

时间:2016-09-25 10:37:16

标签: c++ singly-linked-list

我正在尝试使用对象类实现链接列表。以下是我的实施。

Prof头文件:

#ifndef PROF_H
#define PROF_H

#include <iostream>
#include <string>
using namespace std;

class Prof
{
public:
    string first_name;
    string last_name;
    int room_number;
    string phone_number;
};

#endif /* PROF_H */

LinkedList头文件:

#ifndef LinkedList_h
#define LinkedList_h

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

struct Node {
        Prof prof;
        Node *next;
    };


class LinkedList{   

private:
    Node * head;
    int length;

    // public members
public:
    // Default Constructor 
    LinkedList();

    bool insertProfessor( Node * newNode, int position );


    bool removeProfessor( int position );

    void printProfessors();

    void sortProfessors();

    void searchProfessors(string name);

    bool insert( Node * newNode);

    // Destructor
    ~LinkedList();
};

#endif

LinkedList类:

#include "LinkedList.h"
#include "Prof.h"

// Default Constructor creates the head node.
LinkedList::LinkedList()
{
    head -> prof;
    head -> next=NULL;
    length = 0;
}

bool LinkedList::insertProfessor( Node * newNode, int position )
{
    if ((position <= 0) || (position > length + 1))
    {
        cout << "Error: No such position\n";
        return false;
    }
    if (head -> next == NULL) 
    {
        head -> next = newNode;
        length++;
        return true;
    } 
    int count = 0;
    Node * p = head;
    Node * q = head;
    while (q)
    { 
        if (count == position)
        {
            p -> next = newNode;
            newNode -> next = q;
            length++;
            return true;
        }
        p = q;
        q = p -> next;
        count++;
    }
    if (count == position)
    {
        p -> next = newNode;
        newNode -> next = q;
        length++;
        return true;
    }
    cout << "Error: The professor was not added to the list.\n";
    return false;
}


void LinkedList::sortProfessors()
{
    Node * p = head;
    Node * q = head;
    Node * k = head;
    Node * m = head;
    int count = 0;

    while(count<length){
        while (q)
        {
            p = q;
            k = p -> next;
            m = k -> next;
            if(p -> prof.first_name > k->prof.first_name){
                p->next = m;
                k->next = p;
            } else if(p -> prof.first_name == k->prof.first_name){
              if(p -> prof.last_name > k->prof.last_name){
                 p->next = m;
                k->next = p;
            } 
            }
            q = p -> next;
        }
        count++;
    }
}

void LinkedList::searchProfessors(string name){
    bool found; 
    Node * p = head;
    Node * q = head;
    cout << "Professors";
    cout << "\n---------------------------\n";
    while (!found)
    {
        p = q;

        if((p->prof.first_name==name) || (p->prof.last_name==name)){
            cout << "Found!!\n"<< endl;
            cout << "\t" << p -> prof.first_name<<" "<<p -> prof.last_name << endl;
            cout << "\tPhone number: " << p -> prof.phone_number << endl;
            cout << "\tRoom Number: "<< p -> prof.room_number << endl;
            cout << "\n";
            found = true;
        }
        q = p -> next;
    }
}


bool LinkedList::removeProfessor( int position )
{
    if ((position <= 0) || (position > length + 1))
    {
        cout << "Error: No such position.\n";
        return false;
    }
    if (head -> next == NULL)
    {
       cout << "Error: The list is empty.\n";
       return false;
    }
    int count = 0;
    Node * p = head;
    Node * q = head;
    while (q) 
    {
        if (count == position)
        {
            p -> next = q -> next;
            delete q;
            length--;
            return true;
        }
        p = q;
        q = p -> next;
        count++;
    }
    cout << "nError: No one was removed.\n";
    return false;
}

void LinkedList::printProfessors()
{
    Node * p = head;
    Node * q = head;
    cout << "Professors";
    cout << "\n---------------------------\n";
    while (q)
    {
        p = q;
        cout << "\t" << p -> prof.first_name<<" "<<p -> prof.last_name << endl;
        cout << "\tPhone number: " << p -> prof.phone_number << endl;
        cout << "\tRoom Number: "<< p -> prof.room_number << endl;
        cout << "\n"<<endl;
        q = p -> next;
    }
}


bool LinkedList::insert( Node * newNode){
    while(false){
    if (head -> next == NULL) 
    {
        head -> next = newNode;
        length++;
        return true;
    } else {
        Node * next = head -> next;
        head=next;
        false;
    } 
    }

}

LinkedList::~LinkedList() 
{
    Node * p = head;
    Node * q = head;
    while (q)
    {
        p = q;
        q = p -> next;
        if (q) delete p;
    }
}

主文件:

#include <cstdlib>
#include <string>
#include <iostream>
#include "LinkedList.h"
#include "Prof.h"

using namespace std;

LinkedList professorsList;

int main()
{
    LinkedList professorsList;
    int choice=0;
    cout<<"Please select your choice.\n 1. Insert.\n 2.Search.\n 3.Delete.\n 4.Exit."<<endl;
    cin >> choice;

    string keyword="";
     int position;

    switch(choice){
        case 1:
            cout<<"Please select you source of input.\n 1.Console.\n 2.File.\n";
            cin >> choice;

            break;

        case 2:

            cout<<"Please the name you would like to search.\n";
            cin >> keyword;
            professorsList.searchProfessors(keyword);
            break;

        case 3:

            cout<<"Please the position of the professor you would like to delete.\n";
            cin >> position;
            professorsList.removeProfessor(position);
            return 0;
            break;

        case 4:
            exit(0);
            return 0;
            break;
    }

    return 0;
}

void getInput(int choice){
    string first_name;
            string last_name;
            string phonenumber;
            int room_number;
            string filename;
            Node * professor = new Node;
    switch(choice){
        case 1:


            cout<<"Please enter the professor's first name.\n";
            cin >> first_name;
            cout<<"Please enter the professor's last name.\n";
            cin >> last_name;

            cout<<"Please enter the professor's phone number.\n";
            cin >> phonenumber;

            cout<<"Please enter the professor's room number.\n";
            cin >> room_number;


            professor->prof.first_name = first_name;
            professor->prof.last_name = last_name;
            professor->prof.phone_number = phonenumber;
            professor->prof.room_number = room_number;
            professorsList.insert(professor);
            break;
        case 2:
            cout<< "Enter the file name.\n"<<endl;
            cin>>filename;
            break;
    }
}

当我尝试运行程序时,出现以下错误。

RUN FINISHED; Segmentation fault; core dumped; real time: 170ms; user: 0ms; system: 0ms

请帮我解决这个错误。

0 个答案:

没有答案