资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c语言有序函数代码 c语言数列有序

c语言中的排序代码 ,求大神,有木有?

上面既有排序,又有合并,不想要合并的话,把跟这个函数有关的函数,和调用删除就行了!!!

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、网络空间、营销软件、网站建设、鄂托克前网站维护、网站推广。

#includestdio.h

#includestdlib.h

struct LNode //创建链表结点

{ int data;

struct LNode *next;

};

struct LNode *creat() //链表创建函数

{ struct LNode *head,*p,*rear;

int x,n;

head=(struct LNode *)malloc(sizeof(struct LNode));//链表头指针

rear=head;

puts("输入链表元素,以0结束:");

scanf("%d",x); //链表元素输入

while(x) //链表节点链接

{ p=(struct LNode *)malloc(sizeof(struct LNode));

p-data=x; rear-next=p;

rear=p; scanf("%d",x);

}

rear-next=NULL;

return head-next; //返回链表中第一个有值结点地址

}

struct LNode *Merger_Linklist( struct LNode *L1,struct LNode *L2 ) //链表合并函数

{ struct LNode *p;

for(p=L1;p-next!=NULL;p=p-next) //查找链表L1的尾结点地址

{ if(p-next==NULL)

break;

}

p-next =L2; //将链表L2链接在L1之后

return L1;

}

struct LNode *Rand(struct LNode *L) //链表排序函数

{ struct LNode *Cur; //当前节点

struct LNode *Next; //遍历未排序节点

struct LNode *min; //指向未排序节点中最小节点

int temp;

for(Cur = L; Cur-next!= NULL; Cur = Cur-next)

//从头节点的下一个节点开始,一直到倒数第二个节点

{ min = Cur;

for(Next = Cur-next; Next != NULL; Next = Next-next)

{ if(min-data Next-data)

{min = Next;}

}

temp = Cur-data; //链表数值交换

Cur-data = min-data;

min-data = temp;

}

return L;

}

void print(struct LNode *head,char a) //链表打印函数

{ struct LNode *p;

p=head;

printf("%c = ( ",a);

while( p )

{ printf("%d ",p-data);

p=p-next;

}

puts(" )\n");

}

struct LNode *DeleteC_Linklist( struct LNode *L )//删除链表重复数

{ int key,had;

struct LNode *h,*p,*r,*q;

for(p=L;p-next!=NULL;p=p-next)

{ had = p-data;

for(r=p,q=p-next;q!=NULL;)

{ key = q-data;

if( key==had )

{ r-next=q-next; h=q;

q=q-next; free( h );

}

else

{ q=q-next; r=r-next;

}

}

}

return L;

}

void main () //主函数

{ struct LNode *L1,*L2,*L3;

char a='A',b='B',c='C',d='D';

L1 = creat(); //创建链表L1

Rand(L1); //对L1排序

puts("\n创建的链表经排序后为:");

print(L1,a);

L2 = creat(); //创建链表L2

Rand(L2); //对L2排序

puts("\n创建的链表经排序后为:");

print(L2,b);

L3=Merger_Linklist( L1,L2 ); //合并链表L1和L2

Rand(L3); //对合并后的链表排序

puts("合并后的链表为:");

print(L3,c);

DeleteC_Linklist( L3 );

printf("删除重复数后的链表为:\n");

print(L3,d); //打印删除后的链表

}

C语言用函数写个代码来给数字大小序号排序

#includestdio.h

#define N 100

void paixu(int *, int *, int);//声明函数

int main()

{

int a[N],c[N];

int i,n=0;

printf("请输入n个整数:");

for(i=0;;i++)

{

scanf("%d",a[i]);

n++;

if(getchar()=='\n')

break;

}

paixu(a, c, n);//调用函数,数组只需给出数组名

return 0;

}

void paixu(int a[N],int c[N], int n)

{

int i,j,s=0;

for(i=0;in;i++)

{

c[i]=0;

}

for(i=0;in;i++)

for(j=i+1;jn;j++)

{

if(a[i]==a[j])

c[i]=1;

}

for(i=0;in;i++)

{

s=1;

for(j=0;jn;j++)

{

if(c[j]==0a[i]a[j])

{

s++;

}

}

printf("%d ",s);

}

}

一维数组的有序插入,用C语言写。

以下是一个可能的实现,包括insertX函数和主函数示例:

#include stdio.h

int insertX(int* pa, int n, int x) {

int i, j;

// 找到插入位置

for (i = 0; i n; i++) {

if (pa[i] x) {

break;

}

}

// 将插入位置后的元素后移

for (j = n; j i; j--) {

pa[j] = pa[j - 1];

}

// 插入元素

pa[i] = x;

// 返回插入后数组的长度

return n + 1;

}

int main() {

int n, x;

printf("请输入有序数列的长度n:");

scanf("%d", n);

int a[n];

printf("请输入%d个有序整数:\n", n);

for (int i = 0; i n; i++) {

scanf("%d", a[i]);

}

printf("请输入要插入的整数x:");

scanf("%d", x);

n = insertX(a, n, x);

printf("插入后的有序整数为:\n");

for (int i = 0; i n; i++) {

printf("%d ", a[i]);

}

printf("\n");

return 0;

}

在上述代码中,我们定义了一个insertX函数来实现将一个整数x插入到一个有序数组中的功能。该函数的参数包括一个指向数组首地址的指针pa,数组的长度n,以及要插入的整数x。函数的具体实现过程如下:

遍历数组,找到插入位置,即第一个大于x的元素的位置i;

将插入位置后的元素后移一位;

在插入位置处插入x;

返回插入后数组的长度n+1。

在主函数中,我们先输入有序数列的长度n和n个有序整数,然后输入要插入的整数x。接着调用insertX函数将x插入到数组中,并输出插入后的有序整数序列。

需要注意的是,上述代码并没有对输入的数据进行范围检查,如果输入的数据不符合要求,程序可能会出现错误。因此,在实际使用中应该添加相应的数据检查和错误处理机制。

用c语言编写函数,对给定两个有序(升序)一维数组a,b进行合并,构成有序数组c。

#includestdio.h

#includestdlib.h

#includelimits.h

#define datelimit 1000 + 1 + 1

#define n 5

#define m 5 //datelimit 是最大的数组数 注意别开爆 n和m是a、b两队列的元素数用的时候修改后面的数字就可以了

int main()

{

int a[datelimit],b[datelimit],c[datelimit * 2];

int i,j,head;

int inf = INT_MAX;

for(i = 1 ;i = n;i++)

3scanf("%d",a[i]);

for(i = 1 ;i = m;i++)

scanf("%d",b[i]);

a[n + 1] = inf ;

b[m + 1] = inf ; //处理a或b的队列已经出完

i = 1;

j = 1;

head = 1;

while(i = n + 1 j = m + 1)

{

if(a[i] b[j])

{

c[head] = b[j];

head ++;

j ++;

}

else

{

c[head] = a[i];

head ++;

i ++;

}

} //进入c队列

for(i = 1 ;i = n + m;i++)

printf("%d ",c[i]);

system("pause"); //最好删去这一句 以免使用时出错

return 0;

}


名称栏目:c语言有序函数代码 c语言数列有序
文章链接:http://cdkjz.cn/article/dopecsd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220