c语言指针写增删改查程序,c语言简便实现链表增删改查

c语言简便实现链表增删改查

发布时间:2018-07-28 15:10,

浏览次数:656

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

//第一次拿c开始写数据结构,因为自己写的,追求代码量少,和学院ppt不太一样。有错请指出 #include #include

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值