C语言二级-第十二套

在此程序中,函数fun的功能是将带头结点的单向链表结点数据域中的数据从小到大排序。

即若原链表结点数据域从头至尾的数据为:10、 4、2、8、6,排序后链表结点数据域从头至尾的数据为: 2、4、6、8、10。

#include  <stdio.h>
#include  <stdlib.h>
#define    N    6
typedef struct node {
  int  data;
  struct node  *next;
} NODE;
void fun(NODE  *h)
{ NODE  *p, *q;    int  t;
/**********found**********/
  p = __1__ ;
  while (p) {
/**********found**********/
     q = __2__ ;
     while (q) {
/**********found**********/
        if (p->data __3__ q->data)
        {  t = p->data;  p->data = q->data;  q->data = t;  }
        q = q->next;
    }
    p = p->next;
  }
}
NODE *creatlist(int  a[])
{  NODE  *h,*p,*q;        int  i;
  h = (NODE *)malloc(sizeof(NODE));
  h->next = NULL;
  for(i=0; i<N; i++)
  {  q=(NODE *)malloc(sizeof(NODE));
     q->data=a[i];
     q->next = NULL;
     if (h->next == NULL)  h->next = p = q;
     else    {  p->next = q;  p = q;   }
  }
   return  h;
}
void outlist(NODE  *h)
{ NODE  *p;
  p = h->next;
  if (p==NULL)  printf("The list is NULL!\n");
  else
  {  printf("\nHead  ");
     do
     {  printf("->%d", p->data); p=p->next;  }
     while(p!=NULL);
     printf("->End\n");
  }
}
void main()
{  NODE  *head;
   int  a[N]= {0, 10, 4, 2, 8, 6 };
   head=creatlist(a);
   printf("\nThe original list:\n");
   outlist(head);
   fun(head);
   printf("\nThe list after sorting :\n");
   outlist(head);
}

在此程序中,建立一个带头结点的单向链表,并用随机函数为各结点数据域赋值。函数fun的作用是求出单向链表结点(不包括头结点)数据域中的最大值,并且作为函数值返回。
 

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct aa
{ int data;
  struct aa *next;
} NODE;
int fun (NODE *h)
{ int max=-1;
  NODE *p;
/*************found**************/
  p=h;
  while(p)
       { if(p->data>max)
             max=p->data;
/*************found**************/
          p=h->next;
       }
  return max;
}
void outresult(int s, FILE *pf)
{ fprintf(pf, "\nThe max in link :%d\n",s);
}
NODE *creatlink(int n, int m)
{ NODE *h,*p,*s;
  int i;
  h=p=(NODE *)malloc(sizeof(NODE));
  h->data=9999;
  for(i=1;i<=n;i++)
    { s=(NODE *) malloc(sizeof(NODE));
      s->data=rand()%m; s->next=p->next;
      p->next=s;  p=p->next;
    }
  p->next=NULL;
  return h;
}
void outlink(NODE *h,FILE *pf)
{ NODE  *p;
  p=h->next;
  fprintf(pf, "\n The LIST :\n\n HEAD");
  while(p)
    { fprintf(pf, "->%d",p->data); 
  p=p->next;}
  fprintf(pf, "\n");
}
void main()
{ NODE *head; int m;
  system("CLS");
  head=creatlink(12,100);
  outlink(head,stdout);
  m=fun(head);
  printf("\nThe RESULT :\n"); 
  outresult(m,stdout);
}

在此程序中,编写函数fun,该函数的功能是:将M行N列的二维数组中的数据,按行的顺
序依次放到一维数组中,--维数组中数据的个数存放在形参n所指的存储单元中。
例如,若二维数组中的数据为:
33        33        33        33
44        44        44        44
55        55        55        55
则一维数组中的内容应该是:33 33 33 33 44 44 44 44 55 55 55 55。

#include <stdio.h>
void fun (int (*s)[10], int *b, int *n, int mm, int nn)
{
  
}
void main()
{
  FILE *wf;
  int w[10][10]={{33,33,33,33},{44,44,44,44},{55,55,55,55}}, i, j;
  int a[100]={0},n=0 ;
  printf("The matrix:\n");
  for (i=0; i<3; i++)
    {for (j=0;j<4;j++)  
        printf("%3d",w[i][j]);
     printf("\n");
    }
  fun(w,a,&n,3,4);
  printf("The A array:\n");
  for(i=0; i<n; i++)  
     printf("%3d",a[i]); 
  printf("\n\n");
/******************************/
  wf=fopen("out.dat","w");
  for(i=0; i<n; i++)  
     fprintf(wf,"%3d",a[i]); 
  fclose(wf);
/*****************************/
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A-沫_

感谢、感谢,感谢大佬的打赏!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值