001 -- Circle LinkList, list initiate, add a new node, delete a node, and List traverse

#include <studio.h>
#include <stdlib.h>

typedef struct CLinkList
{
    int data;
    struct CLinkList *next ;
    
} node;


/*initiate a circle list*/

void ds_init(node **pNode)
{
    int item;
    node *temp;
    node *target;
    
    printf("please input the value of the node, and with 0 input to complete the initiate 
");
    
    while (1)
    {
        scanf("%d", &item);
        fflush(stdin);
        
        if(item==0)
            return;
        if ((*pNode)==NULL)
        {
            /*only 1 node in this circle List*/
            *pNode = (node*)malloc(sizeof(struct CLinkList));
            if (!(*pNode))
                exit(0);
            (*pNode)->data = item;
            (*pNode)->next = *pNode;
        }
        else
        {
            /*find the node which next points to*/
            for (target = (*pNode); target->next !=(*pNode); target = target->next )
                ;
            /*create a new Node*/
            
            temp = (node *)malloc(sizeof(struct CLinkList));
            
            if (!temp)
                exit(0);
            
            temp->data = item;
            temp->next = *pNode;
            target->next = temp;
        }
    }
}

/*add new Node*/
/*parameter: first node of the List and the location for adding*/

void ds_insert(node **pNode, int i)
{
    node *temp;
    node *target;
    node *p;
    int item;
    int j = 1;
    
    printf("please input the location you want to add the node: ");
    scanf("%d". &item);
    
    if(i == 1)
    { //the new node will be the first one
        temp = (node*)malloc(sizeof(struct CLinkList));
        if(!temp)
            exit(0);
        
        temp->data = item;
        
        //look for the last node 
        for (target = (*pNode); target->next != (*pNode); target = target->next)
            ;
        temp->next = (*pNode); //new node's pointer to the original first node *pNode
        target->next = temp;  //the last node pointer to the new first node temp 
        *pNode = temp;     // still, *pNode refer to the first note, which is the newly add one. 
    }
    else
    {
        target = *pNode;
        
        for(;j<(i-1);++j)
        {
            target = target->next;
        }
        
        temp = (node *)malloc(sizeof(struct CLinkList));
        
        temp->data = item;
        p = target->next; //original node's(locate in i) pointer 
        target->next = temp; //target next now become the new node 
        temp->next = p; //new node points to the original i node 
    }
    
    
}


//delete a node

void ds_delete(node **pNode, int i)
{
    node *target;
    node *temp;
    int j = 1;
    
    if (i==1)
    {
        //delete the first node 
        
        //find the last node
        for(target=*pNode;target->next!=*pNode;target=target->next)
            ;
        
        temp = *pNode;
        *pNode = *pNode->next //make sure *pNode points to the first node(original it's the seconde node)
        target->next = *pNode; //the last node points to the first one
        free(temp);
    }
    else
    {
        target = *pNode;
        
        for(;j<i-1;++j)
        {
            target = target->next;
        }
        
        temp = target->next;
        target->next = temp->next;
        free(temp);
            
    }
    
    
}


//to return the location of the node in the list*/
int ds_search(node *pNode, int elem)
{
    node *target;
    int i = 1;
    
    for(target = pNode; target!= elem && target->next != *pNode; ++i)
    {
        target=target->next;
    }
    
    if(target->next == pNode) //means there is no such element in the list
        return 0;
    else
        return i;
}

/*go through the list */
void ds_traverse(node *pNode)
{
    node *temp;
    temp = pNode;
    printf("************Elements in the list**************")
    
    do
    {
        printf("%4d", temp->data);
    }while((temp=temp->next)!=pNode);
    
    printf("/n");
    
}

int main()
{
    node *pHead = NULL;
    char opp;
    int find;
    printf("1. initiate a list
 2. add a new note 
 3. delete a node 
 4. return location of a given element 
 5. return all elements of the list 
 0. quit 
 Please select your actions: ");
    
    while(opp!='0')
    {
        scanf("%c", &opp);
        
        switch(opp)
        {
            case '1':
               ds_init(&pHead);
               print("
");
               ds_traverse(pHead);
               break;
            
            case '2':
                printf("please input the location of node for adding: ");
                scanf("%d",&find);
                ds_insert(&pHead,find);
                printf("in location %d to add the node: 
", find);
                ds_traverse(pHead);
                print("
");
                break;
            case '3':
                printf("please input the location for the node you want to delete: ");
                scanf("%d", &find);
                ds_delete(&pHead,find);
                printf("after delete %d location node: 
", find);
                ds_traverse(pHead);
                print("
");
                break;
            case '4':
                printf("which element you want to search for (input the location): ");
                scanf("%d",&find);
                printf("element%d location is: %d
", find, ds_search(pHead,find));
                print("
");
                break;
            case '5':
                ds_traverse(pHead);
                print("
");
                break;
            case '0':
                exit(0);
            
            
            
            
            
            
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Shareishappy/p/7523322.html