---
# System prepended metadata

title: Linked list-Struct 細節探討與學習(下)
tags: [資料結構]

---

# Linked list-Struct 細節探討與學習(下)
###### tags: `資料結構`
Copyright 2021, [月下麒麟](https://hackmd.io/@YMont/note-catalog)

---
## Outline
關於linked-list結構應用，可以先參考上部
[Linked list-Struct 細節探討與學習(上)](https://hackmd.io/@YMont/B18SDqFXt)


## 結構建立, 輸出, 釋放
>linked list 實作
```c=
#include <stdio.h>
#include <stdlib.h>

struct t_node{
  int data;
  struct t_node *next;
};

typedef struct t_node Node;

//declare
Node *createList(int*, int);
void printList(Node *);
void freeList(Node *);

int main(void) {
  Node *first;
  int arr[5] = {17, 19, 21, 23, 25};
  first = createList(arr, 5);
  printList(first);
  freeList(first);

  return 0;
}

// create function
Node *createList(int *arr, int len){
  Node *first, *current, *previous;
  for(int i=0; i<len; i++){
    current = (Node *)malloc(sizeof(Node));
    current->data = arr[i];
    if(i==0){
      first = current;
    } else {
      previous->next = current;
    }
    current->next = NULL;
    previous = current;
  }
  return first;
}

//print function
void printList(Node *first){
  Node *node = first;
  if(first == NULL){
    printf("List is empty\n");
  } else{
    while(node!=NULL){
      printf("%3d",node->data);
      node = node->next;
    }
    printf("\n");
  }
}

//free function
void freeList(Node *first){
  Node *current, *temp;
  current = first;
  while(current!=NULL){
    temp = current;
    current = current->next;
    free(temp);
  }
}

/* Output
./main
17 19 21 23 25
*/
```

---

## 補充比較
**比較1**
一個靜態連結的練習
(重複練習是很重要的)
```c=
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct s_node Node;
struct s_node{
  Node *next;
  char name[12] ;
  int grade ;
};

int main(void) {
  Node *head = (Node*)malloc(sizeof(Node));
  head->grade = 77;
  strncpy(head->name,"john",12);
  head->next = NULL;

  Node *node2 = (Node*)malloc(sizeof(Node));
  node2->grade = 89;
  strncpy(node2->name,"edgar",12);
  head->next = node2;
  node2->next = NULL;

  Node *node3 = (Node*)malloc(sizeof(Node));
  node3->grade = 63;
  strncpy(node3->name,"mary",12);
  node2->next = node3;
  node3->next = NULL;

  while(head!=NULL){
    printf("data:%s\t",head->name);
    printf("grade:%d\t",head->grade);
    printf("addr:%p\n",head);
    head = head->next;
  }
  return 0;
}
```
---
>比較1用不同指標(node)名稱去malloc；比較2用相同指標名稱同樣實作

讀者可以試著將上面的程式碼看完後，與下方比較，
會發現若3個node都用同樣的指標變數(ptr)去承接，
理解上需要稍微留意，
但熟悉了就沒這個問題。

**比較2**
```c=
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct t_node Node;
struct t_node{
  Node *next;
  int data;
  char name[12];
};

// static Node *head = NULL;

int main(void) {
  Node *head = NULL;
  Node *now = NULL;
  Node *ptr = NULL;

  // 第一個物件
  ptr = (Node *)malloc(sizeof(Node));
  ptr->data = 121;
  strncpy(ptr->name, "john", 12);
  head = ptr;       // 因為head==null 故讓head會指向第一個物件 ptr
  now = ptr;
  
  // 第二個物件
  ptr = (Node *)malloc(sizeof(Node));
  ptr->data = 144;
  strncpy(ptr->name, "edgar", 12);

  // 此時的 now 是第一個物件
  now->next = ptr;  // 第一個物件 now->next 指向新的第二個物件 ptr
  now = ptr;        // now 指向第二個物件 ptr

  // 第三個物件
  ptr = (Node *)malloc(sizeof(Node));
  ptr->data = 169;
  strncpy(ptr->name, "mary", 12);

  // 這邊同上
  now->next = ptr;
  now = ptr;

  for(ptr = head; ptr != NULL; ptr = ptr->next){
    printf("name:%s\t",ptr->name);
    printf("data:%d\t",ptr->data);
    printf("addr:%p\n",ptr);
  }
  printf("\n");
  printf("sizeof(*ptr):%ld\n", sizeof(*ptr));
  printf("sizeof(ptr):%ld\n", sizeof(ptr));
  printf("sizeof(Node):%ld\n", sizeof(Node));

  return 0;
}
```
傑克，這真的是太神奇了!

reference:[資料結構-鏈結串列教學-1-新增與印出](https://medium.com/@racktar7743/%E8%B3%87%E6%96%99%E7%B5%90%E6%A7%8B-%E9%8F%88%E7%B5%90%E4%B8%B2%E5%88%97%E6%95%99%E5%AD%B8-1-%E6%96%B0%E5%A2%9E%E8%88%87%E5%8D%B0%E5%87%BA-bb881da97796)
覺得這位作者寫的也不錯，可以參考看看他的觀點。


(未完待續...)