初學者編程練習3

1. 快速排序算法

    [Accepted 2013/03/12]

void quick_sort(int data[], int low, int high) {        //對data[]做快速排序
	int pivoc = 0;

	if(low < high) {                                    //長度大於1
		pivoc = patition(data, low, high);              //將data[]一分爲二
		quick_sort(data, low, pivoc - 1);               //快速排序左邊
		quick_sort(data, pivoc + 1, high);              //快速排序右邊
	}
}

int patition(int data[], int low, int high) {          //將data[]一分爲二
	int m = 0;
	int i = 0;
	int j = 0;
	int temp = 0;

	m = data[low];                                     //記錄基準的值
	if(low < high) {
		i = low;
		j = high + 1;
		do {

			do {
				i = i + 1;
			}while(data[i] < m && i < high);
			do {
				j = j - 1;
			}while(data[j] > m && j > low);

			temp = data[i];                        //交換data[i]和data[j]
			data[i] = data[j];
			data[j] = temp;
		}while(i < j);

		temp = data[i];                            //交換data[i]和data[j]
		data[i] = data[j];
		data[j] = temp;

		temp = data[j];                           //交換data[low]和data[j]
		data[j] = data[low];
		data[low] = temp;
	}
	return j;                                     //返回j
}

 

Well done !

Every morning, you and sunshine ~

 

2. 以“Move-to-Front” 方式編寫一個動態線性查找表

   [Acceptd: 2013/03/12]

struct node* insert(struct node* head, int temp) {
	int flag = 0;
	struct node* q = NULL;
	struct node* p = NULL;
	struct node* p_pre = NULL;

	if(head == NULL) {                      //head爲空 直接返回q
		q = make_node(temp); 
		return q;                     
	}
	p = p_pre = head;
	if(p->data == temp)   return head;     //temp與第一個數據相等 返回head
	p = p->next;
	while(p != NULL) {
		if(p->data == temp) {              //遍歷鏈表查找temp數據
			flag = 1;
			break;
		}
		p_pre = p;
		p = p->next;
	}
	if(flag == 1) {                      //鏈表中存在temp數據, 將該節點放在頭部
		p_pre->next = p->next;
		p->next = head;
		head = p;
	}
	else {                               //鏈表中不存在temp數據,新建節點,並放在頭部
		q = make_node(temp);
		q->next = head;
		head = q;
	}
	return head;
}
 
struct node* make_node(int temp)         //建立新節點
{ 
	struct node* q = NULL;

	q = (struct node*)malloc(sizeof(struct node));
	if(q == NULL) {
		printf("Allocation fails!");
		exit(-1);
	}
	q->data = temp;
	q->next = NULL;   
	return q;
}

void print(struct node* head) {         //打印鏈表
	struct node* p = head;
	
	while(p != NULL) {
		printf("%d    ", p->data);
		p = p->next;
	}

	printf("\n");
}

int main() {
	int temp = 0;
	FILE* fp = NULL;
	char file_name[50] = "data.txt";
	struct node* head = NULL;

	if((fp = fopen(file_name, "r")) == NULL) {
		printf("File open error!");
		exit(-1);
	}

	while(!feof(fp)) {
		fscanf(fp, "%d", &temp);        //讀取data
		head = insert(head, temp);      //插入節點
		print(head);                    //打印鏈表
	}

	return 0;
}


 

3. 以“Move-One-Step” 方式編寫一個動態線性查找表

   [Accepted: 2013/03/13]

struct node* insert(struct node* head, int temp) {
	int flag = 0;
	struct node* p = NULL;
	struct node* q = NULL;
	struct node* p_pre = NULL;

	if(head == NULL) {
		q = make_node(temp);
		return q;
	}

	p = head;
	while(p != NULL) {
		if(p->data == temp) {
			flag = 1;
			break;
		}
		p_pre = p;
		p = p->next;
	}
	if(flag == 0) {
		q = make_node(temp);
		q->next = head;
		return q;
	}
	if(p_pre != NULL) {
		p->data = p_pre->data;
		p_pre->data = temp;
	}
	return head;
}

struct node* make_node(int temp)
{
	struct node* q = NULL;

	q = (struct node*)malloc(sizeof(struct node));
	if(q == NULL) {
		printf("Allocation fails!");
		exit(-1);
	}
	q->next = NULL;
	q->data = temp;
	return q;
}

void print(struct node* head) {
	struct node* p = NULL;

	p = head;
	while(p != NULL) {
		printf("%d    ", p->data);
		p = p->next;
	}
	printf("\n");
}

void free_list(struct node* head) {
	struct node* p = NULL;
	struct node* q = NULL;

	p = head;
	while(p != NULL) {
		q = p->next;
		free(p);
		p = q;
	}
}

int main() {
	int temp = 0;
	FILE* fp = NULL;
	char file_name[50] = "data.txt";
	struct node* head = NULL;

	if((fp = fopen(file_name, "r")) == NULL) {
		printf("File open error!");
		exit(-1);
	}

	while(!feof(fp)) {
		fscanf(fp, "%d", &temp);
		head = insert(head, temp);
		print(head);
	}

	free_list( head);
	fclose(fp);
	return 0;
}

速度越來越快啦,(*^__^*) 嘻嘻……

 

本週加試一題:

4.(附加題) 編程判斷一個有限單向鏈表是否含有一個環(節點數未知)。

    [Accepted 2013/03/21]

bool is_loop(struct node* head) {                      //判斷環是否存在
	struct node* fast = head;
	struct node* slow = head;
	
	if(head == NULL)  {
		printf("The link is empty!\n");
		return false;
	}
	if(head->next == NULL)   return false;
	while(1) {
		slow = slow->next;
		if(slow == NULL)    return false;
		fast =fast->next;
		if(fast == NULL) 	return false;
		fast =fast->next;
		if(fast == NULL) 	return false;
		if(fast == slow)    return true;
	}
}


 

發佈了28 篇原創文章 · 獲贊 4 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章