什么是链表
数据结构->数据存放的思想->集合
数组
int array[10]={1,2,3,4,5,6,7,8,9,10};
缺点:不灵活,运算量大
优化->结构体
数组与链表
#include<stdio.h>
struct Test{
int data;
struct Test *next;
};
int main()
{
int i;
int array[]={1,2,3}; //连续
for(i=0;i<sizeof(array)/sizeof(array[0]);i++)
printf("%d ",array[i]);
putchar('\n');
struct Test t1={1,NULL};
struct Test t2={2,NULL};
struct Test t3={3,NULL};//暂时无任何关系
t1.next=&t2;
t2.next=&t3;
printf("use t1 to t3\n");
printf("%d %d %d\n",t1.data,t1.next->data,t1.next->next->data);
}
链表的静态创建
struct Test{
int data;
struct Test *next;
};
struct Test t1={1,NULL};
struct Test t2={2,NULL};
struct Test t3={3,NULL};//暂时无任何关系
t1.next=&t2;
t2.next=&t3;
printf("%d %d %d\n",t1.data,t1.next->data,t1.next->next->data);
链表的遍历
void printLink(struct Test *head)
{
struct Test *point;
point=head;
while(point != NULL)
{
printf("%d ",point->data);
point=point->next;
}
putchar('\n');
}
链表节点的计算
int getLinkNodeNum(struct Test *head)
{
int cnt=0;
struct Test *point;
point=head;
while(point != NULL)
{
cnt++;
point=point->next;
}
return cnt;
}