zoukankan      html  css  js  c++  java
  • C 链表

    #include<stdio.h>
    #include<stdlib.h>
    typedef struct link
    {
     int data;
     struct link *next;
    }A;

    //创建链表
    A *creat()
    {
     A *head=NULL,*p,*pr;
     p=(A *)malloc(sizeof(A));
     scanf("%d",&p->data);
     while(p->data!=0)
     {
      if(head==NULL)
      {
       head=p;
      }
      else 
      {
       pr->next=p;
      }
      pr=p;
      p=(A *)malloc(sizeof(A));
      scanf("%d",&p->data);
     }
     pr->next=NULL;
     return head;
    }

    //插入链表

    A *insert(A *head,int num,A *node)
    {
     A *p=head;
     while(p->data!=num&&p->next!=NULL)
     {
      p=p->next;
     }
     if(num==p->data)
     {
      node->next=p->next;
      p->next=node;
     }
     else
     {
      printf("123!!! ");
     }
     return head;

    void print(A *head)
    {
     A *p;
     p=head;
     if(p==NULL)
       printf("List is null! ");
     else
     {
      do
      {
       printf("%d ",p->data);
       p=p->next;
      }while(p!=NULL);
     }
    }

    //销毁链表
    void des(A *head)
    {
     A *p=head,*p1=NULL;
     while(p!=NULL)
     {
      p1=p;
      p=p->next;
      free(p1);
     }
    }

    //链表删除
    A *del(A *head,int num)
    {
     A *p1,*p2;
     p1=head;
     while(p1->data!=num&&p1->next!=NULL)
     {
      p2=p1;
      p1=p1->next;
     } 
     if(num==p1->data)
     {
      p2->next=p1->next;
      free(p1);
      p1=NULL;
      printf(" delete %d success! ",num);
     }
     else
     {
      printf(" %d not been found! ",num);
     }
     return head;
    }
    int main()
    {
     A *p,*p1;
     int n;
     p=creat();
     print(p);
     printf(" Please input insert index:");
     scanf("%d",&n);
     p1=(A *)malloc(sizeof(A));
     printf(" Please input insert data:");
     scanf("%d",&p1->data);
     p=insert(p,n,p1);
     print(p);
     printf(" Please input delete index:");
     scanf("%d",&n);
     p=del(p,n);
     print(p);
        des(p);
     return 0; 
    }

  • 相关阅读:
    nginx.conf中配置laravel框架站点
    centos6.4下安装php7+nginx+mariadb环境
    Windows Terminal 安装和运行
    微软 WSL 重装操作系统
    Pulumi 如何在 Windows 环境中设置
    Ubuntu 20.04 安装 JDK
    代码的 Lint 是什么意思
    CentOS 8 手动安装 Go 1.16 版本
    Raspberry Pi 安装 go 后提示错误 Exec format error
    系统管理--查看网卡、内存等
  • 原文地址:https://www.cnblogs.com/YuanYe1/p/4593990.html
Copyright © 2011-2022 走看看