什么是链表
链表是数据存放的一种思想;也是数据的结合,这跟数组有点像,但是数组有以下特点;
元素地址是连续的。其缺点也很明显
如果需要增加一个元素和删除一个元素呢?此时就特别麻烦,非常不灵活
链表的特点:有一个指向自己的指针
这时候就能体现链表的优势了。链表的具体定义和遍历如下:
#include <stdio.h>
struct Test
{
int data;
struct Test *next;
};
void printLink(struct Test *head)
{
struct Test *point = head;
while(point != NULL){
printf("%d ",point->data);
point = point->next;
}
}
int main()
{
int array[] = {1,2,3};
int i;
for(i = 0; i < sizeof(array) / sizeof(array[0]); i++){
printf("%d ", array[i]);
}
putchar('\n');
struct Test t1 = {10, NULL};
struct Test t2 = {20, NULL};
struct Test t3 = {30, NULL};
struct Test t4 = {40, NULL};
struct Test t5 = {50, NULL};
struct Test t6 = {60, NULL}; // 可以随意的加入
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
t5.next = &t6;
printf("use t1 to print three nums\n");
printLink(&t1);
return 0;
}