c语言简便实现链表增删改查「建议收藏」

c语言简便实现链表增删改查「建议收藏」 注:单追求代码简洁,所以写法可能有点不标准。//第一次拿c开始写数据结构,因为自己写的,追求代码量少,和学院ppt不太一样。有错请指出#include<stdio.h>#include<stdlib.h>#include<string.h>typedefstructnode//定义节点{intdata;struc…

大家好,又见面了,我是你们的朋友全栈君。

 注:单追求代码简洁,所以写法可能有点不标准。

//第一次拿c开始写数据结构,因为自己写的,追求代码量少,和学院ppt不太一样。有错请指出
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node//定义节点
{
    int data;
    struct node * next;
}Node;

 

//函数介绍
void printlist(Node * head)//打印链表
int lenlist(Node * head)//返回链表长度
void insertlist(Node ** list,int data,int index)//插入元素
void pushback(Node ** head,int data)//尾部插入
void freelist(Node ** head)//清空链表
void deletelist(Node ** list,int data)//删除元素
Node * findnode(Node ** list,int data)//查找
void change(Node ** list,int data,int temp)//改变值

打印

void printlist(Node * head)//打印链表
{
    for(;head!=NULL;head=head->next) printf("%d ",head->data);
    printf("\n");//为了其他函数打印,最后换行
}

链表长度

int lenlist(Node * head)//返回链表长度
{
    int len;
    Node * temp = head;
    for(len=0; temp!=NULL; len++) temp=temp->next;
    return len;
}

插入元素

void insertlist(Node ** list,int data,int index)//插入元素,用*list将head指针和next统一表示
{
    if(index<0 || index>lenlist(*list))return;//判断非法输入
    Node * newnode=(Node *)malloc(sizeof(Node));//创建
    newnode->data=data;
    newnode->next=NULL;
    while(index--)list=&((*list)->next);//插入
    newnode->next=*list;
    *list=newnode;
}

尾部增加元素

void pushback(Node ** head,int data)//尾插,同上
{
    Node * newnode=(Node *)malloc(sizeof(Node));//创建
    newnode->data=data;
    newnode->next=NULL;
    while(*head!=NULL)head=&((*head)->next);//插入
    *head=newnode;
}

清空链表

void freelist(Node ** head)//清空链表
{
    Node * temp=*head;
    Node * ttemp;
    *head=NULL;//指针设为空
    while(temp!=NULL)//释放
    {
        ttemp=temp;
        temp=temp->next;
        free(ttemp);
    }
}

删除

void deletelist(Node ** list,int data)//删除链表节点
{
    Node * temp;//作用只是方便free
    while((*list)->data!=data && (*list)->next!=NULL)list=&((*list)->next);
    if((*list)->data==data){
        temp=*list;
        *list=(*list)->next;
        free(temp);
    }
}

查找

Node * findnode(Node ** list,int data)//查找,返回指向节点的指针,若无返回空
{
    while((*list)->data!=data && (*list)!=NULL) list=&((*list)->next);
    return *list;
}

改值

void change(Node ** list,int data,int temp)//改变
{
    while((*list)->data!=data && (*list)->next!=NULL)list=&((*list)->next);
    if((*list)->data==data)(*list)->data=temp;
}

 

最后测试

int main(void)//测试
{
    Node * head=NULL;
    Node ** gg=&head;
    int i;
    for(i=0;i<10;i++)pushback(gg,i);
    printf("链表元素依次为: ");
    printlist(head);
    printf("长度为%d\n",lenlist(head));
    freelist(gg);
    printf("释放后长度为%d\n",lenlist(head));
    for(i=0;i<10;i++)pushback(gg,i);
    deletelist(gg,0);//头
    deletelist(gg,9);//尾
    deletelist(gg,5);
    deletelist(gg,100);//不存在
    printf("再次创建链表,删除节点后\n");
    printlist(head);
    freelist(gg);
    for(i=0;i<5;i++)pushback(gg,i);
    insertlist(gg,5,0);//头
    insertlist(gg,5,5);
    insertlist(gg,5,7);//尾
    insertlist(gg,5,10);//不存在
    printlist(head);
    printf("找到%d\n把3变为100",*findnode(gg,5));
    change(gg,3,100);
    change(gg,11111,1);//不存在
    printlist(head);
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/149821.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 博客园整改之思考_整改思路

    博客园整改之思考_整改思路在博客园写博客写了三年半的时间了,当初为什么会选择在博客园写,我也记不清是什么原因了,或许这大概是缘分吧。今年3月份后半段的时候,博客园突然访问不了了,如今通过搜索资料,仍然发现有部分文章访问不了,

    2022年8月4日
  • python如何使用for循环_Python 中for循环的应用

    python如何使用for循环_Python 中for循环的应用1.for…in循环循环,遍历,迭代都是指把容器中的数据一个一个获取出来lst=[1,2,3,4,5]i=0whilei<len(lst): print(lst[i]) i+=1 #无法用while它的索引下标获取集合其中的值;setvar={“a”,”b”,”c”}for..in..应用而生语法:for变量in可迭代对象: code…可迭代对象(通常用到的是:容器类型数据,range对象,迭代器)2

    2022年8月12日
  • 报SQL异常Parameter index out of range (1 > number of parameters, which is 0).「建议收藏」

    报SQL异常Parameter index out of range (1 > number of parameters, which is 0).「建议收藏」数据库错误:Parameterindexoutofrange(1>numberofparameters,whichis0).错误发生原因其实很简单,就是当设置参数时,没有相应的问号与之匹配(或者根本就没有?号).如果是:Parameterindexoutofrange(26>numberofparamet…

    2022年8月21日
  • 微信地位,牢不可破?

    微信地位,牢不可破?作者|李楠本文经授权转自新浪科技QQ与微信接连称霸社交赛道20年后,新的临界点似乎已经到来。回看2019的社交市场,新产品持续推出。截至目前,阿里巴巴、字节跳动、百度、京东、网易、搜狐等新老巨头纷纷入局,映客8500万美元收购积目引起热议,以“灵魂”做标签的Soul崭露头角。有专家向新浪科技表示,00后群体正在崛起,其对社交网络的需求有所差别,这群主力军不一定热衷于微…

    2022年5月14日
  • file指定路径_目标实现的策略与路径

    file指定路径_目标实现的策略与路径FileProvider路径配置策略的理解★FileProvider的使用在AndroidManifest.xml中&amp;lt;providerandroid:name=&quot;android.support.v4.content.FileProvider&quot;android:authorities=&quot;set_your……

    2022年10月25日
  • AWS EC2 增加多个弹性 IP[通俗易懂]

    AWS EC2 增加多个弹性 IP

    2022年2月13日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号