zoukankan      html  css  js  c++  java
  • List_Delete

    /*Sorting from little to large use List*/
    
    
    #include <stdio.h>         /* printf, scanf, NULL */
    #include <stdlib.h>     /* malloc, free */
     
    struct node
    {
        int key;
        struct node *next;
    };
     
    typedef struct node Node;
     
    Node *Head = NULL;
    Node *current;
     
    void Insert(int k)
    {
        Node *new_node;
     
        new_node = (Node *)malloc(sizeof(Node));//It is important but i can't understand now
     
        new_node->key = k;
     
        /* new node is inserted at the begining of the list*/
     
        if ( Head == NULL || Head->key > k )
        {
            new_node->next = Head;
            Head = new_node;
        }
     
        /* new node is inserted somewhere inside the list */
        else
        {
            current = Head;
     
            /* Check what is the value in the next node , after the current node */
            /* if it is larger, or if the next node not exist */
            /* then we shuold insert the node to next current */
            /* else, update current to point to next node */
     
            while(1)
            {
                if( current->next == NULL || current->next->key > k )
                {
                    new_node->next = current->next;
                    current->next = new_node;
                    break;
                }
                else
                    current = current->next;
            }
        }
    }
     
    Node *Delete(int k)
    {
        Node *answer;
        Node *current = Head;
        
        /* Handle the special case that the first node is to be deleted */
        if ( Head != NULL && Head->key == k )
        {
            Head = Head->next;
            return current;
        }
        
        else if ( Head != NULL && Head->key > k )
        {
            return NULL;
        }
        
        while( current != NULL )
        {
            /* check the next node is to be deleted */
            if ( current->next != NULL && current->next->key == k )
            {
                answer = current->next;
                current->next = current->next->next;
                return answer;
            }
            
            else if ( current->next != NULL && current->next->key > k )
                return NULL;
            
            /* else, updated current */
            current = current->next;
        }
        return NULL;
        
        
        /* if current is NULL, then k is not in the list, to return NULL */ 
        
    } 
     
    void Print()
    {
            if( Head == NULL )
            printf("The list is empty!
    ");
        else
        {
            current = Head;
     
            while( current != NULL )
            {
                printf("%d ", current->key);
                current = current->next;
            }
     
            printf("
    ");
        }
    }
     
    int main()
    {
        Node *x;
        
        Insert(15);
        Insert(12);
        Insert(5);
     
         printf("15, 12, 5 are inserted");
         
         Print();
         
         x = Delete(5);    
         if (x != NULL )
         {
             printf("5 is deleted: ");        
            free(x);
            Print();    
        }
        
        x = Delete(12);  
        if ( x != NULL )
        {
            printf("12 is deleted: ");
            free(x);
            Print();    
        }
        
        Insert(6);
        Insert(8);
        Insert(7);
        
        printf("6, 8, 7 is inserted: ");
        Print();
     
        x = Delete(8);  
        if ( x != NULL )
        {
            printf("8 is deleted: ");
            free(x);
            Print();    
        }     
         
        return 0;
    }
    View Code
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    hdu 3265 Posters(线段树+扫描线+面积并)
    hdu 3974 Assign the task(线段树)
    hdu 1540 Tunnel Warfare(线段树)
    poj 2777 Count Color(线段树(有点意思))
    用Flask+Redis维护Cookies池
    用代理抓取微信文章
    Idea-常用快捷键列表
    用Flask+Redis维护代理池
    Selsnium-Chrome-PhantomJS-爬取淘宝美食
    分析Ajax请求抓取今日头条街拍图片
  • 原文地址:https://www.cnblogs.com/h-hkai/p/7944164.html
Copyright © 2011-2022 走看看