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!\n");
    else
    {
        current = Head;
 
        while( current != NULL )
        {
            printf("%d ", current->key);
            current = current->next;
        }
 
        printf("\n");
    }
}
 
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

 

posted @ 2017-12-01 19:34  Veritas_des_Liberty  阅读(608)  评论(0编辑  收藏  举报