“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
// Definition of Node structure struct Node { int Data; Node * Next; };
// Recursive function to insert in a sorted // list. // Note that the function returns the new // Head. // So the call looks like: // Head=InsertSorted(Head, 55); // Look at main function below Node * InsertSorted(Node * Start, int Num) { if (Start==0
// Definition of Node structure struct Node { int Data; Node * Next; };
// Recursive function to insert in a sorted // list. // Note that the function returns the new // Head. // So the call looks like: // Head=InsertSorted(Head, 55); // Look at main function below Node * InsertSorted(Node * Start, int Num) { if (Start==0
Assuming linked list to be sorted in ascending order: This is the correct implementation “iamsuechoi” struct LinkedList { int nValue; LinkedList * pNext; };
void sortedInsert(struct node** headRef, struct node* newNode){
struct node** currentRef = headRef;
while (*currentRef!=NULL && (*currentRef)->data < newNode->data){
currentRef = &((*currentRef)->next);
}
newNode-> = (*currentRef)->next;
*currentRef = newNode;
}
struct LinkedList
{
int nValue;
LinkedList * pNext;
};
void InsertNewOneInSortedLinkedList(LinkedList *Head, LinkedList *NewOne)
{
LinkedList *One = NULL;
LinkedList *PrevOne = NULL;
if(Head->pNext == NULL)
{
Head->pNext = NewOne;
NewOne->pNext = NULL;
}
One = PrevOne = Head;
while(One->pNext)
{
if(One->nValue < NewOne->nValue)
{
PrevOne = One;
One = PrevOne->pNext;
}
else
break;
}
PrevOne->pNext = NewOne;
NewOne->pNext = One;
}
struct LinkedList
{
int nValue;
LinkedList * pNext;
};
void InsertNewOneInSortedLinkedList(LinkedList *Head, LinkedList *NewOne)
{
LinkedList *One = NULL;
LinkedList *PrevOne = NULL;
One = PrevOne = Head;
while(One->pNext)
{
if(One->nValue < NewOne->nValue)
{
PrevOne = One;
One = PrevOne->pNext;
}
else
break;
}
PrevOne->pNext = NewOne;
if(One->pNext == NULL) NewOne->pNext = NULL;
else NewOne->pNext = One;
}
// Definition of Node structure
struct Node
{
int Data;
Node * Next;
};
// Recursive function to insert in a sorted
// list.
// Note that the function returns the new
// Head.
// So the call looks like:
// Head=InsertSorted(Head, 55);
// Look at main function below
Node * InsertSorted(Node * Start, int Num)
{
if (Start==0
// Definition of Node structure
struct Node
{
int Data;
Node * Next;
};
// Recursive function to insert in a sorted
// list.
// Note that the function returns the new
// Head.
// So the call looks like:
// Head=InsertSorted(Head, 55);
// Look at main function below
Node * InsertSorted(Node * Start, int Num)
{
if (Start==0
Assuming linked list to be sorted in ascending order:
This is the correct implementation “iamsuechoi”
struct LinkedList
{
int nValue;
LinkedList * pNext;
};
void InsertNewOneInSortedLinkedList(LinkedList *Head, LinkedList *NewOne)
{
LinkedList *One = NULL;
LinkedList *PrevOne = NULL;
One = PrevOne = Head;
while(One != NULL)
{
if(One->nValue > NewOne->nValue)
break
else
{
PrevOne = One;
One = One->pNext;
}
}
PrevOne->pNext = NewOne;
NewOne->pNext = One;
}
Leave an Answer/Comment