• 数据结构/循环链表


     
    尾指针   *real 
     
    (head->next=dead)
     
    1初始化
    void ds_init( node **pNode)
     
    {
     int item;
     node *temp,*target;
     printf("输入终点的值,输入0完成出初始化 ");
     
     while(1)
     {
      scanf("%d",&item);
       fflush(stdin);
       if (item==0)
       return ;
      if ((*pNode)==NULL)
      {//循环链表只有一个节点
       *pNode=(node*)malloc(sizeof(struct CLinklist));
       if (!(*pNode))
       {
        exit (0);
        (*pNode)->date=item;
        (*pNode)->next=*pNode;
     
     
       }
       else {
        //找到next指向第一个节点的终点
        for (target=(*pNode); target->next!=(*pNode);target=target->next)
        {
         //生成一个新的终点
         temp=(*node)malloc(sizeof(struct CLinklist));
         if (!temp)
          exit(0);
         temp->date=item;
         temp->next=*pNode;
         target->next=temp;
     
            } 
       }
      }
     }
    }
     
     
     
    2链表的插入:
    //链表存储结构的定义
    typedef struct CLinkList
    {
     int date;
     struct CLinkList *next;
     
    }node;
     
    //插入终点
    //参数: 链表的第一个节点;插入的位置
    void ds_insert(node **pNode,int i )
    {
     node *temp;
     node *target,*p;
     int j=i;
     printf("输入要插入的节点的值:");
     scanf("%d",&item);
     if (i==1)
     {
      //新插入的节点的第一个节点
      temp=(node*)malloc(sizeof(struct CLinkList));
      if (!temp)
       exit(0);
      temp->date=item;
      //寻找到最后一个节点
      for (target=(*pNode);target->next!=(*pNode);target=target->next);
       temp->next=(*pNode);
          target->next=temp;
          *pNode=temp;
      }
      else
      {
       target=*pNode;
       for (; j < (i-1); j++)
       {
        target=target->next;
     
        /* code */
        //循环2次
       }
       temp=(node*)malloc(sizeof(struct CLinkList));
       if (!item)
        exit (0);
       temp->date=item;
       p=target->next;
       temp->next=p;
     
     
     
      }
     }
    3 删除节点:
    //删除节点
    //参数说明:参数1:该节点的值;参数2:第i个节点
    void ds_delete(node **pNode ,int i)
    {
     node *target,*temp;
     int j=1;
     if (i==1)
     {
      //删除的是di一个节点
     
      //找到最后一个节点
      for (target =*pNode ;target->next!=*pNode;target=target->next)
      {
       temp=*pNode;
       *pNode=(*pNode)->next;
       target->next=*pNode;
       free(temp);
     
     
       /* code */
      }
    else
    {
     target=*pNode;
     for (; j < i-1; j++)
     {
      target=target->next;
     
      /* code */
     }
     
     temp=target->next;
     target->next=temp->next;
     free(temp);
     
     
    }
     
     }
     
     
     
    }
    4 查找节点:
    //查找节点
     
    int ds_search(node *pNode ,int elem)
    {
     node *target;
     int i=1;
     for (target=*pNode; target->date!=elem&&target->next!=*pNode; ++i)
     {
      target=target->next;
     
      /* code */
     }
    if (target->next==pNode)//表中不存在的元素
     return -1;
    else return 1;
     
     
    }
     
     
    有年轻!有动力!有追逐!有发现!
  • 相关阅读:
    Linux基础命令总结
    在Linux服务器上配置phpMyAdmin
    nginx配置301重定向
    nginx服务器设置url的优雅链接
    nginx服务器绑定域名和设置根目录的方法
    VMware ESXi客户端连接控制台时提示"VMRC控制台连接已断开...正在尝试重新连接"的解决方法
    搭建Solr集群的推荐方案
    汉语-成语:少不更事
    汉语-成语:老成持重
    汉语-成语:少年老成
  • 原文地址:https://www.cnblogs.com/da-guang/p/4140636.html
Copyright © 2020-2023  润新知