# C语言描述顺序表

Sasura_321 2017-12-01 12:01:34

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

#include <stdio.h>
#define MAXSIZE 100

typedef int ElemType;
typedef struct
{
ElemType data[MAXSIZE];
int length;
}Sqlist;

void divide(Sqlist *&L)
{
int i = 0;
int j = L->length-1;
int temp = 0;
while(i<j)
{
if((L->data[i]%2)==0)
{
temp = L->data[i];
}
else
{
i++;
}
if((L->data[j]%2)==1)
{
L->data[i] = L->data[j];
L->data[j] = temp;
}
else
{
j--;
}
}
for(i=0; i<length; i++)
{
printf("%d ",L.elem);
}
}

int main()
{
int L->data[] = {8,4,6,1,44,96,12,5,55,89,54,36};
int length = sizeof(L->data)/sizeof(L->data[0]);
divide(L)；
return 0 ;
}

...全文
156 3 点赞 打赏 收藏 举报

3 条回复

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

#define MAXSIZE 100

typedef int ElemType;

typedef struct
{
ElemType data[MAXSIZE];
int length;
}Sqlist;

void swap(ElemType &a, ElemType &b)
{
ElemType c;
c = a;
a = b;
b = c;
}

void divide(Sqlist *&L)
{
int i = 0;
int j = L->length-1;
int temp = 0;

while(1)
{
for (; i < L->length; i++)
if (L->data[i] % 2 == 0)
break;

for (; j > i; j--)
if (L->data[j] % 2 == 1)
break;

if (i < j)
swap(L->data[i], L->data[j]);
else
break;

/*
if((L->data[i]%2)==0)
{
temp = L->data[i];
}
else
{
i++;
}
if((L->data[j]%2)==1)
{
L->data[i] = L->data[j];
L->data[j] = temp;
}
else
{
j--;
}
*/
}
}

void print_list(Sqlist *s)
{
int i;

for(i=0; i< s->length; i++)
printf("%d ", s->data[i]);
putchar(10);
}

int main()
{
Sqlist *L, s;
int n, i;
printf("Input the number of list: ");
scanf("%d", &n);
if (n > MAXSIZE)
return 0;
L = &s;
srand(time(NULL));
for (i = 0; i < n; i++)
L->data[i] = rand() % 100;
L->length = n;

printf("Before swap: \n");
print_list(L);
divide(L);
printf("After swap: \n");
print_list(L);
return 0 ;
}
``````

• 打赏
• 举报

• 打赏
• 举报

Sasura_321 2017-12-01

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

#define MAXSIZE 100

typedef int ElemType;

typedef struct
{
ElemType data[MAXSIZE];
int length;
}Sqlist;

void swap(ElemType &a, ElemType &b)
{
ElemType c;
c = a;
a = b;
b = c;
}

void divide(Sqlist *&L)
{
int i = 0;
int j = L->length-1;
int temp = 0;

while(1)
{
for (; i < L->length; i++)
if (L->data[i] % 2 == 0)
break;

for (; j > i; j--)
if (L->data[j] % 2 == 1)
break;

if (i < j)
swap(L->data[i], L->data[j]);
else
break;

/*
if((L->data[i]%2)==0)
{
temp = L->data[i];
}
else
{
i++;
}
if((L->data[j]%2)==1)
{
L->data[i] = L->data[j];
L->data[j] = temp;
}
else
{
j--;
}
*/
}
}

void print_list(Sqlist *s)
{
int i;

for(i=0; i< s->length; i++)
printf("%d ", s->data[i]);
putchar(10);
}

int main()
{
Sqlist *L, s;
int n, i;
printf("Input the number of list: ");
scanf("%d", &n);
if (n > MAXSIZE)
return 0;
L = &s;
srand(time(NULL));
for (i = 0; i < n; i++)
L->data[i] = rand() % 100;
L->length = n;

printf("Before swap: \n");
print_list(L);
divide(L);
printf("After swap: \n");
print_list(L);
return 0 ;
}
``````

• 打赏
• 举报

C语言

6.5w+

C语言相关问题讨论

2017-12-01 12:01