Linked list insertion and deletion

#include<stdio.h>
typedef struct _NODE
{
    int data;
    _NODE *next;
}NODE;
void InsertData(NODE *head, int data)
{
    NODE *item = head;
    while(item->next!=NULL)
        item= item->next;
    NODE *node=new NODE();
    node->data = data;
    node->next = NULL;
    item->next = node;
}
void DeleteData(NODE *head, int data)
{
    NODE *item = head->next;
    NODE *pre = head;
    while(item!=NULL && item->data != data)
    {
        item = item->next;
        pre = pre->next;
    }
    if(item!=NULL)
    {
        NODE *tmp = item;
        pre->next=item->next;
        delete tmp;
    }
}
void ShowData(NODE *head)
{
    NODE *list = head->next;
    if(list ==NULL) return ;
    while(list!=NULL)
    {
        printf("%d\t",list->data);
        list = list->next;
    }
    printf("\n");
}
void main()
{
    NODE *head=NULL;
    NODE node;
    node.next = NULL;
    head = &node;
    for(int i=0; i<5;i++)
    {
        InsertData(head,i+1);
    }

    ShowData(head);
    DeleteData(head,3);
    ShowData(head);
    getchar();
}

Guess you like

Origin blog.csdn.net/wyyy2088511/article/details/108271444