vc++相关程序问题

vc++相关程序问题以及其结构体与简单链表相关问题……谢谢了……

img

#include<iostream>
using namespace std;

struct Node {
    int val;
    Node* next;
};

Node* creatlist(int n) {
    Node* Head=new Node;    //头节点 不存储数据
    Head->next = NULL;      
    Node* pre = Head;  //指向下一个节点的过渡值

    cout << "请依次输入" << n << "个链表的值:";
    for (int i = 0;i < n;i++) {
        Node* temp = new Node;
        cin >> temp->val;

        pre->next = temp;
        pre = temp;
        temp->next = NULL;    
    }
    return Head;
}

void display(Node* head) {
    Node* temp=head->next;
    int e;
    cout << "该链表的遍历依次为:";
    while (temp!=NULL) {
        e = temp->val;
        cout << e << " ";
        temp = temp->next;
    }
    cout << "\n";
}

void Clear(Node* head)//释放链表结点
{
    Node *p = head->next;
    Node *q = NULL;
    while (p)
    {
        q = p;//p作为前置指针,q作为后置指针
        p = q->next;//将p指针移动到下一个结点
        free(q);//释放q结点所占空间
    }
    head->next = NULL;//将头结点next指向NULL,若不指向空遍历输出会进入死循环
    printf("链表空间已释放!");
}

int main() {
    int nums;
    Node* head = creatlist(4);
    display(head);
    Clear(head);
    return 0;
}


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