单链表的基本算法

#include<stdio.h>
#include<stdlib.h>
typedef char ElemType;
typedef struct Node
{
    ElemType data;
    struct Node *next;
} SqList;
void InitList(SqList *&L)
{
    L=(SqList *)malloc(sizeof(SqList));
    L->next=NULL;
}
void Insert(SqList *&L,ElemType e)//尾插法 特别关注
{
    SqList *s=L,*r;
    while(s->next!=NULL)
        s=s->next;
    r=s;
    s=(SqList *)malloc(sizeof(SqList));
    r->next=s;
    s->data=e;
    s->next=NULL;

}
void Print(SqList *&L)
{
    SqList *s=L->next;
    while(s!=NULL)
    {
        printf("%c ",s->data);
        s=s->next;
    }
    printf("\n");
}
void PrintLength(SqList *&L)
{
    int i=0;
    SqList *s=L->next;
    while(s!=NULL)
    {
        s=s->next;
        i++;
    }
    printf("%d\n",i);
}
void PrintData(SqList *&L,int n )
{
    int i=0;
    SqList *s=L->next;
    while(s!=NULL)
    {
        s=s->next;
        i++;
        if(i==n-1)
        {
            printf("%c\n ",s->data);
            break;
        }

    }
}
int Find(SqList *L,ElemType n)
{
    int i=0;
    SqList *s=L->next;
    while(s!=NULL)
    {
        i++;
        if(s->data==n)
            break;
        s=s->next;

    }
    return i;
}
bool Insertinto( SqList *&L,int i, ElemType &e)
{
    int j=0;
    SqList *p=L,*q;
    while(j<i-1&&p!=NULL)
    {
        j++;
        p=p->next;
    }

    if(p==NULL)
        return false;
    else
    {
        q=(SqList *)malloc(sizeof(SqList));
        q->data=e;
        q->next=p->next;
        p->next=q;
        return true;
    }

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



}
int main()
{
    SqList *L;
    InitList(L);                            //初始化单链表
    ElemType a,b,c,d,e;
    scanf("%c %c %c %c %c%*c",&a,&b,&c,&d,&e);
    Insert(L,a);
    Insert(L,b);
    Insert(L,c);
    Insert(L,d);
    Insert(L,e);                            //使用尾插法插入元素a,b,c,d,e
    Print(L);                               //输出单链表
    PrintLength(L);                         //输出单链表长度
    if(SqNull(L))
        printf("单链表不为空\n");
    else printf("单链表为空\n");            //判断单链表是否为空
    PrintData(L,3);                         //输出第三个元素
    printf("元素a的位置:%d\n",Find(L,a));  //输出元素a的位置
    ElemType f;
    scanf("%c",&f);
    Insertinto(L,4,f);                      //将f插入到第四个位置
    Print(L);                               //输出单链表
    Delete(L,3);                            //删除第三个元素
    Print(L);                               //输出单链表
    free(L);                                //释放内存
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值