Single Double Linked List
Single Double Linked List
for( ; addMore == 1; ){
cout<<"how many nodes you want to add?"<<endl;
cin>>nodes;
cout<<"if you want to add the nodes from front press 1 and otherwise press 2"<<endl;
cin>>frontEnd;
while(frontEnd != 1 && frontEnd != 2){
cout<<"Invalid Insertion! please enter either 1 or 2 "<<endl;
cin>>frontEnd;
}
if(frontEnd == 1){
for(int i = 0; i < nodes; i++){
cout<<"please enter data for node "<<i+1<<" : ";
cin>>dataValue;
AddFrontNode(head, dataValue);
}
}
else{
for(int i = 0; i < nodes; i++){
cout<<"please enter data for node "<<i+1<<" : ";
cin>>dataValue;
AddEndNode(head, dataValue);
}
}
cout<<"Here below is the data elements of the the list you created:"<<endl;
DisplayList(head);
cout<<"do you want to add more nodes? if yes enter 1 otherwise 0:"<<endl;
cin>>addMore;
if(addMore == 1) continue;
else break;
}
return 0;
}
void AddEndNode(Node*& head, int value){
Node* newNode = new Node();
newNode->data = value;
newNode->next = NULL;
if(head == NULL){
head = newNode;
}
else{
Node* temp = head;
while (temp->next != NULL){
temp = temp->next;
}
temp->next = newNode;
}
}
void AddFrontNode(Node*& head, int value){
Node* newNode = new Node();
newNode->data = value;
newNode->next = head;
head = newNode;
}
void DisplayList(Node* head){
Node* temp = head;
while(temp != NULL){
cout<<temp->data<<"<-->";
temp = temp->next;
}
cout<<"nullptr"<<endl;
}
#include <iostream>
using namespace std;
struct Node{
int data;
Node* next;
Node* prev;
};
void InsertEnd(Node*& head, int value);
void DisplayList(Node* head);
void InsertFront(Node*& head, int value);
int main(int argc, char** argv) {
Node* head = NULL;
InsertEnd(head,23);
InsertEnd(head,45);
InsertEnd(head,64);
cout<<"the list added from end"<<endl;
DisplayList(head);
InsertFront(head,79);
InsertFront(head,1);
cout<<"after inserting elements to the list from front"<<endl;
DisplayList(head);
return 0;
}
void InsertEnd(Node*& head, int value){
Node* newNode = new Node();
newNode->data = value;
newNode->next = NULL;
if(head == NULL){
newNode->prev = NULL;
head = newNode;
}
else{
Node* temp = head;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
}
void InsertFront(Node*& head, int value){
Node* newNode = new Node();
newNode->data = value;
newNode->next = head;
head = newNode;
newNode->prev = NULL;
}
void DisplayList(Node* head){
Node* temp = head;
cout<<"elements of the list"<<endl;
while(temp != NULL){
cout<<temp->data<<"<-->";
temp = temp->next;
}
cout<<"nullptr"<<endl;
}