用C++实现单链表的各种基本运算算法

文章详细介绍了如何使用C语言实现单链表的创建、初始化、插入、删除、长度查询以及元素查找等基本操作。展示了通过函数如`CreateListF`、`CreateListR`、`InitList`、`DestroyList`等进行链表操作的方法。
摘要由CSDN通过智能技术生成

//代码部分

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

typedef char ElemType;
typedef struct LNode
{
    ElemType data;
    struct LNode *next;
} LinkNode;
void CreateListF(LinkNode *&L,ElemType a[],int n)
{
    LinkNode *s;
    L=(LinkNode *)malloc(sizeof(LinkNode));
    L->next=NULL;
    for(int i=0; i<n; i++)
    {
        s=(LinkNode *)malloc(sizeof(LinkNode));
        s-> data=a[i];
        s-> next=L->next;
        L->next=s;
    }
}
void CreateListR(LinkNode *&L,ElemType a[],int n)
{
    LinkNode *s,*r;
    L=(LinkNode *)malloc(sizeof(LinkNode));
    L->next = NULL;
    r=L;
    for(int i=0; i<n; i++)
    {
        s=(LinkNode *)malloc(sizeof(LinkNode));
        s->data = a[i];
        r->next = s;
        r=s;
    }
    r->next =NULL;
}
void InitList(LinkNode *&L)
{
    L=(LinkNode *)malloc(sizeof(LinkNode));
    L->next =NULL;
}
void DestroyList(LinkNode *&L)
{
    LinkNode *pre=L,*p=pre->next;
    while(p!=NULL)
    {
        free(pre);
        pre=p;
        p=pre->next;
    }
    free(pre);
}
bool ListEmpty(LinkNode *L)
{
    return (L->next == NULL);
}

int ListLength(LinkNode *L)
{
    int i=0;
    LinkNode *p =L;
    while(p->next!=NULL)
    {
        i++;
        p=p->next;
    }
    printf("\n");
}
void DispList(LinkNode *L)
{
    LinkNode *p = L->next;
    while (p!=NULL)
    {
        printf("%c",p->data);
        p=p->next;
    }
    printf("\n");
}
bool GetElem(LinkNode *L,int i,ElemType &e)
{
    int j=0;
    LinkNode *p=L;
    if(i<0)return false;
    while (j<i&&p!=NULL)
    {
        j++;
        p=p->next;
    }
    if(p==NULL)
        return false;
    else
    {
        e=p->data;
        return true;
    }
}
int LocateElem(LinkNode *L,ElemType e)
{
    int i=1;
    LinkNode *p=L->next;
    while(p!=NULL&&p->data!=e)
    {
        p=p->next;
        i++;
    }
    if(p == NULL)
        return false;
    else
    {
        return(i);
    }
}
bool ListInsert(LinkNode *&L,int i,ElemType e)
{
    int j=0;
    LinkNode *p=L,*s;
    if(i<=0)
        return false;
    while(j<i-1 && p!=NULL)
    {
        j++;
        p=p->next;
    }
    if(p == NULL)
        return false;
    else
    {
        s=(LinkNode *)malloc(sizeof(LinkNode));
        s->data=e;
        s->next = p->next;
        p->next=s;
        return true;
    }
}

bool ListDelete(LinkNode *&L,int i,ElemType &e)
{
    int j=0;
    LinkNode *p=L,*q;
    if(i<=0)
        return false;
    while(j<i-1&&p!=NULL)
    {
        j++;
        p=p->next;
    }
    if(p==NULL)
        return false;
    else
    {
        q=p->next;
        if(q==NULL)
            return false;
        e=q->data;
        p->next =q->next;
        free(q);
        return true;
    }

}

//验证部分


int main()
{
    LinkNode *h;
    ElemType e;
    printf("单链表的基本运算如下:\n");
    printf("(1)初始化单链表h\n");
    InitList(h);
    printf("(2)依次采用尾插法插入元素a,b,c,d,e\n");
    ListInsert(h,1,'a');
    ListInsert(h,2,'b');
    ListInsert(h,3,'c');
    ListInsert(h,4,'d');
    ListInsert(h,5,'e');
    printf("(3)输出单链表h:");
    DispList(h);
    printf("(4)单链表h长度:%d\n",ListLength(h));
    printf("(5)单链表h为%s\n",(ListEmpty(h)?"空":"非空"));
    GetElem(h,3,e);
    printf("(6)单链表h的第3个元素:%c\n",e);
    printf("(7)元素a的位置:%d\n",LocateElem(h,'a'));
    printf("(8)在第4 个元素位置上插入元素f\n");
    ListInsert(h,4,'f');
    printf("(9)输出单链表h:");
    DispList(h);
    printf("(10)删除h的第3个元素\n");
    ListDelete(h,3,e);
    printf("(11)输出单链表h:");
    DispList(h);
    printf("(12)释放单链表h\n");
    DestroyList(h);
    return 1;

}

//运行截图

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值