#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<dos.h>
#include<stdlib.h>
# include <db_link.h>

class line
{
	private:
		struct dnode
		{
			char data;
			dnode *next,*prev;
		}*f;

		struct node
		{
			dnode *line;
			node *befor,*after;
		};
		node *first,*last;
	public:
		line(){ first=last=NULL;}
		void add_line(int index);
		~line();
		void display();
		void delete_line (int index);
		int no_of_elements();
};
int line::no_of_elements()
{
	node *p;
	int sum=0;
	p=first;
	while(p)
	{
		p=p->after;
		sum++;
	}
	return sum;
}



void line::display()
{
	node *p;
       db_linkedlist<char> obj;
	if(first==NULL)
		cout<<" \nThere are no elements...";
	else
	{
		p=first;
		while(first!=NULL&&p!=NULL)
		{
			obj.show();
		       p= p->after;

		}
	}
}



void line::delete_line(int index)
{
	int count=0;
	node *p;
	if(index<0||first==NULL)
		return ;
	else
	{
		p=first;
		if(!index)
		{
			first=first->after;
			first->befor=NULL;
		}
		else
		{
			while(p&&count!=index)
			{
				p=p->after;
				count++;
			}
			if(p)
			{
				p->befor->after=p->after;
				p->after->befor=p->befor;
			}
			else
				return ;
		}

		delete (p);
		return ;
	}
}

void line::add_line(int index)
{
	int count=0;
	node *p,*c;
	p=new node;
	if(p==NULL)
	{
		cout<<"\nThere is no enugh space to add\n";
		getch();
		return ;
	}
	p->line=f;
	if(index==0)
	{
		p->befor=NULL;
		if(first==NULL)
		{
			p->after=NULL;
			last=p;
		}
		else
		{
			p->after=first;
		}
		first=p;
	}
	else
	{
		for( c=first ; c!=NULL &&count!=index; c=c->after)
			count++;
		if(c==NULL)
		{


			p->after=NULL;
			p->befor=last;
			p->befor->after=p;
			last=p;

			if(count==0)
				first=p;
		}
		else
		{
			p->after=c;
			p->befor=c->befor;
			c->befor=p;
			p->befor->after=p;
		}
	}
}

line::~line()
{
	node *p;
	while(!first)
	{
		p=first;
		first=first->after;
		delete(p);
	}
}


/*
void main()
{	line obj;
	int it;
	textmode(C80);
	clrscr();
	int ch;

	do
	{
		textcolor(15);
		clrscr();
		cout<<"1- ADD LINE.\n";
		cout<<"2- DELETE LINE.\n";
		cout<<"3- NUMBER OF ELEMENTS.\n";
		cout<<"4- EXIT.\n\n\n\n\n\t";
		cout<<"ENTER YOUR CHOICE : ";
		cin>>ch;

		switch(ch)
		{	case 1:
				cout<<"\n\nEnter the item which you want to add : ";
				cin>>it;
				obj.add_line(it);
				break;

			case 2:
				cout<<"Enter the index which you want to delete\n";
				int in;
				cin>>in;
				obj.delete_line(in);
				break;

			case 3:
				int x=obj.no_of_elements();
				cout << "\n\t"<<x;
				getch();
				break;
			case 4:

				exit(0);
		}
	}while(1);
} */
void main()
{
	clrscr();
	//line line;
	line obj;
	db_linkedlist <char> line;
	obj.add_line(0);
	int y=1;
	char ch;
	ch=getch();
	while(ch!=19)
	{
		clrscr();
		switch(ch)
		{
			case 13:
				obj.add_line(y);
				y++;
				break;
			default:

				line.addlast(ch);


		}
		obj.display();
		ch=getch();
	}
}