链表之C语言

本文介绍了如何使用C语言实现单向链表和双向链表的基本操作,包括初始化、添加元素(头插法、按值插入最大最小值)、删除元素及打印链表。通过示例代码展示了链表的创建、遍历、清理等过程,适用于理解链表数据结构的基础知识。
摘要由CSDN通过智能技术生成

单向链表 例子

#include <stdio.h>
#include <stdlib.h>
struct  node
{
    int  data;
    struct node *p_next;
};
struct link
{
     struct node head;
     struct node tail;
     struct link *p_link;
};

 void link_init (struct link *link_p)
{
    link_p->head.p_next=&link_p->tail;
    link_p->tail.p_next=NULL;
    link_p->head.data=0;
    link_p->tail.data=0;
}

void link_dell(struct link *link_p)
{
    while (link_p->head.p_next!=&link_p->tail)
    {
          struct node *  first=&link_p->head;
          struct node  *  mid=first->p_next;
          struct node  * last=mid->p_next;
           first->p_next=last;
           free(mid);
           mid=NULL;
    }
}
void printf_link(struct link *link_p)
{
    struct node *index;
    struct node * first ,*mid ,*last ;
    for (index = &link_p->head; index!=&link_p->tail; index=index->p_next)
    {
            first=index;
            mid=first->p_next;
            last=mid->p_next;
            if(mid!=&link_p->tail)
            printf("data %d \n",mid->data);
    }
}
int link_add_head(struct link *link_p,int data)
{

    struct node * first ,*mid ,*last ;
    struct node * new = calloc(1,sizeof(struct node));
    new->data=data;
    first=&link_p->head;
    mid=first->p_next;
    last=mid->p_next;
    first->p_next=new;
    new->p_next=mid;
    return 0;
}

int main(int argc,char ** argv)
{
    struct link link_node;
    link_init(&link_node);
    link_add_head(&link_node,10);
    link_add_head(&link_node,11);
    link_add_head(&link_node,12);
    printf_link(&link_node);
    link_dell(&link_node);
    return 0;
}

双向链表例子

#include <stdio.h>
#include <stdlib.h>

struct node 
{
	int data;
	struct node * p_next;
	struct node * p_preo;
};

typedef struct link
{	
	struct node head;
	struct node tail;
	struct link * p_link;
}link;

void link_init(link * link_p)
{
	link_p->head.p_next=&link_p->tail;
	link_p->head.p_preo=NULL;
	link_p->tail.p_preo=&link_p->head;
	link_p->tail.p_next=NULL;
}

void link_clean(link * link_p)
{
	struct node * front=NULL,*middle=NULL ,*last=NULL ,* nonius=NULL;	

	nonius=&link_p->head;
	int count =0;

	while(nonius->p_next!=NULL)
	{
		front=nonius;
		middle=front->p_next;
		last=middle->p_next;	
		if(middle!=&link_p->tail)
		{
			free(middle);
			middle=NULL;
			front->p_next=last;
			last->p_preo=front;
			count++;
			nonius=nonius->p_next;

			if(last->p_next!=NULL)
			{
				nonius=&link_p->head;
			}

		}

	}
}

int link_line_add(link * link_p ,int number)
{

	struct node * front=NULL,*middle=NULL ,*last=NULL ,* nonius=NULL;
	struct node *new = (struct node *)malloc (sizeof(struct node));
	if(new==NULL)
	{
		printf("malloc data  failed ! \n");
		return -1;
	}	
	new->p_next=NULL;
	new->p_preo=NULL;
	new->data=number;

	nonius=&link_p->head;
	front=nonius;
	middle=front->p_next;
	last=middle->p_next;

	if(front->p_next==&link_p->tail)
	{
		front->p_next=new;
		new->p_preo=front;
		new->p_next=middle;
		middle->p_preo=new;
	}else
	{
		middle->p_next=new;
		new->p_preo=middle;
		new->p_next=last;
		last->p_preo=new;
	}
	return 0;
}
int link_add_max(link * link_p ,int number)
{

	struct node * front=NULL,*middle=NULL ,*last=NULL ,* nonius=NULL;
	struct node *new = (struct node *)malloc (sizeof(struct node));
	if(new==NULL)
	{
		printf("malloc data  failed ! \n");
		return -1;
	}	
	new->p_next=NULL;
	new->p_preo=NULL;
	new->data=number;
	nonius=&link_p->head;

	while(nonius->p_next!=&link_p->tail)
	{
		if(nonius->p_next ==NULL)
		{
			printf("link data error \n");
			return -1;
		}
		nonius=nonius->p_next;

		front=nonius;
		middle=front->p_next;
		last=middle->p_next;

		if(middle->data<number)
		{
			middle->p_next=new;
			new->p_preo=middle;
			new->p_next=last;
			last->p_preo=new;

			break;
		}


	}
	return 0;
}
int link_add_min(link * link_p ,int number)
{

	struct node * front=NULL,*middle=NULL ,*last=NULL ,* nonius=NULL;

	struct node *new = (struct node *)malloc (sizeof(struct node));

	if(new==NULL)
	{
		printf("malloc data  failed ! \n");
		return -1;
	}

	new->p_next=NULL;
	new->p_preo=NULL;
	new->data=number;
	nonius=&link_p->head;

	while(nonius->p_next!=&link_p->tail)
	{
		if(nonius->p_next ==NULL)
		{
			printf("link data error \n");
			return -1;
		}
		nonius=nonius->p_next;

		front=nonius;
		middle=front->p_next;
		last=middle->p_next;

		if(middle->data>number)
		{
			front->p_next=new;
			new->p_next=middle;
			new->p_preo=front;
			middle->p_preo=new;
			break;
		}
	}
}
int link_del_num(link* link_p,int number)
{

	struct node * front=NULL,*middle=NULL ,*last=NULL ; 
	struct node * nonius  =&link_p->head;
	while(nonius->p_next!=&link_p->tail)
	{       
		nonius=nonius->p_next;

		front=nonius;
		middle=front->p_next;
		last=middle->p_next;
		if(middle->data==number)
		{
			free(middle);
			middle=NULL;
			front->p_next=last;
			last->p_preo=front;
			printf("del %d \n",number);
			break;
		}
	}
	return 0;
}
struct node  * get_number(link * link_p,int number)
{
	struct node * front=NULL,*middle=NULL ,*last=NULL ,*ret=NULL ;
	struct node * nonius  =&link_p->head;

	while(nonius->p_next!=&link_p->tail)
	{
		nonius=nonius->p_next;

		front=nonius;
		middle=front->p_next;
		last=middle->p_next;
		if(middle->data==number)
		{
			ret=middle;
			break;
		}
	}
	return   ret;
}
int  link_print(link * link_p)
{

	const struct node * front=NULL,*middle=NULL ,*last=NULL ;

	struct node * nonius  =&link_p->head;
	int tatol=0;

	printf("\n ");

	while(nonius->p_next!=&link_p->tail)
	{	
		tatol++;	
		nonius=nonius->p_next;
		printf(" %d ",nonius->data);
	}
	printf(" \n link tatol %d   \n ",tatol);

	return 0;
}




int main(int argc,char ** argv)
{

	link link_m;
	link_init(&link_m);
	link_line_add(&link_m,10);	
	link_line_add(&link_m,11);	
	link_line_add(&link_m,12);	
	link_line_add(&link_m,13);	
	link_line_add(&link_m,14);	
	link_line_add(&link_m,15);

	link_print(&link_m);

	link_clean(&link_m);

	printf("{{{{{{{\n");
	link_print(&link_m);

	printf("}}}}}}}}\n");

	link_line_add(&link_m,10);	
	link_line_add(&link_m,11);	
	link_line_add(&link_m,8);	
	link_line_add(&link_m,13);	
	link_line_add(&link_m,14);	

	link_add_max(&link_m,9);

	link_add_min(&link_m,2);

	link_print(&link_m);

	link_del_num(&link_m,2);

	link_print(&link_m);

	printf("get number =  %d  \n",(int)get_number(&link_m,9)->data);

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值