数据结构-顺序表的排序操作-冒泡排序

  • 示例代码如下:(温馨提示:本文全部代码只在 EduCoder 平台上通过测试,仅供参考,如有运行错误请自行改正)
/*修改本程序,对顺序表进行冒泡排序,使其有序*/
#define MAXSIZE 100    /*宏定义*/
#define OK 1
#define OVERFLOW -2

#include "stdio.h"    /*包含输入输出文件*/

typedef int elemtype;
typedef struct          /*定义顺序表的结构*/
{elemtype vec[MAXSIZE]; /*顺序表数据成员所占据的存储空间*/
 int last;              /*顺序表中最后一个元素在数组中的下标(或向量中的位置)从0开始*/
}sequenlist;

 void listprint(sequenlist *L) /*输出线性表*/
 {
  int i;
  for(i=0;i<=(*L).last;i++)
  printf("%d ",L->vec[i]);
  printf("\n");
 }
 
 /***************************以下为代码补充区************************************/


 void maopao(sequenlist*S)
 {
     int i,j,flag,a;
     for (i=0;i<=(S->last)-2 ;i++)
     { flag=1; 
     for(j=(S->last)-1;j>=i-1;j--)
     if(S->vec[j+1]<S->vec[j])
    
     { flag=0; 
     a=S->vec[j+1];
     S->vec[j+1]=S->vec[j];
     S->vec[j]=a;
     
     }
   
    if(flag==1) 
    break;
   }
 }
 
 /******************************************************************************/
 
  int main()
 {
  sequenlist sl;
  int i;
  scanf("%d",&sl.last);
  
  for(i=0;i<=sl.last;i++)
  scanf("%d",&sl.vec[i]);
  listprint(&sl);
  /**************以下为代码补充区*******/

  maopao(&sl);
  /*******************************/
  listprint(&sl);
  return 0;
}

程序改错题:

错误代码:(2处错误)你能自己找到吗 ^ _ ^

#include "stdio.h"
#include "malloc.h"   /*包含动态分配内存函数*/
#define TRUE 1
#define FALSE 0

typedef int elemtype;
typedef struct node   /*链表结点类型定义*/
 {elemtype data;      /*结点数据域*/
  struct node *next;  /*结点的指针域*/
 }linklist;

 linklist *creatlist() 
 {			            
  int x;
  linklist *head,*r,*p;
  p=(linklist*)malloc(sizeof(linklist));
  head=p;
  p->next=NULL;
  r=p;
  scanf("%d",&x);
  while(x!=-1)
   {
    p=(linklist*)malloc(sizeof(linklist));
    p->data=x;
    p->next=NULL;
    r->next=p;
    r=r->next;
    scanf("%d",&x);
   }
  return (head);
  }

   /*有序表中的插入运算*/
   void Insert(linklist *head,elemtype x)
   {linklist *p,*s;
    q=head;
    p=head->next;
    while(p&&x>p->data)
    {q=p;p=p->next;	}
    
    s=(linklist *)malloc(sizeof(linklist));
    s->data=x;
    s->next=p;
    q->next=s;
   }
   
   /*打印出链表head中各个结点的值*/
  void print(linklist *head)  
  {linklist *p;
  p=head->next;
  while(p!=NULL)
  {printf("%d ",p->data);
   p=p->next;
  }
  printf("\n");
  }

 int main() /*主函数*/
 {linklist *head;  /*定义指向链表的指针head*/
  int x;
  printf("please input the linklist and end by -1:\n");
  head=creatlist();
  print(head);
  printf("now start insert,please input the insert value:\n");
  scanf("%d",&x);
  insert(head,x);
  print(head);
  return 0;
  }

修改后的代码:

#include "stdio.h"
#include "malloc.h"   /*包含动态分配内存函数*/
#define TRUE 1
#define FALSE 0

typedef int elemtype;
typedef struct node   /*链表结点类型定义*/
 {elemtype data;      /*结点数据域*/
  struct node *next;  /*结点的指针域*/
 }linklist;

 linklist *creatlist() 
 {			            
  int x;
  linklist *head,*r,*p;
  p=(linklist*)malloc(sizeof(linklist));
  head=p;
  p->next=NULL;
  r=p;
  scanf("%d",&x);
  while(x!=-1)
   {
    p=(linklist*)malloc(sizeof(linklist));
    p->data=x;
    p->next=NULL;
    r->next=p;
    r=r->next;
    scanf("%d",&x);
   }
  return (head);
  }

   /*有序表中的插入运算*/
   void insert(linklist *head,elemtype x)     //1.注意函数名要一致
   {linklist *p,*s,*q ;                       //2.上面错误代码中漏了 *q 哦(⊙o⊙)
    q=head;
    p=head->next;
    while(p!= NULL && x>p->data)              //这里这两种写法都是对的 : while(p&&x>p->data)
    {q=p;p=p->next;	}
    
    s=(linklist *)malloc(sizeof(linklist));
    s->data=x;
    s->next=p;
    q->next=s;
   }
   
   /*打印出链表head中各个结点的值*/
  void print(linklist *head)  
  {linklist *p;
  p=head->next;
  while(p!=NULL)
  {printf("%d ",p->data);
   p=p->next;
  }
  printf("\n");
  }

 int main() /*主函数*/
 {linklist *head;  /*定义指向链表的指针head*/
  int x;
  printf("please input the linklist and end by -1:\n");
  head=creatlist();
  print(head);
  printf("now start insert,please input the insert value:\n");
  scanf("%d",&x);
  insert(head,x);
  print(head);
  return 0;
  }

我把我目前写的关于数据结构 题目 的链接全部汇总整理在下面,有需要的小伙伴自己点击哈。

实验:

  • 4
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值