[問題] 有關鏈結串列的問題
哈囉大家好唷,我想問一下大家,如果我現在手上有一筆存在矩陣中
的資料.我想用利用鏈結串列讓資料印出來,分別是正向跟反向
我要怎麼做呢?
#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
→
07/25 18:17, , 1F
07/25 18:17, 1F
→
07/25 18:17, , 2F
07/25 18:17, 2F
→
07/25 18:18, , 3F
07/25 18:18, 3F
→
07/25 18:31, , 4F
07/25 18:31, 4F
→
07/25 18:36, , 5F
07/25 18:36, 5F
→
07/25 18:40, , 6F
07/25 18:40, 6F
→
07/25 19:38, , 7F
07/25 19:38, 7F
→
07/25 19:57, , 8F
07/25 19:57, 8F
→
07/25 19:58, , 9F
07/25 19:58, 9F
→
07/25 20:07, , 10F
07/25 20:07, 10F
→
07/25 20:13, , 11F
07/25 20:13, 11F
→
07/25 20:18, , 12F
07/25 20:18, 12F
→
07/25 20:18, , 13F
07/25 20:18, 13F
※ 編輯: Godfrey0216 (59.102.168.7), 07/25/2015 20:29:04
→
07/25 20:29, , 14F
07/25 20:29, 14F
※ 編輯: Godfrey0216 (59.102.168.7), 07/25/2015 20:30:15
推
07/25 21:09, , 15F
07/25 21:09, 15F
→
07/25 21:57, , 16F
07/25 21:57, 16F
→
07/25 21:59, , 17F
07/25 21:59, 17F
→
07/25 22:25, , 18F
07/25 22:25, 18F
推
07/26 16:48, , 19F
07/26 16:48, 19F
→
07/26 16:48, , 20F
07/26 16:48, 20F
→
07/26 18:19, , 21F
07/26 18:19, 21F
推
07/26 21:57, , 22F
07/26 21:57, 22F
→
07/26 21:59, , 23F
07/26 21:59, 23F
→
07/26 22:59, , 24F
07/26 22:59, 24F
→
07/26 23:00, , 25F
07/26 23:00, 25F
討論串 (同標題文章)