本文介绍了帮助理解C ++中的链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c ++中有一段链表代码。我想了解它。我完全理解它。但是,有些部分我没有得到。请解释一下H-> Data在此代码中的作用以及p-> Next在此代码中的作用。另外* H和* p在这方面做了什么。据我所知,指针保存其他变量的内存地址。



我尝试过:



I have a piece of code of linked list in c++.I want to understand it. I do understand the most of it. But, some parts I do not get. Please explain me that what H->Data does in this code and what p->Next does in this code. Also what does *H and *p do in this. For what i know that pointers hold memory address of other variables.

What I have tried:

#include <stdio.h>
#include <iostream>
#include <time.h>
using namespace std;

struct List
{	int Data;
	List* Next;
};

int main()
{
    List *H, *p;
    srand(time(0));
    cout << "Initial data: ";
    H = new List; p = H;
    H->Data = rand() % 99 + 1;
    cout << H->Data << " ";
    for (int i=0; i<19; i++)
    {
        p->Next = new List;
        p = p->Next;
        p->Data = rand() % 99 + 1;
        cout << p->Data << " ";
    }
    p->Next = NULL;
    
    cout << "\nData of the list: ";
    p = H;
    while (p != NULL)
    {
        cout << p->Data << " ";
        p = p->Next;
    }

    cout << endl;
    system("pause");
    return 0;
}

推荐答案


这篇关于帮助理解C ++中的链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 08:54