# 无法输出是怎么回事？

Sasura_321 2017-12-08 08:27:11
//有一个顺序表L，假设元素类型ElemType为整数
//设计一个尽可能高级的算法，将所有奇数移到偶数的前面

#include <stdio.h>
#include<stdlib.h>

#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10

typedef int ElemType;
typedef struct
{
ElemType *elem;
int length;
int listsize;
}SqList;

void InitList(SqList &L,int arr[]) //构造一个线性表
{
L.elem = (ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
int i = 0;
L.length = 0;
for (i = 0; i < 8; i++)
{
L.elem[i] = arr[i];
L.length++;
}
L.listsize = LIST_INIT_SIZE;//初始储存容量
}

void MoveList(SqList &L)
{
int i = 0;
int j = L.length-1;
int temp = 0;

while(i<j)
{
while(i<j && L.elem[i]%2 == 0)
i--;
while(j>i && L.elem[j]%2 == 1)
j++;
if (i < j)
{
temp = L.elem[i];
L.elem[i] = L.elem[j];
L.elem[j] = temp;
}
}
}

void DisList(SqList L) //输出
{
int i = 0;
for(i = 0;i<L.length;i++)
printf("%d", L.elem[i]);
printf("\n");

}
int main()
{
SqList L;
int arr[] = {1,2,3,4,5,6,7,8};
InitList(L,arr);
MoveList(L);
DisList(L);
return 0;
}
...全文
93 3 点赞 打赏 收藏 举报

3 条回复

``````#include <stdio.h>
#include<stdlib.h>

#define LIST_INIT_SIZE  100
#define LISTINCREMENT   10

typedef int  ElemType;
typedef  struct
{
ElemType *elem;
int length;
int listsize;
}SqList;

void InitList(SqList &L,int arr[], int size)  //构造一个线性表
{
int i = 0;
L.elem = (ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
L.length = 0;
for (i = 0; i < size; i++)
{
L.elem[i] = arr[i];
L.length++;
}
L.listsize = LIST_INIT_SIZE;//初始储存容量
}

void MoveList(SqList &L)
{
int i = 0;
int j = L.length-1;
int temp = 0;

while(i<j)
{
while(i<j && L.elem[i]%2 == 0)
i++;    /*从开头向后，需要i++*/
while(j>i && L.elem[j]%2 == 1)
j--;    /* 从末尾向前，需要j--*/
if (i < j)
{
temp = L.elem[i];
L.elem[i] = L.elem[j];
L.elem[j] = temp;
}
}
}

void DisList(SqList L)  //输出
{
int i = 0;
for(i = 0;i<L.length;i++)
printf("%d ", L.elem[i]);
printf("\n");

}
int main()
{
SqList L;
int arr[] = {1,2,3,4,5,6,7,8};
InitList(L,arr, 8);
MoveList(L);
DisList(L);
return 0;
}``````

• 打赏
• 举报

paschen 2017-12-10

• 打赏
• 举报

• 打赏
• 举报

C语言

6.5w+

C语言相关问题讨论

2017-12-08 08:27