链表的添加,查找,删除

#include<stdio.h>
#include <stdlib.h>
struct node//结点结构体
{
    int data;
    struct node *next;//定义一个指针结构体指向下一结点
};
struct node *head=NULL;//无结点时指向空
struct node *tail=NULL;//头尾指针

//创建链表,包括尾添加和头添加
void addtail(int data)
{
    //创建结点
    struct node *sta =(struct node*)(malloc(sizeof(struct node)));
    //赋值结点数据
    sta->data=data;
    sta->next=NULL;//一定要记住

    //链接
    //尾添加的情况,
    if(head==NULL||tail==NULL)//如果链表为空
    {
        head=sta;//结点既是头也是尾
         tail=sta;
    }
    else
    {
        tail->next=sta;//链表不为空,尾部指向下一结点
        tail=sta;//尾部后移,因为尾部要指向最后一个结点
    }
    //tail=sta;
}

//两者的结构体是相同的
//创建链表,头添加
void addhead(int data)
{
    //创建结点
    struct node*sta=(struct node*)malloc(sizeof(struct node));
    //结点数据赋值
    sta->data=data;//结点数据赋值
    sta->next=NULL;
    //接入链表
    if(head==NULL)
    {
        head=sta;
        tail=sta;
    }
    else 
    {    // 尾头的区别,尾部指向下一结点,新的结点指向头
        sta->next=head;//指向头部
        head=sta;
    }
}
//头添加类似于倒序,尾添加是正序




#include<stdio.h>
#include <stdlib.h>
struct node//节点结构体
{
    int data;
    struct node *next;//定义一个指针结构体指向下一节点
};
struct node *head=NULL;//无节点时指向空
struct node *tail=NULL;//头尾指针

//创建链表,包括尾添加和头添加
void addnode(int data)
{
    //创建节点
    struct node *sta =(struct node*)(malloc(sizeof(struct node)));
    //赋值节点数据
    sta->data=data;//赋值节点数据
    sta->next=NULL;//一定要记住

    //链接
    //尾添加的情况,
    if(head==NULL||tail==NULL)//如果链表为空
    {
        head=sta;//节点既是头也是尾
         tail=sta;
    }
    else
    {
        tail->next=sta;//链表不为空,尾部指向下一节点,即为新的数据
        tail=sta;//尾部后移,因为尾部要指向最后一个节点
    }
    //tail=sta;
}

//创建链表,头添加
void addhead(int data)
{
    //创建节点
    struct node*sta=(struct node*)malloc(sizeof(struct node));
    //节点数据赋值
    sta->data=data;
    sta->next=NULL;
    //接入链表
    if(head==NULL)
    {
        head=sta;
        tail=sta;
    }
    else
    {    // 尾头的区别,尾部指向下一节点,新的节点指向头
        sta->next=head;//指向头部
        head=sta;
    }
}
//查询指定的结点
struct node *select(int data)
{

     struct node *middle=head;
    while(middle!=NULL)
    {
        if(data==middle->data)
        {
            return middle;//找到了
        }
        middle=middle->next;

    }
    //未找到
    return NULL;

};
//遍历链表
void  sea()
{
    //通过指针去遍历
    struct node *middle=head;
    while(middle!=NULL)
    {
        printf("%d\n",middle->data);
        middle=middle->next;

    }

}
int main(void)
{

    int a[10]= {1,2,3,4,5,6,7,8,9,10};
    // head;
    int i;
    for(i=0; i<10; i++)
    {
        addhead(a[i]);
    }
    sea();//遍历链表并输出
   /* struct node *pfind=select(19);
    if(pfind!=NULL)
        printf("%d\n",pfind->data);与找到对应结点相配的输出
    else printf("no\n");*/
  return 0;
}
//链表的删除
 void Free()
 {
      struct node *middle=head;
    while(middle!=NULL)
    {
        struct node *pt=middle;//需要定义一个指针去释放
        //不能直接释放,因为middle仍指向该结点的地址
        middle=middle->next;
        free(pt);

    }
    //不要忘记头和尾的清空
    head=NULL;
    tail=NULL;
 }

发布了25 篇原创文章 · 获赞 0 · 访问量 395

猜你喜欢

转载自blog.csdn.net/weixin_45726784/article/details/103824289
今日推荐