#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. 简洁的MVC思想框架——Nancy(Session的使用)

    前文提到关于Nancy中GET和POST以及外部引用图片,css和JS的文件等操作.今天所讲的是Nancy关于Session相关操作. Session作为web开发中极其重要的一部分,而Nancy中S ...

  2. Oralce中的package和package body

    1.Oracle Package的作用: 可以简化应用设计.提高应用性能.实现信息隐藏.子程序重载 2.ORACLE中的function   .package.package   bodies.pro ...

  3. Visual Studio 2013 无法创建MVC项目,系统找不到指定的文件.(Exception from HRESULT:08x0070002)

    在Visual Studio 2013中创建新MVC项目,(PS:现在创建个MVC项目,差点都找不到在哪,汗!-) 确定后提示,系统找不到指定的文件.(Exception from HRESULT:0 ...

  4. [WebGL入门]十五,为多边形涂抹颜色(顶点颜色的指定)

    注:文章译自http://wgld.org/.原作者杉本雅広(doxas),文章中假设有我的额外说明,我会加上[lufy:].另外.鄙人webgl研究还不够深入.一些专业词语.假设翻译有误.欢迎大家指 ...

  5. 移植u-boot-2014.4到S5PV210/TQ210(完整)

    本文很多其它的是教会大家怎样学习 1.1   概述 1.2   u-boot配置过程分析 1.3   u-boot编译过程分析 1.4   SPL 1.5   加入自己的单板 1.6   移植u-bo ...

  6. win8装win7出现蓝屏的解决方式

    今天用PE装系统,在进入PE前会出现蓝屏: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzAxODcyMQ==/font/5a6L5L2T/fo ...

  7. jsp页面中自定义标签的小演示

    在实习期遇到公司的pg自定义标签了,同事要我自己自学一下 自定义标签是用户定义的JSP语言元素.当JSP页面包含一个自定义标签时将被转化为servlet.JSP标签扩展可以让你创建新的标签并且可以直接 ...

  8. angularjs 模块化

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  9. bzoj1801: [Ahoi2009]chess 中国象棋(DP)

    1801: [Ahoi2009]chess 中国象棋 题目:传送门 题解: 表示自己的DP菜的抠脚 %题解... 定义f[i][j][k]表示前i行 仅有一个棋子的有j列 有两个棋子的有k个 的方案数 ...

  10. tp5使用oss存储图片

    1.申请Access Key ID和Access Key Secret,以及创建好你的Bucket 2.通过composer安装oss插件 通过cmd,到项目的目录下,输入下面的指令. compose ...