MENU

单链表代码优化

April 30, 2020 • Read: 502 • 笔记

/*
 * @Author: Mr.Sen
 * @LastEditTime: 2020-04-30 20:13:46
 * @Website: https://449293786.site
 * @原创代码,版权所有,转载请注明原作者
 */
#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
    int value;
    struct node *next; 
}link_list;

link_list* creat_tail(int length)
{
    //尾插法
    link_list *head,*node,*tail;
    head=(link_list*)malloc(sizeof(link_list));
    tail=head;//如果节点数为零,那么头结点就是尾节点
    while(length--)
    {
        node = (link_list*)malloc(sizeof(link_list));
        scanf("%d",&node->value);
        tail->next=node;
        tail=node;
    }
    tail->next=NULL;
    return head;
}

void print(link_list* head)
{
    link_list  *node=head;
    while (node->next!=NULL)
    {
        node=node->next;
        printf("->%d",node->value);
    }
}

int main()
{
    print(creat_tail(5));
    return 0;
}
Last Modified: May 10, 2020
Archives Tip
QR Code for this page
Tipping QR Code