c语言 - 将节点添加到链表的开头会导致我不遵循的错误消息



我在以前的帖子中找到了相关的内容,但是当我试图编译建议的代码时,我得到了一个错误消息。正如主题所暗示的,我正试图将一个节点添加到链表的开始。我张贴了整个文件,以提供更多的背景…

#include <stdio.h>
#include <stdlib.h>
struct entry {
    int value;
    struct entry *next;
};
struct entry *findEntry ( struct entry *listPtr, int match)
  {
    while ( listPtr != (struct entry *) 0)
      {
      printf ("Here is the current value%in", listPtr->value);
      if ( listPtr->value == match )
        return (listPtr);
      else
        listPtr = listPtr->next;
      }
    return (struct entry *) 0;
  }
void insertEntry ( struct entry *newNode, struct entry *targetPtr)
// This function inserts a node after the targe Node
{
   // set the pointer inside of new object to point to what target node is pointting to right now
   newNode->next = targetPtr->next;
   printf("Here is what is in new node value%in Here is what is in targetNode value %in",newNode->next, targetPtr->next);
   // Now move the pointer inside of target node, and point to new object
   targetPtr->next = newNode;
}
struct entry newNodef ()
  {
    struct entry node, *nodePtr;
    int newValue;
    printf("Please tell me what value you want to store in the new noden");
    scanf("%i", &newValue);
    node.value = newValue;
    return node;
  }
void addFirst (struct entry **listStart, int value)
{
  struct entry *new_entry = ( struct entry*) malloc (sizof (struct entry));
  new_entry->value = value;
  new_entry->next =*listStart;
  *listStart = new_etnry;
}
int main (void)
{
 struct entry *findEntry ( struct entry *listPtr, int match);
 void addFirst (struct entry **listStart, int value);
 void insertEntry ( struct entry *newNode, struct entry *targetPtr);
 struct entry newNodef ();
 struct entry n1, n2, n3;
 struct entry *listPtr, *targetPtr, *listStart = &n1;
 int search;
 n1.value = 100;
 n1.next = &n2;
 n2.value = 200;
 n2.next = &n3;
 n3.value =300;
 n3.next = (struct entry *) 0;
 struct entry tempNode = newNodef();
 listPtr = &tempNode;
 targetPtr = &n2;
 insertEntry ( listPtr, targetPtr);
 printf("Here is what the new node is pointing to now %i. n", tempNode.next->value );
 printf ("Enter value to locate: ");
 scanf ("%i", &search);
 listPtr = findEntry (listStart, search);
 if ( listPtr != (struct entry *) 0)
   printf ("Found %i.n", listPtr->value);
 else
   printf ("Not found. n");
 int v=700;
 addFirst(&listStart, v);
 return 0;

}

下面是错误信息…

$ make
gcc  temp.c -o temp
temp.c: In function 'addFirst':
temp.c:42:61: error: expected expression before 'struct'
   struct entry *new_entry = ( struct entry*) malloc (sizof (struct entry));
                                                             ^
temp.c:45:16: error: 'new_etnry' undeclared (first use in this function)
   *listStart = new_etnry;
                ^
temp.c:45:16: note: each undeclared identifier is reported only once for each function it appears in
make: *** [temp] Error 1
我基本上遵循了这篇文章底部提供的建议,因为我能理解他的解释。如果有更好的方法来做到这一点,而不是使用指针指向指针,请告知。
  1. sizof -> sizeof的拼写错误

  2. new_etnry -> new_entry拼写错误

  3. insertentry中,将printf指定符更改为%p或将参数更改为newNode->valuetargetPtr->value

  4. newNodef中未使用的变量nodePtr

相关内容

  • 没有找到相关文章

最新更新