SlideShare a Scribd company logo
#include "stdafx.h"
struct Node
{
int data;
Node* next;
Node() = default;
Node(int _data, Node* _next) : data(_data), next(_next){};
};
// run sample code
void RunLL()
{
//Node *head = new Node;
//Node* lt = nullptr;
//Node* gt = nullptr;
//InitNode(head, 122);
//DisplayList(head);
//AddEndNode(head, 60);
//DisplayList(head);
//AddEndNode(head, 182);
//DisplayList(head);
//AddEndNode(head, 2);
//DisplayList(head);
//AddEndNode(head, 202);
//DisplayList(head);
//InsertFront(&head, 15);
//DisplayList(head);
//SortListAscending(head, head);
//DisplayList(head);
//AddSortedNode(head, 86);
//DisplayList(head);
//Node* midNode = FindMiddleNode(head);
//cout << midNode->data << endl << endl;
//bool circular = IsCircular(head);
//string disp = circular ? "true" : "false";
//cout << disp << endl << endl;
//Split(head, 86, &lt, &gt);
//DisplayList(lt);
//DisplayList(gt);
//int numDel = 15;
//Node *ptrDelete = FindANode(head, numDel);
//if (DeleteNode(head, ptrDelete->data))
// cout << numDel << " deleted!n";
//DisplayList(head);
//cout << "The list is reversedn";
//ReverseList(&head);
//DisplayList(head);
//numDel = 60;
//ptrDelete = FindANode(head, numDel);
//if (DeleteNode(head, ptrDelete->data))
//{
// cout << numDel << " deleted!n";
// DisplayList(head);
//}
//if (!circular)
// MakeCircular(head);
//circular = IsCircular(head);
//disp = circular ? "true" : "false";
//cout << disp << endl << endl;
}
// only for the 1st Node
void InitNode(Node* head, int n)
{
head->data = n;
head->next = NULL;
}
Node* TestList()
{
Node* head = new Node;
InitNode(head, 0);
head->next = new Node(3, nullptr);
head->next->next = new Node(7, nullptr);
return head;
}
void DelList(Node* head)
{
if (head->next)
DelList(head->next);
delete head;
}
// find a node with value 'd' (start with head to search entire list)
Node* FindANode(Node* currNode, int d)
{
// end of list, node not found
if (!currNode) return nullptr;
// node found
if (currNode->data == d)
return currNode;
// check next node
FindANode(currNode->next, d);
}
// add new node to end of list
void AddEndNode(Node* currNode, int d)
{
// move on to next node
if (currNode->next)
AddEndNode(currNode->next, d);
else // found end of list, add new node
{
currNode->next = new Node(d, nullptr);
return;
}
}
// add new node to front of list
void InsertFront(Node** head, int n)
{
Node *newNode = new Node;
newNode->data = n;
newNode->next = *head;
*head = newNode;
}
// delete node
bool DeleteNode(Node* currNode, int val)
{
if (!currNode || !currNode->next) return false;
// node found, 'delete' it
if (currNode->next->data == val)
{
Node* tempNode = currNode->next;
currNode->next = currNode->next->next;
delete tempNode;
return true;
}
// check next node
else if (DeleteNode(currNode->next, val))
return true;
// node not found
return false;
}
// add new node (sorted list)
void AddSortedNode(Node* currNode, int d)
{
// list is empty or 'd' becomes head
if (!currNode || currNode->data > d)
{
InsertFront(&currNode, d);
return;
}
// at end of list
if (!currNode->next)
{
AddEndNode(currNode, d);
return;
}
// insert newNode here
if ((currNode->data <= d && currNode->next->data > d)
|| (currNode->data > d && currNode->next->data < d))
{
Node* newNode = new Node;
newNode->data = d;
newNode->next = currNode->next;
currNode->next = newNode;
return;
}
// check next node
AddSortedNode(currNode->next, d);
}
// split a list into two lists (less than, greater than)
void Split(Node* head, int pivot, Node** lt, Node** gt)
{
if (!head) return;
if (head->data < pivot)
{
if (*lt)
AddEndNode(*lt, head->data);
else
{
*lt = new Node;
InitNode(*lt, head->data);
}
}
else if (head->data > pivot)
{
if (*gt)
AddEndNode(*gt, head->data);
else
{
*gt = new Node;
InitNode(*gt, head->data);
}
}
Split(head->next, pivot, lt, gt);
}
// reverse a list
void ReverseList(Node** currNode)
{
// list is empty || has only one node || at end of list
if (!(*currNode) || !(*currNode)->next) return;
// move newCurrNode to the end of the list
Node* newCurrNode = (*currNode)->next;
ReverseList(&newCurrNode);
// reverse the 'next' pointers (next->next is validated by earlier 'if check')
(*currNode)->next->next = (*currNode);
// set 'next' ptr to nullptr
(*currNode)->next = nullptr;
// fix the currNode / 'head' pointer and return it
*currNode = newCurrNode;
}
// sort the list
void SortListAscending(Node* firstNode, Node* currNode)
{
// recursive
#if 1
// swap data
if (currNode->data < firstNode->data)
{
int tmpData = firstNode->data;
firstNode->data = currNode->data;
currNode->data = tmpData;
}
// second / inner 'loop'
if (currNode->next)
SortListAscending(firstNode, currNode->next);
// first / outer 'loop'
else if (firstNode->next)
SortListAscending(firstNode->next, firstNode->next);
#endif
// while loop
#if 0
Node* iter = firstNode;
while (iter)
{
Node* iter2 = firstNode;
while (iter2->next)
{
if (iter->data < iter2->data)
{
int tmpData = iter->data;
iter->data = iter2->data;
iter2->data = tmpData;
}
iter2 = iter2->next;
}
iter = iter->next;
}
#endif
}
// is a list circular (count is used to avoid returning on first cycle)
bool IsCircular(Node* head)
{
// list is empty || hare reached end of list (not circular)
if (!head) return false;
int count = 1;
Node* tortoise = head;
Node* hare = head;
while (hare->next)
{
// move the 'tortoise' ptr once every other cycle
if (count++ % 2 == 0) tortoise = tortoise->next;
hare = hare->next;
// tortoise caught up to the hare (circular list)
if (tortoise == hare && count > 1) return true;
}
return false;
}
// make the list circular
void MakeCircular(Node* head)
{
if (!head) return;
Node* currNode = head;
while (currNode->next)
currNode = currNode->next;
currNode->next = head;
}
// find middle node in a list
Node* FindMiddleNode(Node* head)
{
Node* currNode = head;
Node* midNode = head;
int count = 1;
while (currNode->next)
{
if (count++ % 2 == 0)
midNode = midNode->next;
currNode = currNode->next;
}
return midNode;
}
// disply list from currNode (pass 'head' to display entire list)
void DisplayList(Node *currNode)
{
// display currNode
//cout << currNode->data << " ";
//// if available, move to next node
//if (currNode->next)
// DisplayList(currNode->next);
//// if not, insert two lines for end of list
//else
// cout << "nn";
}
BOOST_AUTO_TEST_SUITE(LinkedLists)
BOOST_AUTO_TEST_CASE(FindNode)
{
Node* head = TestList();
Node* tgt = FindANode(head, 3);
// should fail
//BOOST_CHECK_EQUAL(tgt->data, 11);
// should pass
BOOST_CHECK_EQUAL(tgt->data, 3);
DelList(head);
}
BOOST_AUTO_TEST_CASE(AddToEnd)
{
Node* head = TestList();
AddEndNode(head, 11);
Node* tgt = FindANode(head, 11);
// should fail
//BOOST_CHECK(tgt->next);
// should pass
BOOST_CHECK(!tgt->next);
DelList(head);
}
BOOST_AUTO_TEST_CASE(AddFront)
{
Node* head = TestList();
InsertFront(&head, 11);
Node* tgt = FindANode(head, 11);
// should fail
//BOOST_CHECK_EQUAL(tgt->data, 20);
// should pass
BOOST_CHECK_EQUAL(tgt, head);
DelList(head);
}
BOOST_AUTO_TEST_CASE(DelNode)
{
Node* head = TestList();
DeleteNode(head, 11);
// should fail
//BOOST_CHECK(FindANode(head, 3) == nullptr);
// should pass
BOOST_CHECK(FindANode(head, 11) == nullptr);
DelList(head);
}
// sorted item added to head of list
BOOST_AUTO_TEST_CASE(AddSortedHead)
{
Node* head = TestList();
AddSortedNode(head, 0);
// should fail
//BOOST_CHECK_EQUAL(head->data, 3);
// should pass
BOOST_CHECK(head->data == 0);
DelList(head);
}
// sorted item added to mid of list
BOOST_AUTO_TEST_CASE(AddSortedMid)
{
Node* head = TestList();
AddSortedNode(head, 6);
Node* prev = head;
Node* newNode = FindANode(head, 6);
// find newNode->previous
while (prev->next->data < newNode->data)
prev = prev->next;
// check if newNode is mid list
BOOST_CHECK(prev->data <= newNode->data);
BOOST_CHECK(newNode->next);
DelList(head);
}
// sorted item added to end of list
BOOST_AUTO_TEST_CASE(AddSortedEnd)
{
Node* head = TestList();
AddSortedNode(head, 60);
Node*prev = head;
Node* newNode = FindANode(head, 60);
// find newNode-previous
while (prev->next->data < newNode->data)
prev = prev->next;
// check newNode is end of list (correctly)
BOOST_CHECK(prev->data <= newNode->data);
BOOST_CHECK(!newNode->next);
DelList(head);
}
BOOST_AUTO_TEST_CASE(SplitList)
{
Node* head = TestList();
Node* ltList = nullptr;
Node* gtList = nullptr;
// create 2 new lists from first list
Split(head, 3, &ltList, &gtList);
Node* ltEnd = ltList;
while (ltEnd->next)
ltEnd = ltEnd->next;
BOOST_CHECK(ltEnd->data < gtList->data);
DelList(head);
delete ltList;
delete gtList;
}
BOOST_AUTO_TEST_CASE(RevList)
{
Node* head = TestList();
ReverseList(&head);
// should fail
//BOOST_CHECK_EQUAL(head->data, 0);
// should pass
BOOST_CHECK_EQUAL(head->data, 7);
BOOST_CHECK_EQUAL(head->next->data, 3);
BOOST_CHECK_EQUAL(head->next->next->data, 0);
DelList(head);
}
BOOST_AUTO_TEST_CASE(SortAsc)
{
Node* head = TestList();
int tmp = head->data;
// change list to 3, 7, 0
head->data = head->next->data;
head->next->data = tmp;
tmp = head->next->data;
head->next->data = head->next->next->data;
head->next->next->data = tmp;
// change back to 0, 3, 7
SortListAscending(head, head);
// should fail
//BOOST_CHECK_EQUAL(head->data, 3);
// should pass
BOOST_CHECK_EQUAL(head->data, 0);
BOOST_CHECK_EQUAL(head->next->data, 3);
BOOST_CHECK_EQUAL(head->next->next->data, 7);
DelList(head);
}
BOOST_AUTO_TEST_CASE(CheckCircular)
{
}
BOOST_AUTO_TEST_CASE(BeCircular)
{
}
BOOST_AUTO_TEST_CASE(FindMiddle)
{
}
BOOST_AUTO_TEST_SUITE_END()

More Related Content

PDF
Easily mockingdependenciesinc++ 2
PDF
Operation Flow @ ChicagoRoboto
TXT
Nouveau document texte
 
TXT
Yy
 
PDF
Notes for SQLite3 Usage
DOCX
Data structures
DOCX
C++ adt c++ implementations
PDF
PostgreSQL Procedural Languages: Tips, Tricks and Gotchas
Easily mockingdependenciesinc++ 2
Operation Flow @ ChicagoRoboto
Nouveau document texte
 
Yy
 
Notes for SQLite3 Usage
Data structures
C++ adt c++ implementations
PostgreSQL Procedural Languages: Tips, Tricks and Gotchas

What's hot (18)

PDF
Javascript
TXT
C99
PPT
Mod04 debuggers
PDF
SWP - A Generic Language Parser
TXT
C99.php
PDF
The Ring programming language version 1.9 book - Part 91 of 210
TXT
My All Codes of SAS
TXT
ODP
vfsStream - effective filesystem mocking
PPT
vfsStream - a better approach for file system dependent tests
DOCX
SAS codes and tricks Comprehensive all codes
TXT
SAS codes and tricks Comprehensive all codess
TXT
zinno
PDF
ćŻ«çš‹ćŒïŒŸé‚Łäș›è€ćž«æČ’教的äș‹
PDF
Melhorando sua API com DSLs
PDF
ćŻ«çš‹ćŒïŒŸé‚Łäș›è€ćž«æČ’教的äș‹ (Git 郹戆節錄)
PPTX
PDF
First Steps. (db4o - Object Oriented Database)
Javascript
C99
Mod04 debuggers
SWP - A Generic Language Parser
C99.php
The Ring programming language version 1.9 book - Part 91 of 210
My All Codes of SAS
vfsStream - effective filesystem mocking
vfsStream - a better approach for file system dependent tests
SAS codes and tricks Comprehensive all codes
SAS codes and tricks Comprehensive all codess
zinno
ćŻ«çš‹ćŒïŒŸé‚Łäș›è€ćž«æČ’教的äș‹
Melhorando sua API com DSLs
ćŻ«çš‹ćŒïŒŸé‚Łäș›è€ćž«æČ’教的äș‹ (Git 郹戆節錄)
First Steps. (db4o - Object Oriented Database)
Ad

Viewers also liked (14)

PPTX
Kmc presentation
PPTX
How to Simplify Enterprise Collaboration Using Enterprise Portals
PPTX
SWISSWAY 1500
PPTX
SWISS BULLION TABLES
PPSX
The racer
PDF
Taller de robĂłtica2 copy
PDF
US APPAREL BOARDSHORT CATALOG 2017
PPTX
SWISS BULLION
RTF
P.C.MISHRA Resume
PDF
Atul Joshi - Oct 2016
PDF
All Strategies
DOC
Saleem Lawati CV
PPTX
How Cloud Computing is changing the Automotive Industry - KNOWARTH
PDF
Gyasi et al, 2015b
Kmc presentation
How to Simplify Enterprise Collaboration Using Enterprise Portals
SWISSWAY 1500
SWISS BULLION TABLES
The racer
Taller de robĂłtica2 copy
US APPAREL BOARDSHORT CATALOG 2017
SWISS BULLION
P.C.MISHRA Resume
Atul Joshi - Oct 2016
All Strategies
Saleem Lawati CV
How Cloud Computing is changing the Automotive Industry - KNOWARTH
Gyasi et al, 2015b
Ad

Similar to Linked lists (20)

PDF
In C++Write a recursive function to determine whether or not a Lin.pdf
PDF
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
PDF
This assignment and the next (#5) involve design and development of a.pdf
PDF
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
PDF
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
 
PDF
Dividing a linked list into two sublists of almost equal sizesa. A.pdf
DOCX
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
 
PDF
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
PDF
In C++ I need help with this method that Im trying to write fillLi.pdf
PDF
Implement the following specification of UnsortedType using circular.pdf
PDF
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
PPT
linked-list.ppt
PDF
Complete a C++ class implementation for a linked-list of sorted (asc.pdf
PDF
In the class we extensively discussed a node class called IntNode in.pdf
DOCX
C++ Please write the whole code that is needed for this assignment- wr.docx
PDF
Template LinkedList;I am using templates to make some linkedLists.pdf
PPTX
Lecture 4 data structures and algorithms
PDF
Using the header(dlist) and mainline file (dlistapp) belowYou are .pdf
PDF
you will implement some sorting algorithms for arrays and linked lis.pdf
PDF
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
In C++Write a recursive function to determine whether or not a Lin.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
This assignment and the next (#5) involve design and development of a.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
 
Dividing a linked list into two sublists of almost equal sizesa. A.pdf
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
 
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
In C++ I need help with this method that Im trying to write fillLi.pdf
Implement the following specification of UnsortedType using circular.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
linked-list.ppt
Complete a C++ class implementation for a linked-list of sorted (asc.pdf
In the class we extensively discussed a node class called IntNode in.pdf
C++ Please write the whole code that is needed for this assignment- wr.docx
Template LinkedList;I am using templates to make some linkedLists.pdf
Lecture 4 data structures and algorithms
Using the header(dlist) and mainline file (dlistapp) belowYou are .pdf
you will implement some sorting algorithms for arrays and linked lis.pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf

More from George Scott IV (7)

DOCX
Square selection function
DOCX
Save game function
DOCX
Delete save from folder function
DOCX
DOCX
Strings
DOCX
DOCX
Arrays
Square selection function
Save game function
Delete save from folder function
Strings
Arrays

Recently uploaded (20)

PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Digital Strategies for Manufacturing Companies
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
history of c programming in notes for students .pptx
PDF
System and Network Administraation Chapter 3
PPTX
Essential Infomation Tech presentation.pptx
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Nekopoi APK 2025 free lastest update
PDF
System and Network Administration Chapter 2
PPTX
Transform Your Business with a Software ERP System
PPTX
Introduction to Artificial Intelligence
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
medical staffing services at VALiNTRY
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
Wondershare Filmora 15 Crack With Activation Key [2025
Digital Strategies for Manufacturing Companies
wealthsignaloriginal-com-DS-text-... (1).pdf
history of c programming in notes for students .pptx
System and Network Administraation Chapter 3
Essential Infomation Tech presentation.pptx
How to Choose the Right IT Partner for Your Business in Malaysia
Operating system designcfffgfgggggggvggggggggg
Nekopoi APK 2025 free lastest update
System and Network Administration Chapter 2
Transform Your Business with a Software ERP System
Introduction to Artificial Intelligence
Reimagine Home Health with the Power of Agentic AI​
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
How to Migrate SBCGlobal Email to Yahoo Easily
Design an Analysis of Algorithms II-SECS-1021-03
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
medical staffing services at VALiNTRY
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Navsoft: AI-Powered Business Solutions & Custom Software Development

Linked lists

  • 1. #include "stdafx.h" struct Node { int data; Node* next; Node() = default; Node(int _data, Node* _next) : data(_data), next(_next){}; }; // run sample code void RunLL() { //Node *head = new Node; //Node* lt = nullptr; //Node* gt = nullptr; //InitNode(head, 122); //DisplayList(head); //AddEndNode(head, 60); //DisplayList(head); //AddEndNode(head, 182); //DisplayList(head); //AddEndNode(head, 2); //DisplayList(head); //AddEndNode(head, 202); //DisplayList(head); //InsertFront(&head, 15); //DisplayList(head); //SortListAscending(head, head); //DisplayList(head); //AddSortedNode(head, 86); //DisplayList(head); //Node* midNode = FindMiddleNode(head); //cout << midNode->data << endl << endl; //bool circular = IsCircular(head); //string disp = circular ? "true" : "false"; //cout << disp << endl << endl; //Split(head, 86, &lt, &gt); //DisplayList(lt); //DisplayList(gt); //int numDel = 15; //Node *ptrDelete = FindANode(head, numDel); //if (DeleteNode(head, ptrDelete->data)) // cout << numDel << " deleted!n"; //DisplayList(head);
  • 2. //cout << "The list is reversedn"; //ReverseList(&head); //DisplayList(head); //numDel = 60; //ptrDelete = FindANode(head, numDel); //if (DeleteNode(head, ptrDelete->data)) //{ // cout << numDel << " deleted!n"; // DisplayList(head); //} //if (!circular) // MakeCircular(head); //circular = IsCircular(head); //disp = circular ? "true" : "false"; //cout << disp << endl << endl; } // only for the 1st Node void InitNode(Node* head, int n) { head->data = n; head->next = NULL; } Node* TestList() { Node* head = new Node; InitNode(head, 0); head->next = new Node(3, nullptr); head->next->next = new Node(7, nullptr); return head; } void DelList(Node* head) { if (head->next) DelList(head->next); delete head; } // find a node with value 'd' (start with head to search entire list) Node* FindANode(Node* currNode, int d) { // end of list, node not found if (!currNode) return nullptr; // node found if (currNode->data == d) return currNode; // check next node FindANode(currNode->next, d); }
  • 3. // add new node to end of list void AddEndNode(Node* currNode, int d) { // move on to next node if (currNode->next) AddEndNode(currNode->next, d); else // found end of list, add new node { currNode->next = new Node(d, nullptr); return; } } // add new node to front of list void InsertFront(Node** head, int n) { Node *newNode = new Node; newNode->data = n; newNode->next = *head; *head = newNode; } // delete node bool DeleteNode(Node* currNode, int val) { if (!currNode || !currNode->next) return false; // node found, 'delete' it if (currNode->next->data == val) { Node* tempNode = currNode->next; currNode->next = currNode->next->next; delete tempNode; return true; } // check next node else if (DeleteNode(currNode->next, val)) return true; // node not found return false; } // add new node (sorted list) void AddSortedNode(Node* currNode, int d) { // list is empty or 'd' becomes head if (!currNode || currNode->data > d) { InsertFront(&currNode, d); return; } // at end of list if (!currNode->next) { AddEndNode(currNode, d);
  • 4. return; } // insert newNode here if ((currNode->data <= d && currNode->next->data > d) || (currNode->data > d && currNode->next->data < d)) { Node* newNode = new Node; newNode->data = d; newNode->next = currNode->next; currNode->next = newNode; return; } // check next node AddSortedNode(currNode->next, d); } // split a list into two lists (less than, greater than) void Split(Node* head, int pivot, Node** lt, Node** gt) { if (!head) return; if (head->data < pivot) { if (*lt) AddEndNode(*lt, head->data); else { *lt = new Node; InitNode(*lt, head->data); } } else if (head->data > pivot) { if (*gt) AddEndNode(*gt, head->data); else { *gt = new Node; InitNode(*gt, head->data); } } Split(head->next, pivot, lt, gt); } // reverse a list void ReverseList(Node** currNode) { // list is empty || has only one node || at end of list if (!(*currNode) || !(*currNode)->next) return; // move newCurrNode to the end of the list Node* newCurrNode = (*currNode)->next; ReverseList(&newCurrNode); // reverse the 'next' pointers (next->next is validated by earlier 'if check') (*currNode)->next->next = (*currNode);
  • 5. // set 'next' ptr to nullptr (*currNode)->next = nullptr; // fix the currNode / 'head' pointer and return it *currNode = newCurrNode; } // sort the list void SortListAscending(Node* firstNode, Node* currNode) { // recursive #if 1 // swap data if (currNode->data < firstNode->data) { int tmpData = firstNode->data; firstNode->data = currNode->data; currNode->data = tmpData; } // second / inner 'loop' if (currNode->next) SortListAscending(firstNode, currNode->next); // first / outer 'loop' else if (firstNode->next) SortListAscending(firstNode->next, firstNode->next); #endif // while loop #if 0 Node* iter = firstNode; while (iter) { Node* iter2 = firstNode; while (iter2->next) { if (iter->data < iter2->data) { int tmpData = iter->data; iter->data = iter2->data; iter2->data = tmpData; } iter2 = iter2->next; } iter = iter->next; } #endif } // is a list circular (count is used to avoid returning on first cycle) bool IsCircular(Node* head) { // list is empty || hare reached end of list (not circular) if (!head) return false; int count = 1; Node* tortoise = head;
  • 6. Node* hare = head; while (hare->next) { // move the 'tortoise' ptr once every other cycle if (count++ % 2 == 0) tortoise = tortoise->next; hare = hare->next; // tortoise caught up to the hare (circular list) if (tortoise == hare && count > 1) return true; } return false; } // make the list circular void MakeCircular(Node* head) { if (!head) return; Node* currNode = head; while (currNode->next) currNode = currNode->next; currNode->next = head; } // find middle node in a list Node* FindMiddleNode(Node* head) { Node* currNode = head; Node* midNode = head; int count = 1; while (currNode->next) { if (count++ % 2 == 0) midNode = midNode->next; currNode = currNode->next; } return midNode; } // disply list from currNode (pass 'head' to display entire list) void DisplayList(Node *currNode) { // display currNode //cout << currNode->data << " "; //// if available, move to next node //if (currNode->next) // DisplayList(currNode->next); //// if not, insert two lines for end of list //else // cout << "nn"; }
  • 7. BOOST_AUTO_TEST_SUITE(LinkedLists) BOOST_AUTO_TEST_CASE(FindNode) { Node* head = TestList(); Node* tgt = FindANode(head, 3); // should fail //BOOST_CHECK_EQUAL(tgt->data, 11); // should pass BOOST_CHECK_EQUAL(tgt->data, 3); DelList(head); } BOOST_AUTO_TEST_CASE(AddToEnd) { Node* head = TestList(); AddEndNode(head, 11); Node* tgt = FindANode(head, 11); // should fail //BOOST_CHECK(tgt->next); // should pass BOOST_CHECK(!tgt->next); DelList(head); } BOOST_AUTO_TEST_CASE(AddFront) { Node* head = TestList(); InsertFront(&head, 11); Node* tgt = FindANode(head, 11); // should fail //BOOST_CHECK_EQUAL(tgt->data, 20); // should pass BOOST_CHECK_EQUAL(tgt, head); DelList(head); } BOOST_AUTO_TEST_CASE(DelNode) { Node* head = TestList(); DeleteNode(head, 11); // should fail //BOOST_CHECK(FindANode(head, 3) == nullptr); // should pass BOOST_CHECK(FindANode(head, 11) == nullptr); DelList(head); } // sorted item added to head of list BOOST_AUTO_TEST_CASE(AddSortedHead) {
  • 8. Node* head = TestList(); AddSortedNode(head, 0); // should fail //BOOST_CHECK_EQUAL(head->data, 3); // should pass BOOST_CHECK(head->data == 0); DelList(head); } // sorted item added to mid of list BOOST_AUTO_TEST_CASE(AddSortedMid) { Node* head = TestList(); AddSortedNode(head, 6); Node* prev = head; Node* newNode = FindANode(head, 6); // find newNode->previous while (prev->next->data < newNode->data) prev = prev->next; // check if newNode is mid list BOOST_CHECK(prev->data <= newNode->data); BOOST_CHECK(newNode->next); DelList(head); } // sorted item added to end of list BOOST_AUTO_TEST_CASE(AddSortedEnd) { Node* head = TestList(); AddSortedNode(head, 60); Node*prev = head; Node* newNode = FindANode(head, 60); // find newNode-previous while (prev->next->data < newNode->data) prev = prev->next; // check newNode is end of list (correctly) BOOST_CHECK(prev->data <= newNode->data); BOOST_CHECK(!newNode->next); DelList(head); } BOOST_AUTO_TEST_CASE(SplitList) { Node* head = TestList(); Node* ltList = nullptr; Node* gtList = nullptr; // create 2 new lists from first list
  • 9. Split(head, 3, &ltList, &gtList); Node* ltEnd = ltList; while (ltEnd->next) ltEnd = ltEnd->next; BOOST_CHECK(ltEnd->data < gtList->data); DelList(head); delete ltList; delete gtList; } BOOST_AUTO_TEST_CASE(RevList) { Node* head = TestList(); ReverseList(&head); // should fail //BOOST_CHECK_EQUAL(head->data, 0); // should pass BOOST_CHECK_EQUAL(head->data, 7); BOOST_CHECK_EQUAL(head->next->data, 3); BOOST_CHECK_EQUAL(head->next->next->data, 0); DelList(head); } BOOST_AUTO_TEST_CASE(SortAsc) { Node* head = TestList(); int tmp = head->data; // change list to 3, 7, 0 head->data = head->next->data; head->next->data = tmp; tmp = head->next->data; head->next->data = head->next->next->data; head->next->next->data = tmp; // change back to 0, 3, 7 SortListAscending(head, head); // should fail //BOOST_CHECK_EQUAL(head->data, 3); // should pass BOOST_CHECK_EQUAL(head->data, 0); BOOST_CHECK_EQUAL(head->next->data, 3); BOOST_CHECK_EQUAL(head->next->next->data, 7); DelList(head); } BOOST_AUTO_TEST_CASE(CheckCircular) {