可直接运行的完整C语言版有序单链表的生成和逆置
标签:数据结构、线性表、带头结点的单链表、逆置单链表
欢迎与喜欢数据结构的小伙伴们一起研究和探讨,喜欢你们的意见和建议,将代码分享与你们,有任何疑问可以留言喔,刚刚入门数据结构希望能够共同进步!
include<stdio.h>
include<stdlib.h>
//定义结构体类型
typedef struct danlian {
float m;
struct danlian * Next;
}Node,*pnode;
//有序插入生成带头结点的单链表
void Inserts(Node * Head,float datas) {
Node * new = (Node *)malloc(sizeof(Node));
new->m = datas;
new->Next = NULL;
Node * top = Head;
while (top->Next != NULL&&top->Next->m < datas) top = top->Next;
new->Next = top->Next;
top->Next = new;
}
//逆置单链表
void verst(Node *head) {
Node * top, *p, *q, *r ;
top = head;
p = top->Next;
if (p == NULL) return;
q = p->Next;
p->Next = NULL;
while (q != NULL) {
top->Next = q;
r = q->Next;
q->Next = p;
p = q;
q = r;
}
}
//打印单链表数据
void showlianbiao(Node * head) {
Node *top = head->Next;
while (top != NULL) {
printf("%f->", top->m);
top = top->Next;
}
}
void main(){
Node * Head = (Node *)malloc(sizeof(Node));
Head->Next = NULL;
//有序地插入
Inserts(Head, 6.8);
Inserts(Head, 9.8);
Inserts(Head, 2.8);
showlianbiao(Head);
printf("\n");
//翻转
verst(Head);
showlianbiao(Head);
printf("\n");
}
-------------------------------------------代码运行结果---------------------------------------------

网友评论