读书笔记: 链表(link)操作 Insert, Delete, Print

1
2
3
4
5
6
7
8
9
10
11
12
13
#define IS_FULL (ptr) (!(ptr))

typedef struct list_node *list_pointer;
typedef struct list_node{
    int data;
    list_pointer link;
}list_node;
list_pointer ptr=NULL;

void insert(list_pointer *ptr,list_pointer pre_node, list_pointer node);
void delete(list_pointer *ptr,list_pointer trail,list_pointer node);
void print_list(list_pointer ptr);
list_pointer create_node(int value);

创建Node

1
2
3
4
5
6
7
8
9
10
list_pointer create_node(int value){
    list_pointer temp;
    temp = (list_pointer) malloc(sizeof(list_node));
//    if(IS_FULL(temp){
//        fprintf(stderr, "full");
//        exit(1);
//    }
    temp->data = value;
    return temp;
}

Insert

1
2
3
4
5
6
7
8
9
void insert(list_pointer *ptr,list_pointer pre_node, list_pointer node){
    if (*ptr){
        node->link = pre_node->link;
        pre_node->link = node;
    } else {
        node->link = NULL;
        *ptr = node;
    }
}

Delete

1
2
3
4
5
6
7
8
void delete(list_pointer *ptr,list_pointer trail,list_pointer node){
    // trail is the preceding node
    if(trail){
        trail->link = node->link;
    } else {
        *ptr = (*ptr)->link;
    }
}

Print

1
2
3
4
5
6
7
void print_list(list_pointer ptr){
    printf("The list:");
    for(;ptr;ptr=ptr->link){
        printf("%d ",ptr->data);
    }
    printf("\n");
}
1
2
3
4
5
6
7
8
9
10
void test_pointer(list_pointer *ptr,list_pointer node){
    //input is (&ptr,ptr)
    //ptr->data = 1024;
    printf("%d %d\n",ptr,node);
    //    4208 1048736
    printf("%d %d\n",*ptr,*node);
    //    1048736 1024
    printf("%d\n",**ptr);
    //    1024
}

测试代码

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
29
30
31
32
33
34
35
36
37
38
39
40
int main (int argc, const char * argv[])
{
    list_pointer first,second;
    
    first = create_node(100);
    second =  create_node(20);
    
    first->link = second;
    second->link = NULL;
    
    printf("The list with first and second node \n");
    print_list(first);
    
    printf("The list with empty ptr,insert node(50) \n");
    list_pointer new_node1 = create_node(50);
    insert(&ptr, ptr, new_node1);
    print_list(ptr);
    
    printf("The list with ptr \n");
    ptr = (list_pointer) malloc(sizeof(list_node));
    ptr->link=first;
    ptr->data=1024;
    print_list(ptr);
    
    printf("\n Insert \n");
    printf("The list with ptr,insert node(50) after first node \n");
    list_pointer new_node2 = create_node(50);
    insert(&ptr, first, new_node2);
    print_list(ptr);
    
    printf("\n Delete \n");
    printf("The list with ptr,delete first node\n");
    delete(&ptr, ptr,first);
    print_list(ptr);
    printf("The list with ptr,delete ptr node\n");
    delete(&ptr, NULL,ptr);
    print_list(ptr);
    
    return 0;
}

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
The list with first and second node 
The list:100 20 
The list with empty ptr,insert node(50) 
The list:50 
The list with ptr 
The list:1024 100 20 

 Insert 
The list with ptr,insert node(50) after first node 
The list:1024 100 50 20 

 Delete 
The list with ptr,delete first node
The list:1024 50 20 
The list with ptr,delete ptr node
The list:50 20