Created
September 27, 2015 07:25
-
-
Save Zerk123/7edb06bd449cdf6d13fc 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 *head; | |
| void Insert(int x){ | |
| node *temp = new node; | |
| temp->data = x; | |
| temp->next = NULL; | |
| if (head!= NULL){ | |
| temp->next = head; | |
| } | |
| head = temp; | |
| } | |
| void Reverse(){ | |
| node *temp = head; | |
| node *front; | |
| node *previous=NULL; | |
| while (temp != NULL){ | |
| front = temp->next; | |
| temp->next = previous; | |
| previous = temp; | |
| temp = front; | |
| } | |
| head = previous; | |
| } | |
| void Search(int key){ | |
| node *temp = head; | |
| while (temp != NULL){ | |
| if (temp->data == key){ | |
| cout << "FOUND!!!" << endl; | |
| exit(0); | |
| } | |
| else{ | |
| temp = temp->next; | |
| } | |
| } | |
| cout << "NOT FOUND!!!" << endl; | |
| } | |
| void MoveNode(int pos,int newpos){ | |
| node *temp = head; | |
| node *temp1 = head;; | |
| node *temp2 = head; | |
| if (pos == 1){ | |
| head = temp->next; | |
| temp1 = head; | |
| temp->next = NULL; | |
| for (int i = 0; i < newpos-2; i++){ | |
| temp1 = temp1->next; | |
| } | |
| temp->next = temp1->next; | |
| temp1->next = temp; | |
| return; | |
| } | |
| for (int i = 0; i < pos - 1; i++){ | |
| temp = temp->next; | |
| } | |
| for (int i = 0; i < pos - 2; i++){ | |
| temp1 = temp1->next;} | |
| temp1->next = temp->next; | |
| for (int i = 0; i < newpos - 1; i++){ | |
| temp2 = temp2->next; | |
| } | |
| temp->next = temp2->next; | |
| temp2->next = temp; | |
| } | |
| void InsertAtNthPos(int key,int value){ | |
| node *temp = head; | |
| node *newnode = new node; | |
| newnode->data = value; | |
| if (key == temp->data){ | |
| newnode->next = temp->next; | |
| temp->next = newnode; | |
| } | |
| else{ | |
| temp = temp->next; | |
| while (temp != NULL){ | |
| if (temp->data != key){ | |
| temp = temp->next; | |
| } | |
| else{ | |
| newnode->next = temp->next; | |
| temp->next = newnode; | |
| if (temp != NULL){ | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| void Front(){ | |
| node *temp = head; | |
| while (temp->next != NULL){ | |
| temp = temp->next; | |
| } | |
| cout <<"FRONT:"<< temp->data << endl; | |
| } | |
| void Last(){ | |
| node *temp = head; | |
| cout <<"Last:"<<temp->data << endl; | |
| } | |
| void IsEmpty(){ | |
| node *temp = head; | |
| while (temp != NULL){ | |
| if (temp->data == NULL || temp->data == 0){ | |
| temp = temp->next; | |
| } | |
| else{ | |
| cout << "Link List not empty." << endl; | |
| exit(0); | |
| } | |
| } | |
| cout << "Link List Empty." << endl; | |
| } | |
| void Dup(){ | |
| node *temp=head; | |
| node *temp1 = head; | |
| node *taker = head; | |
| int count = 0; | |
| while (temp != NULL){ | |
| temp1 = temp->next; | |
| while (temp->data != temp1->data){ | |
| count = count++; | |
| temp1 = temp1->next; | |
| if (temp1 == NULL){ | |
| cout << "Number Not Present." << endl; | |
| break; | |
| } | |
| temp = temp->next; | |
| if (temp->data == temp1->data){ | |
| for (int i = 0; i < count - 1; i++){ | |
| taker = taker->next; | |
| } | |
| taker->next = temp1->next; | |
| } | |
| } | |
| } | |
| } | |
| void DestroyList(){ | |
| node *temp = head; | |
| while (temp != NULL){ | |
| node *temp1 = head; | |
| temp = temp->next; | |
| head = temp; | |
| delete temp1; | |
| } | |
| } | |
| void Length(){ | |
| node *temp = head; | |
| int count = 0; | |
| while (temp!=NULL){ | |
| count = count++; | |
| temp = temp->next; | |
| } | |
| cout << "Length Of Link List:" << count << endl; | |
| } | |
| void Print(){ | |
| node *temp = head; | |
| cout << "Link List->" << endl; | |
| do{ | |
| cout << temp->data << endl; | |
| temp = temp->next; | |
| } while (temp != NULL); | |
| } | |
| int main(){ | |
| Insert(1); | |
| Insert(2); | |
| Insert(3); | |
| Insert(4); | |
| Insert(5); | |
| Print(); | |
| cout << endl; | |
| //Search(3); | |
| //Delete(3); | |
| // InsertAtNthPos(5,5); | |
| //Print(); | |
| // Front(); | |
| // Last(); | |
| // IsEmpty(); | |
| // DestroyList(); | |
| //Print(); | |
| //Length(); | |
| // Dup(); | |
| Reverse(); | |
| Print(); | |
| MoveNode(1, 4); | |
| Print(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment