Doubly Linked List
#include<iostream>
using namespace std;
struct Node
{
int data;
Node *next;
Node *prev;
};
Node *Head=NULL;
Node *Tail=NULL;
Node *createNewNode(int value)
{
Node *newNode=new Node;
newNode->data=value;
newNode->next=NULL;
newNode->prev=NULL;
return newNode;
}
int getValue()
{
int x;
cout<<"Enter Input:";
cin>>x;
return x;
}
void createList(int total)
{
Node *currNode=Head=Tail=createNewNode(getValue());
for(int i=1;i<total;i++)
{
Node *newNode=createNewNode(getValue());
currNode->next = newNode;
newNode->prev = currNode;
Tail = newNode;
currNode=currNode->next;
}
}
void show()
{
Node *currNode=Head;
while(currNode!=NULL)
{
cout<<currNode->data<< " ";
currNode=currNode->next;
}
}
void reverse_show()
{
Node *currNode=Tail;
while(currNode!=NULL)
{
cout<<currNode->data<< " ";
currNode=currNode->prev;
}
}
void insert_first(int v)
{
Node *currNode=createNewNode(v);
currNode->next=Head;
Head->prev=currNode;
Head=currNode;
}
void insertAny_Position(int position,int v)
{
Node *currNode=Head;
for(int i=1;i<position;i++)
{
currNode=currNode->next;
}
Node *newNode=createNewNode(v);
newNode->next=currNode->next;
currNode->next=newNode;
}
void insertAt_Last(int Value)
{
Node *currNode = Head;
while(currNode->next != NULL)
{
currNode = currNode->next;
}
Node *newNode=createNewNode(Value);
newNode->prev=currNode;
currNode->next = newNode;
Tail=newNode;
}
void deleteFirst()
{
Node *currNode=Head;
Head=Head->next;
Head->prev=NULL;
delete currNode;
}
void deleteLast()
{
Node *currNode=Head;
while(currNode->next->next!=NULL)
{
currNode=currNode->next;
}
Node *newNode=currNode->next;
newNode->prev=currNode;
delete newNode;
currNode->next=NULL;
Tail=currNode;
}
void removeAnyPosition(int position)
{
Node *currNode=Head;
for(int i=1;i<position;i++)
{
currNode=currNode->next;
}
Node *myNode=currNode->next;
currNode->next=currNode->next->next;
currNode->next->prev=currNode;
delete myNode;
}
int main()
{
createList(4);
cout<<"The create List:"<<endl;
show();
cout<<"\nInsert First:"<<endl;
insert_first(0);
show();
cout<<"\nInsert any position:"<<endl;
insertAny_Position(2,11);
show();
cout<<"\nInsert Last:"<<endl;
insertAt_Last(6);
show();
cout<<"\nDelete from First:"<<endl;
deleteFirst();
show();
cout<<"\nDelete from Any position:"<<endl;
removeAnyPosition(2); //2 is position
show();
cout<<"\nDelete from Last:"<<endl;
deleteLast();
show();
return 0;
}
Excellent info on linked list. Thanks admin for the wonderful post
ReplyDeleteHadoop Training in Chennai
Android Training in Chennai
Selenium Training in Chennai
Digital Marketing Training in Chennai
JAVA Training in Chennai
German Classes in chennai
Big Data Training in Chennai
Hadoop Training in Tambaram
Most Welcome
ReplyDeletebetmatik
ReplyDeletekralbet
betpark
tipobet
slot siteleri
kibris bahis siteleri
poker siteleri
bonus veren siteler
mobil ödeme bahis
KKW