C语言简单的链表

#include <stdio.h>
#include <stdlib.h>




typedef struct _node{
    int value;
    struct _node *next;
    int count;
}Node;
typedef struct _list{
Node * head;
Node * tail;
}List;
void print(List * temp)
{
    int count=0;
    Node * last=NULL;
    for(last=temp->head;last;last=last->next,count++)
        {
            if(count%3==0)
                printf("\n");
            printf("序号:%d\t数值:%d\t",last->count,last->value);


        }
        printf("\n");

}


List scan(List * list)
{
    int number;
    int count=0;
    list->head=NULL;
    list->tail=NULL;


    do
    {
        printf("请填入值:");
        scanf("%d",&number);
        if(number != -1)
        {
            ++count;
            Node * temp=list->tail;
            Node * p=(Node*)malloc(sizeof(Node));
            p->value = number;
            p->next = NULL;
            p->count = count;
            if(temp)
            {
            list->tail=p;
            temp->next=list->tail;
            }
            else
            {
                list->head=p;
                list->tail=list->head;
            }


        }


        }while(number!=-1);
        

}


void list_search(List * temp,int number)
{
        int found=0;
        Node * last;
        for(last=temp->head;last;last=last->next)
        {
            if(number==last->count)
                {
                printf("已找到\n序号为:%d\n数为:%d\n",last->count,last->value);
                found=1;
                }


        }


}


void in_free(Node * temp)
{
    temp->count=0;
    temp->value=0;
    temp->next=NULL;
    temp=NULL;
    free(temp);
}
void list_free(List * temp)
{


    Node * last=(temp->head);
    Node * p=NULL;
    while(last)
    {
        p=last;
        last=last->next;
        in_free(p);
    }
   temp->head=NULL;
   temp->tail=NULL;
}




int main()
{
    
    int search;
    int index;
    List list;
    scan(&list);


        print(&list);
        printf("请输入要查找的序号:");
        scanf("%d",&index);
        
       list_search(&list,index);
       
      list_free(&list);
      
      if(list.head==NULL)
        
        printf("链表未找到");
      else
        printf("OK");
       printf("末尾为:%s",list.head);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值