简体   繁体   English

结构中的指针Malloc +指针算术+ free()+ linkedList

[英]Malloc of pointers in structs + pointer arithmetic + free() + linkedList

I'm trying to implement a linked-list data structure which each node has a identifier key, some data of variable length (malloc), and a pointer to the next node. 我正在尝试实现一个链表数据结构,其中每个节点都有一个标识符键,一些可变长度(malloc)的数据以及一个指向下一个节点的指针。 Now I want to have 3 functions which respectively: sets a new node to the front of the list, prints the values of a given node using identifier key, and deletes a given node. 现在,我要分别具有3个功能:将新节点设置在列表的最前面,使用标识符键打印给定节点的值,并删除给定节点。

The struct I have for the node is as follows: 我对该节点的结构如下:

struct node {
 char key[5];
 int* data;
 node* next;
};
struct node* headNode = NULL;

I have questions regarding each of functions. 我对每个功能都有疑问。 I will list the function codes I have and ask questions regarding that specific function below: 我将列出我拥有的功能代码,并在下面询问有关该特定功能的问题:

The code for my set function: 我的set函数的代码:

void command_set (char key[], int val[], int numOfVal){
 struct node* temp = (node*)malloc(sizeof(node));
 strcpy(temp->key, key);
 temp->data = (int*)malloc(numOfVal*sizeof(int));
 *(temp->data) = *(val);
 temp->next = entry_head;
 entry_head = temp;
 return;
}

Now I have one question regarding this function: 现在我对此功能有一个疑问:

1) Is my method of storing the data valid? 1)我的数据存储方法有效吗? ie "temp->data = (int*)malloc(numOfValues sizeof(int));" 即“ temp-> data =(int *)malloc(numOfValues sizeof(int));” + " (temp->data) = *(val);". +“ (temp-> data)= *(val);”。 What I'm trying to do is dynamically allocate some memory, then store the given values as my node's data in that memory. 我想做的是动态分配一些内存,然后将给定值作为我节点的数据存储在该内存中。


The code for my print function: 我的打印功能代码:

void printNode (char key[], int numOfVal){
 int i;
 struct node *currentNode = headNode;

 while(currentNode->next!=NULL){
     if(!strcmp(currentNode->key,key) ){
        for(i=0; i<numOfVal; i++){
            printf("%d ",*((currentNode->data)+i));
        }
        return;
    }
    currentNode = currentNode->next;
}

I have a one question regarding this function: 我对此功能有一个疑问:

2) The data of a node is a list of integers, so does my way of printing out each integer actually work? 2)节点的数据是一个整数列表,那么我打印出每个整数的方式是否有效? ie "*((currentNode->data)+i)". 即“ *((currentNode-> data)+ i)”。 What I'm trying to do is by using pointer arithmetic I print all the ints stored under data. 我想做的是通过使用指针算术来打印存储在数据下的所有int。


The code for my delete function: 我的删除功能的代码:

void deleteNode (char key[]){
 struct node *currentNode = headNode;
 struct node *prevNode = headNode;
 while(currentNode->next!=NULL){
    if(!strcmp(currentNode->key,key) ){
            prevNode->next = currentNode->next;
            free(currentNode->data);
            free(currentNode->next);
            free(currentNode);
            return;
    }

    prevNode = currentNode;
    currentNode = currentNode->next;
 }

I have two questions regarding this function: 关于此功能,我有两个问题:

3) Am I "deleting" the nodes properly? 3)我是否正确“删除”了节点? By using free(). 通过使用free()。 Is this the way to do it? 这是这样做的方法吗?

4) Is this how you link up nodes after deletion? 4)这是删除后如何链接节点的方法吗? By setting the next pointer to another node. 通过将下一个指针设置为另一个节点。

Please assume that malloc will not return NULL for simplicity. 为了简单起见,请假设malloc不会返回NULL。 Also note that I have simplified my actual code, else there is way too much to post, so there might be slight errors. 另请注意,我已经简化了我的实际代码,否则发布的方式太多了,因此可能会有一些错误。 You may also assum that the while loops will always work (ie there will not be a case where (currentNode->next==NULL). The main point of this post are my questions regarding whether the method of doing something is correct. 您可能还假设while循环将始终有效(即,不会出现where(currentNode-> next == NULL)的情况。这篇文章的重点是我对做某事的方法是否正确的问题。

An example of the program would be: 该程序的一个示例是:

-set ex1 2 3 4 5 设置ex1 2 3 4 5

-get ex1 -获取ex1

2 3 4 5 2 3 4 5

-set ab 32 112 设置ab 32 112

-get ab -获取Ab

32 112 32112

Thanks in advance. 提前致谢。

strcpy(temp->key, key); 

For the the purpose of your program, this is probably ok, but you should use strncpy(temp->key,key,5) to be safe. 出于程序目的,这可能没问题,但是您应该使用strncpy(temp-> key,key,5)保持安全。 Or at least check the length of key to make sure it fits. 或者至少检查钥匙的长度以确保它适合。

*(temp->data) = *(val);

This only sets the first index in the array. 这仅设置数组中的第一个索引。 You should use memcpy here. 您应该在这里使用memcpy。

memcpy (temp->data,val, sizeof (int) * numOfVal);

Your print function prints the first element that doesn't match. 您的打印功能将打印不匹配的第一个元素。 Did you mean to do the opposite? 你是说相反吗?

Your delete function does the thing. 您的删除功能可以执行此操作。 It finds the first node that doesn't match. 它找到不匹配的第一个节点。

You also don't want to free currentNode->next; 您也不想释放currentNode-> next;

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

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