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

 

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • getParameterValues中文乱码[通俗易懂]

    getParameterValues中文乱码[通俗易懂]如果想获得一个元素的value情况时:可以设置为:Stringstr=newString(request.getParameter(“interest”).getBytes(“iso-8859-1”),“utf-8”);如果你获得的是得到复选框中选中的元素value值(有多个),只用在Servlet文件里添加request.setCharacterEncoding(“UTF-8”)…

  • RedFlag Linux 5.0桌面版安装oralce10[通俗易懂]

    RedFlag Linux 5.0桌面版安装oralce10[通俗易懂] 本文中描述的步骤可能有些不是必须的,但笔者没有进一步验证,故将安装过程中的所有步骤均列出在此。—www.bianceng.cn1.安装RedflagLinux5.0桌面版2.安装前得准备,打几个补丁1).redhatlinux9下第一张光盘下的RedHat/RPMS/compat-libstdc++-7.3-2.96.118.i386.rpm(安装方法rpm-ivh

  • [cocos2dx-lua]&quot;Hello Lua&quot;分析「建议收藏」

    [cocos2dx-lua]&quot;Hello Lua&quot;分析

  • html注释快捷键

    html注释快捷键1.选中需要注释的内容—>ctrl+shift+/2.取消注释—>ctrl+shift+\转载于:https://www.cnblogs.com/wyhluckdog/p/10131898.html

  • javascript超强幻灯片代码

    javascript超强幻灯片代码javascript超强幻灯片代码[code]#f_div{    width:150px;    height:100px;    overflow:hidden;    margin-top:0;    margin-right:auto;    margin-bottom:0;    margin-left:0px;}#f_

  • waf(web安全防火墙)主要功能点

    waf(web安全防火墙)主要功能点注入攻击SQL注入防护:阻止恶意SQL代码在网站服务器上执行。命令注入防护:阻止攻击者利用网站漏洞直接执行系统命令。XPATH注入防护:阻止攻击者构造恶意输入数据,形成XML文件实施注入。LDAP注入防护:阻止攻击者将网站输入的参数引入LDAP查询实施注入。SSI注入防护:阻止攻击者将SSI命令在服务端执行,主要发生在.shtml,.shtm,.stm文件。缓冲区溢出防护:阻止请求中填入超过缓冲区容量的数据,防止恶意代码被执行。HPP攻击防护:阻止攻击者利用HPP漏洞来发起注入…

发表回复

您的电子邮箱地址不会被公开。

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