我有一个链接列表,该列表接受一个输入字符串,并将每个字符串存储在列表的节点中。我想打印保存每个字符串的节点的十六进制地址。
我怎样才能做到这一点?我尝试打印保存的单词的十六进制地址,但是我还不知道它是否仍然是节点的相同地址,这是应该打印每个节点的功能:
// print the list
void printList(ListNodePtr currentPtr)
{
// if list is empty
if (isEmpty(currentPtr)) {
puts("List is empty.\n");
}
else {
puts("The list is:");
// while not the end of the list
while (currentPtr != NULL) {
printf("%s %p --> ", currentPtr->data, ¤tPtr);
currentPtr = currentPtr->nextPtr;
}
puts("NULL\n");
}
}
这是将每个单词保存在节点中的功能
void insert(ListNodePtr *sPtr, char *value)
{
ListNodePtr newPtr = malloc(sizeof(ListNode)+1); // create node
if (newPtr != NULL) { // is space available
newPtr->data= malloc(strlen(value));
strcpy(newPtr->data, value);
newPtr->nextPtr = NULL; // node does not link to another node
ListNodePtr previousPtr = NULL;
ListNodePtr currentPtr = *sPtr;
// loop to find the correct location in the list
while (currentPtr != NULL) {
previousPtr = currentPtr; // walk to ...
currentPtr = currentPtr->nextPtr; // ... next node
}
// insert new node at beginning of list
if (previousPtr == NULL) {
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else { // insert new node between previousPtr and currentPtr
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}
}
else {
printf("Not inserted. No memory available.\n" );
}
}
¤tPtr
给您一个指向指针变量的指针,它不是currentPtr
实际指向的位置。值¤tPtr
不会在循环中更改,因为变量本身不会更改位置。
如果要打印currentPtr
指向的位置,请指向节点本身,然后打印plain currentPtr
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句