删除函数的单链表问题以及通过strcmp C / C ++进行排序

问题描述 投票:0回答:1

我的单链表由char *id; char firstname[15]; char lastname[15]; struct rec *next;组成。

因此,我在此作业中遇到的主要问题是通过将ID传递给删除功能来删除记录。它将删除ID但第二个它到达行删除firstname或lastname它会抛出此错误。

"ChristmasList.exe has triggered a breakpoint. occurred"

我的第二个主要错误是我的strcmp总是比较相同的字符串,所以它总是返回0,我也是一个小小的关于如何做到这一点,所以即使它返回-1或1.我不知道它是否会排序正确。

感谢能够提供帮助的任何人,我长期坚持这个问题并需要在我们下一个实验室分配之前修复它,因为我们正在添加此代码。

主要:

// ChristmasList.cpp : Adds, Deletes, and prints items in a structured list
// MyName

#include "stdafx.h"
#include "list.h"
#include<iostream>
#include<cstring>
using namespace std;



int main()
{
    rec myRec; // pointer to rec structure
    int choice = 1; // initializing so choice is not 0 to start
    int order; // ascending/descending input
    char buf[500]; // buffer array
    int result; //result of function calls
    while (choice != 0)
    {
        cout << "Enter 1 to Add an Item.\n";
        cout << "Enter 2 to delete  a record.\n";
        cout << "Enter 3 to Print a list of records.\n";
        cout << "Enter 0 to exit the program\n";
        cin >> choice; // User input for choice
        switch (choice)
        {
        case 1: //Add
            cout << "Enter your ID: "; //ask to enter ID
            cin >> buf; //Input for ID into rec
            myRec.id = buf;
            cout << "Enter your first name: "; // Ask for first name
            cin >> myRec.firstname; // Input first name into rec
            cout << "Enter your last name: "; // Ask for last name
            cin >> myRec.lastname; // Input last name into rec
            if (AddItem(myRec)) // Call Add
            {
                cout << "Success\n";
            }
            else
            {
                cout << "\nFailed to add :(\n";
            }
                break;

        case 2: //Delete
            cout << "Enter the ID of the record you want to delete: "; // ask to enter ID that wants to be deleted
            cin >> buf; // Input id wanting to be deleted
            result = DeleteItem(buf); // Call delete
                break;
        case 3: //Print
            cout << "Enter 0 for ascending order and 1 for descending order: "; // ask for ascending or descending
            cin >> order; // User input for ascending or descending
            PrintList(order); // Call Print
            break;
        case 0:
            break;
        }
    }
    return 0;
}

list.cpp:

#include "stdafx.h"
#include "list.h"
#include<iostream>
#include<cstring>
using namespace std;

rec *first = NULL;
rec *last = NULL;
int counter = 0;
rec *MyNewRec2[500];

int AddItem(rec r)
{
    int comp;
    int comp2;
    rec *MyNewRec;
    MyNewRec = new rec;
    MyNewRec->id = new char[10];
    strcpy_s(MyNewRec->id, strlen(r.id) + 1, r.id); // Copying ID
    strcpy_s(MyNewRec->firstname, strlen(r.firstname) + 1, r.firstname);// Copying first name 
    strcpy_s(MyNewRec->lastname, strlen(r.lastname) + 1, r.lastname);// Copying last name

    while (first != NULL)
    {
        if (*MyNewRec->id == *first->id)
        {
            cout << "Duplicate ID: " << MyNewRec->id << endl;
            return 0;
        }
        else
        {
            MyNewRec2[counter] = MyNewRec;
            last = MyNewRec2[counter];
            first = first->next;
            counter++;
            return 1;
        }
    }
    if (first == NULL) // If no items are in the list
    {
        MyNewRec->next = NULL;
        first = MyNewRec;
        last = MyNewRec;
        MyNewRec2[counter] = MyNewRec;
        counter++;
    }
    if (r.lastname >= "97")
    {
        char *temp;
        temp = r.lastname - 32;
    }
    comp = strcmp(MyNewRec->lastname, (first->lastname - 1));
    comp2 = strcmp(MyNewRec->lastname, (last->lastname - 1));
    if (comp == -1 && comp <= 0) //Move first back in array
    {
        MyNewRec2[counter - 1] = NULL;
        MyNewRec2[counter - 1] = MyNewRec;
        MyNewRec->next = first;
        first = MyNewRec;
        return 1;
    }
    else if (comp2 == 1 && comp2 >= 0) //Move last back in array
    {
        MyNewRec->next = NULL;
        last->next = last;
        MyNewRec2[counter -1] = last;
        last = MyNewRec;
        return 1;
    }
    else
    {
        while (first->next != NULL)
        {
            comp = strcmp(MyNewRec->lastname, first->lastname);
            if (comp == 1)
            {
                first = first->next;
                return 1;
            }
            else if (comp == 1 || comp == 0)
            {
                MyNewRec2[counter - 1] = first;
                MyNewRec->next = first->next;
                first->next = MyNewRec;
                return 1;
            }
        }
    }
    return 1;
    }
    int DeleteItem(char *delid)
    {
    rec *MyNewRec, *temp;
    if (first == NULL)
    {
        return 0;
    }
    while (first != NULL)
    {
        if (*first->id == *delid)
        {
            MyNewRec = first->next;
            //temp = MyNewRec2[counter - 1]; // Random try
            //temp->next = MyNewRec; // Random try
            delete[] first->id;
            delete[] first->firstname;
            delete[] first->lastname;
            delete first;
            counter--;
            return 1;
        }
        else
        {
            first = first->next;
        }
    }
    return 0;
    }
    void PrintList(int order)
    {
    rec *temp = new rec;
    if (order == 0)
    {
        int i = 0;
        temp = MyNewRec2[i];
        while (temp != NULL)
        {
            cout.flush() << temp->id << " ";
            cout.flush() << temp->firstname << " ";
            cout.flush() << temp->lastname << " " << endl;
            i++;
            temp = MyNewRec2[i];
        }
    }
    temp = MyNewRec2[counter - 1];
    if (order == 1)
    {
        while (temp != NULL)
        {
            cout.flush() << temp->id << " ";
            cout.flush() << temp->firstname << " ";
            cout.flush() << temp->lastname << " " << endl;
            counter--;
            temp = MyNewRec2[counter - 1];
        }
    }
}

list.h:

struct rec
{
    char *id;
    char firstname[15];
    char lastname[15];
    struct rec *next;
};

int AddItem(rec r);
int DeleteItem(char *delid);
void PrintList(int order);
c++ visual-studio function sorting singly-linked-list
1个回答
0
投票

这是你的结构:

struct rec
{
    char *id;
    char firstname[15];
    char lastname[15];
    struct rec *next;
};

这是您为新记录分配内存的方式:

MyNewRec = new rec;
MyNewRec->id = new char[10];

这就是你解除分配的方式。

        delete[] first->id;
        delete[] first->firstname;
        delete[] first->lastname;

firstnamelastname成员不是你分配的指针。它们是通过new分配rec时分配的固定数组。或换句话说,如果你没有new它,那么你没有delete它。

删除这两行:

        delete[] first->firstname;
        delete[] first->lastname;

我没有时间调试其余的代码,但我会说这个。这下一行看起来很奇怪和不正确:

comp = strcmp(MyNewRec->lastname, (first->lastname - 1));
comp2 = strcmp(MyNewRec->lastname, (last->lastname - 1));

我没有得到-1的东西。但我得跑......

© www.soinside.com 2019 - 2024. All rights reserved.