0% found this document useful (0 votes)
4 views

doubly linked list

Double linked array

Uploaded by

fanfare642
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

doubly linked list

Double linked array

Uploaded by

fanfare642
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<iostream>

using namespace std;


#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int num;
node *prev;
node *next;
};
typedef struct node n;
n *start,*rear,*temp;
class l
{
public:
n * create(n *);
n * insertbeg(n *);
n * insertend(n *);
n * deletbeg(n *);
n * deletend(n *);
void display(n *);
l()
{
start=rear=NULL;
}
};
n * l :: create(n * start)
{
int data;
start=new n;
cout<<"\n enter data";
cin>>data;
start->num=data;
start->prev=start->next=NULL;
rear=start;
return start;
}
n * l :: insertbeg(n * start)
{
temp=new n;
cout<<"\n enter data";
cin>>temp->num;
if(start==NULL)
{
start=temp;
start->prev=start->next=NULL;
}
else
{
temp->next=start;
start->prev=temp;
temp->prev=NULL;
start=temp;
}
return start;
}
n * l :: insertend(n * start)
{
temp=new n;
cout<<"\n enter data";
cin>>temp->num;
cout<<rear->num;
if(rear==NULL)
{
temp->prev=temp->next;
start=rear=temp;

}
else
{
temp->next=NULL;
temp->prev=rear;
rear->next=temp;
rear=temp;
}
return start;
}
n * l :: deletend(n * start)
{
temp=start;
if(rear==NULL)
{
cout<<"\n deletion is not possible";
}
else if(rear->prev==NULL)
{
temp=rear;
start=rear=NULL;

}
else
{
temp=rear;
rear=rear->prev;
rear->next=NULL;
}

return start;
}
n * l :: deletbeg(n * start)
{
temp=start;
if(start==NULL)
{
cout<<"\n deletion is not possible";
}
else if(start==rear)
{
start=rear=NULL;
delete temp;
}
else
{
start=start->next;
start->prev=NULL;
delete temp;
}
return start;
}

void l :: display(n * start)


{
temp=start;
do
{
cout<<"\n"<<temp->num;
temp=temp->next;
}
while(temp!=NULL);
}
int main()
{
int ch;

l o;
do
{
cout<<"\n 1.create";
cout<<"\n 2.insert at beginning";
cout<<"\n 3.insert at end";
cout<<"\n 4.delete from beginning";
cout<<"\n 5.delete from end";
cout<<"\n 6.display";
cout<<"\n 7.end";
cout<<"\n enter your choice";
cin>>ch;
switch(ch)
{
case 1:start=o.create(start);
continue;
case 2:start=o.insertbeg(start);
continue;
case 3:start=o.insertend(start);
continue;
case 4:start=o.deletbeg(start);
continue;
case 5:start=o.deletend(start);
continue;

case 6:o.display(start);
continue;
case 7:exit(0);
}
}
while(ch!=7);
return 0;

You might also like