- 示例代码如下:(温馨提示:本文全部代码只在 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;
}
我把我目前写的关于数据结构 题目 的链接全部汇总整理在下面,有需要的小伙伴自己点击哈。
- 数据结构 习题 第一章 概论
- 数据结构 习题 第二章 线性表 (C语言描述)
- 数据结构 习题 第三章 栈和队列 (C语言描述)
- 数据结构 习题 第四章 串 (C语言描述)
- 数据结构 习题 第五章 多维数组和广义表(C语言描述)
- 数据结构 习题 综合复习
实验: