Re: [問題] 陣列架構一問
....原文吃光光
經過前人的指點迷津我了解原來 struct node 後面也可以搞陣列!!
找過很多書對這部份的講解都不太清楚,只有看到結構內放陣列這落落長(台)
看起來跟用起來都不太親切
原本我的結構宣告很複雜
struct node{
int data;
struct node *link;
struct node *next;
};
多宣告一個 struct node *next 來做出題目所要的架構
現在知道可以 struct node index[idx] 之後
我原本寫好的程式要來個大減肥了!!
現在的結構宣告就很單純了
struct node{
int data;
struct node *link;
};
然後在索引區那邊就直接用 struct node index[idx] 來做
哇塞!!我只能說我的程式可以減很大的肥了!!
原本要用到兩個額外的指標在索引區和資料區移來移去的現在只要用一個指標
(太開心了以至離題了....切回正題!!)
我試做了一下以下的動作,建立一個struct node index[]
然後在每個index[]後面接一個node
最後再印每個index[]後面接的node
#include <cstdio>
#include <cstdlib>
struct node{
int data;
struct node *link;
};
int main(void)
{
struct node *new_node,index[10];
int i;
for(i=0;i<10;i++)
{
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=i;
new_node->link=NULL;
index[i].link=new_node;
}
for(i=0;i<10;i++)
{
printf("%d\n",index[i].link->data);
}
return 0;
}
我想問的是:
Q1.index[]是屬於結構陣列?所以存取結構成員是index[i].data and index[i].link?
Q2.index[]後面所接上的是結構指標?所以存取其結構成員是
new_node->data and new_node->link?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 211.74.83.7
推
05/08 01:54, , 1F
05/08 01:54, 1F
→
05/08 01:56, , 2F
05/08 01:56, 2F
→
05/09 09:42, , 3F
05/09 09:42, 3F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章