再回首,資料結構——頭插法、尾插法建表
阿新 • • 發佈:2019-02-18
最近在複習資料結構,順便看看大一的時候寫的程式碼,看完之後比當初有了更加深刻的體會。
//頭插法建表 void CreateListF (Node *l, int a[], int n) { Node *s; int i; l = (Node *)malloc(sizeof(Node)); l->next = NULL; //建立頭結點 for (i = 0; i < n; i++) { s = (Node *)malloc(sizeof(Node)); s->data = a[i]; s->next = l->next; l->next = s; } }
//尾插法建表
void CreateListL (NOde *l, int a[], int n)
{
Node *s, *r;
int i;
l = (Node *)malloc(sizeof(Node));
l->next = NULL; //建立頭結點
r = l;
for (i = 0; i < n; i++)
{
s = (Node *)malloc(sizeof(Node));
s->data = a[i];
r->next = s;
r = s;
}
r->next = NULL;
}