链表的增、删、改、查、显示(C 语言实现)

本文详细介绍了如何使用C语言实现链表数据结构的基本操作,包括插入元素、删除元素、修改元素、查找元素以及打印链表内容。通过对链表节点的动态管理和遍历,实现对链表的全面操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*在开始之前最好把基本链表的组成基本原理理解清楚*/
#define LEN  sizeof(struct node)
//链表节点结构体
typedef struct node
{ 
        int num;
        struct node *next;
}Node;

Node *add_node()
{        
         int temp_num;   
         Node *pnew = NULL;
         pnew = (Node *)malloc(LEN);
         printf("please input num:");
         scanf("%d",&temp_num);
         while(getchar() != '\n');
         pnew -> num = temp_num;
         pnew ->next  = NULL;
         return pnew;
}
bool yes_or_no(const char *str)
{
        char  sel;
        while(1)
        {
                 printf("%s",str);
                 scanf("%c",&sel);
                switch(sel)
                {
                            case 'y':
                            case 'Y':
                                                 return true;
                            case  'n':
                            case  'N':
                                                return false; 
                            default:
                                                printf("input ERROR!\n");                                                                                                            
                }
        }
}
Node *creat_list(Node *head)  //创建链表
{
         Node *pnew;
         while(1)
         {
                 pnew = add_node();
                 pnew ->next = head;
                 head = pnew;
                 if(yes_or_no("continue  y/n?") == false)
                 {
                           break;
                 }
         }
         return head;                
}
void print_list(Nod/e *head) //显示链表
{
         Node *p = head;
         if(head != NULL)
         {
                  while(p != NULL)
                  {
                              printf("[num: %d,addr:%p]\n",p->num,p->next);
                              p = p ->next;
                  }
         }
}

void modify_node(Node *head)//修改
{
        int temp_num;
        printf("please input the num that  you wanna modify: ");
        scanf("%d",&temp_num);
        while(getchar() != '\n');
        while(h/ead != NULL)
        {
                  if(head -> num == temp_num)
                  {   
                              printf("please input the num which is modified:");
                              scanf("%d",temp_num);
                              while(getchar() != '\n');
                              head -> num = temp_num;
                              return ;
                  }
                  head = head -> next;
        }
        printf("Not found!\n");return;
}
int list_len(Node *head)//链表长度
{
      int len = 0;
      while(head != NULL)
      {
               len++;
               head = head -> next;
      }
      return head;      
}

Node *find_pre(Node *head,Node *delete)
{
        while(head != NULL)
        {
               if(head ->next == delete)
               {
                        return head;
               }
                  head = head -> next;
        }
}

Node *delete_node(Node *head,Node *delete)
{
       if(head == delete)
       {
               head = head -> next;
       }
       else
       {
                 Node *pre =find_pre(head,delete);
                    pre -> next = delete -> next;
       }
       return head;
}
Node *find_node(Node *head)
{
      int temp_num;
      printf("please input the num that you wanna delete:");
      scanf("%d",&temp_num);
      while(getchar() != '\n');

      while(head != NULL)
      {
             if(head -> num == temp_num)
             {
                           return head;
             }
             head = head -> next;
      }
      return NULL;
}

Node *delete_list(Node *head)//删除节点
{
         Node *delete = find_node(head);
         if(delete ==NULL)
         {
                    printf("Not found!\n");
                    return head;
         }
         head = delete_node(head,delete);
         free(delete);
         return head;
}

int main(int argc, char *argv[])
{
       Node *head = NULL;
       head = creat_list(head);
       print_list(head);
       printf("\n");
       
       modify_node(head);
       print_list(head);
       
       delete_list(head);
       print_list(head);
       printf("list_len = %d\n",list_len(head));
       return 0;
       }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值