zoukankan      html  css  js  c++  java
  • 链表源代码(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; //返回头结点以用来表示整个链表


                                         }
                      }

  • 相关阅读:
    iOS开发之 Xcode6 添加xib文件,去掉storyboard的hello world应用
    iOS开发之Xcode 相对路径与绝对路径
    iOS开发之 在release版本禁止输出NSLog内容
    iOS开发之 xcode6 APP 打包提交审核详细步骤
    iOS开发之 UIScrollView的frame、contentSize、contentOffset和contentInset属性
    10.2&10.3 Xcode开发包
    Reason: image not found
    如何下架app
    UIStackView before iOS9.0
    Reason: image not found
  • 原文地址:https://www.cnblogs.com/timssd/p/4658593.html
Copyright © 2011-2022 走看看