DAY 30
0
Software Development

# 資料結構

`struct` 可以自定義包含多個值的資料結構

``````#include <stdio.h>
#include <stdlib.h>

typedef struct Data{
int a;
int b;
}data;

int main() {
data d={10, 20};
printf("%d %d", d.a, d.b);
}
``````

``````#include <stdio.h>
#include <stdlib.h>

int main() {
int x=10, *p=&x;
printf("%d", *p);
}
``````

## 資料結構的指標

``````#include <stdio.h>
#include <stdlib.h>

typedef struct data {
int a;
int b;
}data;

int main(){
data d, *p=&d;
return 0;
}
``````

``````#include <stdio.h>
#include <stdlib.h>

typedef struct data {
int a;
int b;
}data;

int main(){
data d, *p=&d;
return 0;
}
``````

``````#include <stdio.h>
#include <stdlib.h>

typedef struct data {
int a;
int b;
}data;

int main(){
data d={1, 2}, *p=&d;
printf("%d", p->a);
return 0;
}
``````

``````#include <stdio.h>
#include <stdlib.h>

typedef struct Node{
int val;
struct Node *next;
}node;
``````

``````#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
int val;
struct Node *next;
} node;

int main() {
node myList, a, b, c;
myList.next=&a;
a.val=1;
a.next=&b;
b.val=2;
b.next=&c;
c.val=3;
c.next=NULL;

return 0;
}
``````

``````#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
int val;
struct Node *next;
} node;

int main() {
node myList, a, b, c;
myList.next = &a;
a.val = 1;
a.next = &b;
b.val = 2;
b.next = &c;
c.val = 3;
c.next = NULL;

node *p = &myList;
for (; p != NULL;) {
p = p->next;
printf("%d ", p->val);
}
return 0;
}
``````

``````#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
int val;
struct Node *next;
} node;

int main() {
node myList, *p = &myList;

for (int i = 0; i != 10; i++) {
node *new = malloc(sizeof(node));
new->val = i;
p->next = new;
p = p->next;
}
p->next = NULL;

p = &myList;
while (p != NULL) {
p = p->next;
printf("%d ", p->val);
}
return 0;
}
``````

``````#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
int val;
struct Node *next;
} node;

int main() {
node myList, *p = &myList;

for (int i = 0; i != 10; i++) {
node *new = malloc(sizeof(node));
new->val = i;
p->next = new;
p = p->next;
}
p->next = NULL;

p = &myList;
node *second = p->next->next, *third = p->next->next->next;
second->next=third->next;
free(third);

while (p != NULL) {
p = p->next;
printf("%d ", p->val);
}
return 0;
}
``````