在有序链表中插入结点,仍保持其有序性

    如果在一个有序数组中,你想插入一个数据,还需要把其后所有元素往后移一位。

    那么有没有一个比较好的数据结构可以实现简单便捷的插入操作呢?

    那就是链表,链表由两个部分组成,前面的部分为数据部分,用来存储数据,后面的部分为指针部分,用来指向下一个结点,链表的数据结构就很好地解决了插入麻烦的问题。(本文只介绍尾插法)只需要让前一个结点的指针指向要插入的数据,让插入的数据的指针,指向上一个结点原本指向的结点即可!

    下面是代码的实现:
    

#define _CRT_SECURE_NO_WARNINGS 1

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

/*
* 本程序是对于有序链表插入操作的练习
* 郭文峰
* 2018/9/30
*/

struct node
{
	int data;
	struct node *next;
};

int main(void)
{
	struct node *head = NULL;
	struct node *p = NULL;
	struct node *q = NULL;
	struct node *t = NULL;

	int i = 0;
	int a = 0;
	int n = 0;

	//输入n的值,代表链表有几个元素
	scanf("%d", &n);
	for (i = 1; i <= n; i++)
	{
		scanf("%d", &a);

		//动态申请一个内存来存储下一个结点,并用临时指针p指向这个结点
		p = (struct node *)malloc(sizeof(struct node));
		p->data = a;
		p->next = NULL;

		//用尾插法插入临时指针p指向的结点
		if (head == NULL)
		{
			head = p;

		}
		else
		{
			q->next = p;
		}

		q = p;//让q指向此结点

	}

	//输入带插入的值
	scanf("%d", &a);
	//从头指针开始遍历
	t = head;
	while (t != NULL)
	{
		if (t->next->data > a)
		{
			p = (struct node *)malloc(sizeof(struct node));
			p->data = a;
			p->next = t->next;
			t->next = p;
			break;//插入即完毕,退出循环
		}
		t = t->next;
	}

	//输出所有结点
	t = head;
	while (t != NULL)
	{
		printf("%d   ", t->data);
		t = t->next;
	}

	system("pause");

	return 0;

}

猜你喜欢

转载自blog.csdn.net/weixin_42678507/article/details/82906622