简体   繁体   English

如何在main中使用指针数组?

[英]How do I use an array of pointers in main?

I tried asking my question but I don't appear to be asking it correctly and I have been stuck for 2 months now. 我曾尝试问过我的问题,但似乎并没有正确地提出问题,而且我已经被困了2个月了。 (which is sad) (很伤心)

For reference only: I built a linked list from nodes: 仅供参考:我从节点构建了一个链表:

struct node  {
int number;
node *next;  }; 

To link these in main I used -> to assign values 为了链接这些我主要使用->分配值

void insertAsFirstElement(node *&head, node *&tail, int number){
node *temp = new node; 
temp->number = number;
temp->next = NULL; 
head = temp;
tail = temp; }

Now I am trying to make a skiplist, which should have the same structure as my original node except the node* next should be an array of pointers of type node. 现在,我尝试制作一个跳过列表,该列表应具有与原始节点相同的结构,但下一个node *应该是node类型的指针数组。

struct node {
int number;
node *next[3];
};

I am getting confused on how to have an array of node pointers. 我对如何具有节点指针数组感到困惑。 I notice they tend to look like this: node **next and then declared to have memory allocated dynamically. 我注意到它们倾向于如下所示:node ** next,然后声明为动态分配内存。 I just want my arrays to be of size 4. So [3]. 我只希望数组的大小为4。所以[3]。

My problem is how can I create new nodes with the array of node pointers in main() and put something in the first slot of the nodes array? 我的问题是如何使用main()中的节点指针数组创建新节点,然后将某些内容放入节点数组的第一个插槽中?

This does not work for putting things in the array but it does work for putting in the number. 这不适用于将事物放入数组,但确实适用于放入数字。

void insertAsFirstElement(node *&head, node *&tail, int number){

node *temp = new node; 
temp->number = number;
cout<<temp->number<<endl;
temp->next[0] = tail; 
cout<<temp->next[0]<<endl;
head->next[0] = temp;
cout<<head->next[0]<<endl;
}

Please help me. 请帮我。

The -> operator is a shorthand. ->运算符是简写。

Without the -> operator, you would write 如果没有->运算符,您将编写

(*var).prop;

With the -> operator, you write: 使用->运算符,您可以编写:

var->prop;

Thus, to store a node in the first position of the list, you write: 因此,要将节点存储在列表的第一个位置,请编写:

void StoreNode(node *node){
    node *temp = new node;
    temp->next[0]=node;
}

And to retrieve data from a node in the list, you can write: 要从列表中的节点检索数据,您可以编写:

temp->next[0]->number

which is the same as writing 和写作一样

(*temp).next[0]->number

which is the same as writing 和写作一样

( *((*temp).next[0]) ).number

This line of your code seems a little confused: 您的代码这一行似乎有些困惑:

void insertAsFirstElement(node *&head, node *&tail, int number){

Remember, you are just passing your function the address of a node. 记住,您只是在向函数传递节点地址。 Therefore, all you need is 因此,您需要做的就是

void insertAsFirstElement(node *head, node *tail, int number){

Inside the function itself, you will have to find the correct location in the list, that is when you get into the ** notations. 在函数本身内部,您将必须在列表中找到正确的位置,即进入**符号时。

The code at a first look seems ok. 乍一看,该代码看起来还可以。 An array of pointers is just that, an array of elements where each one is a pointer, and you use it exactly with the syntax your code shows. 指针数组就是这样,它是一个元素数组,每个元素都是一个指针,您可以将其与代码显示的语法一起使用。

Note however that when declaring an array of pointers inside a class the elements are not automatically initialized so you probably want to fix the elements that are not used yet to NULL. 但是请注意,在类内声明指针数组时,元素不会自动初始化,因此您可能希望将尚未使用的元素修复为NULL。 Moreover in a skip list you're probably going to need to know at which "level" the node has been inserted. 此外,在跳过列表中,您可能需要知道节点已插入到哪个“级别”。

Are you sure your problem is in that part? 您确定问题出在那部分吗? Often with C++ a mistake doesn't appear right in the point it's done, but much later. 通常,对于C ++,完成时并不会立即显示错误,但会晚得多。 This happens because of the "undefined behavior" rule of the language (aka "programmers never make mistakes"). 发生这种情况是由于语言的“未定义行为”规则所致(又名“程序员从不犯错误”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM