链表的插入

头插法、尾插法、有序插入

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

typedef struct student {
    int num;
    float score;
    struct student* pnext;
}stu, * pstu;

void list_head_insert(pstu* pphead, stu** pptail, int i) {                   //头插法
    pstu pnew = (pstu)malloc(sizeof(stu));                                  //申请结构体节点的空间
    pnew->num = i;
    pnew->pnext = NULL;
    if (*pphead == NULL) { *pphead = pnew; *pptail = pnew; }                 //链表为空,头尾指针都指向新节点
    else { pnew->pnext = *pphead; *pphead = pnew; }                          //新节点*pnext指向原本头节点,新节点作为头节点
}

void list_tail_insert(pstu* pphead, stu** pptail, int i) {              //尾插法
    pstu pnew = (pstu)calloc(1, sizeof(stu));                         //申请结构体节点的空间,calloc不用初始化节点pnext
    pnew->num = i;
    if (*pphead == NULL) { *pphead = pnew; *pptail = pnew; }      //链表为空,头尾指针都指向新节点
    else {(*pptail)->pnext = pnew; *pptail = pnew; }             //原本尾节点的*pnext指向新节点,新节点作为尾节点
}
void list_print(pstu phead)         //链表打印
{
    while (phead)
    {
        printf("%d ", phead->num);
        phead = phead->pnext;
    }
}

void list_sort_insert(pstu* pphead, stu** pptail, int i) {           //有序插入
    pstu pnew = (pstu)calloc(1, sizeof(stu));
    pnew->num = i;
    pstu pcur, ppre;
    pcur = ppre = *pphead;
    if (*pphead == NULL) { *pphead = pnew; *pptail = pnew; }                            //链表为空,头尾指针指向新节点
    else if (i < (*pphead)->num) { pnew->pnext = *pphead; *pphead = pnew; }             //判断是否插入头部,头插法
    else {                                                                              //否则插入
        while (pcur) {
            if (i > (pcur->num)) { ppre = pcur; pcur = pcur->pnext; }               //插入值大于pcur->num,ppre指向pcur,pcur指向下一个节点
            else { ppre->pnext = pnew; pnew->pnext = pcur; break; }                  //ppre节点的pnext指向新节点,新节点的pnext指向pcur
        }
        if(pcur==NULL){ (*pptail)->pnext = pnew; *pptail = pnew; }               //插入到尾部,尾插法
    }
}
int main() {
    pstu phead = NULL, ptail = NULL;
    int i;
    while (scanf_s("%d", &i) != EOF)
    {
        list_sort_insert(&phead, &ptail, i);
    }
    list_print(phead);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链表是一种常见的数据结构,用于存储一系列的元素。在Python中,可以使用类来定义链表,并通过插入操作来在链表中添加新的元素。 首先,我们需要定义一个节点类,用于表示链表中的每个元素。节点类通常包含两个属性:一个存储数据的值和一个指向下一个节点的指针。 接下来,我们可以定义一个链表类,用于管理整个链表链表类通常包含两个属性:一个指向链表头部的指针和一个指向链表尾部的指针。 链表插入操作可以分为两种情况。第一种情况是在链表的头部插入元素,这需要更新链表头部指针的指向,并将新的节点的指针指向旧的链表头部。第二种情况是在链表的中间或尾部插入元素,这需要找到插入位置的前一个节点,然后将前一个节点的指针指向新的节点,并将新的节点的指针指向原来的下一个节点。 以下是一个简单的Python链表插入的示例代码: ```python class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None self.tail = None def insert_at_beginning(self, data): new_node = Node(data) if self.head is None: self.head = new_node self.tail = new_node else: new_node.next = self.head self.head = new_node def insert_at_position(self, data, position): new_node = Node(data) if position == 0: self.insert_at_beginning(data) else: current = self.head count = 0 while current.next and count < position - 1: current = current.next count += 1 new_node.next = current.next current.next = new_node def display(self): current = self.head while current: print(current.data) current = current.next # 创建链表对象 my_list = LinkedList() # 在头部插入元素 my_list.insert_at_beginning(1) my_list.insert_at_beginning(2) # 在指定位置插入元素 my_list.insert_at_position(3, 1) my_list.insert_at_position(4, 0) # 显示链表元素 my_list.display() ``` 该示例代码创建了一个具有4个节点的链表,并通过插入操作将元素1、2、3和4插入链表中,并最终显示链表的元素。注意,链表插入操作的时间复杂度是O(n),其中n是链表的长度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值