数据结构学习day2

单链表排序,单链表每个节点进行销毁。

主函数

#include "link.h"

int main(int argc,const char*argv[])
{
	//申请头节点函数,返回头节点地址给L
	
	Phpde L = get_head();

	//头插法创建整表
	int a[10] = {10,20,30,40,50,60,70,80,90,95};
	int i;
#if 0
	for(i=0;i<10;i++)
	{
		heda_insert(L,a[i]);
	}
#endif
	//尾插法创建整表
	for(i=0;i<10;i++)
	{
		tail_insert(L,a[i]);
	}
	
	//单链表遍历
	node_output(L);//输出节点

	//任意位置插入一个节点
	insert_any_pos(L,4,100);
	node_output(L);//输出节点

	//头节点删除
	head_dele(L);	
	node_output(L);//输出节点

	//尾节点删除
	taile_dele(L);
	node_output(L);//输出节点
	
/*	//任意位置删除
	dele_any_pos(L,6);
	node_output(L);

	//按照值查找并修改
	value_find_change(L,100);
	node_output(L);
	
	//按照位置查找并修改
	pos_find_change(L,3);
	node_output(L);
*/
	//单链表逆置
//	node_reverse(L);
//	node_output(L);

	//单链表排序
	node_sort(L);
	node_output(L);

	//销毁
	 destroy_List(L);
	 node_output(L);


	return 0;
}

link.h文件

#ifndef  _LINK_H_
#define  _LINK_H_
#include <myhead.h>

typedef struct node
{
	union
	{
		int len;//头节点存储链表长度
		int data;
	};//联合体特点,头节点只能使用len,正常节点使用data
	struct node *next;
}Node,*Phpde;

Phpde get_head();
int  heda_insert(Phpde L,int e);
int  node_output(Phpde L);
int tail_insert(Phpde L,int e);
int insert_any_pos(Phpde L,int a,int b);
int head_dele(Phpde L);  
int taile_dele(Phpde L);
int dele_any_pos(Phpde L,int pos);
int	value_find_change(Phpde L,int pos);
int pos_find_change(Phpde,int pos);
int node_reverse(Phpde L);
int node_sort(Phpde L);
void destroy_List(Phpde L);
#endif

link.c文件


#include "link.h"

Phpde get_head()
{
	Phpde p = malloc(sizeof(Node));
	if(p==NULL)
	{
		printf("申请头节点失败\n");
		return NULL;
	}
	p->len=0;//数据域赋值
	p->next = NULL;//指针域赋值(只有头节点)

	return p;
}

int  heda_insert(Phpde L,int e)
{
	if(L==NULL)
	{
		printf("单链表不存在\n");
		return -1;
	}
	Phpde p = malloc(sizeof(Node));
	p->data = e;//新节点数据赋值

	//将第一个节点的地址放入新节点指针域
	p->next = L->next;

	//将新节点地址放入头节点指针域
	L->next = p;

	L->len++;//长度加1
}

int  node_output(Phpde L)
{
	if(L==NULL || L->len==0)
	{
		printf("单链表不存在\n");
		return -1;
	}
	int i;
	Phpde t = L;//指向头节点
	for(i=0;i<L->len;i++)
	{
		t = t->next;
		printf("%d\t",t->data);
	}
	printf("\n");
}

int tail_insert(Phpde L,int e)
{
	if(L==NULL)
	{
		printf("单链表不存在\n");
		return -1;
	}
	//尾插法
	Phpde t=L;
	int i;
	for(i=0;i<L->len;i++)
	{
		t = t->next;
	}
	Phpde p = malloc(sizeof(Node));
	p->data = e;//新节点数据赋值

	t->next = p;
	p->next = NULL;
	L->len++;

	return 0;
}
//要插入的位置pos,要插入的值e
int insert_any_pos(Phpde L,int a,int b)
{
	if(L==NULL)
	{
		printf("插入失败\n");
		return -1;
	}
	Phpde t = L;
	int i;
	for(i=0;i<a-1;i++)
	{
		t = t->next;
	}
	Phpde p = malloc(sizeof(Node));
	p->data = b;//新节点数据赋值

	p->next = t->next;
	t->next = p;
	L->len++;

	return 0;
}

int head_dele(Phpde L)
{
	if(L==NULL)
	{
		printf("插入失败\n");
		return -1;
	}
	Phpde t = L->next;
	L->next = t->next;
	L->len--;

	free(t);
	t = NULL;//释放节点空间

	return 0;
}

int taile_dele(Phpde L)
{
	if(L==NULL)
	{
		printf("插入失败\n");
		return -1;
	}
	int i;
	Phpde t = L;
	for(i=0;i<L->len-1;i++)
	{
		t = t->next;
	}
	Phpde Q = t->next;
	t->next = NULL;
	L->len--;

	free(Q);
	Q = NULL;

	return 0;
}

int dele_any_pos(Phpde L,int pos)
{
	if(L==NULL)
	{
		printf("插入失败\n");
		return -1;
	}
	int i;
	Phpde t = L;
	for(i=0;i<pos-1;i++)
	{
		t = t->next;
	}
	Phpde Q = t->next;
	t->next = t->next->next;
	L->len--;

	free(Q);
	Q==NULL;

	return 0;
}

int	value_find_change(Phpde L,int pos)
{
	if(L==NULL)
	{
		printf("单链表不存在\n");
		return -1;
	}
	int i,flag=-1;
	Phpde t = L;
	for(i=0;i<L->len;i++)
	{
		t = t->next;
		if(pos == t->data)
		{
			flag = i+1;//记录修改的位置
			t->data = 59;
			break;
		}
	}
	if(flag==-1)
	{
		printf("不存在该节点\n");
		return -1;
	}
	return 0;
}
int pos_find_change(Phpde L,int pos)
{
	if(L==NULL)
	{
		printf("修改失败\n");
		return -1;
	}
	int i;
	Phpde t = L;
	for(i=0;i<pos;i++)
	{
		t = t->next;
	}
	printf("查找成功,该节点是: %d\n",t->data);
	t->data = 55;
	printf("修改成功\n");

	return 0;
}

int node_reverse(Phpde L)
{
	if(L==NULL)
	{
		printf("修改失败\n");
		return -1;
	}
	int i;
	Phpde t = L->next;
	Phpde Q = L->next->next;
	while(Q!=NULL)
	{
		t->next = Q->next;//链接防止丢失
		Q->next = L->next;//将头节点地址放入逆置
		L->next = Q;//将逆置的节点放入头节点
		Q = t->next;//将Q往后移动一位
	}
}

int node_sort(Phpde L)
{
	if(L==NULL)
	{
		printf("修改失败\n");
		return -1;
	}
	int i,j;
	Phpde t;
	Phpde Q;
	Phpde R;
	for(i=0;i<L->len-1;i++)
	{
		t = L->next;//第1位
		Q = t->next;//第2位
		R = L;//第0位
		for(j=0;j<L->len-i-1;j++)
		{
			if(t->data < Q->data)
			{
				Q = t->next;
				t->next = Q->next;
				Q->next = t;
				R->next = Q;
				R = R->next;
			}
			else
			{
				t = t->next;
				Q = Q->next;
				R = R->next;
			}
		}
	}
	return 0;
}
void destroy_List(Phpde L)
{
	Phpde t = L;
	while(L)
	{
		t = L->next;
		free(L);
		L = t;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值