链表相关代码(C语言),包含单链表、双链表、双向循环链表增删改查

链表反转(逆序)

//链表逆序
bool reverse_linklist(struct node *ph){struct node *pbefore=NULL,*pafter,*pcur=NULL;pbefore=ph;pcur=ph->next;if(pcur==NULL)return false;while(pcur!=NULL){pafter=pcur->next;//当前的下一个节点指向现在前面一个,pcur->next=pbefore;pbefore=pcur;pcur=pafter;}ph->next->next=NULL;//将最后一个节点指向空ph->next=pbefore;//将头结点指向逆序后的第一个节点}

顺序表:

顺序表的按下标插入、按值插入、控制台输入插入、插入整个数组

#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#define MAXSIZE 10
struct seqlist{int data[MAXSIZE];int lenght;
};
void init_seq(struct seqlist *s){ //初始顺序表 memset(s->data,0,sizeof(s->data));s->lenght=0;
}
//按下标插入
bool insert_pos_val(struct seqlist *s,int pos,int val){int i;//判断插入位置是否合法if( s->lenght ==MAXSIZE)return false;if(pos<0 || pos > s->lenght )return false;for(i=s->lenght;i>pos;i--){s->data[i]=s->data[i-1]; }s->data[pos]=val;s->lenght ++;
}
//控制台输入
int  input_seqlinst(struct seqlist *s,int pos){int data,i;if(pos>=MAXSIZE){printf("插入失败,下标越界\n");return -1;}printf("输入数据:");for(i=0;i<pos;i++){scanf("%d",&data);insert_pos_val(s,i,data);}
}
//按值插入
int  insert_seqlist(struct seqlist *s,int val,int new_val){int i,y=-1;for(i=0;i<s->lenght;i++){if(s->data[i]==val){y=i;break;}}if(y==-1){printf("没有相同的位置\n");return -1;}for(i=s->lenght;i>y;i--){s->data[i]=s->data[i-1];}s->data[y]=new_val;s->lenght++;
}
//插入数组
void insert_arr(struct seqlist *s,int *a,int len){int i;for(i=0;i<len;i++){insert_pos_val(s,i,a[i]);}
}//打印
void printf_seqlist(struct seqlist *s){int i;for(i=0;i<s->lenght;i++){printf("%d ",s->data[i]);}puts("");
}
int main(int argc, const char *argv[])
{struct seqlist st;int val,new_val,pos,len;int arr[]={23,56,34};init_seq(&st);printf("输入数组最大个数:");scanf("%d",&pos);
int a=input_seqlinst(&st,pos);
//  insert_pos_val(&st,0,10);
//  insert_pos_val(&st,1,30);
//  insert_pos_val(&st,2,50);
//  insert_pos_val(&st,3,40);printf_seqlist(&st);if(a==-1)return-1;/*插入数组*/len=sizeof(arr)/sizeof(int);insert_arr(&st,arr,len);/**赋值函数**/
//  printf("输入旧值和新值 val,new_val:");
//  scanf("%d%d",&val,&new_val);//insert_seqlist(&st,val,new_val);printf_seqlist(&st);return 0;
}

链表:

单链表:

单链表结点的增删改查

#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#include<stdlib.h>struct node{int data;struct node *next;
};void init_node(struct node *h){int data=0;h->next=NULL;
}bool insert_val(struct node *ph,int val){struct node *pcur =NULL;struct node *pnew=(struct node *)malloc(sizeof(struct node));pnew->data=val;pnew->next=NULL;for(pcur=ph;pcur->next != NULL;pcur=pcur->next){}pcur->next=pnew;return true;}//打印
void printf_linklist(struct node *ph){struct node *pcur ;for(pcur=ph->next;pcur->next != NULL;pcur=pcur->next){printf("%d\n",pcur->data);}puts("");
}int main(int argc, const char *argv[])
{struct node head;init_node(&head);insert_val(&head,100);insert_val(&head,200);insert_val(&head,300);insert_val(&head,400);printf_linklist(&head);return 0;
}

双向链表

双向链表结点的增删改查

#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>/*****************************双向链表**************/struct node {int data;struct node *prev;struct node *next;
};
void init_dlinklist(struct node *ph){ph->data=0;ph->prev=ph->next=NULL;
}
//尾部插入
bool insert_val_tail(struct node *ph,int val){struct node *pnew;struct node *pcur;pnew=(struct node *)malloc(sizeof(struct node));pnew->data=val;pnew->next=NULL;pnew->prev=NULL;pcur=ph;while(pcur->next!=NULL){pcur=pcur->next;}pnew->prev=pcur;pcur->next=pnew;
}//值插
bool insert_val(struct node *ph,int val,int newval){struct node *pcur;struct node *pnew;pnew=(struct node *)malloc(sizeof(struct node));pcur=ph->next;pnew->data=newval;pnew->prev=NULL;pnew->next=NULL;while(pcur!=NULL && pcur->data!=val){pcur=pcur->next;//   pnew=pnew->next;}pnew->next=pcur->next;pnew->next->prev=pnew;pcur->next=pnew;
}
//删除
bool del_linklist(struct node *ph,int val){struct node *pdel;pdel=ph->next;while(pdel!=NULL&& pdel->data != val){pdel=pdel->next;}pdel->prev->next=pdel->next;if(pdel->next!=NULL) //如果删除的不是最后一个数就执行下面一条语句   pdel->next->prev=pdel->prev;pdel->prev=NULL;pdel->next=NULL;free(pdel);
}//打印
void print_dlinklist(struct node *ph){struct node *pcur;pcur=ph->next;while(pcur!=NULL){printf("%d ",pcur->data);pcur=pcur->next;}puts("");}int main(int argc, const char *argv[])
{struct node head;init_dlinklist(&head);/*链表的怎删改查测试*/insert_val_tail(&head,100);insert_val_tail(&head,200);print_dlinklist(&head);insert_val(&head,200,234);print_dlinklist(&head);return 0;
}

双向循环链表

双向循环结点的增删改查

#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>/*****************************双向循环链表**************/struct node {int data;struct node *prev;struct node *next;
};
void init_cllinklist(struct node *ph){ph->data=0;ph->prev=ph->next=ph;
}
//尾插
bool insert_val_tail(struct node *ph,int val){struct node *pcur=ph->prev;struct node *pnew;pnew=(struct node*)malloc(sizeof(struct node));pnew->data=val;pnew->next=pnew;pnew->prev=pnew;/*    while(pcur->next !=ph){pcur=pcur->next;}*/pnew->next=pcur->next;pnew->prev=pcur;pcur->next->prev=pnew;pcur->next=pnew;}
//值插
bool init_cllinklist_val(struct node *ph,int val,int newval){struct node *pcur;struct node *pnew;pnew=(struct node*)malloc(sizeof(struct node));pcur=ph->next;pnew->data=newval;pnew->next=pnew;pnew->prev=pnew;while(pcur->data!=val && pcur->next!=ph){pcur=pcur->next;}pnew->next=pcur->next;pnew->prev=pcur;pcur->next->prev=pnew;pcur->next=pnew;
}
//删除
bool del_clinklist(struct node*ph,int val){struct node *pdel;pdel=ph->next;while(pdel!=ph && pdel->data!=val){pdel=pdel->next;}pdel->prev->next=pdel->next;//核心pdel->next->prev=pdel->prev;
}//打印
void prinf_cllinklins(struct node * ph){struct node *pcur;pcur=ph->next;while(pcur!=ph){printf("%d ",pcur->data);pcur=pcur->next;}puts("\n-----------------");}
int main(int argc, const char *argv[]){struct node head;init_cllinklist(&head);/*链表的怎删改查测试*/insert_val_tail(&head,100);insert_val_tail(&head,400);prinf_cllinklins(&head);init_cllinklist_val(&head,300,234);prinf_cllinklins(&head);return 0;
}

链表相关代码(C语言)相关推荐

  1. 猴子选王c语言链表程序代码,C语言程序设计-猴子选大王[链表应用]

    2032 猴子选大王 Description 有N只猴子,从1~N进行编号.它们按照编号的顺时针方向排成一个圆圈,然后从第一只猴子开始报数.第一只猴子报的第一个数字为1,以后每只猴子报的数字都是它们前 ...

  2. Algorithm:C++语言实现之链表相关算法(链表相加、链表的部分翻转、链表划分、链表去重、重复元素全部删除)

    Algorithm:C++语言实现之链表相关算法(链表相加.链表的部分翻转.链表划分.链表去重.重复元素全部删除) 目录 一.链表 1.1.链表相加 1.2.链表相加 2.1.链表的部分翻转 2.2. ...

  3. Algorithm:C++语言实现之链表相关算法(单链公共结点问题、一般LCA、括号匹配、最长括号匹配、逆波兰表达式Reverse Polish Notation、直方图矩形面积、收集雨水问题)

    Algorithm:C++语言实现之链表相关算法(单链公共结点问题.一般LCA.括号匹配.最长括号匹配.逆波兰表达式Reverse Polish Notation.直方图矩形面积.收集雨水问题) 目录 ...

  4. 质因数知识以及相关代码(C语言)

    质因数知识以及相关代码(C语言) 以下包括 分解质因数 最大公约数 最小公倍数 互质 一.质因数 1.质因数的相关数论知识 在数论里是指能整除给定正整数的质数.除了1以外,两个没有其他共同质因子的正整 ...

  5. 数据结构严蔚敏C语言版—线性表顺序存储结构(顺序表)C语言实现相关代码

    数据结构严蔚敏C语言版-线性表顺序存储结构(顺序表)C语言实现相关代码 1.运行环境 2.准备工作 1)项目构建 1>新建一个SeqList项目 2>新建两个文件Sources和Heade ...

  6. C语言的链表—完整代码

    链表完整代码.经测试,应该没什么毛病 #include<stdio.h> #include<malloc.h> #include<string.h>typedef ...

  7. c语言约瑟夫环问题,C++_详解约瑟夫环问题及其相关的C语言算法实现,约瑟夫环问题 N个人围成一圈 - phpStudy...

    详解约瑟夫环问题及其相关的C语言算法实现 约瑟夫环问题 N个人围成一圈顺序编号,从1号开始按1.2.3......顺序报数,报p者退出圈外,其余的人再从1.2.3开始报数,报p的人再退出圈外,以此类推 ...

  8. 面试基础算法及编程 第二弹(链表相关:主要考察指针的应用)

    // # -*- coding:utf-8 -*- // # @Author: Mr.chen(ai-chen2050@qq.com) // # @Date: 2018-08-16 16:35:13 ...

  9. 静态链表相关算法学习

    大话数据结构学习笔记-静态链表学习 c语言真是好东西,它具有指针能力,使得它可以非常容易地操作内存中的地址和数据,这比其他高级语言更加灵活方便. 后来的面向对象的语言,如java.C#等,虽然不使用指 ...

最新文章

  1. git pull 报错:git - error: RPC failed; curl 18 transfer closed with outstanding read data remaining 解决
  2. 数字三角形W(加强版) codevs 2189
  3. 《挑战30天C++入门极限》C/C++中字符串常量的不相等性及字符串的Copy
  4. BZOJ2689 : 堡垒
  5. rollup配置及使用
  6. JavaWeb——response与request
  7. httpclient4下载图片 java实现
  8. 【一起学Rust】Rust介绍与开发环境搭建
  9. 读《消费者行为心理学》-笔记
  10. Android Studio系统盘瘦身
  11. openGL增强表面细节----高度贴图
  12. PDFCrackers Plus版
  13. 鸿蒙系统的家电,美的九阳搭载鸿蒙系统的家电正式上市,这手机系统上市真的稳了...
  14. Android7.1 源码修改之Settings音量调节界面增加通话音量调节
  15. ADI DSP的寄存器详细说明在哪里?
  16. 互联网文艺复兴者——互联网之父Vinton G. Cerf
  17. 机械手引导视觉系统定位抓取
  18. Ceph入门到精通-Ceph之对象存储网关RADOS Gateway(RGW)
  19. 如何使用ansys将等高线txt文件转为iges文件
  20. Windows Server 2012高级文件服务器管理-动态访问控制

热门文章

  1. 超外差接收机原理图讲解(四)--音频部分
  2. python语言学习零基础教学视频_零基础学Python语言CAP全套课程
  3. rtx2060为什么叫智商卡_【装机帮扶站】第347期:RTX2060上市,如何购买更实惠?...
  4. 网上书店可行性分析报告(原创)
  5. MYSQL死掉后自动重启脚本
  6. [FineReport]调用存储过程
  7. 四核处理器_国产“二合一”平板电脑评测:搭载13.3英寸2K屏,四核处理器
  8. mongodb 限制ip访问
  9. 最新仿映客直播APP开发实战项目IOS开发实战8天
  10. 华为网络配置(NAT)