线性链表

<span style="font-size:24px;">
#include<stdio.h>
#include<malloc.h>
typedef struct nnode{
int data;
struct nnode* link;}node;

node* create_list(int n);
void printf_list(node *p_head);
void insert_node(node *p_head,int val_data);
void add_node(node *p_head,int val_data);
void delete_node(node *head,node *p_head);

int main()
{
	node *head;
	head=create_list(4);
	printf_list(head);
	add_node(head,4);
	printf_list(head);
	insert_node(head->link,0);
	printf_list(head);
	delete_node(head,head->link->link);
	printf_list(head);
	return 0;
}

//create an n nodes list
node* create_list(int n)
{
	int i;
	node *head,*p,*q;		//if no head,we cann't return the adress of list head
	head =(node *)malloc(sizeof(node));
	p=head;
	p->data=0;
	for(i=1;i<n;i++)
	{
		q=(node *)malloc(sizeof(node));
		p->link=q;
		p=q;
		p->data = i;

	}
	q->link=NULL;
	return head;
}

//printf list which head is p_head
void printf_list(node *p_head)
{
	while(p_head->link != NULL)
	{
		printf("%d ",p_head->data);
		p_head=p_head->link;	
	}
	printf("%d\n",p_head->data);
}

//insert a node after p_head->link
void insert_node(node *p_head,int val_data)
{
	node *p;
	p=(node *)malloc(sizeof(node));
	p->data=val_data;
	p->link=p_head->link;
	p_head->link=p;
}

//add anode in the list which head is p_head
void add_node(node *p_head,int val_data)
{
	node *p;
	p=(node *)malloc(sizeof(node));
	while(p_head->link!=NULL)
		p_head=p_head->link;
	p_head->link=p;
	p->data=val_data;
	p->link=NULL;
}

//delete a node from list head in address p_head
void delete_node(node *head,node *p_head)
{
	while(head->link != p_head)
		head=head->link;
	head->link=head->link->link;
	free(p_head);
}</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值