链表删除结点课程无法继续

#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;
}

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

阅读 2.7k
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

推荐问题
logo
101 新手上路
子站问答
访问
宣传栏