简单的链表创建,添加节点(链表数据从小到大排序),显示,销毁函数

 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<stdbool.h>
  4 typedef struct Node
  5 {
  6     int data;
  7     struct Node* next;
  8 }Node;
  9 
 10 Node* creat_node(int data)
 11 {
 12     Node* node = malloc(sizeof(Node));
 13     node->data = data;
 14     node->next = NULL;
 15     return node;
 16 }
 17 typedef struct List
 18 {
 19     Node* head;
 20     Node* tail;
 21     size_t size;
 }List;
 23 
 24 List* creat_list()
 25 {
 26     List* list = malloc(sizeof(List));
 27     list->head = NULL;
 28     list->tail = NULL;
 29     list->size = 0;
 30     return list;
 31 }
 32 void add_head_list(List* list,int data)
 33 {
 34     Node* node = creat_node(data);
 35     if(0 == list->size)
 36     {
 37         list->head = node;
 38         list->tail = node;
 39         list->size++;
          return;
 41     }
 42     node->next = list->head;
 43     list->head = node;
 44     list->size++;
 45     for(Node* node1 = list->head;node->next!= NULL;node=node->next)
 46     {
 47         for(Node* node2 = node1->next;NULL!=node2;node2=node2->next)
 48         {
 49             if(node1->data>node2->data)
 50             {
 51                 int temp = node1->data;
 52                 node1->data = node2->data;
 53                 node2->data = temp;
 54             }
 55         }
 56     }
 57 }
 void show(List* list)
 59 {
 60     for(Node* node = list->head;node != NULL;node=node->next)
 61     {
 62         printf("%d",node->data);
 63     }
 64 }
 65 bool head_del_list(List* list)
 66 {
 67     if(0 == list->size)return false;
 68     if(1 == list->size)
 69     {
 70         list->head = NULL;
 71         list->tail = NULL;
 72         list->size--;
 73         return true;;
 74     }
 75     Node* node = list->head->next;
 76     list->head = node;
 77     list->size--;
 78     return true;
 }
 81 void free_list(List* list)
 82 {
 83     while(NULL!=list->head)
 84     {
 85         head_del_list(list);
 86     }
 87     free(list);
 88 }
 89 void main()
 90 {
 91     List* list = creat_list();
 92     add_head_list(list,1);
 93     add_head_list(list,13);
 94     add_head_list(list,11);
 95     add_head_list(list,5);
 96     show(list);
 97 }
                                                  
整数链表排序的c源代码 说明:试按以下给出的排序算法为整数链表编写一个排序函数: 该算法是按表元键值的各位值进行排序。 设有一个整数链表,其中表元的键值为不超过三位数的整数,不妨设键值形式ABC。其中A表示键值的百位数,B为十位数,C为个位数。首先按键值中的个位值C对链表作分拆和链接,先把链表分拆成10个队列链表,然后以C的值从0至9的顺序把分拆后的十个队列链表重新收集成一个链表。接着依次对键值中的B和A进行同样的分拆和链接操作,则最后收集起来的链表是按键值从小到大排序链接的。如有一个链表按它们的键值其表元的链接顺序依次为: 153 678 56 288 457 653 721 876 433 254 按它们的键值的个位分拆,得到十个队列链表,列出它们的键值顺序有: 0: 空链表 1: 721 2: 空链表 3: 153 653 433 4: 254 5: 空链表 6: 56 876 7: 457 8: 678 288 9: 空链表 顺序将它们收集一起后,链表的键值顺序有: 721 153 653 433 254 56 876 457 678 288 再按它们键值的十位分拆,得到十个队列链表,列出它们的键值顺序有: 略。 顺序将它们收集在一起后,链表的键值顺序有: 略。 再按它们键值的百位分拆,得到十个队列链表,列出它们的键值顺序有: 略。 顺序将它们收集一起后,链表的键值顺序有: 56 153 254 288 433 457 653 678 721 876 要求: 1、 试用C语言编程实现以上功能 2、 10个数字随机生成 3、 程序可读性好 ps:头文件#include<stdafx.h> 中包含 #include <stdio.h> #include <malloc.h> #include <time.h> #include <stdlib.h>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值