单链表的创建,查找,插入,删除操作

本文深入讲解了链表的基本操作,包括创建、插入、删除及查找等关键步骤,并提供了详细的C语言实现代码,适合初学者理解和掌握链表数据结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct node{
    int num;
    struct node *next;
};
struct node *Create()
{
    struct node *head,*p,*q;
    int x,i=2;
    head=(struct node *)malloc(sizeof(struct node));
    head->next=NULL;
    q=head;
    printf("请输入第1个学生的成绩:"); 
    scanf("%d",&x);
    while(x!=-1)
    {
        p=(struct node *)malloc(sizeof(struct node));
        p->num=x;
        q->next=p;
        q=p;
        printf("请输入第%d个学生的成绩:",i);
        scanf("%d",&x);
        i++; 
    }
    q->next=NULL;
    return head;
}
struct node *Getk(struct node *head,int k)
{
    struct node *p=head->next;
    int s=1,q;
    while(p&&s<k)
{
        p=p->next; s++;
}
    if(s==k) return p;
    else return NULL;
}
    
struct node *Insert(struct node *head,int Index,int i)
{
    struct node *p,*s;
    p=Getk(head,Index-1);
    s=(struct node *)malloc(sizeof(struct node));
    s->num=i;
    s->next=p->next;
    p->next=s;
    return head;
}
struct node *Delete(struct node *head,int Loc)
{
    struct node *w,*c;
    w=Getk(head,Loc-1);
    c=w->next;
    w->next=c->next;
    free(c);
    return head;
     
}
void Print(struct node *head)
{
    struct node *L;
    L=head->next;
    int Index=1;
    while(L!=NULL)
    {
        printf("第%d个学生的成绩是%d\n",Index,L->num);
        L=L->next;
        Index++;
    }
}
void main()
{
    struct node *phead,*x1;
    int l,s;
    phead=Create();
    Print(phead);
    printf("想要查找哪个结点:");
    scanf("%d",&l); 
    x1=Getk(phead,l);
    printf("第%d个同学的成绩是%d\n",l,x1->num);
    int y,loc;
    printf("请输入要增加的结点位置:");
    scanf("%d",&y);
    printf("请输入成绩大小:");
    scanf("%d",&loc);
    Insert(phead,y,loc);
    Print(phead);
    int o;
    printf("请输入要删除的结点位置:");
    scanf("%d",&o);
    Delete(phead,o);
    Print(phead);
    
}

单链表种线性数据结构,其中每个节点包含数据指向下个节点的引用。在Python中,我们可以创建链表节点的类并实现基本的操作,如查找插入删除。 1. 查找操作:如果你需要查找特定值,你可以遍历整个链表,通过比较每个节点的数据来找到目标。这里是个简单的查找函数示例: ```python class Node: def __init__(self, data=None): self.data = data self.next = None def search(node, target): while node is not None: if node.data == target: return True node = node.next return False ``` 2. 插入操作:向链表中添加新节点通常是在链表的末尾,可以先创建新节点,然后让它指向当前最后个节点的`next`: ```python def insert(node, data): new_node = Node(data) if node is None: return new_node else: while node.next is not None: node = node.next node.next = new_node return node ``` 3. 删除操作:移除某个节点通常会根据给定的值或位置来删除。如果知道要删除的节点,可以更新前个节点的`next`指向它后面的节点;如果知道要删除的位置,从头开始遍历直到找到对应位置: ```python def delete(node, target): if node is None or node.data == target: return None elif node.next is not None and node.next.data == target: node.next = node.next.next return node else: prev = node while prev.next is not None: if prev.next.data == target: prev.next = prev.next.next return node prev = prev.next return node ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值