Created
September 27, 2015 07:27
-
-
Save Zerk123/6c9dcfb84fb0058b0712 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<iostream> | |
| using namespace std; | |
| struct node{ | |
| int data; | |
| node *next; | |
| node *prev; | |
| }; | |
| node *head; | |
| node *GetNode(int x){ | |
| node *temp = new node; | |
| temp->data = x; | |
| temp->next = NULL; | |
| temp->prev = NULL; | |
| return temp; | |
| } | |
| void InsertAtHead(int Y){ | |
| node *newnode = GetNode(Y); | |
| if (head == NULL){ | |
| head = newnode; | |
| return; | |
| } | |
| newnode->next = head; | |
| head->prev = newnode; | |
| head = newnode; | |
| } | |
| void DeleteNthNode(int key){ | |
| node *temp = head; | |
| node *temp1 = head; | |
| int count = 0; | |
| if (head->data == key){ | |
| temp1 = temp1->next; | |
| head = temp1; | |
| delete temp; | |
| return; | |
| } | |
| while (temp != NULL) | |
| { | |
| if (temp->data != key){ | |
| count = count++; | |
| temp = temp->next; | |
| } | |
| else{ | |
| for (int i = 0; i < count - 1; i++){ | |
| temp1 = temp1->next; | |
| } | |
| temp1->next = temp->next; | |
| temp->next->prev = temp1; | |
| delete temp; | |
| break; | |
| } | |
| } | |
| } | |
| void Search(int key){ | |
| node *temp = head; | |
| while (temp != NULL){ | |
| if (temp->data != key){ | |
| temp = temp->next; | |
| } | |
| else{ | |
| cout << "Found!!!\n"; | |
| exit(0); | |
| } | |
| } | |
| cout << "Not found!!! \n"; | |
| } | |
| void Length(){ | |
| node *temp = head; | |
| int count = 0; | |
| while (temp != NULL){ | |
| count = count++; | |
| temp = temp->next; | |
| } | |
| cout << "Length of the Link List Is:" << count << "\n"; | |
| } | |
| void InsertAtNth(int key,int val){ | |
| node *temp = head; | |
| node *newnode = new node; | |
| newnode->data = val; | |
| newnode->next = NULL; | |
| newnode->prev = NULL; | |
| int count = 0; | |
| while (temp != NULL){ | |
| if (temp->data != key){ | |
| temp = temp->next; | |
| } | |
| else{ | |
| break; | |
| } | |
| } | |
| newnode->next = temp->next; | |
| newnode->prev = temp; | |
| temp->next = newnode; | |
| newnode->next->prev = newnode; | |
| } | |
| void IsEmpty(){ | |
| node *temp = head; | |
| while (temp != NULL){ | |
| if (temp->data == NULL || temp->data == 0){ | |
| temp = temp->next; | |
| } | |
| else{ | |
| cout << "Double Link List Is Not Empty." << endl; | |
| break; | |
| } | |
| } | |
| } | |
| void DestroyList(){ | |
| node *temp = head; | |
| while (temp != NULL){ | |
| node *temp1 = head; | |
| temp = temp->next; | |
| head = temp; | |
| delete temp1; | |
| } | |
| } | |
| void Front(){ | |
| node *temp = head; | |
| while (temp != NULL){ | |
| temp = temp->next; | |
| } | |
| cout << "Front:" << temp->data << endl; | |
| } | |
| void Last(){ | |
| node *temp = head; | |
| cout <<"Last:" <<temp->data << endl; | |
| } | |
| void MoveNode(int key, int newposkey){ | |
| node *temp = head; | |
| node *temp1 = head; | |
| node *taker = head; | |
| while (temp != NULL){ | |
| if (temp->data != key){ | |
| temp = temp->next; | |
| } | |
| else{ | |
| break; | |
| } | |
| } | |
| temp1 = temp->prev; | |
| temp1->next = temp->next; | |
| temp->next->prev = temp1; | |
| while (taker != NULL){ | |
| if (taker->data != newposkey){ | |
| taker = taker->next; | |
| } | |
| else{ | |
| break; | |
| } | |
| temp->next = taker->next; | |
| taker->next = temp; | |
| temp->prev = taker; | |
| temp->next->prev = temp; | |
| } | |
| } | |
| void Print(){ | |
| node *temp = head; | |
| cout << "Double Link List->" << endl; | |
| do{ | |
| cout << temp->data << endl; | |
| temp = temp->next; | |
| } while (temp != NULL); | |
| } | |
| int main(){ | |
| InsertAtHead(1); | |
| InsertAtHead(2); | |
| InsertAtHead(3); | |
| InsertAtHead(4); | |
| InsertAtHead(5); | |
| Print(); | |
| // DeleteNthNode(5); | |
| // Search(5); | |
| //InsertAtNth(5,8); | |
| //Print(); | |
| //Length(); | |
| //IsEmpty(); | |
| //DestroyList(); | |
| MoveNode(2, 5); | |
| Print(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment