学习笔记之数据结构篇-02单链表

来源:互联网 发布:获奖证书制作软件 编辑:程序博客网 时间:2024/05/14 18:42
/*单链表的简要实现  *  *  */  #include <stdio.h>      #include <stdlib.h>          struct Node;    typedef struct Node* PtrToNode;    typedef PtrToNode List;    typedef PtrToNode Position;    typedef int ElementType;        struct Node{        ElementType data;        Position Next;    };        /*整表创建(头插法)*/    List CreateList_Pre();    /*整表创建(尾插法)*/    List CreateList_Bac();    /*整表删除*/    void DeleteList(List L);    /*得到当前链表的长度*/    int ListLength(List L);    /*得到指定位置的元素*/    int getElement(List L, ElementType i);    /*插入指定值到指定位置前面*/    void Insert_Pre(List L, ElementType element, int i);    /*插入指定值到指定位置后面*/    void Insert_Bac(List L, ElementType element, int i);    /*删除指定位置的元素*/    void Delete(List L, ElementType i);    /*遍历链表*/    void ListTraverse(List L);        List CreateList_Pre(){        Position NewNode;        List L = malloc(sizeof(struct Node));            if (L == NULL)            return;        L->Next = NULL;            for (int i = 1; i <= 10; i++){            NewNode = malloc(sizeof(struct Node));            NewNode->data = i;            NewNode->Next = L->Next;            L->Next = NewNode;        }        return L;    }        List CreateList_Bac(){        Position NewNode;        Position FirstNode;        List L = malloc(sizeof(struct Node));        FirstNode = L;            if (L == NULL)            return;            for (int i = 1; i <= 10; i++){            NewNode = malloc(sizeof(struct Node));            NewNode->data = i;            L->Next = NewNode;            L = NewNode;        }        L->Next = NULL;        return FirstNode;    }        void DeleteList(List L){        Position P, Q;        P = L->Next;            while (P != NULL){            Q = P->Next;            free(P);            P = Q;        }        L->Next = NULL;    }        int ListLength(List L){        Position P;        int count = 0;        P = L->Next;            while (P != NULL){            P = P->Next;            count++;        }        return count;    }        int getElement(List L, ElementType i){        Position P;        int count = 1;        P = L->Next;            if (P == NULL && i < count)            return;            while (P != NULL && i > count){            P = P->Next;            count++;        }            return P->data;    }        void Insert_Pre(List L, ElementType element, int i){        Position P, NewNode;        int count = 1;        P = L;            while (P->Next != NULL && count < i){            P = P->Next;            count++;        }            if (P == NULL || i < count)            return;            NewNode = malloc(sizeof(struct Node));        NewNode->data = element;        NewNode->Next = P->Next;        P->Next = NewNode;    }        void Insert_Bac(List L, ElementType element, int i){        Position P, NewNode;        int count = 1;        P = L->Next;            while (P != NULL && count < i){            P = P->Next;            count++;        }            if (P == NULL || i < count)            return;            NewNode = malloc(sizeof(struct Node));        NewNode->data = element;        NewNode->Next = P->Next;        P->Next = NewNode;    }        void Delete(List L, ElementType i){        Position P, Tmp;        int count = 1;        P = L;            while (P->Next != NULL && i > count){            P = P->Next;            count++;        }            if (P->Next == NULL || i < count)            return;            Tmp = P->Next;        P->Next = Tmp->Next;        free(Tmp);    }        void ListTraverse(List L){        Position P;        P = L->Next;            while (P != NULL){            printf("%d ", P->data);            P = P->Next;        }        printf("\n");    }        void main(){        List L;            L = CreateList_Bac();        printf("新建链表(尾插法): ");        ListTraverse(L);        printf("当前链表长度为: %d\n", ListLength(L));        printf("当前链表第7个元素为: %d\n", getElement(L, 7));            Insert_Pre(L, 16, 5);        printf("插入至指定元素前面: ");        ListTraverse(L);        Insert_Bac(L, 16, 5);        printf("插入至指定元素后面: ");        ListTraverse(L);            Delete(L, 5);        printf("删除指定元素: ");        ListTraverse(L);            DeleteList(L);        printf("删除链表: ");        ListTraverse(L);        printf("当前链表长度为: %d\n", ListLength(L));            printf("\n");            L = CreateList_Pre();        printf("新建链表(头插法): ");        ListTraverse(L);        printf("当前链表长度为: %d\n", ListLength(L));        printf("当前链表第7个元素为: %d\n", getElement(L, 7));            Insert_Pre(L, 16, 5);        printf("插入至指定元素前面: ");        ListTraverse(L);        Insert_Bac(L, 16, 5);        printf("插入至指定元素后面: ");        ListTraverse(L);            Delete(L, 5);        printf("删除指定元素: ");        ListTraverse(L);            DeleteList(L);        printf("删除链表: ");        ListTraverse(L);        printf("当前链表长度为: %d\n", ListLength(L));    }    

0 0
原创粉丝点击