#include<stdio.h>
#include<stdlib.h>
#include"LinkList.h" //创建单链表
void CreateList(LinkList L,DataType a[],int n){
int i;
for(i=1;i<=n;i++)
InsertList(L,i,a[i-1]);
}
//用链表实现选择排序。 将链表分为两段,p指向应经排序的链表部分。q指向未排序的链表部分
void SelectSort(LinkList L){
ListNode *p,*q,*t,*s;
p=L;
while(p->next->next!=NULL){ //用q指针进行遍历链表
for(s=p,q=p->next;q->next!=NULL;q=q->next)
if(q->next->data<s->next->data)
s=q; //假设q指针指向的元素值小于s指向的元素值,则s=q
if(s!=q){ //假设*s不是最后一个结点,则将s指向的结点链接到p指向的链表后面
t=s->next; //将结点*t从q指向的链表中取出
s->next=t->next;
t->next=p->next; //将结点*t插入到p指向的链表中
p->next=t;
}
p=p->next;
}
}
void main_Select(){
LinkList L,p;
int n=8;
DataType a[]={45,67,21,98,12,39,81,53};
InitList(&L);
CreateList(L,a,n);
printf("排序前:\n");
for(p=L->next;p!=NULL;p=p->next)
printf("%d ",p->data);
printf("\n");
SelectSort(L);
printf("排序后:\n");
for(p=L->next;p!=NULL;p=p->next)
printf("%d ",p->data);
printf("\n");
system("pause");
}
//链表的插入排序
void InsertSort(LinkList L){
ListNode *p=L->next;
ListNode *pre,*q;
L->next=NULL; //初始时,已排序链表为空
while(p!=NULL){ //p是指向待排序的结点
if(L->next==NULL){ //假设*p是第一个结点。则插入到L,并令已排序的最后一个结点的指针域为空
L->next=p;
p=p->next;
L->next->next=NULL;
}else{ //p指向待排序的结点,在L指向的已经排好序的链表中查找插入位置
pre=L;
q=L->next;
while(q!=NULL&&q->data<p->data){ //在q指向的有序表中寻找插入位置
pre=q;
q=q->next;
}
q=p->next; //q指向p的下一个结点。保存待排序的指针位置
p->next=pre->next; //将结点*p插入到结点*pre的后面
pre->next=p; //p指向下一个待排序的结点
p=q;
}
}
}
void main_Insert(){
LinkList L,p;
int n=8;
DataType a[]={87,34,22,93,102,56,39,21};
InitList(&L);
CreateList(L,a,n);
printf("排序前:\n");
for(p=L->next;p!=NULL;p=p->next)
printf("%d ",p->data);
printf("\n");
InsertSort(L);
printf("排序后:\n");
for(p=L->next;p!=NULL;p=p->next)
printf("%d ",p->data);
printf("\n");
system("pause");
}
void main(){
main_Select();
main_Insert();
}
//单链表
#pragma once
#include<stdio.h>
#include<stdlib.h>
typedef int DataType; typedef struct Node
{
DataType data; //数据域
struct Node *next; //指针域
}ListNode,*LinkList; //将单链表初始化为空。 动态生成一个头结点。并将头结点的指针域置为空
void InitList(LinkList *head){
if((*head=(LinkList)malloc(sizeof(ListNode)))==NULL)
exit(-1);
(*head)->next=NULL;
}
//推断单链表是否为空,就是通过推断头结点的指针域是否为空
int ListEmpty(LinkList head){
if(head->next==NULL)
return 1;
else
return 0;
}
//查找单链表中第i个结点。查找成功返回该结点的指针表示成功;否则返回NULL表示失败
ListNode *Get(LinkList head,int i){
ListNode *p;
int j;
if(ListEmpty(head))
return NULL;
if(i<1)
return NULL;
j=0;
p=head;
while(p->next!=NULL&&j<i){
p=p->next;
j++;
}
if(j==i)
return p;
else
return NULL;
}
//查找线性表中元素值为e的元素,查找成功将相应元素的结点指针返回。否则返回NULL表示失败
ListNode *LocateElem(LinkList head,DataType e){
ListNode *p;
p=head->next;
while(p){
if(p->data!=e)
p=p->next;
else
break;
}
return p;
}
//查找线性表中元素值为e的元素,查找成功将相应元素的序号返回,否则返回0表示失败
int LocatePos(LinkList head,DataType e){
ListNode *p;
int i;
if(ListEmpty(head))
return 0;
p=head->next;
i=1;
while(p){
if(p->data==e)
return i;
else{
p=p->next;
i++;
}
}
if(!p)
return 0;
}
//在单链表中第i个位置插入一个结点。结点的元素值为e。插入成功返回1,失败返回
int InsertList(LinkList head,int i,DataType e){
ListNode *p,*pre;
int j;
pre=head;
j=0;
while(pre->next!=NULL&&j<i-1){
pre=pre->next;
j++;
}
if(!pre){
printf("wrong place\n");
return 0;
}
if((p=(LinkList)malloc(sizeof(ListNode)))==NULL)
exit(-1);
p->data=e;
p->next=pre->next;
pre->next=p;
return 1;
}
//删除单链表中的第i个位置的结点。删除成功返回1。失败返回0
int DeleteList(LinkList head,int i,DataType *e){
ListNode *pre,*p;
int j;
pre=head;
j=0;
while(p->next!=NULL&&pre->next->next!=NULL&&j<i-1){
pre=pre->next;
j++;
}
if(j!=i-1){
printf("delete wrong place\n");
return 0;
}
p=pre->next;
pre->next=p->next;
free(p);
return 1;
}
int ListLength(LinkList head){
ListNode *p;
int count=0;
p=head;
while(p->next!=NULL){
p=p->next;
count++;
}
return count;
}
void DestroyList(LinkList head){
ListNode *p,*q;
p=head;
while(p!=NULL){
q=p;
p=p->next;
free(q);
}
}

_DataStructure_C_Impl:LinkListBasedSort的更多相关文章

  1. _DataStructure_C_Impl:图的邻接矩阵存储

    //_DataStructure_C_Impl:邻接矩阵 #include<stdio.h> #include<stdlib.h> #include<string.h&g ...

  2. _DataStructure_C_Impl:SeqListBasedSort

    // _DataStructure_C_Impl:Sort #include<stdio.h> #include<stdlib.h> #define MaxSize 50 ty ...

  3. _DataStructure_C_Impl:链串

    //_DataStructure_C_Impl:链串 #include<stdio.h> #include<stdlib.h> #include<string.h> ...

  4. _DataStructure_C_Impl:AOE网的关键路径

    //_DataStructure_C_Impl:CriticalPath #include<stdio.h> #include<stdlib.h> #include<st ...

  5. _DataStructure_C_Impl:Dijkstra算法求最短路径

    // _DataStructure_C_Impl:Dijkstra #include<stdio.h> #include<stdlib.h> #include<strin ...

  6. _DataStructure_C_Impl:图的最小生成树

    #include<stdio.h> #include<stdlib.h> #include<string.h> typedef char VertexType[4] ...

  7. _DataStructure_C_Impl:Floyd算法求有向网N的各顶点v和w之间的最短路径

    #include<stdio.h> #include<stdlib.h> #include<string.h> typedef char VertexType[4] ...

  8. _DataStructure_C_Impl:图的遍历

    #include<stdio.h> #include<stdlib.h> #include<string.h> //图的邻接表类型定义 typedef char V ...

  9. _DataStructure_C_Impl:求图G中从顶点u到顶点v的一条简单路径

    #pragma once #include<stdio.h> #include<stdlib.h> #define StackSize 100 typedef int Data ...

随机推荐

  1. VB学习笔记(一)VB操作字符串

    在vb中 dim a# 定义a变量为双精度型变量~ #是类型符 % 整型 & 长整型 !单精度 $ 字符型 VB中strconv 的作用 StrConv("要转换的字符串" ...

  2. springMVC接受数组

    var obj = {}; var params = new Array(); var selected1 = $('#datatable').DataTable().rows('.selected' ...

  3. 【转载】spring-boot 项目跳转到JSP页面

    原路径:https://blog.csdn.net/qq_36820717/article/details/80008225 1.新建spring-boot项目  目录结构如下 2.新建TestCon ...

  4. window下搭建Python3.7+selenium3.1.1+pycharm环境

    1.安装Python3.7 1.1  下载 Python并安装 Python3.5 (勾选上 Add Python3.7 to PATH) 点击 Install Now,安装完成后将python路径加 ...

  5. 养活一款APP要“烧”多少钱?

    Duang!又一款APP刷爆朋友圈.大片范儿的电影截图.意味深长的经典对白均出自一款名为“足记”的APP. 足记团队刚于去年8月完成天使期融资,投资方是光速创投和紫辉创投,目前正准备A轮融资.且近一周 ...

  6. IOS开发-经常使用站点集合

    1.    https://developer.apple.com  //苹果开发人员站点 2.    https://itunesconnect.apple.com  //itunes站点 3.   ...

  7. Android 主界面长按创建快捷方式

    Android中创建快捷方式主要有两种方式.一是在代码中直接加入生成桌面快捷方式的代码:二是通过小部件加入; 这篇文章主要讲另外一种方法! 1.通过在AndroidManifest文件里为Activi ...

  8. Android5.0以上系统的移动网络开关

    笔者近期遇到一个非常有意思的bug,贴出来和大家分享下. 那是一个温暖的早晨,阳光晒得人非常舒服.一封bug邮件像一片叶子飘到我的邮箱. 一番交流.笔者确认负责的Widget开关在Android5.0 ...

  9. ★★★【卡法 常用js库】: js汇合 表单验证 cookie设置 日期格式 电话手机号码 email 整数 小数 金额 检查参数长度

    [卡法 常用js库]: js汇合 表单验证  cookie设置  日期格式  电话手机号码  email  整数  小数  金额   检查参数长度 // +---------------------- ...

  10. [AtCoder Regular Contest 096 E] Everything on It 解题报告 (第二类斯特林数+容斥原理)

    题目链接:https://arc096.contest.atcoder.jp/tasks/arc096_c Time limit : 4sec / Memory limit : 512MB Score ...