1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include<iostream>
using namespace std;
struct node
{
	  int data;
	  node* next;
	  node* previous;
};
node* head;//Global  variable
node* tail;//Global  variable

//-------------------Insert First--------------------------//

node* insert_node(int value)
{
	
	node* first_node = new node;
	first_node->data=value;
	first_node->next=NULL;
	first_node->previous=NULL;
	head=first_node;
	tail=first_node;
	return first_node;
}

//---------------------------Insert Before First-----------------------

node* insert_before_first(int value)
{

	node* new_node = new node;
    new_node->data=value;
    new_node->next=head;
    new_node->previous=NULL;
	if(head!= NULL)
	{
		head->previous=new_node;
	}
	
	head=new_node;
	return new_node;
}

// ------------------------INSERT AT POSITION--------------------

node* insert_position (int value , int position)
{
	
	node * new_node = new node;
	node * temp= new node;
	temp = head;
	for(int i=1 ; i<position-1 ; i++)
	{
	     temp = temp->next;
	}
	new_node->data = value;
	new_node->next = temp -> next;
	new_node->previous = temp;
	temp->next = new_node;
	return new_node;
}
//-----------------------Insert before last ------------------------------
node * insert_before_last(int value)
{
	
	node * new_node = new node;
	node * current = new node;
	current = head;
	while(current->next!=NULL)
	{
		current=current->next;
	} 
	new_node->data = value;
	
	new_node->next=current;
	new_node->previous=current->previous;
	current->previous->next=new_node;
	return new_node;
}
//-----------------------------------Delete First---------------------------
void delete_first ()
{
	node * temp = new node;
	temp = head;
	head = head->next;
	head->previous = NULL;
	delete temp;
}

//--------------------------------------Delete Last -------------------------
void delete_last ()
{
	node *current = new node;
 	node *previous = new node;
 	current=head;
 	while(current->next!=NULL)
 	{
 		previous=current;
 		current=current->next;
	 }
	 tail=previous;
	 previous->next=NULL;
	 delete current;
	
}

//-----------------------------Delete from position----------------------

void delete_position( int pos)
{
	node * current = new node;
    current = head ;
    for(int i=1 ; i<pos ; i++)
    {
    	current = current->next;
	}
	current->previous->next=current->next;
	current->next->previous=current->previous;
	
	

}


//-----------------------Display Function -----------------------------
void display ()
{
	node* temp = new node;
	temp =head;
      if(temp != NULL) {
        cout<<"\nThe list Elements: ";
        while(temp != NULL) {
          cout<<temp->data<<" ";
          temp = temp->next;
        }
      } 
	  else
      {
        cout<<"\nThe list is empty.";
      }
}
int main()
{
	insert_node(50);
	insert_before_first(40);
	insert_before_first(20);
	insert_before_first(60);
	insert_before_first(70);
	insert_before_first(10);
	insert_position(30,3);
	insert_before_last(5);
	delete_first();
	delete_last();
	delete_position(4);
	
	display();
}