• 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)
  • 相关阅读:
    Tablespace 和 Datafile 关系
    SQL Server DBA常见面试 笔试问题
    查看log_buffer
    锁的查询及解锁
    ORACLE 锁机制
    查看oracle数据库是否归档和修改归档模式(转)
    Oracle kill session – 删除被死锁的会话
    滴水穿石python 学习笔记基础
    滴水穿石Java 生成PDF文件iText使用之入门
    滴水穿石IIS中的200K文件或者Request获取内容大小限制问题
  • 原文地址:https://www.cnblogs.com/h-hkai/p/7944164.html
Copyright © 2020-2023  润新知