您的位置:首页 > 理论基础 > 数据结构算法

简单数据结构之链表(无头节点)

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: