/*
链式物理结构演示
*/
#include <stdio.h>
typedef struct node {
int num;
struct node *p_next;
} node;
int main() {
node node1 = {1, NULL}, node2 = {2, NULL}, node3 = {3, NULL}, node4 = {4, NULL};
node *p_node = NULL, *p_tmp = NULL;
//把node1挂在最前边
node1.p_next = p_node;
p_node = &node1;
//把node2挂在最前边
node2.p_next = p_node;
p_node = &node2;
//把node3挂在最后边
if (!p_node) {
p_node = &node3;
}
else {
for (p_tmp = p_node;p_tmp;p_tmp = p_tmp->p_next) {
if (!(p_tmp->p_next)) {
p_tmp->p_next = &node3;
break;
}
}
}
//把node4挂在最后边
if (!p_node) {
p_node = &node4;
}
else {
for (p_tmp = p_node;p_tmp;p_tmp = p_tmp->p_next) {
if (!(p_tmp->p_next)) {
p_tmp->p_next = &node4;
break;
}
}
}
//打印所有节点内容
for (p_tmp = p_node;p_tmp;p_tmp = p_tmp->p_next) {
printf("%d ", p_tmp->num);
}
printf("\n");
return 0;
}
链式物理结构演示
*/
#include <stdio.h>
typedef struct node {
int num;
struct node *p_next;
} node;
int main() {
node node1 = {1, NULL}, node2 = {2, NULL}, node3 = {3, NULL}, node4 = {4, NULL};
node *p_node = NULL, *p_tmp = NULL;
//把node1挂在最前边
node1.p_next = p_node;
p_node = &node1;
//把node2挂在最前边
node2.p_next = p_node;
p_node = &node2;
//把node3挂在最后边
if (!p_node) {
p_node = &node3;
}
else {
for (p_tmp = p_node;p_tmp;p_tmp = p_tmp->p_next) {
if (!(p_tmp->p_next)) {
p_tmp->p_next = &node3;
break;
}
}
}
//把node4挂在最后边
if (!p_node) {
p_node = &node4;
}
else {
for (p_tmp = p_node;p_tmp;p_tmp = p_tmp->p_next) {
if (!(p_tmp->p_next)) {
p_tmp->p_next = &node4;
break;
}
}
}
//打印所有节点内容
for (p_tmp = p_node;p_tmp;p_tmp = p_tmp->p_next) {
printf("%d ", p_tmp->num);
}
printf("\n");
return 0;
}