c++ - 链表删除结点课程无法继续
PHPz
PHPz 2017-04-17 11:53:19
[C++讨论组]
#include <iostream>
using namespace std;

struct List
{
    int num;
    List *next;
};

List *head;

void deleteNode(List *&head)
{
    //在下面编写删除代码
    int num;
    cin>>num;
    List *p=NULL,*q=NULL;
   p = head;
if(p->num == num){
    head =p->next;
        delete p;
    return;
}
    q=p->next;
    while( q != NULL ){
        if(q->num==num){
            p->next=q->next;
            delete q;
            return;
        }
     **//就是这里开始提示完成代码后,依然没有到下一步。。。
         if (q->num>num){
            return ;
        }
        p = q;
        q = q->next;
    }**


List *Create()
{
    List *p = NULL;
    List *q = NULL;
    head = NULL;
    for ( int i = 0; i < 3; i++ ) {
        p = new List;
        p->num = i * 2;
        if ( head == NULL ) {
            head = p;
        }
        else {
            q->next = p;
        }
        q = p;
    }

    if ( head != NULL ) {
        q->next = NULL;
    }

    return head;
}

void displayList(List *head)
{
    while ( head != NULL ) {
        cout << head->num;
        head = head->next;
        if ( head != NULL ) {
            cout << "->";
        }
    }
    cout << endl;
}

int main() {
    Create();
    deleteNode(head);
    displayList(head);
    return 0;
}

完全按照提示后,填写一直没有得到提示继续。

PHPz
PHPz

学习是最好的投资!

全部回复(1)
黄舟

我测试好像没有问题,不明白你想要问什么...

root@localhost:~# ./a.out
0
2->4
root@localhost:~# ./a.out
1
0->2->4
root@localhost:~# ./a.out
2
0->4
root@localhost:~# ./a.out
3
0->2->4
root@localhost:~# ./a.out
4
0->2

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号