单链表的基本算法

#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;
}

单链表是一种线性数据结构,它由一系列节点组成,每个节点包含一个数据元素和指向下一个节点的指针。设计和实现单链表基本算法主要包括以下几个方面: 1. **创建链表**:初始化一个新的链表,通常通过头节点head开始,其next指针为null。 ```python class Node: def __init__(self, data=None): self.data = data self.next = None def create_list(): head = Node() # 创建空链表 return head ``` 2. **添加元素**:在链表的末尾添加新节点,可以遍历整个链表找到最后一个节点然后添加。 ```python def append(head, data): new_node = Node(data) if not head: head = new_node else: current = head while current.next: current = current.next current.next = new_node return head ``` 3. **查找元素**:遍历链表,直到找到指定数据或遍历结束。 ```python def search(head, target): current = head while current and current.data != target: current = current.next if current: return True else: return False ``` 4. **删除元素**:移除给定值的第一个匹配项,需要考虑头节点的情况。 ```python def delete(head, data): if not head or head.data == data: if head is not None: head = head.next return head current = head while current.next and current.next.data != data: current = current.next if current.next: current.next = current.next.next return head ``` 5. **遍历链表**:用于打印或处理链表的所有节点。 ```python def traverse(head): current = head while current: print(current.data, end=" -> ") current = current.next print("None") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值