zoukankan      html  css  js  c++  java
  • 自己的链表创建插入输出删除程序

    #include<stdlib.h>
    #include<stdio.h>
    #define max 10
    struct list                    
          {
          int data;
          struct list *next;
          };                           //定义结构体
    
    
    
    typedef struct list node;       //结构体的变量
    typedef node* link;           //结构体指针
    
    
    
    
    //链表输出函数的实现方式
    void link_output(link new)
        
    {   
       link q=new;
       int i;
       for(i=0;q!=NULL;i++,q=q->next)
          
          {
            printf("the %dshuju is %d\n",i+1,q->data); 
            
        }
       printf("out_put success\n");
       
    }
    
    
    
    
    
    //链表插入函数的实现部分
    void link_insert(link new)
    {  int i,n;
       link insert,p;
       p=new;
       insert=(link)malloc(sizeof(node));
       if(insert==NULL)
           {
              printf("插入数据的创建失败");
           exit(0);
           }
       else
           {   
             printf("please set you insert number");
          scanf("%d",&n);
          insert->data=1000;
          insert->next=NULL;
          for(i=1;i!=n&&p!=NULL;p=p->next,i++);
          
          insert->next=p->next;
          p->next=insert;
          
          
          
           }
        
    }
    
    //释放链表程序函数
    void free_link(link new)
    
    {   int i=0;
        printf("wait for free:\n");
      link pointer=new;
        printf("%d\n",pointer);
      while(pointer!=NULL)
          {
        
         new=pointer->next;
         free(pointer);
          pointer=new;    
          //这句应该放在free后面,要是放在最前面会造成pointer成为野指针所以会造成其他应用程序的数据出错
         
         printf("free success  %d\n",++i);
        };
       
    }
    
    
    
    
    
    
    
    
    int main()
    {
      link new,p,q;         //定义链表中间指针变量,实现链表的传递
       int n;
      int i,j;
      int a[max]={12,35,78,90,67,28,74,36,78,54};
      new=(link)malloc(sizeof(node));    //注意malloc函数的使用:
      if(new==NULL)                  
          {
         printf("memory allocate failure");
        }
      else
          {  
           printf("please input the data number:");
           scanf("%d",&n);
           new->data=a[0];
           q=new;
    
           //定义中间变量,实现进行链表的传递
           
           for(i=1;i<max;i++)
               {
               p=(link)malloc(sizeof(node));
            q->next=p;
            p->data=a[i];
            q=p;
            p->next=NULL;
               }
         }
      link_insert(new);
      link_output(new);
      printf("wait for free:\n");
      free_link(new);
         
      
    }

     更改后的(主要更改在表头添加节点数据)

    #include<stdlib.h>
    #include<stdio.h>
    #define max 10
    struct list                    
          {
          int data;
          struct list *next;
          };                           //定义结构体
    
    
    
    typedef struct list node;       //结构体的变量
    typedef node* link;           //结构体指针
    
    
    
    
    //链表输出函数的实现方式
    void link_output(link new)
        
    {   
       link q=new;
       int i;
       for(i=0;q!=NULL;i++,q=q->next)
          
          {
            printf("the %dshuju is %d\n",i+1,q->data); 
            
        }
       printf("out_put success\n");
       
    }
    
    
    
    
    
    //链表插入函数的实现部分
    link link_insert(link new)
    {  int i,n;
       link insert,p;
       p=new;
       insert=(link)malloc(sizeof(node));
       if(insert==NULL)
           {
              printf("插入数据的创建失败");
           exit(0);
           }
       else
           {   
             printf("please set you insert number");
          scanf("%d",&n);
          insert->data=1000;
          insert->next=NULL;
          if(n==0)   //需要插入链表的开头,将链表开头更换
              {
                 insert->next=new;
               new=insert;
               return new;
              }
          for(i=1;i!=n&&p!=NULL;p=p->next,i++);
          
          insert->next=p->next;
          p->next=insert;
          
          
          
           }
    }
    
    //释放链表程序函数
    void free_link(link new)
    
    {   int i=0;
        printf("wait for free:\n");
      link pointer=new;
        printf("%d\n",pointer);
      while(pointer!=NULL)
          {
        
         new=pointer->next;
         free(pointer);
          pointer=new;    
          //这句应该放在free后面,要是放在最前面会造成pointer成为野指针所以会造成其他应用程序的数据出错
         
         printf("free success  %d\n",++i);
        };
       
    }
    
    
    
    
    
    
    
    
    int main()
    {
      link new,p,q;         //定义链表中间指针变量,实现链表的传递
       int n;
      int i,j;
      int a[max]={12,35,78,90,67,28,74,36,78,54};
      new=(link)malloc(sizeof(node));    //注意malloc函数的使用:
      if(new==NULL)                  
          {
         printf("memory allocate failure");
        }
      else
          {  
           printf("please input the data number:");
           scanf("%d",&n);
           new->data=a[0];
           q=new;
    
           //定义中间变量,实现进行链表的传递
           
           for(i=1;i<max;i++)
               {
               p=(link)malloc(sizeof(node));
            q->next=p;
            p->data=a[i];
            q=p;
            p->next=NULL;
               }
         }
      new=link_insert(new);
      link_output(new);
      printf("wait for free:\n");
      free_link(new);
         
      
    }
  • 相关阅读:
    DockerFile体系结构
    Nignx(二) server_name 规则
    解决Redis分布式锁——死锁问题
    redis缓存穿透,缓存击穿,缓存雪崩原因+解决方案
    Docker_Linux
    正则例子
    部属流程
    Mysql insert语句的优化
    MySQL innodb_fast_shutdown参数讲解
    MySQL技术内幕InnoDB存储引擎(表&索引算法和锁)
  • 原文地址:https://www.cnblogs.com/hao02171990/p/3028834.html
Copyright © 2011-2022 走看看