C的链表实现
来源:互联网 发布:淘宝店铺图片描述尺寸 编辑:程序博客网 时间:2024/05/16 11:03
#include<stdio.h>#include<stdlib.h>struct node{ int num; struct node *next;};struct node *head=NULL;cre_list() //初始化结点{ head = (struct node *)malloc(sizeof(struct node)); head->next = NULL;}add_node(int num) //插入结点{ struct node *ptr = (struct node *)malloc(sizeof(struct node)); ptr->num = num; ptr->next = head; head=ptr;}add_five_node(int num) //在5之后插入结点{ int i = 0; struct node *p,*q; p=head; q=head; struct node *ptr = (struct node *)malloc(sizeof(struct node)); ptr->num = num; while((p->num)!=5) { p = p->next; } ptr->next=p->next; p->next=ptr;}struct node *opp_node(struct node *head)//链表逆序{ struct node *p,*front,*q; front=NULL; p=head; while(p!=NULL) { q=p->next;//尾插入法 p->next=front; front=p; p=q; } head =front; return head;}display_node() //遍历链表{ struct node *p = head; do { printf("%d\t",p->num); p = p->next; }while(p != NULL); printf("\n");}delete_node() //删除偶数结点{ struct node *p,*q; p = head; q = head->next; while(q != NULL) { if((q->num)%2 == 0){ p->next=q->next; free(q); q=p->next; continue;}p = p->next;q = q->next; } if((head->num)%2 == 0) { p = head;head = head->next;free(p);p = NULL; }}int main(){ int i=0; cre_list(); for(i;i<10;i++) { add_node(i); } display_node(); add_five_node(12); add_five_node(14); add_five_node(16); add_five_node(18); display_node(); delete_node(); display_node();head=opp_node(head); display_node(); return 0;}
链表删除偶结点时,head结点暂不考虑,利用两个指针删除从第二个结点开始的偶结点,待删除完之后,再回头判断头结点是否为偶数,若为偶数则删除头结点。
- c的链表实现
- C的链表实现
- c链表的实现
- C 链表的实现
- 链表的C语言实现之单链表的实现
- 链表的C语言实现之单链表的实现
- 链表的C语言实现之单链表的实现
- 链表的C语言实现之单链表的实现
- C语言单向链表的实现
- c实现的一个经典链表
- C语言实现数据结构的链表
- 双向链表的实现 c
- 链表的类C实现
- 链表的c语言实现
- 链表的C语言实现
- C语言实现的单向链表
- C实现链表的算法
- C 链表及其接口的实现
- Windows 7安装Eclipse 3.7搭建C/C++开发环境
- 腾讯游戏的5rnm
- 漫谈linux文件IO(转)
- Android OpenGL ES 简明开发教程三:3D绘图基本概念
- linux 内核的链表操作(好文不得不转)
- C的链表实现
- Hadoop在linux下安装和配置
- SecureCRT连接ubuntu
- Android通信方式(三)------- Webservice概述
- 基于 android 数据备份恢复的一种实现
- DestroyWindow
- 文件与目标操作
- ubuntu 备份与还原
- 在Android中使用VideoView播放视频