数据结构与算法分析c语言描述(Mark Allen)--线性表链表方法实现

线性表--链表实现


头文件

#define ElementType int
#define INF INT_MAX
#ifndef _List_H
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Postion;


//在表尾巴插入一个元素
void InsertAfter(List L,ElementType X);

//初始化一个空的线性表
List MakeEmpty();

//判断是否为空
int IsEmpty(List L);

//判断是否是最后一个元素
int IsLast(Postion P,List L);

//在线性表L中查找X第一次出现的位置
Postion Find(ElementType X,List L);

//在位序I前面插入一个新的元素X
void Insert(ElementType X,List L,Postion P);

//插入到第k个位置
void InsertKth(ElementType X,List L,int K);

//删除第k个位置的链表
void DeleteKth(List L,int K);

//找到元素所在位置是第几个
int FindKth(ElementType X,List L);

//删除指定的位序i的元素
void Delete(ElementType X,List L);

//返回线性表L的长度n
int length(List L);

//寻找前驱元素并且返回
Postion FindPrevious(ElementType X,List L);

//删除整个表
void DeleteList(List L);

//寻找头节点
Postion Header(List L);

//寻找
Postion First(List L);

//
Postion Advance(Postion P);

//取得
ElementType Retrieve(Postion P);

//打印这个表
void PrintList(List L);

//从该处打印链表 直到END
void Print2End(List L,Postion Begin,Postion End);

#endif


struct Node
{
    ElementType Element;
    Postion Next;
};



简单的实现

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#include "list.h"
int main(int argc, char const *argv[])
{
    int choice;
    List L = MakeEmpty();
    while (1)
    {

        printf("\n\n\n\t\t\tPlease input a number to choose what you want to do.\n\n");
        printf("\t\t\t1.Print whole linklist.\n");
        printf("\t\t\t2.insert a element in front of this position.\n");
        printf("\t\t\t3.Find the element and return its postion of K.\n");
        printf("\t\t\t4.the length of list.\n");
        printf("\t\t\t5.delete whole list.\n");
        printf("\t\t\t6.delete the kth element.\n");
        printf("\t\t\t7.delete the element which is in the list.\n");
        printf("\t\t\t8.insert a number in the rear of list.\n");
        printf("\t\t\t9.get a random list.\n");
        printf("\t\t\t0.exit.\n\n\t\t\t");
        scanf("%d", &choice);
        if (choice == 0)
        {
            break;
        }
        else
        {
            ElementType TempElement;
            int K;
            switch (choice)
            {
            case 1:
                PrintList(L);
                break;

            case 2:
                printf("\t\t\tplease input a int number as a element and K means its postion.\n");
                scanf("%d %d", &TempElement, &K);
                InsertKth(TempElement, L, K);
                break;

            case 3:
                printf("\t\t\tplease input a int number as a element.\n");
                scanf("%d", &TempElement);
                printf("your number is in the %dth postion.\n", FindKth(TempElement, L));
                break;

            case 4:
                printf("ths list length is %d.\n", length(L));
                break;

            case 5:
                printf("DELETE COMPLETE.\n");
                DeleteList(L);
                break;

            case 6:
                printf("\t\t\tplease input K.Kth means its postion.\n");
                scanf("%d", &K);
                printf("DELETE COMPLETE.\n");
                DeleteKth(L, K);
                PrintList(L);
                break;

            case 7:
                printf("\t\t\tplease input a int number as a element.\n");
                scanf("%d", &TempElement);
                printf("DELETE COMPLETE.\n");
                Delete(TempElement, L);
                break;

            case 8:
                printf("\t\t\tplease input a int number\n");
                ElementType TempElement;
                scanf("%d",&TempElement);
                InsertAfter(L,TempElement);
                break;

            case 9:
                int NumRandom;
                printf("\t\t\tplease input a int number K and we'll get a k length random list\n");
                scanf("%d",&NumRandom);
                srand((unsigned)time(NULL));
                for(int i=0;i<NumRandom;i++)
                {
                    TempElement=rand()%101;
                    InsertAfter(L,TempElement);
                }
                PrintList(L);
                break;

            }
        }
    }
    system("pause");
    return 0;
}

//在表后插入一个数据
void InsertAfter(List L,ElementType X)
{
    while(L->Next!=NULL)
    {
        L=L->Next;
    }
    List P=(List )malloc (sizeof(struct Node));
    P->Element=X;
    P->Next=NULL;
    L->Next=P;
}

//删除第k个位置的链表
void DeleteKth(List L, int K)
{
    Postion TmpCell;
    Postion P = L;
    int count = 0;

    while (P != NULL && count  < K -1)
    {
        P = P->Next;
        count++;
    }
    if (P == NULL)
    {
        printf("NOT FIND.\n");
        return;
    }
    if (!IsLast(P, L))
    {
        TmpCell = P->Next;
        P->Next = TmpCell->Next;
        free(TmpCell);
    }
}

//找到元素所在位置是第几个
int FindKth(ElementType X, List L)
{
    Postion P = L;
    int count = 0;
    while (P != NULL && P->Element != X)
    {
        count++;
        P = P->Next;
    }

    if (P == NULL)
    {
        printf("NOT FIND.\n");
        return INF;
    }
    return count;
}

//插入到第k个位置
void InsertKth(ElementType X, List L, int K)
{
    Postion P = L;
    int count = 0;
    while (P != NULL && count < K-1)
    {
        P = P->Next;
        count++;
    }
    if (P == NULL)
    {
        printf("NOT FIND.\n");
        return;
    }
    Insert(X, L, P);
}
//打印整个链表
void PrintList(List L)
{
    printf("Head->");

    Postion P = L->Next;

    while (P != NULL)
    {
        printf("%d->", P->Element);
        P = P->Next;
    }
    printf("Null\n");
}

//从该处打印链表 直到END
void Print2End(List L, Postion Begin, Postion End)
{
    Postion P = Begin;
    while (P != End)
    {
        printf("%d->", P->Element);
        P = P->Next;
    }
    printf("%d", P->Element);
}

//查找元素 并且返回地址
Postion Find(ElementType X, List L)
{
    Postion P;
    P = L->Next;
    while (P != NULL && P->Element != X)
    {
        P = P->Next;
    }
    return P;
}

int IsEmpty(List L)
{
    return L->Next == NULL;
}

//判断是否是最后一个元素
int IsLast(Postion P, List L)
{
    //辣鸡代码
    // int count=0;
    // while(P!=NULL)
    // {
    //     count++;
    //     P=P->Next;
    // }
    // return count==P;

    //优雅代码
    return P->Next == NULL;
}

//建立空的表
List MakeEmpty()
{
    List Ptrl;
    Ptrl = (List)malloc(sizeof(struct Node));
    Ptrl->Element = 0;
    Ptrl->Next = NULL;
    return Ptrl;
}

/*insert (after legal postion p)*/
/*header implementation assumed*/
/*parameter L is unused in this implementation*/

void Insert(ElementType X, List L, Postion P)
{
    Postion TmpCell;

    TmpCell = (Postion)malloc(sizeof(struct Node));
    //空间申请失败
    if (TmpCell == NULL)
    {
        printf("out of space!!\n");
        exit(1);
    }
    //插入到p的尾部
    TmpCell->Element = X;
    TmpCell->Next = P->Next;
    P->Next = TmpCell;
}

//删除指定的位序i的元素
void Delete(ElementType X, List L)
{
    Postion P, TmpCell;

    //查找他的前驱元素
    P = FindPrevious(X, L);
    //p tmpcell
    //需要删除某个元素只需要找到它的前一个元素
    //然后修改它指向 被删除元素的下一个位置 最后free删除它完成操作

    //findprevious例程如果没有找到对应的前驱元素
    //会访问到最后一个元素 因此需要加一个判断
    //排除没有找到该元素的情况
    if (!IsLast(P, L))
    {
        TmpCell = P->Next;
        P->Next = TmpCell->Next;
        free(TmpCell);
    }
}

//返回线性表L的长度n
int length(List L)
{
    int count = 0;
    while (L->Next != NULL)
    {
        count++;
        L = L->Next;
    }
    return count;
}

//寻找前驱元素并且返回
Postion FindPrevious(ElementType X, List L)
{
    Postion P;

    P = L;
    while (P->Next != NULL && P->Next->Element != X)
    {
        P = P->Next;
    }
    return P;
}

//删除整个表
void DeleteList(List L)
{
    Postion P, TmpCell;

    P = L;

    //p tmpcell
    while (P->Next != NULL)
    {
        TmpCell = P->Next;
        P->Next = TmpCell->Next;
        free(TmpCell);
    }
}

//寻找头节点
Postion Header(List L)
{
    return L;
}

//寻找第一个元素
Postion First(List L)
{
    return L->Next;
}

//返回下一个元素
Postion Advance(Postion P)
{
    return P->Next;
}

//取得
ElementType Retrieve(Postion P)
{
    return P->Element;
}

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值