循环链表(2) - 插入节点至已排序链表

编写一个程序,在一个已排序的循环链表中插入一个新的节点。

例如,假设初始的循环链表如下所示:

插入新的节点7之后,上面的循环链表变为了下面所示:

算法:

假设需要插入的新节点为newNode, 则插入操作可以分为下面的3种情形:

1) 链表为空:
    a)  因为循环链表只有newNode这一个节点,则自我循环.
          newNode->next = newNode;
    b) 调整头指针
          *head = newNode;
2) 新节点需要插入到链表头节点的前面:
  (a) 遍历链表,找出尾节点.
         while(current->next != *head)
            current = current->next;
  (b) 修改尾节点的后向指针.
         current->next = newNode;
  (c) 调整newNode的后向指针为原始头节点.
         newNode->next = *head;
  (d) 调整头指针,指向新的节点.
         *head = newNode;
3) 新节点需要插入到某个节点的后面:
   (a) 定位需要被插入节点的位置.
         while ( current->next!= *head && current->next->data < newNode->data)
             current = current->next;
   (b) 将新节点的后向指针调整为定位节点的后向指针
         newNode->next = current->next;
   (c) 修改定位节点的后向指针
         current->next = newNode;  

代码实现
#include <iostream>

//链表节点
struct Node
{
	int data;
	Node *next;
};

void swap(int *a, int*b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

//在一个已排序的循环链表中插入新节点
void sortedInsert(Node** head, Node* newNode)
{
	Node* current = *head;

	// 情形1
	if (current == NULL)
	{
		newNode->next = newNode;
		*head = newNode;
	}
	// 情形2
	else if (current->data >= newNode->data)
	{
		//如果新节点值小于头节点值,则需要调整尾节点的后向指针
		while (current->next != *head)
			current = current->next;
		current->next = newNode;
		newNode->next = *head;
		*head = newNode;
	}
	// 情形3
	else
	{
		//定位需要被插入数据的节点
		while (current->next != *head && current->next->data < newNode->data)
			current = current->next;

		newNode->next = current->next;
		current->next = newNode;
	}
}

//在循环链表头部插入新的节点
void push(Node **head, int data)
{
	Node *newNode = new Node;
	Node *temp = *head;
	newNode->data = data;
	newNode->next = *head;

	//如果链表不为空,则将最后一个节点的后向指针设置为新节点
	//即新节点变成了新的头节点。
	if (*head != NULL)
	{
		while (temp->next != *head)
			temp = temp->next;
		temp->next = newNode;
	}
	else
		newNode->next = newNode; //新节点做为链表第一个节点

	*head = newNode;  //调整头节点
}

//打印循环链表
void printList(Node *head)
{
	Node *temp = head;
	if (head != NULL)
	{
		do
		{
			std::cout<<" "<<temp->data<<" ";
			temp = temp->next;
		} while (temp != head);
	}
}

int main()
{
	const int arrSize = 6;
	int arr[arrSize] = { 12, 56, 2, 11, 1, 90 };

	Node *head = NULL;

	for (int i = 0; i < arrSize; i++)
	{
		Node *newNode = new Node;
		newNode->data = arr[i];
		sortedInsert(&head, newNode);
	}

	printList(head);
	std::cout << std::endl;

	return 0;
}

输出:
1 2 11 12 56 90

时间复杂度: O(n) ,其中n是给定链表中的节点个数

代码改进

其中,上述代码中的情形2的算法还能优化。可以使用值交换的方法,避免遍历整个链表。优化后的代码如下所示:

// 情形2的代码实现
else if (current->data >= newNode->data)
{
    swap(&(current->data), &(newNode->data)); //假设存在swap函数
    newNode->next = (*head)->next;
    (*head)->next = newNode;
}
时间: 2024-08-11 05:42:06

循环链表(2) - 插入节点至已排序链表的相关文章

剑指Offer15 合并两个已排序链表

1 /************************************************************************* 2 > File Name: 15_MergeTwoSortList.cpp 3 > Author: Juntaran 4 > Mail: [email protected] 5 > Created Time: 2016年08月30日 星期二 15时49分47秒 6 ********************************

83. 移除已排序链表中重复的节点 Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 移除单链表中重复的节点 public class Solution { public ListNode DeleteDup

链表插入和删除,判断链表是否为空,求链表长度算法的,链表排序算法演示——C语言描述

关于数据结构等的学习,以及学习算法的感想感悟,听了郝斌老师的数据结构课程,其中他也提到了学习数据结构的或者算法的一些个人见解,我觉的很好,对我的帮助也是很大,算法本就是令人头疼的问题,因为自己并没有学习过算法的系统性的课程,现在还是处于不断摸索的阶段,好多算法题目根本就没有什么思路,导致自己对好多题目都很是头疼,就算是自己做过的一些算法的题目,再次遇到也还是不一定会做出来,他给出的建议就是,看懂别人的程序,然后自己去敲,一定会出错,然后调试,有错误接着调试,一直到没有错误为止,并且要时常的去复习

将一个已排序的链表或数组转化成一棵平衡二叉树

Problem:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 解题思路:这里要充分利用元素的有序性,来构造一棵平衡二叉树,这样可以避免为了维持二叉树的平衡性,而进行各种旋转操作.可以每次都向树中插入一个序列中的中间元素,这样就可以保证该结点的左子树和右子树含有结点的数目相等(或只相差一个).这样反复操作,就可以构造一颗类完全

双链表删除/插入节点

//双链表删除节点 dnode *del(dnode *head, int num) { dnode *p1, *p2; p1 = head; while (num != p1->data && p1->next != NULL) { p1 = p1->next; } if (num == p1->data) { if (p1 == head) { head = head->next; head->pre = NULL; free(p1); } else

链表算法-链表前面插入节点

链表算法-链表前面插入节点

链表(15)----给定链表中间某个节点,将待插入节点插入给定节点之前

1. 链表定义 typedef struct ListElement_t_ { void *data; struct ListElement_t_ *next; } ListElement_t; typedef struct List_t_{ int size; int capacity; ListElement_t *head; ListElement_t *tail; } List_t; 2.给定链表中间某个节点,将待插入节点插入给定节点之前 先将待插入节点插入给定节点之后,然后交换这两个节

链表算法-后面插入节点

链表算法-后面插入节点

(六)双链表的结构和插入节点

(六)双链表的结构和插入节点 双链表结构双链表算法之插入节点(尾部插入)双链表算法之插入节点(头部插入) 双链表结构 ??双链表并不是有两个链表,而是有两个遍历方向的链表,因此我们说的双链表其实是双向链表的简称. 单链表节点 = 有效数据 + 指针(指向下一个节点) 双链表节点 = 有效数据 + 两个指针(分别指向前一节点和后一节点) 1/* 2 *双链表节点 3 */ 4struct node 5{ 6    int data; 7    struct node *pPrev; 8    st