看板 C_and_CPP 關於我們 聯絡資訊
哈囉大家好唷,我想問一下大家,如果我現在手上有一筆存在矩陣中 的資料.我想用利用鏈結串列讓資料印出來,分別是正向跟反向 我要怎麼做呢? #include<stdio.h> #include<stdlib.h> #include <iostream> #include <cmath> using namespace std ; int isPrime(int p) { int i; for (i=2;i<=sqrt((double)p);i++) { if(p%i==0) return 0 ; } return 1 ; } struct _node { int data[1024] ; struct _node *next ; } ; typedef struct _node node ; node *getnode () /* 此函數產生一個新節點 */ { node *new_node; node *p; new_node = (node *) malloc(sizeof(node)); /* malloc 會動態地配置大小為sizeof 的記憶體*/ /* sizeof 會傳回一個型態為node之值*/ if ( new_node == NULL) { printf ("記憶體不足"); exit(1); } return(p); } void freenode (node *p) /* 此函數將節點還給記憶體 */ { free(p); } node *Inverse(node *head) { node *p = head, *temp = NULL; while(p->next != NULL) { head = p->next; p->next = temp; temp = p; p = head; } p->next = temp; return head; } int main() { node *head, *ptr; head = NULL ; ptr = NULL ; int n , c = 0 ; int m ; int j ; int r ; char ch; int ans[1027] = {0} ; cout << "請輸入N值 : " << endl ; cin >> n ; for ( int i = 1 ; i <= n ; i++) { if( n%i ==0) { c++ ; ans[c] = i ; cout << "第" << c << "個因數 : " << ans[c] << endl ; } } cout << "因數總個數為 : " << c << "個" << endl ; cout << "====================================================" << endl; for( j = 1 ; j <=c ; j++ ) { if(isPrime(ans[j])) cout << ans[j] << "為質數" << endl; else cout << ans[j] << "不為質數" << endl; } cout << "=========================================" << endl; cout << "請選擇列印因數方法" << endl; cout << "1) 正向列印出所有因數 " << endl; cout << "2) 反向列印出所有因數 " << endl; cout << "3) 離開 " << endl; while(1){ ch = getchar(); switch(ch){ case '1' : for( m=1 ; m<c ; m++) cout << ans[m] << "," ; break ; case '2' : for (int m=c; m>0; m--) cout <<ans[m] << "," ; break ; case '3' : exit(0) ; } } system("pause") ; return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.102.168.7 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1437818285.A.F9C.html
Feis: 你為什麼要這麼做 ? 07/25 18:17
Godfrey0216: 因為我這樣做阿!! 07/25 18:17
Godfrey0216: 因為我想這樣做阿!! 07/25 18:18
Feis: 了解~ 07/25 18:31
LPH66: (水晶球占卜中) 07/25 18:36
Frozenmouse: 我的水晶球裂了T.T 07/25 18:40
KoenigseggG: 你要怎麼做呢? 07/25 19:38
Godfrey0216: 我的想法是,一直輸出矩陣的資料,把其當成節點來看 07/25 19:57
Godfrey0216: 但是我的想法程式跑不出來~~ 07/25 19:58
Feis: 那你不覺得應該放一下你的程式碼嗎? QQ 07/25 20:07
huei820504: 你可以用for按照你想的順序跑? 07/25 20:13
Godfrey0216: 我是很想放阿,但是放了你們會笑死,我雖然有概念 07/25 20:18
Godfrey0216: 但程式碼慘不忍睹! 07/25 20:18
※ 編輯: Godfrey0216 (59.102.168.7), 07/25/2015 20:29:04
Godfrey0216: 覺得我的輸出跟鏈結一點關係也沒有 07/25 20:29
※ 編輯: Godfrey0216 (59.102.168.7), 07/25/2015 20:30:15
cpper: 你應該去找一本c/c++資料結構教科書,裡面範例仔細練習過 07/25 21:09
twitcha101: 你在ch = getchar();的前面 +個getchar();洗\r 07/25 21:57
twitcha101: 後面case1 改m<c+1 07/25 21:59
twitcha101: 拍謝我放錯焦點了,沒看清楚 07/25 22:25
Frozenmouse: 別怕人家笑,有貼出來才好討論XD 07/26 16:48
Frozenmouse: 我在main裡好像沒看到對串列的操作? 07/26 16:48
Godfrey0216: 對阿,就是不知道怎麼連結在一起,把他一個一個輸出 07/26 18:19
Frozenmouse: 所以你是想把ans存成串列嗎?因為正反序印好像也有了 07/26 21:57
Frozenmouse: 要能正反序印建議看看雙向鏈結串列,你寫的是單向的 07/26 21:59
Godfrey0216: yes!我現在的問題是,我書出把他變成,ptr->ans[], 07/26 22:59
Godfrey0216: 是不行的!鏈結串列這邊我真的頭很暈y 07/26 23:00