111111

#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>

typedef struct node  {
    int data;
    struct node *next;
} NODE;
NODE *Head = NULL;

NODE  *makeANode(int data)
{
    NODE *p = (NODE *)malloc(sizeof(NODE));
    p->data = data;
    p->next = NULL;
    return p;
}
    
int insertNodeHead(int data, NODE *head)
{
    if (head == NULL) {
        head = (NODE *) malloc(sizeof(NODE));
        if (head == NULL) {
            printf("malloc fail\n");
            return -1;
        }
        head->data = data;
        return 0;
    }
    
    NODE *tmp = makeANode(data);
    tmp->next = head->next;
    head->next = tmp;
    return 0;
}
int insertNodeTail(int data, NODE *head)
{
    if (head == NULL) {
        head = (NODE *) malloc(sizeof(NODE));
        head->data = data;
        return 0;
    }
    NODE *tmp = makeANode(data);
    NODE *p = head->next;
    while(p->next != NULL) {
        p = p->next;
    }
    p->next = tmp;
    return 0;
}

int insertNodeSomeAfter(int value, int data, NODE* head)
{
    if (head == NULL) {
        head = (NODE *) malloc(sizeof(NODE));
        head->data = data;
        return 0;
    }
    NODE *tmp = makeANode(data);
    
    NODE *p = head->next;
    for (p; p->next != NULL; p = p->next) {
        if (p->data == value) {
            tmp->next = p->next;
            p->next = tmp;
            break;
        }
    }
    
    return 0;
}

int DeleteNodeOne(int data, NODE *head)
{
    if (head == NULL) {
        return -1;
    }
    if (head->data == data) {
        NODE *p = head;
        head = head->next;
        free(p);
        return 0;
    }
    NODE *tmp = head->next;
    for (tmp; tmp->next != NULL; tmp = tmp->next) {
        if (tmp->next->data == data) {
            NODE *p = tmp->next;
            tmp->next = p->next;
            p->next = NULL;
            free(p);
            return 0;
        }
    }
    
    return 0;
}

int main()
{
    int num = 0;
    scanf("%d", &num);
    int i = num / 2;
    int hd = 0;
    NODE *p = Head;
    scanf("%d", &hd);
    insertNodeHead(hd, Head);
    if (p ==NULL)
    {
        printf("head is null\n");
    }
    for (int m = 0; m < num - 1; m++) {
        int value = 0, data = 0;
        scanf("%d%d", &data, &value);
        insertNodeSomeAfter(value, data, Head);
    }
    
    int del = 0;
    scanf("%d", &del);
    
    for (p; p->next != NULL; p = p->next) {
        printf("%d ", p->data);
    }
    DeleteNodeOne(del, Head);
    

    if (p ==NULL)
    {
        printf("head is null\n");
    }
    for (p; p->next != NULL; p = p->next) {
        printf("%d ", p->data);
    }
    
    
    
    return 0;
}

 

上一篇:【深究系列】LRU算法设计


下一篇:论文解读(DAEGC)《Improved Deep Embedded Clustering with Local Structure Preservation》