//单链表基本操作
#include <stdio.h>
#include <stdlib.h>
typedef int datatype;
typedef struct node{
datatype data;
struct node *next;
}Lnode,*linklist;
/******************************************/
/函数名称:creatbystack() /
/函数功能:头插法建立带头结点的单链表 /
/**************************************/
linklist creatbystack()
{
linklist head,s;
datatype x;
head=(linklist)malloc(sizeof(Lnode));
head->next=NULL;
printf(“请输入若干整数序列(以0结束输入):\n”);
scanf("%d",&x);
while(x!=0)
{
s=(linklist)malloc(sizeof(Lnode));
s->data=x;
s->next=head->next;
head->next=s;
scanf("%d",&x);
}
return head;
}
/<

这篇博客介绍了如何使用C语言实现有序单链表的插入操作。首先通过头插法和尾插法创建带头结点的单链表,然后提供了一个`insert`函数用于在链表中插入元素并保持有序,最后展示如何打印链表和释放链表内存。在`main`函数中,演示了创建链表、打印链表、插入元素和再次打印链表的过程。
最低0.47元/天 解锁文章
384

被折叠的 条评论
为什么被折叠?



