Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace std;
- #include<iostream>
- struct Node{
- int data;
- struct Node *next, *prev;
- };
- Node* Head = NULL;
- int Size = 0;
- void Traverse(){
- struct Node* temp = Head;
- cout<<"\tFORWARD TRAVERSE\nThe Linked List is : \t";
- cout<<"\tThe Linked List is : \t";
- for (int i=0; i<Size; i++)
- {
- cout<<temp->data<<" <-> ";
- temp = temp->next;
- }
- cout<<"\tSize - "<<Size<<endl;
- cout<<"\n------------------------------------------------------------------------\n";
- }
- void InsertEnd(int x){
- cout<<"Inserting "<<x<<" at the end"<<endl;
- struct Node* node = new Node;
- node->data = x;
- if (Head == NULL){
- Head = node;
- node->next = node;
- node->prev = node;
- Size++;
- Traverse();
- return;
- }
- Head->prev->next = node;
- node->next = Head;
- node->prev = Head->prev;
- Head->prev = node;
- Size++;
- Traverse();
- }
- void InsertBeg(int x){
- cout<<"Inserting "<<x<<" at the beginning"<<endl;
- struct Node* node = new Node;
- node->data = x;
- if (Head == NULL){
- Head = node;
- node->next = node;
- node->prev = node;
- Size++;
- Traverse();
- return;
- }
- Head->prev->next = node;
- node->next = Head;
- node->prev = Head->prev;
- Head->prev = node;
- Head = node;
- Size++;
- Traverse();
- }
- void DeleteBeg(){
- if (Head == NULL){
- cout<<"List is empty"<<endl;
- return;
- }
- if (Head == Head->next){
- Head = NULL;
- Size--;
- return;
- }
- cout<<"Deleting the first element"<<endl;
- struct Node* temp = Head;
- Head = Head->next;
- Head->prev = temp->prev;
- temp->prev->next = Head;
- delete temp;
- Size--;
- Traverse();
- }
- void DeleteEnd(){
- if (Head == NULL){
- cout<<"List is empty"<<endl;
- return;
- }
- if (Head == Head->next){
- Head = NULL;
- Size--;
- return;
- }
- cout<<"Deleting the last element"<<endl;
- struct Node* temp = Head->prev;
- Head->prev = temp->prev;
- temp->prev->next = Head;
- delete temp;
- Size--;
- Traverse();
- }
- void InsertPos(int x, int pos){
- if (pos > Size){
- cout<<"Invalid Position"<<endl;
- return;
- }
- if (pos == 0){
- InsertBeg(x);
- return;
- }
- if (pos == Size){
- InsertEnd(x);
- return;
- }
- cout<<"Inserting "<<x<<" at position "<<pos<<endl;
- Node* node = new Node;
- node->data = x;
- if(pos > Size/2){
- Node* temp = Head->prev;
- pos = Size - pos - 1;
- while(pos--){
- temp = temp->prev;
- }
- node->next = temp;
- node->prev = temp->prev;
- temp->prev->next = node;
- temp->prev = node;
- }
- else{
- Node* temp = Head;
- while(pos--){
- temp = temp->next;
- }
- node->next = temp;
- node->prev = temp->prev;
- temp->prev->next = node;
- temp->prev = node;
- }
- Size++;
- Traverse();
- }
- void DeletePos(int pos){
- if (pos > Size){
- cout<<"Invalid Position"<<endl;
- return;
- }
- if (pos == 0){
- DeleteBeg();
- return;
- }
- if (pos == Size - 1){
- DeleteEnd();
- return;
- }
- cout<<"Deleting the element at position "<<pos<<endl;
- if(pos > Size/2){
- Node* temp = Head->prev;
- pos = Size - pos - 1;
- while(pos--){
- temp = temp->prev;
- }
- temp->prev->next = temp->next;
- temp->next->prev = temp->prev;
- delete temp;
- }
- else{
- Node* temp = Head;
- while(pos--){
- temp = temp->next;
- }
- temp->prev->next = temp->next;
- temp->next->prev = temp->prev;
- delete temp;
- }
- Size--;
- Traverse();
- }
- int main(){
- InsertEnd(1);
- InsertEnd(2);
- InsertEnd(3);
- InsertEnd(4);
- InsertBeg(100);
- InsertBeg(200);
- InsertPos(1000, 1);
- DeletePos(7);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement