简单数据结构之链表(无头节点)
2014-05-14 16:55
459 查看
/************************************************************************************** * Function : 链表学习 * Create Date : 2014/05/13 * Author : NTSK13 * Email : beijiwei@qq.com * Copyright : 欢迎大家和我一起交流学习,转载请保持源文件的完整性。 * 任何单位和个人不经本人允许不得用于商业用途 * Version : V0.1 *************************************************************************************** 简单数据结构之链表(无头节点) **************************************************************************************/ #include<stdio.h> #include<stdlib.h> #include<malloc.h> typedef struct node //建立节点 数据结构 { int data; struct node *next; }Lnode; void init_list(Lnode *ph)//链表初始化 { ph=NULL; } int get_length(Lnode *ph) { int len=0; Lnode *ptmp=ph; if(ph==NULL) return (0); while(ptmp->next) { len++; ptmp=ptmp->next; } return (len+1); } int search_list(Lnode * ph,int val)//查找链表元素 { Lnode *ptmp=ph; while( ptmp->next != NULL) { if(ptmp->data == val ) return (1); ptmp=ptmp->next; } return (0); } void insert_head_list(Lnode ** ph,int val)//插入链表元素 插头 { Lnode *ptmp=NULL; ptmp=(Lnode *)malloc( sizeof(Lnode) ); if(ptmp==NULL) { printf("Allocate memory fail !!! \n"); fflush(stdout); } ptmp->data=val; ptmp->next=*ph; *ph=ptmp; } void insert_tail_list(Lnode **ph,int val)//插入链表元素 插尾 { Lnode *ptmp=NULL; Lnode *pht=*ph; ptmp=(Lnode *)malloc( sizeof(Lnode) ); if(ptmp==NULL) { printf("Allocate memory fail !!! \n"); fflush(stdout); } ptmp->data=val; ptmp->next=NULL; if(*ph==NULL) { *ph=ptmp; }else { while( (*ph)->next!=NULL) *ph=(*ph)->next; (*ph)->next=ptmp; *ph=pht; } } void delete_nth_list(Lnode **ph,int n)//删除链表第n元素 n= 0,1,2,... { int i=0; Lnode *pre=NULL; Lnode *post=NULL; pre=post=*ph; if(n==0) { *ph=(*ph)->next; free(pre); }else { while(i != n) { pre=post; post=post->next; i++; } pre->next=post->next; free(post); } } void traverse_list(Lnode *ph)//遍历链表 { int len=get_length(ph);//获取链表长度 int i=0; Lnode *ptmp=ph; for(i=0;i<len;i++) { printf("%d \n",ptmp->data); fflush(stdout); ptmp=ptmp->next; } } void traverse_list2(Lnode *ph)//遍历链表 { Lnode *ptmp=ph; while(ptmp->next) { printf("%d \n",ptmp->data); fflush(stdout); ptmp=ptmp->next; } } void destroy_list(Lnode **ph) { Lnode *ptmp=*ph; while( (*ph)->next ) { ptmp=*ph; *ph=(*ph)->next; free(ptmp); } *ph=NULL; } int main() { Lnode * phead=NULL;//定义头指针 int array[5]={0,1,2,3,4}; int i=0; for(i=0;i<5;i++) { insert_tail_list(&phead,array[i]); } traverse_list(phead); delete_nth_list(&phead,2);//删除链表第2个元素 0,1,2,... printf("\n\n"); traverse_list(phead); printf("\n\n"); for(i=0;i<5;i++) { insert_head_list(&phead,array[i]); } traverse_list2(phead); printf("\n\n"); printf("%d \n",search_list(phead,2) );//查找链表元素 destroy_list(&phead); return (0); }
相关文章推荐
- 简单数据结构之链表(无头节点)
- 简单数据结构之链表(有头节点)
- 简单的无头节点单链表
- 简单数据结构之链表(有头节点)
- 数据结构之C++实现无头节点链表(List)(无主函数)
- 【数据结构】C语言实现无头节点单链表
- 【数据结构】C语言实现链表节点的插入和删除
- 数据结构_队列_用链表动态建立释放节点实现队列各种操作_C++实现
- 读书笔记之编程之美 - 3.4 从无头链表中删除节点
- 简单数据结构——单向链表
- 巧妙删除无头单链表中的节点(算法中的“狸猫换太子”)
- 数据结构复习 之 一个简单双向链表的实现
- 编程之美-3.4-从无头单链表中删除节点-单链表逆置
- 从无头单链表中删除节点
- 2.17-比较有头节点的单链表与无头节点插入元素的区别;
- 从无头单链表中删除节点
- 数据结构之简单链表(尾部插入数据)
- 简单数据结构――单向链表
- 建立简单的链表 节点是结构体
- 数据结构:链表的简单操作