• 链表源代码(C语言实现)


    源代码(C语言实现)

          ①.构造链表节点

                 typedef struct Node    //一个单独的节点
                       {
                             int data;//数据域
                             struct Node * pNext;//指针域
                        }NODE,* PNODE;

             ②.链表的初始化

               PNODE init_list()
                      {
                              int len ;//链表的长度
                              int i;//循环开关
                              int val;//节点的值

                              PNODE pHead = (PNODE)malloc(sizeof(NODE));//造出头结点

                              if (NULL==pHead)
                              {
                                   printf("链表动态分配失败!程序终止! ");
                                   exit(-1);
                              }
                              else
                              {
                                    printf("请输入要创建链表的长度len=");
                                    scanf("%d",&len);

                                    if (0==len)
                                    {   
                                         pHead->pNext = NULL;
                                          return pHead;  //对空链表的处理
                                     }
                                     if (len<0)  //对长度为负数的处理
                                      {
                                             printf("链表的长度不能为负数! ");
                                             exit(-1);
                                      }

                                     PNODE pTail = pHead;//pTail指向的是尾节点
                                     pTail->pNext = NULL;

                                     for (i=0;i<len;i++)
                                      {
                                           PNODE pNew = (PNODE)malloc(sizeof(NODE));//造出新的节点
                                           printf("请输入第%d个节点的数值val =",i+1);
                                           scanf("%d",&val);

                                           pNew->data = val;  //赋值
                                           pTail->pNext = pNew; //连接新的节点
                                           pTail = pNew;      //pTail后移
                                       }
                                       pTail->pNext = NULL;//最后一个节点的指针域必须为空
                                       return pHead; //返回头结点以用来表示整个链表


                                         }
                      }

  • 相关阅读:
    深入学习图数据库语言Gremlin 系列文章链接汇总
    HugeGraph入门
    Connection 'ens33' is not available on device ens33 because device is strictly unmanaged
    正则里的\s和s有什么区别?
    LXMERT: Learning Cross-Modality Encoder Representations from Transformers
    Pixel-BERT: Aligning Image Pixels with Text by Deep Multi-Modal Transformers
    在vue中使用jsx语法
    JavaScript数据结构和算法
    如何用智能手机或PAD观看笔记本电脑的视频?
    linux shell和windows bat编写
  • 原文地址:https://www.cnblogs.com/timssd/p/4658593.html
Copyright © 2020-2023  润新知