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, *Tail = NULL;
- int Size = 0;
- void Traverse(){
- struct Node* temp = Head;
- cout<<"\tThe Linked List is : \t";
- while(temp != NULL){
- 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;
- Tail = node;
- node->next = NULL;
- node->prev = NULL;
- Size++;
- Traverse();
- return;
- }
- Tail->next = node;
- node->prev = Tail;
- node->next = NULL;
- Tail = 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;
- Tail = node;
- node->next = NULL;
- node->prev = NULL;
- Size++;
- Traverse();
- return;
- }
- node->next = Head;
- node->prev = NULL;
- Head->prev = node;
- Head = node;
- 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 = Tail;
- 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 DeleteBeg(){
- if (Head == NULL){
- cout<<"List is empty"<<endl;
- return;
- }
- cout<<"Deleting from the beginning"<<endl;
- if (Head == Tail){
- delete Head;
- Head = NULL;
- Tail = NULL;
- Size--;
- Traverse();
- return;
- }
- Node* temp = Head;
- Head = Head->next;
- Head->prev = NULL;
- delete temp;
- Size--;
- Traverse();
- }
- void DeleteEnd(){
- if (Head == NULL){
- cout<<"List is empty"<<endl;
- return;
- }
- cout<<"Deleting from the end"<<endl;
- if (Head == Tail){
- delete Head;
- Head = NULL;
- Tail = NULL;
- Size--;
- Traverse();
- return;
- }
- Node* temp = Tail;
- Tail = Tail->prev;
- Tail->next = NULL;
- delete temp;
- Size--;
- Traverse();
- }
- void DeletePos(int pos){
- if(Head == NULL){
- cout<<"List is empty"<<endl;
- return;
- }
- if(pos >= Size){
- cout<<"Invalid Position"<<endl;
- return;
- }
- if(pos == 0){
- DeleteBeg();
- return;
- }
- if(pos == Size-1){
- DeleteEnd();
- return;
- }
- cout<<"Deleting from position "<<pos<<endl;
- if(pos > Size/2){
- Node* temp = Tail;
- 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);
- InsertPos(796, 4);
- DeletePos(3);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement