Re: [問題] 有關鏈結串列的問題
※ 引述《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
推
07/27 17:58, , 1F
07/27 17:58, 1F
推
07/27 19:19, , 2F
07/27 19:19, 2F
→
07/27 19:20, , 3F
07/27 19:20, 3F
→
07/27 20:28, , 4F
07/27 20:28, 4F
推
07/27 22:12, , 5F
07/27 22:12, 5F
→
07/27 22:12, , 6F
07/27 22:12, 6F
→
07/27 22:35, , 7F
07/27 22:35, 7F
→
07/27 22:35, , 8F
07/27 22:35, 8F
討論串 (同標題文章)