NODE *head 的头结点不存放数据#include<stdio.h>
#include<stdlib.h>
typedef struct NODE_tag
{
int data;
struct NODE_tag *next;
} NODE;
void ListAppend(NODE *head, int data)
{
NODE *p = head;
while(p->next != NULL)
p = p->next;
p->next = malloc(sizeof(NODE));
p->next->data = data;
p->next->next = NULL;
}
void ListPrint(NODE *head)
{
NODE *p = head->next;
while(p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
putchar('\n');
}
void ListFree(NODE *head)
{
NODE *p = head->next;
NODE *temp;
while(p != NULL)
{
temp = p;
p = p->next;
free(temp);
}
free(head);
}
int main(void)
{
NODE *head = malloc(sizeof(NODE));
head->next = NULL;
for(int i = 0; i < 10; ++i)
ListAppend(head, i);
ListPrint(head);
ListFree(head);
printf("head: 0x%.8X\n", (unsigned int)head);
return 0;
}
0 1 2 3 4 5 6 7 8 9
head: 0x034B61E0
请按任意键继续. . .
但这并不是说 NODE *head 的头结点就不能存放数据#include<stdio.h>
#include<stdlib.h>
typedef struct NODE_tag
{
int data;
struct NODE_tag *next;
} NODE;
void ListAppend(NODE *head, int data)
{
NODE *p = head;
if(head->data == -1)
{
head->data = data;
return;
}
while(p->next != NULL)
p = p->next;
p->next = malloc(sizeof(NODE));
p->next->data = data;
p->next->next = NULL;
}
void ListPrint(NODE *head)
{
NODE *p = head;
while(p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
putchar('\n');
}
void ListFree(NODE *head)
{
NODE *p = head->next;
NODE *temp;
while(p != NULL)
{
temp = p;
p = p->next;
free(temp);
}
free(head);
}
int main(void)
{
NODE *head = malloc(sizeof(NODE));
head->data = -1; // 一个不可能的值,用来判断头结点有没有创建
head->next = NULL;
for(int i = 0; i < 10; ++i)
ListAppend(head, i);
ListPrint(head);
ListFree(head);
printf("head: 0x%.8X\n", (unsigned int)head);
return 0;
}
0 1 2 3 4 5 6 7 8 9
head: 0x031161E0
请按任意键继续. . .
NODE **head 的头结点存放数据#include<stdio.h>
#include<stdlib.h>
typedef struct NODE_tag
{
int data;
struct NODE_tag *next;
} NODE;
void ListAppend(NODE **head, int data)
{
NODE *node = malloc(sizeof(NODE));
node->data = data;
node->next = NULL;
if(*head == NULL)
{
*head = node;
return;
}
NODE *p = *head;
while(p->next != NULL)
p = p->next;
p->next = node;
}
//void ListPrint(NODE **head)
//{
// NODE *p = *head;
//
// while(p != NULL)
// {
// printf("%d ", p->data);
// p = p->next;
// }
// putchar('\n');
//}
void ListPrint(NODE *head)
{
NODE *p = head;
while(p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
putchar('\n');
}
void ListFree(NODE **head)
{
NODE *p = *head;
NODE *temp;
while(p != NULL)
{
temp = p;
p = p->next;
free(temp);
}
*head = NULL;
}
int main(void)
{
NODE *head = NULL;
for(int i = 0; i < 10; ++i)
ListAppend(&head, i);
ListPrint(head);
ListFree(&head);
printf("head: 0x%.8X\n", (unsigned int)head);
return 0;
}
0 1 2 3 4 5 6 7 8 9
head: 0x00000000
请按任意键继续. . .
但这并不是说 NODE **head 的头结点就不能 不存放数据 ^_^#include<stdio.h>
#include<stdlib.h>
typedef struct NODE_tag
{
int data;
struct NODE_tag *next;
} NODE;
void ListAppend(NODE **head, int data)
{
if(*head == NULL)
{
*head = malloc(sizeof(NODE));
(*head)->next = NULL;
return;
}
NODE *node = malloc(sizeof(NODE));
node->data = data;
node->next = NULL;
NODE *p = *head;
while(p->next != NULL)
p = p->next;
p->next = node;
}
//void ListPrint(NODE **head)
//{
// NODE *p = (*head)->next;
//
// while(p != NULL)
// {
// printf("%d ", p->data);
// p = p->next;
// }
// putchar('\n');
//}
void ListPrint(NODE *head)
{
NODE *p = head->next;
while(p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
putchar('\n');
}
void ListFree(NODE **head)
{
NODE *p = *head;
NODE *temp;
while(p != NULL)
{
temp = p;
p = p->next;
free(temp);
}
*head = NULL;
}
int main(void)
{
NODE *head = NULL;
for(int i = 0; i < 10; ++i)
ListAppend(&head, i);
ListPrint(head);
ListFree(&head);
printf("head: 0x%.8X\n", (unsigned int)head);
return 0;
}
1 2 3 4 5 6 7 8 9
head: 0x00000000
请按任意键继续. . .
使用二级指针可以修改main函数的head变量,还记得swap函数吧#include<stdio.h>
void SwapInt(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void SwapPointer(int **pa, int **pb)
{
int *temp = *pa;
*pa = *pb;
*pb = temp;
}
int main(void)
{
int x = 100, y = 0;
SwapInt(&x, &y);
printf("x: %d y: %d\n", x, y);
int a = 123, b = 456;
int *pa = &a, *pb = &b;
SwapPointer(&pa, &pb);
printf("pa: %d pb: %d\n", *pa, *pb);
return 0;
}
x: 0 y: 100
pa: 456 pb: 123
请按任意键继续. . .
还有就是 ListPrint函数
因为是输出这个链表,所以使用 NODE **head 和 NODE *head 都可以
就像#include<stdio.h>
void PrintInt(int x)
{
printf("%d\n", x);
}
void PrintPointer(int *x)
{
printf("%d\n", *x);
}
int main(void)
{
int x = 100;
PrintInt(x);
PrintPointer(&x);
return 0;
}
100
100
请按任意键继续. . .
至于怎么用,这好像也属于一种习惯问题
就像#include<stdio.h>
int main(void)
{
printf("hello world!\n");
return 0;
}
#include<stdio.h>
int main(void) {
printf("hello world!\n");
return 0;
}
|