天天看點

單連結清單基本算法

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

typedef int LinkType;
typedef struct LinkNode{
    LinkType data;
    struct LinkNode *next;
}LinkList;
//連結清單初始化
void  Linklistinit(LinkList **head)
{
    if (head == NULL)//輸入不合法
        return;
    *head = NULL;
}
//連結清單尾部插入
void  LinkListPushBack(LinkList **head, LinkType valve)
{   
    LinkNode *new_Node = (LinkNode *)malloc(sizeof(LinkNode));
    new_Node->data = valve;
    new_Node->next = NULL;
    LinkNode *cur = *head;
    if (head == NULL)//輸入不合法
        return;
    if ((*head) == NULL)//連結清單為空
    {
        *head = new_Node;
    }
    else
    {
        while (cur->next != NULL)
            cur = cur->next;
        cur->next = new_Node;
    }
}
//删除連結清單尾部元素
void LinkListPopBack(LinkNode** head)
{
    LinkNode *cur = *head;
    if (head == NULL)
        return;
    if (*head == NULL)
        return;
    if ((*head)->next == NULL)
    {
        free(*head);
        *head = NULL;
    }
    else
    {
        while (cur->next->next != NULL)
            cur = cur->next;
        free(cur->next);
        cur->next = NULL;
    }
}
//列印連結清單
void print(LinkList *head)
{
    LinkNode *cur = head;
    while (cur != NULL)
    {
        printf("%d ", cur->data);
        cur = cur->next;
    }
}
//連結清單頭插
void LinkListPushFront(LinkNode** head, LinkType value)
{
    LinkNode *new_Node = (LinkNode *)malloc(sizeof(LinkNode));
    new_Node->data = value;
    new_Node->next = NULL;
    if (head == NULL)
        return;
    if (*head == NULL)
    {
        *head = new_Node;
    }
    else
    {
        new_Node->next = *head;
        *head =new_Node;
    }
}
//連結清單頭删
void LinkListPopFront(LinkNode** head)
{
    LinkNode *cur = *head;
    if (head == NULL)
        return;
    if (*head == NULL)
        return;
    if ((*head)->next==NULL)
    {
        free(*head);
        *head = NULL;
    }
    else
    {
            cur = cur->next;
            free(*head);
            *head = cur;
    }
}
//查找元素在連結清單中的位置 ,傳回這個值對應的節點位址 
LinkNode* LinkListFind(LinkNode* head, LinkType to_find)
{
    LinkNode *cur = head;
    if (head == NULL)
        return NULL;
    else
    {
        while (cur != NULL)
        {
            if (cur->data == to_find)
                return cur;
            cur = cur->next;
        }
    }
    return NULL;
}
//在指定pos節點的前面插入新節點
void LinkListInsertFront(LinkNode** head, LinkNode* pos, LinkType value)
{
    LinkNode *cur = *head;
    if (head == NULL)
        return; //不合法
    if (*head == NULL)
        return;//連結清單為空
    LinkNode *new_Node = (LinkNode *)malloc(sizeof(LinkNode));
    new_Node->data = value;
    new_Node->next = NULL;
    if (*head == pos) //如果pos是頭結點
    {
        new_Node->next = *head;
        *head = new_Node;
    }
    else             //如果pos不是頭結點
    {
        while (cur->next != pos)
            cur = cur->next;
        if (cur->next != NULL) //找到pos
        {
            new_Node->next = cur->next;
            cur->next = new_Node;
        }
        else            //沒有找到pos
        {
            return;
        }

    }
}
void LinkListInsertAfter(LinkNode** head, LinkNode* pos, LinkType value)
{
    LinkNode *cur = *head;
    if (head == NULL)
        return;
    LinkNode *new_Node = (LinkNode *)malloc(sizeof(LinkNode));
    new_Node->data = value;
    new_Node->next = NULL;
    if (*head == pos)
    {
        new_Node->next = (*head)->next;//這兩句話不能換位置
        (*head)->next = new_Node;       //
    }
    else
    {
        while (cur != pos)
            cur = cur->next;
        if (cur != NULL)
        {
            new_Node->next = cur->next;//
            cur->next = new_Node;
        }
    }
}
void LinkListErase(LinkNode** head, LinkNode* pos)
{//連結清單隻有一個節點,pos是該節點,pos不是該節點
 //連結清單不止一個節點,pos是頭節點,pos不是頭結點
    LinkNode *cur = *head;
    if (head == NULL)
        return;
    if (*head == NULL)
        return;
    if ((*head)->next == NULL)
    {
        if (*head == pos)
        {
            free(*head);
            *head = NULL;
        }
        else
            return;
    }
    else
    {
        if (*head == pos)
            *head = cur->next;
        else
        {
            while (cur->next != pos)
                cur = cur->next;
            if (cur->next != NULL)  //
            {
                pos = cur->next;
                cur->next = pos->next;
                free(pos);
            }
            else
                return;
        }
    }
}
void LinkListRemove(LinkNode** head, LinkType to_delete)
{
    LinkNode *cur = *head, *dele;
    if (head == NULL)
        return;
    if (*head == NULL)
        return;
    if ((*head)->data == to_delete)
    {
        if ((*head)->next == NULL)
        {
            free(*head);
            *head = NULL;
        }
        else
        {
            dele = *head;
            *head = cur->next;
            free(dele);
        }
    }
    else
    {
        while (cur->next != NULL && cur->next->data != to_delete)//如果要查的數不存在
            cur = cur->next;
        if (cur->next != NULL)
        {
            dele = cur->next;
            cur->next = cur->next->next;
            free(dele);
        }
        else
            return;
    }
}
int LinkListEmpty(LinkNode* head)
{
    if (head == NULL)
        return ;
    else
        return ;
}

int main()
{
    int a;
    LinkList *L,*d;
    Linklistinit(&L);
    LinkListPushBack(&L, );
    LinkListPushBack(&L, );
    LinkListPushBack(&L, );
    LinkListPopBack(&L);
    LinkListPopFront(&L);
    LinkListPopFront(&L);
    d=LinkListFind(L,);
    printf("%d  ", d);
    LinkListPushFront(&L, );
    LinkListInsertFront(&L, L->next->next, );
    LinkListInsertAfter(&L, L->next->next->next, );
    LinkListErase(&L, L->next->next);
    LinkListRemove(&L, );
    a=LinkListEmpty(L);
    printf("%d", a);
    print(L);
    system("pause");
    return ;

}
           

繼續閱讀