Bubble Sort Doubly Link List - c++

I want to sort doubly link list using bubble sort. It is having loop problem and also the swap is not taking place properly.
The problem is the greatest number should be at the end after one iteration of i. But it doesn't and stop in between. I tried various options but could not solve it. Please help me solve this problem
#include <iostream>
using namespace std;
class node{
public:
node *next;
node *prev;
int data;
node(int x, node *p, node *n)
{
next = n;
prev = p;
data = x;
}
};
class list{
private:
node *head;
node *tail;
public:
list()
{
head = NULL;
tail = NULL;
}
void addtoend(int x)
{
if(head == NULL)
{
head = new node (x,NULL,NULL);
tail = head;
}
else if(tail->prev == NULL)
{
tail = new node(x,head,NULL);
head->next = tail;
}
else
{
node *temp = tail;
tail = new node(x,tail,NULL);
temp->next = tail;
}
}
void bubblesort()
{
node *temp = head;
node *temp2 = temp;
for(int i=0;i<10;i++)
{
cout<<"I: "<<i<<endl;
temp = head;
while(1)
{
temp2 = temp;
if(temp->next->data < temp->data)
{
if(temp == head)
{
cout<<"HEAD"<<endl;
head = head->next;
node *after = head->next;
head->prev = NULL;
temp->prev = head;
head->next = temp;
temp->next = after;
after->prev = temp;
}
else if(temp->next == tail)
{
cout<<"TAIL"<<endl;
node *back = temp->prev;
temp->next = NULL;
tail->next = temp;
temp->prev = tail;
back->next = tail;
tail->prev = back;
tail = tail->next;
tail->next=NULL;
}
else
{
cout<<"ANY"<<endl;
node *back = temp->prev;
node *after = temp->next->next;
back->next=temp->next;
temp->prev = temp->next;
after->prev->next = temp;
after->prev->prev = back;
after->prev = temp;
temp->next = after;
}
}
display();
display0();
temp = temp2->next;
cout<<"HEAD: "<<head->data<<endl;
cout<<"Tail: "<<tail->data<<endl;
cout<<endl;
if(temp->next->next== NULL)
{
break;
}
}
}
}
void display()
{
node *temp = head;
while(temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
void display0()
{
node *temp = tail;
while(temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->prev;
}
cout<<endl;
}
};
main()
{
list l1;
int x[10] = {17,15,8,12,10,5,4,1,7,2};
for(int i=0;i<10;i++)
{
l1.addtoend(x[i]);
}
l1.display();
l1.bubblesort();
l1.display();
}

How-to implement bubblesort algorithm for double-linked list
There are at least two ways to implement the bubblesort algorithm in c++ for a double-linked list. Swapping the content aka the data of two neighbored nodes as shown under I. and re-wiring the pointers as shown under II.:
The advantage of II. is that no temporary memory temp2 is required. Therefore this is the preferred solution as stated also by the OP.
I. Swapping data
Just replace the OP bubblesort function by this one:
void bubblesort()
{
bool is_sorted = false;
node *temp = head;
while (!is_sorted)
{
is_sorted = true;
temp = head;
while (temp->next != NULL)
{
if(temp->next->data < temp->data)
{
is_sorted = false;
int temp2 = temp->data;
temp->data = temp->next->data;
temp->next->data = temp2;
}
temp = temp->next;
}
}
display();
display0();
}
The algorithm uses a Boolean variable is_sorted to check if the sorting of the double-linked list is finished. Otherwise the bubbles are still rising. It swaps only the data values of the double linked list.
II. Re-wiring the pointers
This is the required solution for the OP and the preferred one:
void bubblesort()
{
bool is_sorted = false;
node *temp = head;
while (!is_sorted)
{
is_sorted = true;
temp = head;
while (temp->next != NULL)
{
if(temp->next->data < temp->data)
{
is_sorted = false;
if(temp == head)
{
//temp->prev->next = temp->next; // 1. (impossible)
temp->next->prev = NULL; // 2.
head = temp->next;
}
else
{
temp->prev->next = temp->next; // 1.
temp->next->prev = temp->prev; // 2.
}
temp->prev = temp->next; // 3.
if(temp->next == tail)
{
temp->next = NULL; // 4.
tail = temp;
//temp->next->prev = temp2; // 5. (impossible)
}
else
{
temp->next = temp->next->next; // 4.
temp->next->prev = temp; // 5.
}
temp->prev->next = temp; // 6.
temp = temp->prev;
}
temp = temp->next;
}
}
display();
display0();
}
Output
The output of the program with updated bubblesort functions is:
17 15 8 12 10 5 4 1 7 2
1 2 4 5 7 8 10 12 15 17
17 15 12 10 8 7 5 4 2 1
1 2 4 5 7 8 10 12 15 17
First line shows the input list without sorting. The second shows the sorting result in ascending order, while the third shows the descending order, which proves that the forward and reverse links of the list are still working. The last line is the sorted list output in the main function.
The original order of the list is lost, of course.
The full source code is also available on github.
Hope it helps.

This is the code that I submitted and it is working properly. I find my mistakes by creating a visual picture on paper. Also my instructor has instructed to swap nodes, in any question data swapping is not acceptable.
void bubblesort()
{
node *temp = head;
int len = length();
for(int i=0;i<(len - 1);i++)
{
cout<<i<<endl;
temp = head;
int c=0;
while(c<(len - i - 1))
{
cout<<"Temp: "<<temp->data<<" ";
cout<<"Temp: "<<temp->next->data<<" "<<endl;
if(temp->next->data < temp->data)
{
if(temp == head)
{
cout<<"HEAD"<<endl;
head = head->next;
node *after = head->next;
head->prev = NULL;
temp->prev = head;
head->next = temp;
temp->next = after;
after->prev = temp;
temp = head->next;
}
else if(temp->next == tail)
{
cout<<"TAIL"<<endl;
node *back = temp->prev;
temp->next = NULL;
tail->next = temp;
temp->prev = tail;
back->next = tail;
tail->prev = back;
tail = tail->next;
tail->next=NULL;
temp= tail;
}
else
{
cout<<"ANY"<<endl;
node *back = temp->prev;
node *after = temp->next->next;
back->next=temp->next;
temp->prev = temp->next;
after->prev->next = temp;
after->prev->prev = back;
after->prev = temp;
temp->next = after;
temp = after->prev;
}
}
else
temp = temp->next;
display();
display0();
cout<<"HEAD: "<<head->data<<endl;
cout<<"Tail: "<<tail->data<<endl;
cout<<endl;
c++;
}
}
}

Related

How to use subscript operator overloading in linklist using c++

I want to use subscript operator overloading in linklist but everytime it give me Segmentation fault (core dumped) ERROR! MY TASK IS : (Overload [] operator. Use for loop in main to display it.) I ALSO PROVIDING THE TASK LINK BELOW
//task link
[LINK OF TASK] https://anonymfile.com/r1XKK/dsa-a3.pdf
//MY CODE IS :
#include <iostream>
using namespace std;
class LinkedList
{
private:
class Node
{
public:
int data;
Node * next;
Node(int data)
{
this->data = data;
this->next = NULL;
}
};
public:
Node *head;
LinkedList(){
head = NULL;
}
//Write a copy constructor. Also copy must be deep.
LinkedList(LinkedList& S)
{
head = S.head;
}
//Overload [] operator. Use for loop in main to display it.
void operator[](int i) {
head->data = i;
}
void InsertAtEnd(int data){
if (head == NULL)
{
head = new Node(data);
return;
}
Node * temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = new Node(data);
}
void Insert(int d1, int d2)//Add the node of data d2 after the node with data d1. If d2 is not available add it to the end.
{
if (head == NULL)
{
Node * n = new Node(d2);
n->next = head;
head = n;
return;
}
Node * temp = head;
while (temp != NULL)
{
if (temp->data == d1)
{
Node * temp1 = temp->next;
temp->next = new Node(d2);
temp->next->next = temp1;
}
temp = temp->next;
}
}
void Delete(int data){
Node * todelete;
if(head->data == data){
todelete = head;
head = head->next;
free(todelete);
return;
}
Node *temp = head;
while(temp->next != NULL){
if(temp->next->data == data){
todelete = temp->next;
temp->next = temp->next->next;
free(todelete);
break;
}
temp = temp->next;
}
} // Deletes a node with data.
int getSize(){
Node * temp = head;
int size = 0;
while(temp != NULL){
temp = temp->next;
size++;
}
return size;
} //returns the count of elements in the list
bool IsEmpty(){
if(head == NULL){
return true;
}
else{
return false;
}
} //Returns true if empty.
void Merge(Node * list){
//merge
Node * temp = head;
while(temp != NULL){
if(temp->next == NULL and list != NULL){
temp->next = list;
break;
}
temp = temp->next;
}
//DISPLAY
while(head!=NULL){
cout<<head->data<<"->";
head=head->next;
}
cout<<"NULL"<<endl;
} //Merges the to the calling class.
void Erase(){
Node * erase;
while(head!= NULL){
erase = head;
head = head->next;
head = NULL;
}
free(erase);
} //Deletes every node in an array.
void SelectiveErase(int num) //Find num and delete everything after num.
{
Node * temp = head;
Node * todelete;
while(temp != NULL){
if(temp->data == num){
Node * erase = temp->next;
while(temp->next != NULL){
erase = temp->next;
temp->next = temp->next->next;
temp->next = NULL;
}
free(erase);
break;
}
temp = temp->next;
}
}
int FindNCount(int find)//Find and return count of all occurrence.
{
int counter = 0;
bool flag = false;
Node * temp = head;
while(temp->data!= find){
temp = temp->next;
counter++;
}
return counter;
}
int RemoveDuplicate(int find)//Find and remove every duplicate element in the list. Make //elements unique.
{
Node * temp = head;
Node *temp1;
while(temp != NULL){
temp1 = temp;
while(temp1->next != NULL){
if(temp->data == temp1->next->data and temp->data == find and temp1->next->data == find){
Node *todelete = temp1->next;
temp1->next = temp1->next->next;
free(todelete);
}
else{
temp1 = temp1->next;
}
}
temp = temp->next;
}
return find;
}
void FindNReplace(int find, int data)//Find and replace all occurrence recursively.
{
Node * temp = head;
while(temp != NULL){
if(temp->data == find){
temp->data = data;
break;
}
temp = temp->next;
}
}
void Display(){
static Node * temp= head;
if(temp == NULL){ cout << "NULL" << endl; return;}
cout << temp->data<<"->";
temp = temp->next;
Display();
}
};
void Swap() // swap the contents of one list with another list of same type and size. Also write parameter
{
LinkedList L,L1;
cout<<"AFTER SWAPING THE VALUE OF FIRST LIST \n";
while(L.head != NULL && L1.head != NULL){
int temp = L.head->data;
L.head->data = L1.head->data;
L1.head->data = temp;
cout<<L.head->data<<"\n";
L.head = L.head->next;
L1.head = L1.head->next;
}
cout<<endl;
}
int main()
{
// You must call Display function after every function.
LinkedList L{};
L[23];
// LinkedList L1;
// L1.InsertAtEnd(5);
// L1.InsertAtEnd(6);
//L.Erase();
// cout<<L.FindNCount(1)<<endl;
//L.SelectiveErase(2);
//L.Display();
//L.Merge(L1.head);
//L.RemoveDuplicate(2);
//L.Display();
//Swap();
return 0;
}
Overloading the subscript operator should return something. The assignment looks a bit vague, but I hope this will fix it:
//Overload [] operator. Use for loop in main to display it.
Node* operator[](int i) {
Node* nodePtr = head;
int counter = 0;
while (nodePtr != NULL && counter != i) {
nodePtr = nodePtr->next;
counter++;
}
return nodePtr;
}

How to insert node in sorted doubly linked list at the end?

#include <iostream>
#include <stdlib.h>
using namespace std;
struct Node{
int data;
Node* next;
Node* prev;
};
Node* head;
void display(){
Node* temp = head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
void insert(int value){
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = value;
temp->prev = NULL;
temp->next = NULL;
if(head==NULL){
head = temp;
}
else{
Node* t = head;
while(t->next!=NULL){
t = t->next;
}
t->next = temp;
temp->next = NULL;
temp->prev = t;
}
}
void insertAtFirst(int value){
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = value;
temp->prev = NULL;
temp->next = head;
head->prev = temp;
head = temp;
}
void sortedInsert(int value){
Node* curr = head;
while(curr->next!=NULL)
curr = curr->next;
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = value;
if(head==NULL || temp->data < head->data){
temp->prev = NULL;
temp->next = head;
head->prev = temp;
head = temp;
}
else if(head->next!= NULL && temp->data >= head->data){
Node* pred = head;
while(temp->data > pred->data){
pred = pred->next;
}
pred->prev->next = temp;
temp->prev = pred->prev;
temp->next = pred;
pred->prev = temp;
}
else if(curr->next==NULL){
curr->next = temp;
temp->prev = curr;
temp->next = NULL;
}
}
int main()
{
insert(10);
insert(20);
insert(25);
insert(35);
insertAtFirst(1);
insertAtFirst(29);
sortedInsert(30);
sortedInsert(40);
//1 10 20 25 29 30 35 40
display();
return 0;
}
I think I have implemented the doubly linked list probably. However, the problem I am facing is that when I try to input a node that has a greater data than the existing data of the last node, it doesn't save. There is probably something wrong with the logic I have employed - looked up some previous answers but failed to fix the problem.
As you try to insert sorted elements in your linked list, you cannot avoid using pointer of pointers in order to keep trace of the current and previous element.
Consider the following function:
void sortedInsert(int value)
{
Node **curr = nullptr;
Node **prev = nullptr;
int cc = 0;
for (curr = &head;
*curr && ((cc = ((*curr)->data < value)));
prev=curr, curr = &(*curr)->next);
Node* newElem = (Node*)malloc(sizeof(Node));
newElem->data = value;
newElem->next = *curr;
newElem->prev = *prev;
*curr = newElem;
}
Try to draw a diagram of your elements with their address...it helps a lot to understand how pointers and super-pointers work.
Z.

Function moving list elements to the end of the list not working multiple times

I need to make a function that moves the nth element in a singly linked list to the end of the list. I created some code that does that but it only works once if I try to do it again it moves the selected element to the end but the one that was moved previously gets deleted/dissapears. My theory is that it doesnt actually change the tail reference. so im stuck right now!
void move(int n)
{
if (head == NULL || head->next == NULL)
{
return;
}
node *first = head;
node *temp =new node;
for (int i = 1; i < n-1; i++)
{
first=first->next;
}
temp = first->next;
first->next=first->next->next;
temp->next = NULL;
tail->next = temp;
tail=temp;
}
my input:
1 2 3 4 5
after moving the 3rd element for the first time:
1 2 4 5 3
after moving the 3rd element(4) for the 2nd time:
1 2 5 4
but it should be
1 2 5 3 4
I checked your code with my own implementation. Your function move() is working fine. However, you should not be using 'new' in your 8th line of code as highlighted by #molbdnilo and #PaulMakenzie. But it is not responsible for this problem. There is a problem with some other part of your code.
#include<iostream>
using namespace std;
class List
{
struct Node
{
int number;
Node* next;
};
Node* head;
Node* tail;
public:
List()
{
head = NULL;
tail = NULL;
}
void insert(int num)
{
Node* temp = new Node();
temp->number = num;
temp->next = NULL;
if (head == NULL)
{
head = temp;
tail = temp;
}
else
{
Node* point = head;
while (point->next != NULL)
point = point->next;
point->next = temp;
tail = point->next;
}
}
void display()
{
Node* point = head;
while (point != NULL)
{
cout << point->number << " ";
point = point->next;
}
}
void move(int n)
{
if (head == NULL || head->next == NULL)
{
return;
}
Node *first = head;
Node *temp;
for (int i = 1; i < n-1; i++)
{
first=first->next;
}
temp = first->next;
first->next=first->next->next;
temp->next = NULL;
tail->next = temp;
tail=temp;
}
};
int main()
{
List a;
a.insert(1);
a.insert(2);
a.insert(3);
a.insert(4);
a.insert(5);
a.move(3);
a.move(3);
a.display();
}

Insert element into a sorted Circular Double Linked List

i want to Insert an element into a sorted Circular Double Linked List ,, here's what i tried :
void insertSorted(Node *&head,int x){
Node *temp = new Node();
temp->data = x;
temp->next = temp;
temp->prev = temp;
if(head == NULL){
head = temp;
return;
}
Node *p = head;
Node *q = NULL;
do{
q = p;
p=p->next;
}while(p != head && x>p->data);
if(q == NULL){
temp->next = head;
head->prev = temp;
head = temp;
}
else {
q->next = temp;
if(p!=NULL){
temp->next = p;
p->prev = temp;
}
temp->prev = q;
}
}
The Code works , but the problem is with the first element everytime it's not sorted ,,, example inserting 10 9 8 1 2 ,,, the output will be 10 1 2 8 9 ,, it should be 1 2 8 9 10
if(q == NULL) never gonna occur as per your code. So, the problem occurs here .
As you are getting problem for the first node then , the condition should be (q == head) and update the head. that your code is doing correct.
void sortedInsert(int data){
Nodo* ins = (Nodo*)malloc(sizeof(Nodo));
ins->data = data;
Nodo* itr = head;
//case: empty
if (head == NULL) {
ins->next = ins;
ins->prev = ins;
head = ins;
return;
}
//case: at begining
else if (head->data > data){
head->prev->next = ins;
ins->prev = head->prev;
ins->next = head;
head->prev = ins;
head = ins;
return;
}
else {
while (itr->data < data) {
// case: at end of list
if (itr->next == head) {
ins->next = head;
ins->prev = itr;
itr->next = ins;
head->prev = ins;
return;
}
else {
itr = itr->next;
}
}
// case: middle
itr->prev->next = ins;
ins->prev = itr->prev;
itr->prev = ins;
ins->next = itr;
return;
}
}

Insert Node in a Sorted Doubly linked list

I am not able to figure out, why is my code to insert into a sorted doubly linked list failing on some test cases.Please let me know. I dont know of the test cases, they are system generated.
Node* SortedInsert(Node *head,int data)
{
// Complete this function
// Do not write the main method.
Node * temp = (Node*)malloc(sizeof(Node));
temp->data = data;
temp->next = NULL;
temp->prev = NULL;
if (head == NULL)
{
head = temp;
return head;
}
if (temp->data <= head->data)
{
temp->next = head;
head->prev = temp;
head = temp;
return head;
}
Node *curr = head;
while (curr->next!=NULL)
{
if (temp->data <= curr->data)
{
curr->prev->next = temp;
temp->prev = curr->prev;
temp->next = curr;
curr->prev = temp;
return head;
}
curr = curr->next;
}
curr->next = temp;
temp->prev = curr;
return head;
}
Thanks
Once you reach the last node, you should again compare its data with the new node and insert accordingly.
curr->next = temp;
temp->prev = curr;
return head;
}
If execution reaches this part, then at present curr is pointing to the last node. Now you should again compare its data like the following:
if (temp->data <= curr->data)
{ // insert before last node
curr->prev->next = temp;
temp->prev = curr->prev;
temp->next = curr;
curr->prev = temp;
return head;
}
// else insert at the end.
curr->next = temp;
temp->prev = curr;
return head;
}
Alternatively, you can write an if condition for node at the end of the list
Node* SortedInsert(Node *head,int data)
{
struct Node* p = head;
struct Node* q = NULL;
struct Node* r = new Node;
r->data=data;
r->prev=NULL;
r->next=NULL;
if(p==NULL){
p=r;
head=p;
}
else if(p!=NULL&&p->data>r->data){
p->prev=r;
r->next=p;
p->next=NULL;
head = r;
}
else{
p=head;
while(p!=NULL) {
if(p!=NULL&&p->data>r->data){ //If node is not at the end of list and smaller than some node
(p->prev)->next = r;
r->next = p;
r->prev = p->prev;
p->prev=r;
return head;
}
else if(p->next==NULL) //If node is at the end of list
{
p->next = r;
r->prev = p;
r->next = NULL;
return head;
}
else{}
p=p->next;
}
}
return head;
}
/*
* For reference:
*
* link to the problem :-
* https://www.hackerrank.com/challenges/insert-a-node-into-a-sorted-doubly-linked-list
*
* DoublyLinkedListNode {
* int data;
* DoublyLinkedListNode* next;
* DoublyLinkedListNode* prev;
* };
*
* Function to insert a node in sorted DLL with given data & return head
*/
DoublyLinkedListNode* sortedInsert(DoublyLinkedListNode* head, int data) {
DoublyLinkedListNode** prev = &head;
DoublyLinkedListNode* newNode = new DoublyLinkedListNode(data);
while ( *prev && ((*prev)->data <= data) ) {
prev = &(*prev)->next;
}
newNode->prev = *prev;
newNode->next = *prev;
*prev = newNode;
return head;
}
This solution works in all cases (insert in between, insert at the head, insert at the end etc). I was just trying to write a code without any conditions. While searching for some error, I ended up on this SO thread. Do correct me if this solution doesn't work for some test cases.
This is my code that passed all the test cases.
DoublyLinkedListNode* sortedInsert(DoublyLinkedListNode* head, int data) {
DoublyLinkedListNode *temp = head;
if (!temp) {
temp = new DoublyLinkedListNode(data);
head = temp;
return head;
}
while (temp->data < data && temp->next != nullptr) {
temp = temp->next;
}
if (temp->data > data) {
DoublyLinkedListNode *t = new DoublyLinkedListNode(data);
t->next = temp;
if (temp == head) {
head = t;
temp->prev = t;
return head;
}
temp->prev->next = t;
t->next = temp;
temp->prev = t;
} else {
DoublyLinkedListNode *t = new DoublyLinkedListNode(data);
t->next = temp->next;
temp->next = t;
temp->next->prev = temp;
if (t->next != nullptr) t->next->prev = t;
}
return head;
}
Here is some code in order to Insert a Node in Sorted Doubly Linked List
Node* SortedInsert(Node *head,int data)
{
struct Node *temp;
struct Node *newNode = new Node();
newNode->data=data;
newNode->next=NULL;
newNode->prev=NULL;
temp=head;
if(head==NULL)
{
head = newNode;
}
else
{
while(temp!=NULL)
{
if(temp->data<data&&temp->next!=NULL)
{
temp=temp->next;
}
else
{
if(temp->next==NULL&&temp->data<data)
{
temp->next = newNode;
newNode->prev = temp;
break;
}
else if(temp->prev==NULL&&temp->data>data)
{
newNode->next=temp;
temp->prev = newNode;
head=newNode;
break;
}
else
{
newNode->next = temp;
newNode->prev = temp->prev;
temp->prev->next =newNode;
temp->prev=newNode;
break;
}
}
}
}
return head;
}
Here is the complete C++ program for inserting a node at appropriate position in a sorted doubly linked list:
void InsertInSortedDoublyLinkedList()
{
Node *head;
head = CreateDoublyLinkList(3);
PrintDoublyLinkedList(head);
head = SortedInsert(head, 6);
printf("\n Linked list after insertion in sorted order\n");
PrintDoublyLinkedList(head);
}
void PrintDoublyLinkedList(Node *head)
{
Node *temp = head;
printf("NULL -> ");
while (temp)
{
printf("%d ->", temp->data);
temp = temp->next;
}
printf("NULL");
}
Node* CreateDoublyLinkList(int numberOfNodes)
{
Node *head = NULL, *temp, *temp1;
int startingValue = 3;
if (numberOfNodes == 0)
{
return head;
}
temp = (Node*)malloc(sizeof(Node));
temp->data = startingValue;
temp->next = NULL;
temp->prev = NULL;
head = temp;
startingValue += 2;
numberOfNodes--;
for (; numberOfNodes > 0; numberOfNodes--, startingValue += 2, temp = temp->next)
{
temp1 = (Node*)malloc(sizeof(Node));
temp1->data = startingValue;
temp1->next = NULL;
temp1->prev = temp;
temp->next = temp1;
}
return head;
}
Node* SortedInsert(Node *head,int data)
{
Node *temp = NULL, *temp1,*newNode;
int nodeInserted = 0;
if (head == NULL)
{
head = (Node*) malloc(sizeof(Node));
head->data = data;
head->next = NULL;
head->prev = NULL;
}
else
{
if (head->data > data)
{
//insertion need to take place before head itself.
newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = head;
newNode->prev = NULL;
head->prev = newNode;
head = newNode;
}
else
{
temp1 = head;
temp = head ->next;
while (temp)
{
if (temp->data > data)
{
//we need to insert the node before temp
newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = temp1;
newNode->next = temp;
temp1->next = newNode;
temp->prev = newNode;
nodeInserted = 1;
break;
}
temp1 = temp;
temp = temp->next;
}
if (!nodeInserted)
{
//node insertion need to take place at tail.
newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = temp1;
newNode->next = NULL;
temp1->next = newNode;
}
}
}
return head;
}
struct Node
{
int data;
Node *next;
Node *prev;
}
Node* SortedInsert(Node *head,int data)
{
Node* p1,*p2;
int n=10;
p2=(Node*)malloc(sizeof(struct Node));
p2->next=NULL;
p2->prev=NULL;
p2->data=data;
p1=head;
if(p1==NULL)
return p2;
while(p1->next!=NULL)
{
if(p1->data<data)
p1=p1->next;
else
break;
}
/*Three cases arise when p1->next == NUll i.e. we are end of list
case 1: insert after the end
case 2: insert in between last and second last node
case 3: insert node at the beginning of the list
*/
if(p1->next==NULL)
{
if(p1->data<data)
{
p2->prev=p1;
p1->next=p2;
}
else if(p1->data > data && p1->prev!=NULL)
{
p2->next=p1;
p2->prev=p1->prev;
if(p1->prev!=NULL)
p1->prev->next=p2;
p1->prev=p2;
}
else
{
p2->next=p1;
p2->prev=p1->prev;
if(p1->prev!=NULL)
p1->prev->next=p2;
p1->prev=p2;
head=p2;
}
}
// Here we have only one case where new node is inserted between two nodes of the list
else
{
p2->next=p1;
p2->prev=p1->prev;
if(p1->prev!=NULL)
p1->prev->next=p2;
p1->prev=p2;
}
return head;
}