数据结构-链表

  • 链表的创建及插入
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
	int data;
	struct node *next;
}node_t;

void insert_list(node_t *ph,int x,int pos)//ph 头节点 x待插入数据 pos待插入位置
{
	int i;
	node_t *ptmp = ph,*pnew=NULL;

	for(i=0;i<pos-1;i++)//循环pos-1次 找到待插入位置的前一个节点
	{
		ptmp = ptmp->next;
	}
	
	//创建新节点
	pnew = (node_t*)malloc(sizeof(node_t));
	if(NULL==pnew)
	{
		puts("malloc error");
		exit(-1);
	}

	//赋值
	pnew->data = x;
	pnew->next = NULL;

	//插入
	pnew->next = ptmp->next;
	ptmp->next = pnew;

}

node_t *creat_empty_list()
{
	node_t *q=(node_t*)malloc(sizeof(node_t));
	if(NULL==q)
	{
		puts("malloc error");
		exit(-1);
	}

	q->next = NULL;

	return q;

}

void print_list(node_t *ph)
{
	while(ph->next!=NULL)
	{
		ph = ph->next;
		printf("%d ",ph->data);
	}
}
int main()
{
	node_t *ph=NULL;
	ph = creat_empty_list();
	insert_list(ph,100,1);
	insert_list(ph,10,1);
	insert_list(ph,60,1);
	insert_list(ph,88,2);

	print_list(ph);
	return 0;
}
  • 链表的删除
void delete_list(node_t *ph,int pos)//删除指定位置节点  pos 要删除的位置
{
	int i;
	node_t *ptmp=ph,*pdel=NULL;
	for(i=0;i<pos-1;i++)//循环pos-1次 找到待删除位置的前一个节点
	{
		ptmp = ptmp->next;
	}
	
	//删除
	pdel = ptmp->next;
	ptmp->next = pdel->next;
	pdel->next = NULL;
	
	//释放
	if(pdel!=NULL)
	{
		free(pdel);
		pdel = NULL;
	}
	
}
  • 链表的错误处理及查找
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
	int data;
	struct node *next;
}node_t;

int length_list(node_t *ph)//链表长度 也就是数据节点个数
{
	int count=0;//记录数据节点个数
	while(ph->next!=NULL)
	{
		ph = ph->next;
		count++;
	}
	return count;
}
void insert_list(node_t *ph,int x,int pos)//ph 头节点 x待插入数据 pos待插入位置
{
	if(pos<1||pos>length_list(ph)+1)//错误处理
	{
		puts("insert error");
		return;//插入失败
	}
	int i;
	node_t *ptmp = ph,*pnew=NULL;

	for(i=0;i<pos-1;i++)//循环pos-1次 找到待插入位置的前一个节点
	{
		ptmp = ptmp->next;
	}
	
	//创建新节点
	pnew = (node_t*)malloc(sizeof(node_t));
	if(NULL==pnew)
	{
		puts("malloc error");
		exit(-1);
	}

	//赋值
	pnew->data = x;
	pnew->next = NULL;

	//插入
	pnew->next = ptmp->next;
	ptmp->next = pnew;

}

node_t *creat_empty_list()//创建空链表
{
	node_t *q=(node_t*)malloc(sizeof(node_t));
	if(NULL==q)
	{
		puts("malloc error");
		exit(-1);
	}

	q->next = NULL;

	return q;

}

void print_list(node_t *ph)//遍历打印
{
	while(ph->next!=NULL)
	{
		ph = ph->next;
		printf("%d ",ph->data);
	}
	putchar('\n');
}

void delete_list(node_t *ph,int pos)
{
	int i;
	
	if(pos<1||pos>length_list(ph))//错误处理
	{
		puts("del error");
		return;
	}
	node_t *ptmp=ph,*pdel=NULL;
	for(i=0;i<pos-1;i++)//循环pos-1次 找到待删除位置的前一个节点
	{
		ptmp = ptmp->next;
	}
	
	//删除
	pdel = ptmp->next;
	ptmp->next = pdel->next;
	pdel->next = NULL;
	
	//释放
	if(pdel!=NULL)
	{
		free(pdel);
		pdel = NULL;
	}
	
}

void search_list(node_t *ph,int data)//查找链表中是否存在data
{
	while(ph->next!=NULL)
	{
		ph = ph->next;
		if(ph->data==data)
		{
			puts("has found");
			return;//结束函数
		}
	}
	puts("not found!");
}
int main()
{
	node_t *ph=NULL;
	ph = creat_empty_list();
	insert_list(ph,100,1);
	insert_list(ph,10,1);
	insert_list(ph,60,1);
	insert_list(ph,88,2);
	print_list(ph);//60 88 10 100
	
	delete_list(ph,3);
	print_list(ph);
	
	search_list(ph,88);
	
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值