如何在C ++中删除指针

时间:2018-12-03 03:50:38

标签: c++ pointers

我已经在编辑和编译程序两周了,但是每当我尝试删除一个指针时,它都会停止工作。删除包含输入的员工ID的指针时,请帮助我确定问题所在。我是c ++的新手,这使我发疯。

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

struct nameType
{
    string first, middle, last;

};

struct addressType
{
    string add1, add2, city, state;
    int zip;
};

struct dateType
{
    int month, day, year;

};

struct contactType
{
    int phone, cellphone, fax, pager;
    string email;
};

struct employeeType
{
    nameType name;
    string empID;
    addressType address;
    dateType hireDate;
    dateType quitDate;
    contactType contact;
    string deptID;
    double salary;

    struct employeeType *pnext, *prev;
};

employeeType *pc, *del, *head;

char choice;


void AddEmp()
{
    pc = new employeeType;
    del -> pnext = pc;
    pc -> prev = del;
    del = pc;
    pc -> pnext = NULL;



    cout << "\t\n\nNAME "<<endl;    
    cout<< "First: ";
    cin >> pc -> name.first;
    cout<< "Middle: ";
    cin >> pc -> name.middle;
    cout << "Last: ";
    cin >> pc -> name.last;

    cout <<"\nEmployee ID: ";
    cin >> pc -> empID;



    cout<<"\t\nEnter choice: [A] ADD, [D] DISPLAY, [X] DELETE, [E], EXIT: ";
    cin >> choice;  
    system ("cls");     

}

void dispEntry()
{
    pc = head -> pnext;

    do
    {

        cout <<"\t\t\t\t\t\n\nEMPLOYEE'S CONTACT DETAILS"<<endl<<endl;
        cout <<"["<<pc<<"]";

        cout <<"\t\t\t\nNAME"<<endl;
        cout << "\nName: " << pc -> name.first << "\nMiddle: "  << pc -> 
       name.middle << "\nLast: "<< pc -> name.last<< endl<<endl;

        cout << "Employee ID: "<<pc -> empID<<endl;


        pc = pc ->pnext;                


    }while (pc !=NULL);

    cout<<"\t\nEnter choice: [A] ADD, [D] DISPLAY, [X] DELETE, [E], EXIT: ";
    cin >> choice;  
}

HERE是删除指针的代码,但是它不起作用。

void delEntry ()
{

    string dataDel;
    system ("cls"); 
    cout <<" DELETE DATA"<< endl;
    cout<<"Enter Employee ID:";
    cin >> dataDel;



    while (dataDel != pc -> empID )
    {
        pc = pc -> pnext;       

    }

    del = pc;

    if(pc -> pnext = NULL)
    {
        pc = del -> prev;   
        pc ->pnext = NULL;      

        delete del;
        del = pc;


    }
    else 
    {
        pc = del -> prev;   
        pc -> pnext = del -> pnext;  
        del -> pnext -> prev = pc;


        delete del;
        del = pc;



    }

}


int main()
{
    char entryChoice;   
    pc = new employeeType;
    head = pc;
    del = pc;
    pc -> prev = pc -> pnext = NULL;




    cout << "\t\t\t\t\nTHIS IS A PROGRAM TO ENTER EMPLOYEE'S CONTACT 
    DETAILS."<<endl<<endl;


    cout << "\t\t\t\t\nEnter choice: [A] ADD, [D] DISPLAY, [X] DELETE, [E], 
    EXIT  "<<endl<<endl;
    cout << "\t\t\tEnter choice :";
    cin >> choice;
    system ("cls");


   while (choice == 'A', 'D', 'X', 'E')
   {

        switch(choice)
        {

            case 'A': 
                AddEmp();
                break;

            case 'D':
                dispEntry();
                break;

            case 'X':
                delEntry ();
                dispEntry();
                break;



        }
    }

}

1 个答案:

答案 0 :(得分:1)

声明

if (pc -> pnext = NULL) 

始终等于false,因为它等效于

if (NULL)

因此,下面的else块将始终使用,并且对于列表中的最后一个元素将始终失败,因为del -> pnextNULL和行

del -> pnext -> prev = pc;

引用NULLif语句必须改为

if (pc -> pnext == NULL) 

您还需要切换线路

del = pc;
pc -> pnext = NULL;

AddEmp()中,因为否则您的尾部指针del将具有未定义的pnext