zoukankan      html  css  js  c++  java
  • 尾插法链表拆分

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 /*
     4 尾插法链表拆分:1.建立空链表。2.插入节点。3.遍历并输出插入节点后的链表数据
     5 */
     6 typedef struct node
     7 {
     8     int data;
     9     struct node * next;
    10 }NODE;
    11 //建立空链表
    12 NODE * createList()
    13 {
    14     NODE * head = (NODE *)malloc(sizeof(NODE));
    15     head->next = NULL;
    16 
    17     return head;
    18 }
    19 //插入节点
    20 NODE * insertNode(NODE * pt,int nodeData)
    21 {
    22     NODE * cur = (NODE *)malloc(sizeof(NODE));
    23     cur->data = nodeData;
    24 
    25     pt->next = cur;
    26     cur->next = NULL;
    27     pt = cur;
    28 
    29     return pt;
    30 }
    31 //遍历并输出插入节点后的链表元素
    32 void traverList(NODE *head)
    33 {
    34     head = head->next;
    35     while(head)
    36     {
    37         printf("%d
    ",head->data);
    38         head = head->next;
    39     }
    40 }
    41 
    42 int main(void)
    43 {
    44     //建立空链表
    45     NODE * head = createList();
    46     //插入节点
    47     NODE * pt = head;//定义pt保存尾节点的地址
    48     for(int i=0;i<50;i++)
    49     {
    50         pt = insertNode(pt,i);
    51     }
    52     //遍历并输出插入节点后的链表元素
    53     traverList(head);
    54     return 0;
    55 }
  • 相关阅读:
    shell中单引号、双引号、反斜杠简说
    shell脚本
    求素数
    SqlBulkCopy高效写入数据库Demo
    地图面面观之百望山
    FileUpload控件客户端验证
    如何将shapefile进行拆分
    Python 字符串操作
    如何重装oracle
    资料
  • 原文地址:https://www.cnblogs.com/wangchaomahan/p/9693975.html
Copyright © 2011-2022 走看看