链表打印出错怎么办#include <iostream>

链表打印出错怎么办?为什么插入元素的时候,插到第四个元素之后就只能打三个数据

#include
#include
using namespace std;

typedef struct student {
    int    num; // 学号
    char    name[10]; // 姓名 
    student* pre;
    student* next;
} student;
student* head = NULL;

void Insert(int x, char *n) {
    student* temp = new student();
    temp->num = x;
    for (int i = 0; n[i] != 0; i++) { temp->name[i] = n[i]; }
    temp->pre = NULL;
    temp->next = NULL;
    if (head == NULL) {head = temp; return;}
    student* current = head;
    if (current->next != NULL)current = current->next;
    temp->pre = current;
    current->next = temp;}

void Delete(int n) {//删除第N个数据元素
    student* current = head;
    if (n == 1) {
        head = head->next; 
        if (head == NULL) { cout << "Node now is empty!\n"; return; }
        head->pre = NULL;
        return;
    }
    else {
        for (int i = 0; i < n - 1; i++) { current = current->next; }
        if (current != NULL ) {
            student* p = current->pre;
            student* n = current->next;
            if (current->next == NULL) { p->next = n; return; }
            p->next = n;
            n->pre = p;
            delete current; return;
        }
        else cout << "invalid data!";        
        return;
    }
}
 
void Research(int x) {
    student* current = head;
    while(current != NULL){
        if (x == current->num) {
            cout << current->num << '\t' << current->name<<"\n"; return; }
        current = current->next;   };
    if (current == NULL) {
        cout << "invalid data!"; return;}}

void Print() {
    student* current = head;
    if (head == NULL) { cout << "Node is empty!\n"; return; }
    for(int i=1;current->next!=NULL;i++) {
        cout << current->num << '\t' << current->name<<'\n';
        current = current->next; 
    }cout << current->num << '\t' << current->name << '\n'; return;
}

void findbeforeAndAfter(int x,int i,int j) {
    student* before; student* after;
    student* current = head;
    for (int I = 0; current != NULL||current->num != x; I++) { 
        current = current->next;    }
    if (current->num == x) {
        before = current; 
        for (int I = 0; I < i; I++)before = before->pre;
        after = current;
        for (int I = 0; I < j; I++)after = after->next;
        cout <<"该学生第i个前驱学生信息为:\n" << before->num << '\t' << before->name << '\n';
        cout << "该学生第j个后继学生信息为:\n" << after->num << '\t' << after->name << '\n'; return;  }
    else {cout << "输入错误!"; return;}
}

int main() {
    int choose;
    cout << "1:插入数据元素\t2.删除数据元素\t3.查找数据元素\t4.显示线性表中的数据元素\t5.特殊查找函数\t6.退出\n";

    loop:cout<<"选择一个操作:";
    cin >> choose;

    switch (choose) {
    case(1): {
        int num; char name[10]; string s;
        cout << "num:";
        cin >> num;
        cout << "name:";
        cin >> s;
        int i = s.length();
        for (int i = 0; i < s.length(); i++) { name[i] = s[i]; }
        name[i] = '\0';
        Insert(num, name);
        goto loop; }
        case(2): {
            int x;
            cout << "删除第几个元素:";
            cin >> x;
            Delete(x);
            goto loop; }
        case(3): {
            int x;
            cout << "请输入要查找的学号:";
            cin >> x;
            Research(x); 
            goto loop; }
        case(4): { Print(); goto loop; }
        case(5): {
            int x, i, j;
            cout << "给定学号x,找到该学生第i个前驱及该学生第j个后继的学生信息,分别键入x,i,j的值:";
            cin >> x; cin >> i; cin >> j;
            findbeforeAndAfter(x, i, j);
            goto loop;  }
        case(6):return 0;
    }
    }

img

在Print()函数中,for循环的条件是current->next!=NULL,但是最后一个节点的next指针应该是NULL,所以最后一个节点的数据没有被打印出来。可以将循环条件改为current!=NULL。

在Insert()函数中,当链表中有多个节点时,如果新插入的节点是第四个节点之后的节点,那么temp->pre = NULL;会使得temp的pre指针指向NULL,也就是没有指向前面一个节点,导致后面打印链表的时候只能打印前三个节点。需要修改插入操作,将temp的pre指针指向当前节点的前一个节点,而不是直接指向NULL。修改后的代码如下:

void Insert(int x, char *n) {
    student* temp = new student();
    temp->num = x;
    for (int i = 0; n[i] != 0; i++) { temp->name[i] = n[i]; }
    temp->pre = NULL;
    temp->next = NULL;
    if (head == NULL) {
        head = temp;
        return;
    }
    student* current = head;
    while (current->next != NULL) {
        current = current->next;
    }
    current->next = temp;
    temp->pre = current;
}
这样就能正确地打印出链表中的所有节点,并且插入节点后也能正确打印链表中的所有节点了。

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^