c語言實現基本的數據結構(二) 鏈表(包括鏈表的三種簡單排序算法)


#include "stdafx.h"
#include <stdlib.h>
//創建一個節點,data為value,指向NULL
Node* Create(int value){
    Node* head = (Node*)malloc(sizeof(Node));
    head->data = value;
    head->next = NULL;
    return head;
}
//銷毀鏈表
bool Destroy_List(Node* head){
    Node* temp;
    while (head){
        temp = head->next;
        free(head);
        head = temp;
    }
    head = NULL;
    return true;
}
//表后添加一個節點,Create(value)
bool Append(Node* head,int value){
    Node* n = Create(value);
    Node* temp = head;
    while (temp->next){
        temp = temp->next;
    }
    temp->next = n;
    return 0;
}
//打印鏈表
void Print_List(Node* head){
    Node* temp = head->next;
    while (temp){
        printf("%d->", temp->data);
        temp = temp->next;
    }
    printf("\n");
}
//在鏈表的第locate個節點后(頭節點為0)插入創建的節點Create(value)
bool Insert_List(Node* head, int locate, int value){
    Node* temp = head;
    Node* p;
    Node* n = Create(value);
    if (locate < 0)
        return false;
    while (locate--){
        if (temp->next == NULL){
            temp->next = Create(value);
            return true;
        }
        temp = temp->next;
    }
    p = temp->next;
    temp->next = n;
    n->next = p;
    return true;
}
//刪除第locate個節點后(頭節點為0)的節點
bool Delete_List(Node* head, int locate){
    Node* temp = head;
    Node* p;
    if (locate < 0)
        return false;
    while (locate--){
        if (temp == NULL){
            return false;
        }
        temp = temp->next;
    }
    p = temp->next->next;
    free(temp->next);
    temp->next = NULL;
    temp->next = p;
    return true;
}
//獲取鏈表長度(不包括頭節點)
int Size_List(Node* head){
    Node* temp = head;
    int size = 0;
    while (temp->next){
        temp = temp->next;
        size++;
    }
    return size;
}
//鏈表的三種排序(選擇,插入,冒泡)
bool Sort_List(Node* head){
    int t = 0;
    int size = Size_List(head);
    //選擇排序
    /*for (Node* temp = head->next; temp != NULL; temp = temp->next){
        for (Node* p = temp; p != NULL; p = p->next){
            if (temp->data > p->data){
                printf("換%d和%d\n", temp->data, p->data);
                t = temp->data;
                temp->data = p->data;
                p->data = t;
            }
        }
    }*/
    //插入排序
    /*for (Node* temp = head->next->next; temp != NULL; temp = temp->next){
        for (Node* p = head; p->next != NULL; p = p->next){
            if (p->next->data > temp->data)
            {
                printf("換%d和%d\n", temp->data, p->next->data);
                t = temp->data;
                temp->data = p->next->data;
                p->next->data = t;
            }
        }
    }*/
    //冒泡排序
    for (Node* temp = head->next; temp->next != NULL; temp = temp->next){
        for (Node* p = head->next; p->next != NULL; p = p->next){
            if (p->data > p->next->data){
                t = p->data;
                p->data = p->next->data;
                p->next->data = t;
            }
        }
    }
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM