file:func_dec.h
des: fuctions declaration
*****************************/
//fun_name:get_argc()
//usage:得到矩陣維數
//argcs:
//argcs usage:返回矩陣維數
int get_argc();
//fun_name:display()
//usage:打印矩陣
//argcs:鏈表頭指針,矩陣維數
//argcs usage:
void display(struct Node *,int );
//fun_name:free_mem()
//usage:釋放內存
//argcs:要釋放鏈表鏈表頭指針
//argcs usage:返回1代表釋放成功,否則失敗
int free_mem(struct Node *);
///fun_name:fill_blank()
//usage:對副鏈表進行數字填充
//argcs:副鏈表頭指針,索引節點,填充數字
//argcs usage:
void fill_blank(struct Node *,int ,int fill_num);
//fun_name:creat_link()
//usage:創建壹個鏈表,並返回鏈表頭指針
//argcs:鏈表初始頭指針,要創建矩陣維數
//argcs usage:
struct Node *creat_link(struct Node *,int );
//fun_name:Rotating()
//usage:對鏈表進行角度旋轉處理 90,180,270度
//argcs:鏈表頭指針,矩陣維數,旋轉選項
//argcs usage: 1 是90度旋轉
// 2 是180度旋轉
// 3 是270度旋轉
struct Node * Rotating(struct Node *,int ,int );
/************
*********/
struct Node{
int num;
struct Node *next;
};
/******************************
程序名:link
程序功能:實現用鏈表完成矩陣的旋轉轉換
作者:Hamlet
版本:1.2
程序說明:相對與1.1版本進行了函數的縮減,將無用的函數刪除,但未縮減時間和空間復雜度
func_dec.h是對程序函數聲明
struct_dec.h是對結構體聲明
遺憾的是本程序沒有使用叠代,如果用的話,程序將很漂亮。
開始寫作時間:2007-09-14
最後修改時間:2007-09-15
*******************************/
#include "stdio.h"
#include "stdlib.h"
#include "struct_dec.h"
#include "func_dec.h"
//得到矩陣參數
int get_argc(){
int argc = 0;
while(!argc){
printf("input argc:\n");
scanf("%d",&argc);
}
return argc;
}
//打印矩陣
void display(struct Node *head,int argc){
int switch_line = 0;
if(head == NULL)
return ;
while(head != NULL){
printf("%5d",head->num);
switch_line++;
if(!(switch_line%argc))
printf("\n");
head = head->next;
}
}
//內存釋放,1代表釋放成功
int free_mem(struct Node *head){
struct Node *st_temp;
if(head == NULL)
return 0;
while(head != NULL){
st_temp = head->next;
free(head);
head = st_temp;
}
return 1;
}
void fill_blank(struct Node *head,int search_node,int fill_num){
int current_node = 0;
while(current_node < search_node){
head = head->next;
current_node++;
}
head->num = fill_num;
return ;
}
//創建鏈表 arcg*argc
//返回鏈表頭指針
struct Node *creat_link(struct Node *head,int argc){
int current_node = 0;//當前所在接點處
int ele_amount = argc*argc;//元素個數
for(;current_node < ele_amount;current_node ++){
struct Node *st_temp = (struct Node*)malloc(sizeof(struct Node));
if(st_temp == NULL){
printf("memory loss!\n");
exit(0);
}
st_temp->num = current_node;
st_temp->next = head;
head = st_temp;
}
return head;
}
/*
函數名稱:Rotating()
函數功能:將矩陣旋轉
傳入參數:struct Node *head,int argc
傳出參數:無
參數說明:choice:1代表是旋轉90度,2代表旋轉180度,3代表旋轉270度
180度是旋轉兩個90度,270度是旋轉3個90度。
*/
struct Node * Rotating(struct Node *head,int argc,int choice){
struct Node *vice_head = NULL;
int loop_time = 0;//旋轉多少個90度
int r_counter = 0,c_counter = 0;//當前接點對應在數組中的位置 行與列
int current_node = 0;//當前接點標記
int search_node = 0;//索引接點
vice_head = creat_link(vice_head,argc);//創建副鏈表
struct Node * head_temp = head;
while(current_node < argc*argc){
r_counter = (int)current_node/argc;
c_counter = current_node%argc;
search_node = (argc-c_counter-1)*argc + r_counter;//索引搜索算法函數
fill_blank(vice_head,search_node,head_temp->num);
current_node++;
head_temp = head_temp->next;
}
//數據回拷
head_temp = head;
while(head_temp != NULL){
head_temp->num = vice_head->num;
head_temp = head_temp->next;
vice_head = vice_head->next;
}
free_mem(vice_head);
return head;
}
int main(){
int argc = 0;// 矩陣維數
int choice = 0;//選項 1-90度 2-180度 3-270度
int loop_time = 0;//旋轉次數計數器
struct Node *head = NULL;
argc = get_argc();
head = creat_link(head,argc);
//原矩陣打印
display(head,argc);
printf("1:90 2:180 3:270\n");
printf("input your choice:");
while(!choice)
scanf("%d",&choice);
while(loop_time++ < choice)
head = Rotating(head,argc,choice);
display(head,argc);
if(free_mem(head))
printf("success!\n");
else printf("error!\n");
return 0;
}
轉置90,180,270都可以,希望對妳有幫助