//Author: Shubham Patil SE comp
#include <cstdio>
#include <iostream>
using namespace std;
class node{
public:
int data;
node *next;
node *prev;
};
class Linked_List
{
private:
node *head,*tail;
public:
Linked_List() //constructor
{
head = NULL;
tail=NULL;
}
void add_node(int n)
{
node *tmp = new node; //dynamic allocation
tmp->data=n;
tmp->next=NULL;
tmp->prev=NULL;
if(head == NULL) //first node
{
head = tmp;
tail = tmp;
}
else //else nodes
{
tail->next=tmp;
tmp->prev=tail; //prev
tail=tail->next; //next
}
}
void display() //func to display the linked list
{
node *temp= head;
while(temp!=NULL)
{
cout<<temp->data;
temp = temp->next;
cout<<endl;
}
cout<<"reverse"<<endl;
temp=tail;
while(temp!=head)
{
cout<<temp->data;
temp = temp->prev;
cout<<endl;
}
cout<<head->data; //for first node
}
};
int main()
{
Linked_List a;
a.add_node(1);
a.add_node(2);
a.add_node(3);
a.add_node(4);
a.add_node(5);
a.add_node(6);
a.display();
return 0;
}
#include <cstdio>
#include <iostream>
using namespace std;
class node{
public:
int data;
node *next;
node *prev;
};
class Linked_List
{
private:
node *head,*tail;
public:
Linked_List() //constructor
{
head = NULL;
tail=NULL;
}
void add_node(int n)
{
node *tmp = new node; //dynamic allocation
tmp->data=n;
tmp->next=NULL;
tmp->prev=NULL;
if(head == NULL) //first node
{
head = tmp;
tail = tmp;
}
else //else nodes
{
tail->next=tmp;
tmp->prev=tail; //prev
tail=tail->next; //next
}
}
void display() //func to display the linked list
{
node *temp= head;
while(temp!=NULL)
{
cout<<temp->data;
temp = temp->next;
cout<<endl;
}
cout<<"reverse"<<endl;
temp=tail;
while(temp!=head)
{
cout<<temp->data;
temp = temp->prev;
cout<<endl;
}
cout<<head->data; //for first node
}
};
int main()
{
Linked_List a;
a.add_node(1);
a.add_node(2);
a.add_node(3);
a.add_node(4);
a.add_node(5);
a.add_node(6);
a.display();
return 0;
}
0 Comments