如何实现链表并允许用户使用C+选择要删除的特定节点+;? 我花了一整天的时间来做这些,我知道指针和链表是什么,但是我不知道如何真正地编码它,我所发现的是java和C例子,因为我使用C++,所以它们没有帮助。 提前感谢您查看我的代码并帮助我,考虑到我为此花了多少天的时间感到紧张和困惑,我真的很感激。我不会说谎,我的deleteNode函数的大部分可能都是垃圾。但就像我说的,我只是迷路了,我甚至不知道从哪里开始,或者如何前进,因为我只理解概念

如何实现链表并允许用户使用C+选择要删除的特定节点+;? 我花了一整天的时间来做这些,我知道指针和链表是什么,但是我不知道如何真正地编码它,我所发现的是java和C例子,因为我使用C++,所以它们没有帮助。 提前感谢您查看我的代码并帮助我,考虑到我为此花了多少天的时间感到紧张和困惑,我真的很感激。我不会说谎,我的deleteNode函数的大部分可能都是垃圾。但就像我说的,我只是迷路了,我甚至不知道从哪里开始,或者如何前进,因为我只理解概念,c++,linked-list,C++,Linked List,这是我的数据文件 John Doe 80 Jane Smith 70 Bill Jones 50 Pat Hughes 90 Sam Sosa 40 这是我的头文件 #include<string> using namespace std; class student { public: student(); // constructor method void st_fn(string fn); string st_fn();

这是我的数据文件

John Doe    80
Jane Smith  70
Bill Jones  50
Pat  Hughes 90
Sam  Sosa   40
这是我的头文件

#include<string>
using namespace std;
class student {
public:
    student();   // constructor method
    void st_fn(string fn);
    string st_fn();
    void st_ln(string ln);
    string st_ln();
    void st_score(float s);
    float st_score();
    string st_pass_or_fail();
    // need a pointer to the next object
    student *nxt_ptr;
protected:   // protected can be inherited
    float m_score;
    string m_ln;
    string m_fn;
    string m_pf_msg;
};
student::student()  //constructor
{
    nxt_ptr = NULL;
    m_score = 0;
}
void student::st_fn(string fn)
{
    m_fn = fn;
}
string student::st_fn()
{
    return m_fn;
}
void student::st_ln(string ln)
{
    m_ln = ln;
}
string student::st_ln()
{
    return m_ln;
}
void student::st_score(float s)
{
    m_score = s;
}
float student::st_score()
{
    return m_score;
}
string student::st_pass_or_fail()
{
    if (m_score >= 60)
        m_pf_msg = "PASSED";
    else
        m_pf_msg = "FAILED";

    return m_pf_msg;
}
#包括
使用名称空间std;
班级学生{
公众:
student();//构造函数方法
无效st_fn(字符串fn);
字符串st_fn();
无效st_ln(字符串ln);
字符串st_ln();
无效st_分数(浮动s);
浮动st_分数();
字符串st_pass_或_fail();
//需要指向下一个对象的指针吗
学生*nxt\ptr;
protected://protected可以被继承
浮动m_分数;
字符串m_ln;
字符串m_fn;
字符串m_pf_msg;
};
student::student()//构造函数
{
nxt_ptr=NULL;
m_分数=0;
}
void student::st_fn(字符串fn)
{
m_fn=fn;
}
字符串student::st_fn()
{
返回m_fn;
}
无效学生::st_项次(字符串项次)
{
m_ln=ln;
}
字符串student::st_ln()
{
返回m_ln;
}
无效学生::st_分数(浮动s)
{
m_分数=s;
}
浮动学生::st_分数()
{
返回m_分数;
}
字符串student::st_pass_或_fail()
{
如果(m_分数>=60)
m_pf_msg=“通过”;
其他的
m_pf_msg=“失败”;
返回m_pf_msg;
}
这是我的.cpp文件

#include<iostream>
#include<string>
#include<fstream>
#include "Header.h"
using namespace std;
int display_menu()
{
    int option;

    cout << endl << endl;
    cout << "1. Display List" << endl;
    cout << "2. Add a student" << endl;
    cout << "3. Delete first student" << endl;
    cout << "4. Search by Last Name" << endl;
    cout << "5. Exit" << endl;
    cin >> option;

    return option;
}
student * search_last_name(student *h)
{
    student *f = NULL;
    student *t = NULL;
    string s_ln;

    // prompt for last name to search for
    cout << "Enter Last Name of the Student";
    cin >> s_ln;

    if (h != NULL)
    {
        t = h;
        while (t != NULL)
        {
            if (t->st_ln() == s_ln)
                f = t;   // found the last name so save t

            t = t->nxt_ptr;
        }
    }
    else
        cout << "List is empty" << endl;

    return f;

}
void add_student(student *&head)  // h is the head of the list
{
    student *new_st, *r;
    string fn, ln;
    float s;

    cout << "Enter new students first name, last name and score";
    cin >> fn >> ln >> s;

    // instantiate a new node, use new_st
    new_st = new student;
    new_st->st_fn(fn);
    new_st->st_ln(ln);
    new_st->st_score(s);

    if (head == NULL)
        head = new_st;
    else
    {
        // find the last node, use r for this
        // write code
        r = head;
        while (r->nxt_ptr != nullptr)
            r = r->nxt_ptr;

        // add to the back of the list
        // write code
        r->nxt_ptr = new_st;
    } // end of else
} // end of add student
student * delete_front(student * head)
{
    student *t;
    // delete front node
    // check for empty list

    if (head != NULL)
    {
        // delete first node
        t = head;
        head = head->nxt_ptr;
        delete(t);
    }
    else
        cout << "List is empty - nothing to delete" << endl;

    return head;
}
void deleteNode(struct Node *head, struct Node *n)
{
    // When node to be deleted is head node
    if (head == n)
    {
        n = head->next;

        //Remove the link of next node
        head->next = head->next->next;



        return;
    }

    //When not first node, folow the  normal deletion process

    //Find the previous node
    struct Node *prev = head;
    while (prev->next != NULL && prev->next != n)
        prev = prev->next;

    // Check if node really exists in Linked List
    if (prev->next == NULL)
    {
        cout << endl << "Given node is not present in Linked List";
        return;
    }

    //Remove node from linked list should it exist
    prev->next = prev->next->next;


    return;
}

void display_list(student *t)
{
    if (t == NULL)
        cout << "List is Empty!";
    else
    {

        while (t != NULL)
        {
            cout << "******Student******" << endl;
            cout << t->st_ln() << endl;
            cout << t->st_fn() << endl;
            cout << t->st_score() << endl << endl;
            t = t->nxt_ptr;
        }
    }
}
int main()
{
    string ln, fn;
    float s;
    int n;
    ifstream infile;
    student *head = NULL;   //pointer to a student object
    student *cp = NULL;     // pointer to end of the list
    student *new_st = NULL;  // pointer to a new student object
    student *f = NULL;      // pointer to found node

    int option;  // the numbe of menu item the user selects


    infile.open("lab8d.txt");

    while (!infile.eof())
    {
        infile >> fn >> ln >> s;
        //instantiate a student object
        new_st = new student;
        //load the object with data
        new_st->st_fn(fn);
        new_st->st_ln(ln);
        new_st->st_score(s);

        // check for empty list - its a special case
        if (head == NULL)
        {
            head = new_st;
            cp = new_st;
        }
        else   // list is not empty
        {
            cp->nxt_ptr = new_st;
            cp = new_st;
        }

    }  // end of loop

       // loop to give the user some options

    option = display_menu();

    while (option != 5)
    {
        if (option == 1)
            display_list(head);
        else if (option == 2)
            add_student(head);
        else if (option == 3)
            head = delete_front(head);
        else if (option == 4)
        {
            f = search_last_name(head);

            if (f != NULL)
            {
                cout << f->st_fn() << endl;
                cout << f->st_ln() << endl;
                cout << f->st_score() << endl;
                cout << f->st_pass_or_fail() << endl;
            }
            else
                cout << "Name not in the list" << endl;
        }
        else if (option == 6)
        {
            cout << "Enter the number of the node you would like to delete: " << endl;
            cin >> n;

        }

        option = display_menu();

    }


    system("pause");

    return 0;

}
#包括
#包括
#包括
#包括“Header.h”
使用名称空间std;
int显示菜单()
{
int选项;
cout nxt_ptr!=nullptr)
r=r->nxt\U ptr;
//添加到列表的后面
//编写代码
r->nxt\u ptr=新建;
}//其他的结束
}//添加学生的结束
学生*删除前(学生*头部)
{
学生*t;
//删除前端节点
//检查空列表
if(head!=NULL)
{
//删除第一个节点
t=头部;
头部=头部->nxt\U ptr;
删除(t);
}
其他的
cout next=头部->下一步->下一步;
返回;
}
//如果不是第一个节点,请执行正常的删除过程
//查找上一个节点
结构节点*prev=头部;
而(上一个->下一个!=NULL&&上一个->下一个!=n)
上一个=上一个->下一个;
//检查链接列表中是否确实存在节点
如果(上一个->下一个==NULL)
{
cout next->next;
返回;
}
无效显示列表(学生*t)
{
如果(t==NULL)
库特;
//实例化学生对象
新生=新生;
//用数据加载对象
新建->新界北(新界北);
新建一层->一层(层);
新评分->新评分;
//检查空列表-这是一种特殊情况
if(head==NULL)
{
头=新的头;
cp=新的;
}
else//列表不是空的
{
cp->nxt\u ptr=新建;
cp=新的;
}
}//循环结束
//循环为用户提供一些选项
选项=显示菜单();
while(选项!=5)
{
如果(选项==1)
显示列表(标题);
否则如果(选项==2)
增加学生(负责人);
否则如果(选项==3)
头部=删除前(头部);
否则如果(选项==4)
{
f=搜索姓氏(头);
如果(f!=NULL)
{

cout st_fn()函数越简单、自包含,测试和调试就越容易,失败的可能性就越小

尝试类似以下内容:

学生h:

#include <string>

class student_list;

class student
{
public:
    student();
    student(const std::string &fn, const std::string &ln, float s, student *nxt = NULL);

    void next(student *s);
    student* next() const;

    void firstName(const std::string &fn);
    std::string firstName() const;

    void lastName(const std::string &ln);
    std::string lastName() const;

    void score(float s);
    float score() const;

    std::string pass_or_fail() const;

    void display(bool showPassOrFail = false) const;

    friend class student_list;

protected:   // protected can be inherited
    student *m_next;
    std::string m_fn;
    std::string m_ln;
    float m_score;
};
#include <string>

class student_list;

class student
{
public:
    student();
    student(const std::string &fn, const std::string &ln, float s);

    void firstName(const std::string &fn);
    std::string firstName() const;

    void lastName(const std::string &ln);
    std::string lastName() const;

    void score(float s);
    float score() const;

    std::string pass_or_fail() const;

    void display(bool showPassOrFail = false) const;

    friend class student_list;

protected:   // protected can be inherited
    std::string m_fn;
    std::string m_ln;
    float m_score;
};
Student.cpp:

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

student::student()
    : m_next(NULL), m_score(0)
{
}

student::student(const std::string &fn, const std::string &ln, float s, student *nxt)
    : m_next(nxt), m_fn(fn), m_ln(ln), m_score(s)
{
}

void student::next(student *s)
{
    m_next = s;
}

student* student::next() const
{
    return m_next;
}

void student::firstName(const std::string &fn)
{
    m_fn = fn;
}

std::string student::firstName() const
{
    return m_fn;
}

void student::lastName(const std::string &ln)
{
    m_ln = ln;
}

std::string student::lastName() const
{
    return m_ln;
}

void student::score(float s)
{
    m_score = s;
}

float student::score() const
{
    return m_score;
}

std::string student::pass_or_fail() const
{
    if (m_score >= 60)
        return "PASSED";
    else
        return "FAILED";
}

void student::display(bool showPassOrFail) const
{
    std::cout << lastName() << std::endl;
    std::cout << firstName() << std::endl;
    std::cout << score() << std::endl;

    if (showPassOrFail)
        std::cout << pass_or_fail() << std::endl;
}
#include "Student.h"
#include <iostream>

student::student()
    : m_score(0)
{
}

student::student(const std::string &fn, const std::string &ln, float s)
    : m_fn(fn), m_ln(ln), m_score(s)
{
}

void student::firstName(const std::string &fn)
{
    m_fn = fn;
}

std::string student::firstName() const
{
    return m_fn;
}

void student::lastName(const std::string &ln)
{
    m_ln = ln;
}

std::string student::lastName() const
{
    return m_ln;
}

void student::score(float s)
{
    m_score = s;
}

float student::score() const
{
    return m_score;
}

std::string student::pass_or_fail() const
{
    if (m_score >= 60)
        return "PASSED";
    else
        return "FAILED";
}

void student::display(bool showPassOrFail) const
{
    std::cout << lastName() << std::endl;
    std::cout << firstName() << std::endl;
    std::cout << score() << std::endl;

    if (showPassOrFail)
        std::cout << pass_or_fail() << std::endl;
}
#包括“Student.h”
#包括
学生::学生()
:m_分数(0)
{
}
student::student(常量std::string&fn,常量std::string&ln,float s)
:m_fn(fn)、m_ln(ln)、m_分数
{
}
void student::firstName(const std::string&fn)
{
m_fn=fn;
}
std::string student::firstName()常量
{
返回m_fn;
}
void student::lastName(常量std::string&ln)
{
m_ln=ln;
}
std::string student::lastName()常量
{
返回m_ln;
}
无效学生::分数(浮动s)
{
m_分数=s;
}
float student::score()常量
{
返回m_分数;
}
std::string student::pass\u或\u fail()常量
{
如果(m_分数>=60)
返回“通过”;
其他的
返回“失败”;
}
无效学生::显示(bool showPassOrFail)常量
{

问题是不清楚要删除什么和应该保留什么。例如,如果要删除一个head,您将调用函数deleteNode(head,head).但head是指向列表的指针。如果调用此函数,指向列表的指针将不会更改,但head节点将消失。我的意思是,这是程序中的一个基本问题。还有许多其他问题。我建议开始考虑接口。我建议将链接列表逻辑与所有其他逻辑分离。这使sit更容易测试和调试链表。让链表正常工作,然后添加学生,最后添加菜单系统。同时注意(!infle.eof())这是一个非常常见的bug。在这里写得很好:正如安德烈所说,不清楚你想删除什么,何时知道。你知道如何删除一个节点,所以你需要决定哪一个,然后做什么。这个标准是什么?@肥乡是的,但是至少它会编译,并且是OP的起点,显然是C++。教基督教的蠢驴。
#include "Student.h"
#include "StudentList.h"

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <limits>
#include <cstdlib>

int read_number()
{
    int value;

    while (!(std::cin >> value))
    {
        std::cout << "Must be a number, try again: ";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

    return value;
}

int display_menu()
{
    int option;

    std::cout << std::endl << std::endl;

    std::cout << "1. Display List" << std::endl;
    std::cout << "2. Add a student" << std::endl;
    std::cout << "3. Delete first student" << std::endl;
    std::cout << "4. Search by Last Name" << std::endl;
    std::cout << "5. Delete student by index" << std::endl;
    std::cout << "6. Exit" << std::endl << std::endl;

    std::cout << "Choice: ";

    do
    {
        option = read_number();

        if ((option >= 1) && (option <= 6))
            break;

        std::cout << "Must be 1..6, try again: ";
    }
    while (true);

    return (option != 6) ? option : -1;
}

int main()
{
    student_list students;

    std::ifstream infile("lab8d.txt");
    if (infile.is_open())
    {
        student *cp = NULL;
        std::string ln, fn;
        float s;

        while (infile >> fn >> ln >> s)
            cp = students.insert(fn, ln, s, cp);

        infile.close();
    }

    // loop to give the user some options

    int option;  // the number of menu item the user selects

    while ((option = display_menu()) != -1)
    {
        switch (option)
        {
            case 1:
            {
                students.display();
                break;
            }

            case 2:
            {
                // prompt for student info
                std::string info;
                std::cout << "Enter new student's first name, last name, and score: ";

                std::string ln, fn;
                float s;

                if (std::cin >> fn >> ln >> s)
                    students.insert(fn, ln, s);

                break;
            }

            case 3:
            {
                if (!students.remove(0))
                    std::cout << "List is empty" << std::endl;

                break;
            }

            case 4:
            {
                // prompt for last name to search for
                std::string ln;
                std::cout << "Enter Last Name of the Student: ";

                if (std::cin >> ln)
                {
                    student *f = students.search_last_name(ln);
                    if (f)
                        f->display(true);
                    else
                        std::cout << "Name not found in List" << std::endl;
                }

                break;
            }

            case 5:
            {
                std::cout << "Enter the index of the Student to Delete: " << std::endl;
                int idx = read_number();

                if (!students.remove(idx))
                    std::cout << "Index not found in List" << std::endl;

                break;
            }
        }
    }

    std::system("pause");

    return 0;
}
#include <string>

class student_list;

class student
{
public:
    student();
    student(const std::string &fn, const std::string &ln, float s);

    void firstName(const std::string &fn);
    std::string firstName() const;

    void lastName(const std::string &ln);
    std::string lastName() const;

    void score(float s);
    float score() const;

    std::string pass_or_fail() const;

    void display(bool showPassOrFail = false) const;

    friend class student_list;

protected:   // protected can be inherited
    std::string m_fn;
    std::string m_ln;
    float m_score;
};
#include "Student.h"
#include <iostream>

student::student()
    : m_score(0)
{
}

student::student(const std::string &fn, const std::string &ln, float s)
    : m_fn(fn), m_ln(ln), m_score(s)
{
}

void student::firstName(const std::string &fn)
{
    m_fn = fn;
}

std::string student::firstName() const
{
    return m_fn;
}

void student::lastName(const std::string &ln)
{
    m_ln = ln;
}

std::string student::lastName() const
{
    return m_ln;
}

void student::score(float s)
{
    m_score = s;
}

float student::score() const
{
    return m_score;
}

std::string student::pass_or_fail() const
{
    if (m_score >= 60)
        return "PASSED";
    else
        return "FAILED";
}

void student::display(bool showPassOrFail) const
{
    std::cout << lastName() << std::endl;
    std::cout << firstName() << std::endl;
    std::cout << score() << std::endl;

    if (showPassOrFail)
        std::cout << pass_or_fail() << std::endl;
}
#include "Student.h"

#include <list>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <limits>
#include <algorithm>
#include <cstdlib>

int read_number()
{
    int value;

    while (!(std::cin >> value))
    {
        std::cout << "Must be a number, try again: ";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

    return value;
}

int display_menu()
{
    int option;

    std::cout << std::endl << std::endl;

    std::cout << "1. Display List" << std::endl;
    std::cout << "2. Add a student" << std::endl;
    std::cout << "3. Delete first student" << std::endl;
    std::cout << "4. Search by Last Name" << std::endl;
    std::cout << "5. Delete student by index" << std::endl;
    std::cout << "6. Exit" << std::endl << std::endl;

    std::cout << "Choice: ";

    do
    {
        option = read_number();

        if ((option >= 1) && (option <= 6))
            break;

        std::cout << "Must be 1..6, try again: ";
    }
    while (true);

    return (option != 6) ? option : -1;
}

int main()
{
    std::list<student> students;

    std::ifstream infile("lab8d.txt");
    if (infile.is_open())
    {
        std::string ln, fn;
        float s;

        while (infile >> fn >> ln >> s)
            students.push_back(student(fn, ln, s));

        infile.close();
    }

    // loop to give the user some options

    int option;  // the number of menu item the user selects

    while ((option = display_menu()) != -1)
    {
        switch (option)
        {
            case 1:
            {
                if (students.empty())
                    std::cout << "List is Empty!" << std::endl;
                else
                {
                    for(const auto &s : students)
                    {
                        std::cout << "******Student******" << std::endl;
                        s.display();
                    }
                }

                break;
            }

            case 2:
            {
                // prompt for student info
                std::string info;
                std::cout << "Enter new student's first name, last name, and score: ";

                std::string ln, fn;
                float s;

                if (std::cin >> fn >> ln >> s)
                    students.push_back(student(fn, ln, s));

                break;
            }

            case 3:
            {
                if (students.empty())
                    std::cout << "List is empty" << std::endl;
                else
                    students.erase(students.begin());

                break;
            }

            case 4:
            {
                // prompt for last name to search for
                std::string ln;
                std::cout << "Enter Last Name of the Student: ";

                if (std::cin >> ln)
                {
                    auto f = std::find(students.begin(), students.end(),
                        [&](const student &s){ return (s.lastName() == ln); }
                    );
                    if (f != students.end())
                        f->display(true);
                    else
                        std::cout << "Name not found in List" << std::endl;
                }

                break;
            }

            case 5:
            {
                std::cout << "Enter the index of the Student to Delete: " << std::endl;
                int idx = read_number();

                if ((idx < 0) || (idx >= students.size()))
                    std::cout << "Index not found in List" << std::endl;
                else
                    students.erase(std::next(students.begin(), idx));

                break;
            }
        }
    }

    std::system("pause");

    return 0;
}