链表的相关知识

一、链表与顺序结构的差别。

       顺序结构是连续的存储空间,方便查找,但是增加与修改比较麻烦,链表是链式结构,不会导致空间的碎片化,而顺序结构会导致这个问题。链表因为存储不是连续的所以查找比较麻烦,但是删除添加比较方便。

二、链表的创建步骤

  1、带头节点的链表创建

         (1) 头插法创建链表     

                创建链表首先需要创建一个节点,利用malloc函数在堆空间中申请一个内存,用相应的数据类型指针去保存这个地址。这里用一个结构体数据类型代码如下

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

//链表节点的信息
struct node
{
	int num;
	char name[32];
	struct node *next;
};



struct node*create_node()
{
	struct node *pnew = NULL;
	pnew = (struct node*)malloc(sizeof(struct node));
	if(NULL == pnew)
	{
		printf("malloc error! %s, %d\n", __FILE__, __LINE__);
		exit(-1);
	}
	pnew->next = NULL;
	return pnew;
}


void list_insert(struct node *head)
{
	//1、创建新的节点
	struct node *pnew = NULL;
	pnew = create_node();

	printf("请输入学号:");
	scanf("%d", &pnew->num);
	while(getchar()!='\n');
	
	printf("请输入姓名:");
	scanf("%s", pnew->name);
	while(getchar()!='\n');

	//2、加入链表
#if   0
	if(head->next == NULL)//空链表
	{
		head->next = pnew;
	}
	else
	{
		pnew->next = head->next;
		head->next = pnew;
	}
#endif

	pnew->next = head->next;
	head->next = pnew;
	return ;
}



void  show_list(struct node *head)
{
	if(head->next == NULL)
	{
		printf("空链表!\n");
	    return ;
	}
    
	struct node *p = head->next;
	while(p!=NULL)
	{
		printf("[%d %s %p]-->", p->num, p->name,p->next);
		p = p->next;
	}
	printf("\n");
	
}

(2)j查找操作。返回找到的地址。

struct node *list_search_by_num(struct node *head, int num)
{
	struct node *p = head->next;
	while(p != NULL)
	{
		if(p->num == num)
		{
			return p;
		}
		p = p->next;
	}
	return NULL;
}

(3)删除操作

//删除操作
//成功: 0
//失败:-1
int list_del_by_num(struct node *head, int num)
{
	//查找学号是否存在
    struct node *pdel = NULL;
	pdel = list_search_by_num(head, num);
	if(NULL == pdel)
	{
		printf("删除失败,学号不存在!\n");
		return -1;
	}

	//删除操作
	struct node *p = head;
	while(p->next != pdel)
	{
		p = p->next;
	}
    p->next = pdel->next;
	free(pdel);
	return 0;
}

(4)释放操作

struct node* list_free(struct node *head)
{
	struct node *pdel = NULL;
	while(head != NULL)
	{
		pdel = head;
		head = head->next;
		free(pdel);
	}
	return head;
}

(5)排序操作

//排序操作
void list_sort(struct node *head)
{
	struct node *new_head = NULL;
	struct node *pmax = NULL;
	struct node *head1 = head->next;
	head->next = NULL;

    struct node *p = NULL;
	while(head1 != NULL)
	{
		//找最大值
        p = head1;
		pmax = head1;
		while(p!=NULL)
		{
			if(p->num > pmax->num)
			{
				pmax = p;
			}
			p = p->next;
		}

		//将最大值从head1链表中移除
	    if(pmax == head1)
		{
			head1 = head1->next;
		}
		else
		{
			p = head1;
			while(p->next != pmax)
			{
				p = p->next;
			}
			p->next = pmax->next;
		}
		pmax->next = NULL;

		//加入新的链表
		pmax->next = new_head;
		new_head = pmax;
	}
	head->next = new_head;
    return ;
}

        总结:链表里面学会增删改查,包括排序这些基本操作,是一切的前提。还是很重要的。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值