首页 > 其他分享 >单链表的实现

单链表的实现

时间:2023-07-22 15:33:43浏览次数:32  
标签:结点 单链 struct 实现 List next int LNode

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

//创建一个单链表
struct LNode                //结点
{
    int data;               //数据
    struct LNode* next;     //指向下个结点的指针   
};


void List_Print(struct LNode* p)//自定义列表打印函数
{
    while(p)
    {
        printf("%d ",p->data);
        p = p->next;
    }
    printf("\n");
}

void List_Insert(struct LNode* p,int i,int e)//插入操作。在表L中的第i个位置上插入指定元素。
{
    struct LNode* q;
    q=p;//将头结点地址保存
    for(int j=0;(j<i-1)&&(q!=NULL);j++)//找到第i的前面那个结点
    {
        q=q->next;
    }
    struct LNode* s=(struct LNode*)malloc(sizeof(struct LNode));
    s->data=e;  //将数值写入
    s->next=q->next; //将i-i的后继结点的指针(也就是i的头指针),赋值给要插入结点的后继
    q->next=s;//将原本指向i结点的后继指针,改为指向插入结点的头指针
}


struct LNode* Build_List()//头插法建立单链表
{
    struct LNode *s,*p,*r;
    p=(struct LNode*)malloc(sizeof(struct LNode));
    p->data=0;
    r=p;
    int x;
    scanf("%d",&x);
    while (x!=999)
    {
        s=(struct LNode*)malloc(sizeof(struct LNode));
        s->data=x;
        p->next=s;
        p=s;
        scanf("%d",&x);
    }
    p->next=NULL;
    return r;
}


void List_Delete(struct LNode* p,int i) //删除操作。将单链表中的第i个元素去除。
{
    struct LNode* r;
    r=p;
    for(int j=0;j<i-1;j++)//找到i-1个结点的后继
    {
        r=r->next;
    }
    struct LNode* s;
    s=r->next;//临时保存要删除的结点,以用来释放
    r->next=r->next->next;//将指针跳过第i个
    free(s);
}

void List_Find(struct LNode* p,int data)//查找,
{

}


void Start(struct LNode* p)
{
    int n;
    printf("***********1-Build_List***********\n");
    printf("***********2-List_Insert**********\n");
    printf("***********3-List_Delete**********\n");
    printf("***********4-List_Print***********\n");
    while (1)
    {
        printf("Input\n");
        scanf("%d",&n);
        switch (n)
        {
        case 1:p=Build_List();break;
        case 2:List_Insert(p,2,89);break;
        case 3:List_Delete(p,5) ;break;
        case 4:List_Print(p);break;    
        }
    }
}


int main()
{
    struct LNode* p; //一个指向单链表的指针
    Start(p);
    return 0;
}

标签:结点,单链,struct,实现,List,next,int,LNode
From: https://www.cnblogs.com/lzzcforever/p/17573447.html

相关文章

  • 双向链表的实现
    //带头节点的双链表#include<stdio.h>#include<stdlib.h>typedefstructDNode{intdata;structDNode*prior;structDNode*next;}DNode;//初始化头结点DNode*Init_DLinkList(){DNode*L;L=(DNode*)malloc(sizeof(DNode));L->......
  • 顺序栈的实现
    /*顺序栈是指利用顺序存储结构实现的栈,即利用一组地址连续的存储单元依次存放自栈底到栈顶的数据元素,同时附设指针top指示栈顶元素在顺序表中的位置。做法:以top=0表示空栈,另外设指针base指示栈底元素在顺序栈中的位置。当top与base的值相等时,表示空栈。*/#inc......
  • SpringCloud-Nacos配置中心实现原理(转)
    原文:https://blog.csdn.net/Zong_0915/article/details/113089265作者:Zong_0915 一.NacosConfig实现原理解析首先,NacosConfig针对配置的管理提供了4种操作):获取配置,从NacosConfigServer中读取配置。监听配置:订阅感兴趣的配置,当配置发生变化的时候可以收到一个事件。发布......
  • 记录实现复制EXCEL数据到前端表单
     背景是用户需要把原本在Excel上填报的数据搬运到线上系统进行填报,在做了一个带输入框的表格之后用户提出希望能够实现将EXCEL上的数据复制黏贴到这个表格里的功能。 实现功能的整体思路是在input框上监听黏贴事件,获取到复制的数据。由于在Excel中\t表示换格,\n表示换行,所以......
  • 利用for循环实现乘法表、三角形
    publicclassChengfademo{publicChengfademo(){}publicstaticvoidmain(String[]args){for(inti=1;i<=9;++i){for(intj=1;j<=i;++j){System.out.print(i+"*"+j+"......
  • C# 实现抓取财经网站页面内容的实例方法
    ​ protectedvoidEnter_Click(objectsender,EventArgse)        {            WebClientwe=newWebClient();  //主要使用WebClient类            byte[]myDataBuffer;            myDataBuffer=we.DownloadData......
  • 模拟ArrayList(顺序表)的底层实现
    模拟ArrayLIst的底层实现packagecom.tedu.api04.list;importjava.util.Objects;/***@authorLIGENSEN*Date:2023/7/2011:35*/publicclassArrayListDemo{publicstaticvoidmain(String[]args){ArrList<String>list=newArrList<>......
  • c#实现一元二次方程求解器示例分享
    ​ c#实现一元二次方程求解器示例,需要的朋友可以参考下usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceWindowsFo......
  • Android实现沉浸式状态栏功能
    Android中实现沉浸式状态栏的功能,供大家参考,具体内容如下1.先上效果图,实现沉浸式状态栏有两种方式,一种是通过写Theme主题的方式,另一种是写代码的方式。若要使多个页面出现沉浸式状态栏,则使用主题的方式更方便,如果只要使单个页面出现,则使用代码方式更好!当然了,看个人喜好而去。......
  • element-ui表格实现表头快速筛选
        filterChange(obj){console.log('obj',obj)constkeys=Object.keys(obj)constvalues=Object.values(obj)console.log('keys',keys)console.log('values',values)letpltKeys=......