c - 链表插入简单

标签 c linked-list

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
   int key;
   struct node* next;
} node_t;

typedef node_t* node_ptr;

void main()
{
    node_ptr p = NULL;
    node_ptr listhead =NULL;

    /*need to create a listhead with key = 1,2,3..10*/

    int i; 
    for (i =1; i<= 10;i ++) 
    {
       node_ptr temp;
       p =(node_ptr)malloc(sizeof(node_t));
       p->key = i;
       p->next = NULL;
       if( list_head == NULL ) 
       {
         list_head= p;   
       }
       else
       {
       temp = listhead;

         while(temp->next != NULL)
         {
           temp = temp->next;
         }
         temp->next =  p;
       }
    }
}

我仍然对链表感到很困惑,我不确定我是否做对了并且很确定它是错误的,有人可以帮助我吗?这只是 练习题:) 并且还更新了 node_ptr listhead=NULL; 的行;问题中给出了所以我不能改变上面的东西。

顺便说一下,为了清楚起见,问题要求将键 1,2,3..10 插入列表头。

最佳答案

“插入”可以表示在列表的开头插入,而不是在列表中的某个点或列表的末尾插入。对于这个作业,“插入”应该是什么意思?

带有 for 循环和微小变化的示例代码,例如使用 node_ptr 而不是 node_t * 来对应赋值类型定义,以及使用 int main() 而不是 void main()。

#include <stdlib.h>

typedef struct node{
    int key;
    struct node* next;
} node_t;

typedef node_t* node_ptr;

int main()
{
    node_ptr list_head = NULL;
    node_ptr p;
    node_ptr temp;
    int i; 

    /* create a list with keys = 1,2,3..10 */

    for (i = 1; i <= 10; i++) 
    {
        p = (node_ptr)malloc(sizeof(node_t));
        p->key = i;
        p->next = NULL;
        if( list_head == NULL ) 
        {
            list_head= p;   
        }
        else
        {
            for(temp = list_head; temp->next != NULL; temp = temp->next);
            temp->next =  p;
        }
    }
    return 0;
}

使用指向节点指针的指针的替代版本。这消除了对 list_head == NULL 的初始检查。它超出了您现在用于作业的范围,但了解如何执行此操作可能对以后的作业有用。

#include <stdlib.h>

typedef struct node{
    int key;
    struct node* next;
} node_t;

typedef node_t* node_ptr;
typedef node_t ** node_ptr_ptr;

int main()
{
    node_ptr list_head = NULL;
    node_ptr p;
    /* ptr to either list_head or to last node.next */
    node_ptr_ptr pptemp;
    int i; 
    /* create a list with keys = 1,2,3..10 */
    for (i = 1; i <= 10; i++) 
    {
        p = (node_ptr)malloc(sizeof(node_t));
        p->key = i;
        p->next = NULL;
        for(pptemp = &list_head; *pptemp != NULL; pptemp = &(*pptemp)->next);
        *pptemp =  p;
    }
    return 0;
}

对于这种特殊情况,由于 temp(或 pptemp)在 main 中,因此每个循环只需要初始化一次并推进一次:

#include <stdlib.h>

typedef struct node{
    int key;
    struct node* next;
} node_t;

typedef node_t* node_ptr;
typedef node_t ** node_ptr_ptr;

int main()
{
    node_ptr list_head = NULL;
    node_ptr p;
    /* ptr to either list_head or to last node.next */
    node_ptr_ptr pptemp = &list_head;
    int i; 
    /* create a list with keys = 1,2,3..10 */
    for (i = 1; i <= 10; i++) 
    {
        p = (node_ptr)malloc(sizeof(node_t));
        p->key = i;
        p->next = NULL;
        *pptemp = p;
        pptemp = &p->next;
    }
    return 0;
}

关于c - 链表插入简单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46985155/

相关文章:

c - 删除链表中的节点

c - 使用循环链表实现Queue

c - 在 Xterm Tektronix 4014 模式下隐藏光标

c++ - 我在哪里可以学习有关C++编译器的 “what I need to know”?

c - 从缓冲区分配不同的内存块

c - 读取 ntdll.dll + 偏移量会导致访问冲突

c - 如何从 C 中的结构复制字符?

c - 如何在访问链表中的下一个时修复段错误错误?

C 程序用于移动使用链表构建的堆栈中的项目

java - 链接列表中的 list.add(String) 对我来说无法正常工作