【问题标题】:Why doesn't C List Pop work on first run?为什么 C List Pop 在第一次运行时不起作用?
【发布时间】:2013-08-27 06:58:03
【问题描述】:

在 C 中为我的链表实现 Pop 函数时遇到了一些麻烦。

这是一个非常奇怪的错误,因为它在第一次运行时不起作用。

Node * operations = GetOperations(); // Retrieve linked-list of operations
Print(operations);
while (1) {
    if (operations == NULL) {
        return 0;
    }

    Node * temp = Pop(&operations);
    printf("Node popped is: %s\n", temp->data);
    Print(operations);
    Node * temp2 = Pop(&operations);
    printf("Node2 popped is: %s\n", temp2->data);
    Print(operations);
    // Other code down here... //
    return 1;
}

还有我的 Pop 代码:

Node * Pop (Node ** head) {
    if (*head == NULL) {
        printf("Error popping! List is empty.\n");
        return NULL;
    }

    Node * temp = *head;
    *head = (*head)->next;
    return temp;
}

我的节点代码:

typedef struct node {
    char * data;
    struct node * next;
} Node;

当我运行程序并返回 GetOperations 时(-> 代表下一个节点):

Op1->Op2->Op3...

我得到以下输出:

Operations: Op1->Op2->Op3
Node popped is: (null)
Operations: Op1->Op2->Op3
Node2 popped is: Op1
Operations: Op2->Op3

如您所见,第一个 pop 永远不会起作用。

【问题讨论】:

  • 您可能希望使用调试器逐行逐行执行代码以查看会发生什么。
  • data 之后的双引号似乎无法匹配。 printf("Node popped is: %s\n", temp->data");这些额外的双引号是否也出现在您的代码中,或者它们是复制粘贴的产物?
  • @verbose 抱歉,这些是复制/粘贴的人工制品! Joachim,我会试试 gdb 调试器,希望能有所收获。
  • @JoachimPileborg... 你注意到他的head 声明了吗?
  • 你能展示你的打印功能吗?还有你的 Getinformation 吗?

标签: c list pointers linked-list


【解决方案1】:

我刚刚在我的 GetOperations() 和 Print() 实现中使用了您的代码。代码工作正常。

//Structure to represent node
typedef struct node 
{
    char *data;
    struct node * next;
} Node;

//Pop top value from Linked list
Node * Pop (Node ** head) 
{
    if (*head == NULL)
    {
        printf("Error popping! List is empty.\n");
        return NULL;
    }
    Node * temp = *head;
    *head = (*head)->next;
return temp;
} 

//Create node and save data
Node *createNode(int i)
{
    char *n=malloc(sizeof(4*sizeof(char)));
    n[0]='O';  n[1]='p'; n[2]=(char)(((int)'0')+i); n[3]='\0';
    Node *temp=(Node*)malloc(sizeof(struct node));
    temp->data=n;
    temp->next=NULL;
return temp;
}

//Create a list with data
Node* GetOperations()
{
    int i;
    Node *head=NULL,*current;
    for(i=0;i<5;i++)
    {
         if(head==NULL)
         {
             head=createNode(i+1);
             current=head;
         }
         else
         {
             Node *temp=createNode(i+1);
             current->next=temp;
             current=temp;
         }
    }
return head;
}

//Print the linked list
void Print(Node *ptr)
{
    while(ptr)
    {
         printf("%s",ptr->data);
         ptr=ptr->next;
         if(ptr) printf("->"); 
    }
    printf("\n");
}

//Start of program execution
int main()
{
    Node * operations = GetOperations(); // Retrieve linked-list of operations
    Print(operations); // Print list
    if (operations == NULL) return 0;
    Node *temp=NULL;
    temp = Pop(&operations);
    printf("Node popped is: %s\n", temp->data);
    free(temp);  // Free popped data's memory
    Print(operations);
    temp = Pop(&operations);
    printf("Node2 popped is: %s\n", temp->data);
    free(temp);  // Free popped data's memory
    Print(operations);
    // Other code down here... //
return 0;   
}

我得到了这个输出:

Op1->Op2->Op3->Op4->Op5
Node popped is: Op1    
Op2->Op3->Op4->Op5
Node popped is: Op2
Op3->Op4->Op5        

【讨论】:

  • 嗨,我也试过用一些简单的程序来测试它,它们也都可以正常工作。我不知道我的代码中发生了什么。GetOperations() 绝对没问题,因为我打印了它的输出并且一切看起来都很好。任何人,我只是在GetOperations() 之后完成了Pop,我的程序可以继续:)。感谢您的帮助。
  • @Travv92 您的 Print() 清楚地表明节点指针(操作)指向头节点,但我想知道为什么您的脚本没有在第一次调用中弹出。如果您可以使用完整的实现代码编辑您的帖子,我们可以检查一下。
  • 嗨,我找到了问题:)。愚蠢的我一直在将空节点推送到代码不同区域的列表前面!感谢您的帮助!
猜你喜欢
  • 2012-12-28
  • 2012-01-31
  • 1970-01-01
  • 2021-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 2021-05-20
相关资源
最近更新 更多