//联系人:石虎 QQ: 1224614774昵称:嗡嘛呢叭咪哄
一、冒泡排序
//1、对以下一组数据进行降序排序(冒泡排序)。“24,17,85,13,9,54,76,45,5,63”
int main(int argc,char *argv[]) {
int array[10] = {24,17, 85,13, 9,54, 76,45, 5,63};
int num =sizeof(array)/sizeof(int);
for(int i =0; i < num-1; i++) {
for(int j =0; j < num – 1 – i; j++) {
if(array[j] < array[j+1]) {
int tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
}}}
for(int i =0; i < num; i++) {
printf(“%d”, array[i]);
if(i == num-1) {
printf(“\n”);
}else {
printf(” “);
}
}
}
二、选择排序
//2、对以下一组数据进行升序排序(选择排序)。“86, 37, 56, 29, 92, 73, 15, 63, 30, 8”
void sort(int a[],int n){
int i, j, index;
for(i =0; i < n – 1; i++) {
index = i;
for(j = i +1; j < n; j++) {
if(a[index] > a[j]) {
index = j;
}}
if(index != i) {
int temp = a[i];
a[i] = a[index];
a[index] = temp;
}}}
int main(int argc,const char * argv[]) {
int numArr[10] = {86,37, 56,29, 92,73, 15,63, 30,8};
sort(numArr,10);
for (int i =0; i < 10; i++) {
printf(“%d, “, numArr[i]);
}
printf(“\n”);
return0;
}
三、链表逆序
//3、如何实现链表翻转(链表逆序)?
思路:每次把第二个元素提到最前面来。
#include <stdio.h>
#include <stdlib.h>
typedef struct NODE {
structNODE *next;
int num;
}node;
node *createLinkList(int length) {
if (length <=0) {
returnNULL;
}
node *head,*p,*q;
int number =1;
head = (node *)malloc(sizeof(node));
head->num =1;
head->next = head;
p = q = head;
while (++number <= length) {
p = (node *)malloc(sizeof(node));
p->num = number;
p->next =NULL;
q->next = p;
q = p;
}
return head;
}
void printLinkList(node *head) {
if (head ==NULL) {
return;
}
node *p = head;
while (p) {
printf(“%d “, p->num);
p = p -> next;
}
printf(“\n”);
}
node *reverseFunc1(node *head) {
if (head ==NULL) {
return head;
}
node *p,*q;
p = head;
q = NULL;
while (p) {
node *pNext = p ->next;
p -> next = q;
q = p;
p = pNext;
}
return q;
}
int main(int argc,const char * argv[]) {
node *head =createLinkList(7);
if (head) {
printLinkList(head);
node *reHead =reverseFunc1(head);
printLinkList(reHead);
free(reHead);
}
free(head);
return0;
}
谢谢!!!