-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.cpp
46 lines (45 loc) · 963 Bytes
/
node.cpp
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
41
42
43
44
45
46
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <cstdio>
typedef struct node{
int val;
struct node* next;
}node_t;
extern void add(node_t* head, int a);
extern void rm(node_t* head);
int main(void){
node_t* head = NULL;
head = (node_t*)malloc(sizeof(node_t));
head->val = 10;
for (int i = 9; i > 0; i--){
add(head, i);
}
rm(head);
node_t* current = head;
while (current != NULL){
printf("%d\n", current->val);
current = current->next;
}
free(head);
head = NULL;
return 0;
}
void add(node_t* head, int a){
node_t* current = head;
while (current->next != NULL){
current = current->next;
}
current->next = (node_t*)malloc(sizeof(node_t));
current->next->val = a;
current->next->next = NULL;
}
void rm(node_t* head){
node_t* current = head;
while (current->next->next != NULL){
current = current->next;
}
current->next->val = 0;
free(current->next);
current->next = NULL;
}