• 单链表的基本操作


    #include <stdio.h>

    #include <stdlib.h>

    typedef struct node

    {

     int data;

     struct node * next;

    }node;

    node *creat()  //创建单链表

    {  

    node *head;//头结点  

    node *cur;//当前结点  

    node *temp;//临时结点  

    int x;//数据  

    int cycle = 1;  

    head = (node*)malloc(sizeof(node));//为头结点分配内存  

    head->next = NULL; //初始head->next为空  

    cur = head;  

    while(cycle)  

    {    

    printf("please input a data(end to 0): ");  

    scanf("%d",&x);

     if(x!=0)  

    {      

    temp = (node*)malloc(sizeof(node));      

    temp->data = x;    

     temp->next = cur->next;//尾结点置空      

    cur->next = temp;      

    cur=temp;  

    }  

    else  

    {   

    cycle = 0;

     }  

    }

      return head;  

    }

    int length(node *head)//单链表长度

    {

     int n = 0;  

    node *cur;  

    cur = head;    

    while ((cur->next)!=NULL)  

    {      

    cur=cur->next;   

    n++;  

    }    

    return n;

    }

    void print(node *head)//单链表的打印

    {   

    node *cur;   

    int n = length(head);

      if(head==NULL||head->next==NULL)   

    {    

    printf("There is no data! ");    

    return;   

    }   

    printf("These %d datas are: ",n);

    cur=head->next;   

    while(cur!=NULL)   

    {      

    printf("%d   ",cur->data);   

    cur=cur->next;   

    }   

    printf(" ");

    }

    node * del(node *head,int num)//单链表的删除

    {  

    node *cur;

     node *per;  

    cur = head->next;//当前结点

     per = head;//当前结点的前结点    

    if(head==NULL||head->next==NULL)//头结点为空或者头结点后无结点 直接返回  

    {   

    return head;

     }

     while(num!=(cur->data)&&(cur->next)!=NULL)  

    {   

    per=cur;   

    cur = cur->next;

     }

     if (num==(cur->data))  

    {  

     per->next = cur->next;  

     free(cur);  

    }

     return head;

    }

    node *sort(node *head)//单链表的排序

    {  

    node *cur;  

    int n = length(head);  

    int temp;

     if(head==NULL||head->next==NULL)//头结点为空或者头结点后无结点 直接返回  

    {  

     return head;  

    }

     bool exchanged;

     for (int i = 0;i<n-1;i++)//冒泡排序

     {  

     exchanged = false;   

    cur = head->next; //当前结点   

    for(int j = 0;j<n-1-i;j++)   

    {   

     if ((cur->data) > (cur->next->data))   

     {              

    temp = cur->data;     

    cur->data = cur->next->data;     

    cur->next->data = temp;     

    exchanged = true;    

    }   

     cur = cur->next;  //向下移动一个结点   

    }  

     if (!exchanged)  

     {    

    return head;

      }     

    }  

    return head;

    }

    node *insert(node *head,int num)//单链表的插入
    {
     node *cur;
     node *per;
     cur = head->next;
     per = head ;
     if(head==NULL||head->next==NULL)//头结点为空或者头结点后无结点 直接返回
     {
      return head;
     }
     node *temp = (node*)malloc(sizeof(node));
     temp->data = num;
     while((temp->data)>(cur->data)&&(cur->next!=NULL))
     {
      per = cur;
      cur = cur->next;
     }

     if ((temp->data) <= (cur->data))//中间插入
     {
          temp->next = cur;
       per->next = temp;
     }

    else  //比链表里的所以数据都大  插到尾结点后
     {
       temp->next = cur->next;
       cur->next = temp;
     }
     return head;
    }

    node * reverse(node *head)

    {

     node *per,*cur,*temp;

     if(head==NULL||head->next==NULL)//头结点为空或者头结点后无结点 直接返回

     {   

    return head;

     }

     per = NULL;//前一个结点

     cur = head->next;//当前结点

     while(cur)

     {      

    temp = cur->next;//临时结点保存下一个结点指针   

    cur->next = per;//当前结点的next指针改为指向前一个结点   

    per = cur;//前一个结点向后移动   

    cur = temp;//当前结点向后移动

     }  

    head->next = per;//头结点的next指向最后一个结点  此时cur==NULL  

    return head;

    }

    void RemoveHead(node *head)//删除头元素

    {  

    node *temp = head->next;

     head->next = temp->next;

     free(temp);

    }

    int main(int argc, char* argv[])

    {

     int x;  

     node * head = creat();

     print(head);

     printf("the length of the link:%d ",length(head));

     printf("oder the link: ");  

     sort(head);  

     print(head);

     printf("please input the data you want to delete: ");

     scanf("%d",&x);

     del(head,x);  

     print(head);

     printf("please input the data you want to insert: ");

     scanf("%d",&x);

     insert(head,x);

     print(head);

     printf("reverse the link: ");

     reverse(head);

     print(head);

     RemoveHead(head);
     print(head);

     return 0;

    }

  • 相关阅读:
    time,implicitly_wait,WebDriverWait三种等待方式
    iframe,window,alert切换
    pandas
    TestCase,Testsuit,TestLoder,TextTestRunner实现对测试用例全部执行或部分执行
    静态,类,实例,冒泡
    configparser读取
    ddt,data,unpack用法
    mybatis入门教程之搭建一个简单的mybatis项目并启动它
    修改hosts文件后不生效,该怎么办
    在JavaScript种遇到这样的错误如何解决XML 解析错误:格式不佳 位置:http:/... 行 27,列 32:
  • 原文地址:https://www.cnblogs.com/marshhu/p/3437947.html
Copyright © 2020-2023  润新知