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

/*单链表的简要实现 
 * 
 * 
 */  
#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值