这是我第一次开通博客,并写一些内容,所以有不对的地方欢迎大家的批评与指正!!!
以下是我对链表一些简单知识点的总结,包括链表的创建,遍历以及删除,清除等。
一,创建链表
链表同一维数组类似,用于存放线性表,并且便于边读入,边输出,即链表的长度可不必预先设定;因此链表在增添,删除数据方面要简便于数组,但同时,链表难以实现类似于数组一样通过下标直接访问对应内容,常需遍历。
首先,链表的创建依赖于数据结构即构造一种新的结构(由存储内容与指向下一结构的指针构成),在我看来指向下一个结构的指针正是构成连表的“链”。
typedef struct _node{
int value;//要存储的内容
struct _node *next;//指向下一个的指针
}Node;
注意!!此处不能为
typedef struct _node{
int value;//要存储的内容
Node *next;//指向下一个的指针
}Node;
在这里构建链表以输入一系列数字,以-1为结束的标志
构建链表的函数如下
Node *add(Node *head , int number)//为保证能多次使用,返回一指针,返回不能为空
{
//add to Linked-List
Node *p = (Node*)malloc(sizeof(Node));
p->value = number;
p->next = NULL;
//find the last
Node *last = head;
if ( last ){
while ( last->next ){
last=last->next;
}
//attach
last->next = p;//
}
else{
head = p;//如果将返回值定位void,则此处对head的修改无效
}
return head;
}
主函数如下
int main()
{
Node *head = NULL;
int number;
do{
scanf("%d", &number);
if (number != -1 ){
head=add(head,number);
}
}while ( number != -1);
return 0;
}
以上是较常用的链表构建函数
方案二:
为防止将
head=add(head,number);
此步给head赋值遗漏,可以尝试将head的地址传入
int main()
{
Node *head = NULL;
int number;
do{
scanf("%d", &number);
if (number != -1 ){
add(&head,number);
}
}while ( number != -1);
return 0;
}
Node *add(Node** pHead , int number)
{
//add to Linked-List
Node *p = (Node*)malloc(sizeof(Node));
p->value = number;
p->next = NULL;//新建结构并进行初始化
//find the last
Node *last = *pHead;//找到最后一位,同时将新建结构连接到最后一位上
if ( last ){
while ( last->next ){
last=last->next;
}
last->next = p;//attach
}
else{
*pHead = p;
}
return head;//此处有无返回不重要
}
此方法直接传入head的地址,对其修改,可以省去再给head赋值的步骤
方案三:
构建新的数据结构
typeddef struct _List{
Node* head;
/*还可加入其他指针 例如:Node* tail;用于标注末尾,方便寻找
}List;
void add(List* list , int number)
{
//add to Linked-List
Node *p = (Node*)malloc(sizeof(Node));
p->value = number;
p->next = NULL;//新建结构并进行初始化
//find the last
Node *last = List->head;//找到最后一位,同时将新建结构连接到最后一位上
if ( last ){
while ( last->next ){
last=last->next;
}
last->next = p;//attach
}
else{
list->head = p;
}
}
int main()
{
List list;
int number;
list.head = list.tail = NULL;
do{
scanf("%d", &number);
if (number != -1 ){
add(&list,number);
}
}while ( number != -1);
return 0;
}
以上即有关链表构建的内容,需注意,这里并没有解决malloc失败的情况!!!
二,链表的遍历
此处以输出链表中所有数据为例进行遍历
void print ( Node* head)
{
Node* p;
for ( p=head ; p ; p=p->next){//常用于遍历
printf ("%d\t",p->value);
}
}
三,搜索到特定数字并删除
搜索到特定数字:
scanf("%d",&number1);
Node *p;
int isfound = 0;
for ( p=head ; p ; p=p->next ){
if ( p->value == number1 ){
printf("找到了\n");
isfound = 1;
break;
}
}
if ( !isfound ){
printf ("没有找到\n")
}
删除找到的数字:
Node* q,*p;
for ( q=NULL,p=head ; p ; q=p,p=p->next){
if ( p->value==number1){
if ( q ){
q->next=p->next;
}
else{
head=p->next;
}
free(p);
break;
}
}
四,清除链表
for ( p=head ; p ; p=q ){
q=p->next;
free(p);
}
五,关于加标指针tail
tail用于标注指针末尾,便于搜索
typeddef struct _List{
Node* head;
Node* tail;
}List;
void add(List* list , int number)
{
//add to Linked-List
Node *p = (Node*)malloc(sizeof(Node));
p->value = number;
p->next = NULL;//新建结构并进行初始化
//find the last
Node *last = List->head;//找到最后一位,同时将新建结构连接到最后一位上
if ( last ){
while ( last->next ){
last=last->next;
}
last->next = p;//attach
}
else{
list->head = p;
}
List->tail->next = p;//find the tail ; tail->next = n ; n->next = 0;
}
以上即关于链表的一些简单内容,复杂的方面后续会有介绍。