【问题标题】:i want to know what this *list pointer is about in this code我想知道这个 *list 指针在这段代码中是关于什么的
【发布时间】:2021-01-05 13:36:03
【问题描述】:

这段代码是关于交换单链表的两个节点。 *list 变量仅在这两个函数中,甚至不在 main 函数中。我还没有定义 *list 指针变量是什么,但这段代码仍然有效。为什么这样有效,为什么我们将 *list 参数传递给这些函数,而这个 *list 变量除了这两个函数之外别无他处,列表指针得到什么值以及为什么请解释一下。

'''

int main()
{
    int n, pos1, pos2;

    // Input node count to create
    printf("Enter number of node to create: ");
    scanf("%d", &n);

    createList(n);

    // Display list
    printf("\n\nData in list before swapping: \n");
    displayList();

    printf("\nEnter first node position to swap: ");
    scanf("%d", &pos1);
    printf("\nEnter second node position to swap: ");
    scanf("%d", &pos2);

    if (swap(head, pos1, pos2) == 1)
    {
        printf("\nData swapped successfully.\n");
        printf("Data in list after swapping %d node with %d: \n", pos1, pos2);
        displayList();
    }
    else 
    {
        printf("Invalid position, please enter position greater than 0 and less than nodes in list.\n");
    }

    return 0;
}

/**
 * Counts total number of nodes in a linked list.
 * @param *list Pointer to head node of linked list.
 * @returns Total count of nodes in list.
 */
int count(struct node *list)
{
    int nodes = 0;

    while (list != NULL) 
    {
        nodes++;
        list = list->next;
    }

    return nodes;
}

int swap(struct node *list, int pos1, int pos2){
    struct node *node1, *node2, *prev1, *prev2, *temp;
    int i;
    
    //get the far postion among both
    const int maxpos = (pos1 > pos2) ? pos1 : pos2;
    
    //get total in the list
    const int totalnodes = count(list);
    
    //validate swap positions
    if((pos1 <= 0 || pos1 > totalnodes) || (pos2 <= 0 || pos2 > totalnodes)){
        return -1;
    }
    
    //if both position are same then no swapping required
    if(pos1 == pos2){
        return 1;
    }
    
    //identify both nodes to swap 
    i = 1;
    
    temp = list;
}

'''

【问题讨论】:

  • 你能分享一下countswap是如何从主函数中调用的吗?
  • 确定我会编辑这个并向你展示主要功能

标签: c data-structures linked-list swap singly-linked-list


【解决方案1】:

main() 和被调用的函数中传递的变量名称可能不同。这是一个简单的例子。

int foo(int a , int b)
{
return a+b;
}

int main()
{
int c=5,RandomVar=6,sum;
sum = foo(c,RandomVar);
printf("%d",sum);
return 0;
}

在上述情况下,当调用foo() 时,a 将等于cb 将等于RnadonVar

现在,在您的情况下,*list 是一个指针,用于将整个链接“列表”传递给由链接列表的head 标识的函数。 您使用head 遍历链接列表。因此,在您的情况下,列表的“swap()function can traverse link list , it is having the*listbeing passed to it which is thehead”。

【讨论】:

    【解决方案2】:

    这个函数:

    int swap(struct node *list, int pos1, int pos2){
    

    声称它返回一个int。但是,该函数包含一个在函数末尾运行的执行路径,不返回任何内容

    关于

    struct node *node1, *node2, *prev1, *prev2, *temp;
    

    上述行中使用的唯一变量是:*temp 建议将该行替换为;

    struct node *temp;
    

    然而,这个变量只是被设置而不被使用。

    关于;

    int i;
    

    这个i 变量已设置但从未使用过。

    关于;

    const int maxpos = (pos1 > pos2) ? pos1 : pos2;
    

    从不使用 const maxpos。建议去掉这条线

    struct node 未在发布的代码中的任何地方定义

    【讨论】:

      【解决方案3】:

      函数中变量的名称只限于函数本身。所以变量*list 只在函数本身内被调用。但是当您调用swap 函数时,您传递的是一个内存引用,该引用仅在函数本身内称为*list

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-27
        • 2022-09-27
        • 2019-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-19
        相关资源
        最近更新 更多