struct in c

C is a proceduring language.If you want to make a very efficient process,C is a good choice.But I do not introduce everything about C.I just show the detail of C.

Now,i will show the use of struct in C.

Just detail,no grammar.

Struct has two definition.

define

1
2
3
4
struct ListNode{
int val;
struct ListNode* next;
};

If you use this struct and you want to create a variable.You should do following:

1
struct ListNode* n = (struct ListNode*)malloc(sizeof(struct ListNode));

How to assignment.

We also make some mistakes for assignment.Here i will show a wrong example first.

wrong example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28



#include <stdlib.h>

struct listNode{
int value;
struct listNode* next;
};

void init(struct listNode* a,int beign,int end){
a = (struct listNode*)malloc(sizeof(struct listNode));
a->value = 1;
a->next = NULL;
for(beign;beign < end;beign++){
a->next = (struct listNode*)malloc(sizeof(struct listNode));
a = a->next;
a->value = beign;
a->next = NULL;
}
}

int main(){
struct listNode* a; //when you create a struct variable.You should give space first.
struct listNode* b = a;
init(b,3,10);
return 0;
}

right example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include <stdlib.h>

struct listNode{
int value;
struct listNode* next;
};

void init(struct listNode* a,int beign,int end){
for(beign;beign < end;beign++){
a->next = (struct listNode*)malloc(sizeof(struct listNode));
a = a->next;
a->value = beign;
a->next = NULL;
}
}

int main(){
struct listNode* a = (struct listNode*)malloc(sizeof(struct listNode));
a->value = 1;
a->next = NULL;
struct listNode* b = a;
init(b,3,10);
return 0;
}

operation

1
2
3
4
typedef struct ListNode{
int val;
struct ListNode* next;
};

If you use this struct and you want to create a variable.You should do following:

1
ListNode* n = (ListNode*)malloc(sizeof(ListNode));

How to create list:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#include <stdlib.h>
typedef struct ListNode{
int val;
struct ListNode* next;
};

int main(){
ListNode* p = (struct ListNode*)malloc(sizeof(struct ListNode));
ListNode* l = p;
p->val = 1;
p->next = NULL;
int i = 2;
for(i ;i < 10 ;i++){
ListNode* n = (ListNode*)malloc(sizeof(ListNode));
n->val = i;
n->next = NULL;
p->next = n;
p = p->next;
}
while(l != NULL){
printf("%d",l->val);
l = l->next;
}
return 0;
} //this code maybe not perfect.

Head insertion method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#include <stdlib.h>
typedef struct ListNode{
int val;
struct ListNode* next;
};

int main(){
ListNode* p = (struct ListNode*)malloc(sizeof(struct ListNode));
ListNode* l;
p->val = 1;
p->next = NULL;
int i = 2;
for(i ;i < 10 ;i++){
ListNode* n = (ListNode*)malloc(sizeof(ListNode));
n->val = i;
n->next = p;
p = n;
l = p;
}
while(l != NULL){
printf("%d",l->val);
l = l->next;
}
return 0;
}

End insertion method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#include <stdlib.h>
typedef struct ListNode{
int val;
struct ListNode* next;
};

int main(){
ListNode* p = (struct ListNode*)malloc(sizeof(struct ListNode));
ListNode* l = p;
p->val = 1;
p->next = NULL;
int i = 2;
for(i ;i < 10 ;i++){
ListNode* n = (ListNode*)malloc(sizeof(ListNode));
n->val = i;
n->next = NULL;
p->next = n;
p = p->next;
}
while(l != NULL){
printf("%d",l->val);
l = l->next;
}
return 0;
}

Then,I will update.