往有序单循环链表的插入元素使原链表依旧有序
生活随笔
收集整理的這篇文章主要介紹了
往有序单循环链表的插入元素使原链表依旧有序
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
解題思路:與有序單鏈表類似,只不過加了尾指針指向鏈表頭部
#include<iostream>
using namespace std;typedef struct TNode
{int data;struct TNode* next;
}TNode;TNode* insertNum(TNode* head, int num)
{TNode* node = new TNode;node->data = num;node->next = NULL;if (!head){node->next = node;return node;}if (head->data > num){node->next = head;head->next = node;return node;}TNode* pre = head;TNode* cur = head->next;while (cur != head&&cur->data < num){pre = cur;cur = cur->next;}//插入node->next = cur;pre->next = node;return head;
}int main()
{int num;TNode* head = NULL;int A[] = { 0, 11, 3, 4, 3, 2, 10, 44 };int len = sizeof(A) / sizeof(A[0]);for (int i = 0; i < len; i++){head = insertNum(head, A[i]);}//遍歷循環(huán)單鏈表(無頭結(jié)點(diǎn)),終止條件:利用數(shù)組大小int i = 0;for (TNode* cur = head; i < len; cur = cur->next,i++)cout << cur->data<<endl;return 0;
}
循環(huán)單鏈表的遍歷:
總結(jié)
以上是生活随笔為你收集整理的往有序单循环链表的插入元素使原链表依旧有序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 往有序链表的插入元素使原链表依旧有序
- 下一篇: 链表划分为左小、中相等、右大