我无法让我的链表发挥作用

时间:2013-10-20 22:49:05

标签: c++ singly-linked-list

当我调用函数Get_Keywords时,头指针总是重置为 来自main函数的最后一个输入值。我无法弄清楚。一世 在另一个测试文件中复制了链表逻辑,它只执行了 精细。我还尝试了另一个类驱动文件中的类似链表 从main调用。这个测试的唯一区别是没有 值从主要传递给类。

我必须以这种方式抽象程序,因为它是一项家庭作业。

#include "emaillist.h"
using namespace std;

email::email()  //Default constructor
{
    head = NULL;    
    tail = temp = current = head;

    kwhead = NULL;
    kwtail = NULL;
    kwcurrent = NULL;
    kwreference = NULL;
}

email::~email() //Destructor
{
    if(head)
    {
        delete head->emailm.date;
        delete head->emailm.time;
        delete head->emailm.from_address;
        delete head->emailm.to_address;
        delete head->emailm.subject_title;
        delete head->emailm.body;

        delete head;
        delete kwhead;
    }

    head = NULL;
    tail = NULL;
    temp = NULL;
    current = NULL;

    kwhead = NULL;
    kwtail = NULL;
    kwcurrent = NULL;
    kwreference = NULL;
}

int email::Load_from_file(char emailfilename[])
{
    head = new email_node;
    head->emailm.date = new char[50];
    head->emailm.time = new char[50];
    head->emailm.from_address = new char[100];
    head->emailm.to_address = new char[100];
    head->emailm.subject_title = new char[100];
    head->emailm.body = new char [300];

    return 0;
}

void email::Get_keywords(char *&userkwptr)
{
    kwnode *kwtemp = 0;
    kwtemp = new kwnode;
    kwtemp->keywordptr = new char[76];
    kwtemp->keywordptr = userkwptr;
    kwtemp->next = 0;

    cout << userkwptr << endl;

    if(!kwhead)
    {
        cout << "there was no kwhead." << endl;
        kwhead = kwtemp;
        kwcurrent = kwtemp;
        kwtail = kwtemp;

        cout << "headpointer value only" << kwhead->keywordptr << endl;
    }
    else
    {
        cout << "there was a kwhead." << endl;
        kwcurrent->keywordptr = userkwptr;
        kwcurrent->next = kwtemp;
        kwcurrent = kwtemp;
        kwtail = kwtemp;
    }
    cout << "The head pointer value is what?    " << kwhead->keywordptr << endl;
    cout << "the keyword pointer is this  " << kwcurrent->keywordptr << endl;

    void email::Display_keywords()
    {

        cout << "testing 1, 2, 3, testing   " << kwhead->keywordptr << endl;
        kwcurrent = kwhead;
        while(kwcurrent->next != NULL)
        {
            cout << kwcurrent->keywordptr << endl;
            kwcurrent = kwcurrent->next;
        }

        kwcurrent = kwhead;
        return;
    }

    int email::Find_junk_email()
    {
        return 0;
    }

    #include <iostream>
    #include <cstring>
    #include <cctype>
    #include <fstream>
    using namespace std;

    struct email_data
    {
        char *date;
        char *time;
        char *from_address;
        char *to_address;
        char *subject_title;
        char *body;
    };

    struct email_node
    {
        email_data emailm;
        email_node *next;
    };

    struct kwnode
    {
        char *keywordptr;
        kwnode *next;
    };

    class email
    {
    public:
        email();
        ~email();
        int Load_from_file(char emailfilename[]);
        void Get_keywords(char *&userkwptr);
        void Display_keywords();
        int Find_junk_email();


    private:
        email_node *head;
        email_node *tail;
        email_node *temp;
        email_node *current;

        kwnode *kwhead;
        kwnode *kwtail;
        kwnode *kwcurrent;
        kwnode *kwreference;
    };

    #include "emaillist.h"
    using namespace std;

    void Welcome_mess();
    int Menu();
    void Read_all_emails();


    void Welcome_mess()
    {

        cout << "The purpose of this program is to have the user entere in a list" << endl;
        cout << "of words to use as search items to a body of emails stored in a " << endl;
        cout << "separate file.  The program will then sort the words the user wants" << endl;

        cout << "which contain the users keywords" << endl << endl;
    }

    int Menu( email &firstsearch)
    {
        return 0;
    }


    int main()
    {
        email();
        email firstsearch;

        char *userkwptr;
        userkwptr = new char[76];

        char *filename;
        filename = new char[76];
        filename = "emails.txt";

        Welcome_mess();

        int menuchoice = 13;
        int wordlength = 0;


        while(menuchoice != 6)
        {
            cout << "Menu" << endl << endl;
            cout << "Please enter one of the corresponding chocies." << endl << endl;

            cout << "1 = Enter Keywords" << endl;
            cout << "2 = Display Keywords." << endl;
            cout << "3 = Search for junk e mails." << endl;
            cout << "4 = LOAD all of the junk e mails and display them   
                in sequential order by date." << endl;
            cout << "6 = Exit the program" << endl;

            cin >> menuchoice;
            cout << endl;

            switch(menuchoice)
            {       
                case 1:
                    cout << "Please enter your first keyword." << endl;
                    cin.ignore();
                    cin.getline(userkwptr, 75, '\n');   
                    firstsearch.Get_keywords(userkwptr);
                    wordlength = strlen(userkwptr);

                    while(wordlength != 0)
                    {
                        cout << "Please enter your next keyword." << endl;
                        cin.getline(userkwptr, 75, '\n');   
                        firstsearch.Get_keywords(userkwptr);
                        wordlength = strlen(userkwptr);
                    }

                    menuchoice = 0;
                    break;
                case 2:
                    firstsearch.Display_keywords();
                    menuchoice = 0;
                    break;
                case 3:

                    menuchoice = 0;
                    break;
                case 4:

                    menuchoice = 0;
                    break;
                case 6:
                    cout << "You have successfully exited the Menu." << endl;
                    menuchoice = 6;
                    return 0;
                    break;
                default:
                    cout << "There was an error with the menu" << endl;
                    cout << "Please see the tutor."  << endl;

                    return menuchoice;
                    break;
            }   
        }



        cout << endl << endl;

        system("PAUSE");

        return 0;
    }

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

据我所知,你第一次进入Get_keywords函数,没有头,所以if的真正块被执行,你创建的新临时节点被分配给kwhead,但是我没有看到它的下一个被分配了一个非0的值。这意味着你有一个与其头节点分开的链表。您是否调试过代码并查看了创建的列表?