看板 C_and_CPP 關於我們 聯絡資訊
※ 引述《Godfrey0216 (Godfrey)》之銘言: : 哈囉大家好唷,我想問一下大家,如果我現在手上有一筆存在矩陣中 : 的資料.我想用利用鏈結串列讓資料印出來,分別是正向跟反向 #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 friendData { int ans[1024]; }; struct linkedListNode { struct friendData data; struct linkedListNode *nextPtr; }; typedef struct linkedListNode LinkedListNode; int main() { LinkedListNode *startPtr, *newPtr, *currentPtr ; startPtr = NULL; int n , c ; int m ; int j ; int r ; char ch ; int num[1024] ; cout << "請輸入N值 : " << endl ; cin >> n ; newPtr = (LinkedListNode *)malloc(sizeof(LinkedListNode)); for ( int i = 1 ; i <= n ; i++) { if( n%i ==0) { c++ ; num[c] = i ; cout << "第" << c << "個因數 : " << num[c] << endl ; } } cout << "因數總個數為 : " << c << "個" << endl ; cout << "====================================================" << endl; for( int i = 1 ; i <=c ; i++ ) { if(isPrime(num[i])) cout << num[i] << "為質數" << endl; else cout << num[i] << "不為質數" << endl; } cout << "====================================================" << endl; cout << "請選擇列印因數方法" << endl; cout << "1) 正向列印出所有因數 " << endl; cout << "2) 反向列印出所有因數 " << endl; cout << "3) 離開 " << endl; startPtr == NULL; newPtr->data.ans; newPtr->nextPtr = NULL; if (startPtr == NULL) { startPtr = newPtr; } else { currentPtr = startPtr; while (currentPtr != NULL) { if (currentPtr->nextPtr == NULL) { currentPtr->nextPtr = newPtr; break; } currentPtr = currentPtr->nextPtr; } } while(1){ ch = getchar(); switch(ch){ case '1' : { currentPtr = startPtr; while (currentPtr != NULL) cout << currentPtr-> data.ans <<endl; currentPtr = currentPtr->nextPtr; } break ; case '2' : for (int i=c; i>0; i--) cout <<num[i] << "," ; break ; case '3' : exit(0) ; break ; } } system("pause") ; return 0; } 親愛的大家,我後來又把程式改成這樣子,為什麼正向print出來會是 一直出現0x5d5fd0在螢幕上跑呢?不會停!問題出在哪了? 感覺好像資料沒進到鏈結空間裏耶! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.102.168.7 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1437981340.A.506.html
stupid0319: 你先試著找看是哪一行印出0x5d5fd0?? 07/27 17:58
Frozenmouse: 你從頭到尾只配了一個node而且也沒寫資料進去@@ 07/27 19:19
Frozenmouse: 另外為何你的node資料要是int陣列? 07/27 19:20
Godfrey0216: 因為我要他存num[]裡面的值,所以開了個陣列給他 07/27 20:28
Frozenmouse: 串列本身就是群集的概念,通常你是要存很多個int陣列 07/27 22:12
Frozenmouse: 才可能會這樣寫…我覺得你應該是想存int吧?XD 07/27 22:12
Godfrey0216: 不,我在main()裡面有用陣列存了我全部的因數 07/27 22:35
Godfrey0216: 而我現在想用串列把這些因數print出來! 07/27 22:35