an algorithem for the implementation of circular doubly
linked list



an algorithem for the implementation of circular doubly linked list..

Answer / fatema

Question 1: Write an algorithm for the implementation of a circular doubly linked list
(10 Marks)

-> Operations
1)insertion
2)forward traversal
3)reverse traversal
4)search
#include<iostream.h>
cll*hd;
struct cll
{
private:
int data;
cll *next;
public:
cll* insert_one(int d,cll* c)
{
cll*NEW;
NEW=new cll;
NEW->data=d;
NEW->next=NULL;
if(c==NULL)
{
c=NEW;
c->next=c;
}
else
{
cll*c1=c;
while(c1->next!=c)
c1=c1->next;
c1->next=NEW;
NEW->next=c;
}
return c;
}
cll* delete_one(int,cll*);
void ftraverse(cll* c)
{
if(c==NULL)
{
cout<<"\nlist empty\n";
return;
}
else
{
cll *c1=c;
cout<<c1->data<<"->";
c1=c1->next;
while(c1!=c)
{
cout<<c1->data<<"->";
c1=c1->next;
}
cout<<"NULL\n";
}
}
void rtraverse(cll* c)
{
if(c->next==hd)
{
cout<<c->data<<"->";
return;
}
else
rtraverse(c->next);
cout<<c->data<<"->";
}
void search(int d,cll* c)
{
cll*c1=c;
if(c==NULL)
{
cout<<"\nlist empty\n";
return;
}
else
{
if(c->data == d)
{
cout<<"found\n";
return ;
}
while(c->next !=c1)
{
if(c->data==d)
{
cout<<"found\n";
return ;
}
c=c->next;
}
if(c->data ==d)
{
cout<<"found\n";
return ;
}
cout<<" search unsuccess ful \n";
}
}
void function()
{
cout<<"******************************************\n";
cout<<"program to implement a circular linked list \n";
cout<<"******************************************\n";
cll * head;
head=NULL;
cout<<"\n\nMENU :\n";
cout<<"1)insertion\n"
<<"2forward traversal\n"
<<"3)reverse traversal\n"
<<"4)search\n"
<<"5)exit\n\n";
cout<<"Enter your option :";
int opt;
cin>>opt;
int d;
while(opt!=5)
{
switch(opt)
{
case 1:
cout<<"Enter data to the node :";
cin>>d;
head=insert_one(d,head);
cout<<"inserted\n";
break;
case 2:
cout<<"The forward traversal is :\n";
ftraverse(head);
break;
case 3:
cout<<"The reverse traversal is :\n";
hd=head;
rtraverse(head);
cout<<"NULL\n";
break;
case 4
cout<<"Enter the element to be searched :";
int d;
cin>>d;
search(d,head);
break;
case 5:
break;
default:
cout<<"invalid option";
break;
}
cout<<"\n\nMENU :\n";
cout<<"1)insertion\n"
<<"2)forward traversal\n"
<<"3)reverse traversal\n"
<<"4)search\n"
<<"5)exit\n\n";
cout<<"Enter your option :";
cin>>opt;
}
}
};
void main()
{
cll list;
list.function();
}
.

Is This Answer Correct ?    15 Yes 8 No

Post New Answer

More C Interview Questions

What is function pointer and where we will use it

2 Answers   Infosys, NetApp,


How can I implement opaque (abstract) data types in C? What's the difference between these two declarations? struct x1 { ... }; typedef struct { ... } x2;

2 Answers  


The number of bytes of storage occupied by short, int and long are a) 2, 2 and 4 b) 2, 4 and 4 c) 4, 4 and 4 d) none

0 Answers  


enum { SUNDAY, MONDAY, TUESDAY, }day; main() { day =20; printf("%d",); getch(); } what will be the output of the above program

1 Answers  


write a c program to print a given number as odd or even without using loop statements,(no if ,while etc)

10 Answers  






main() { int a=4,b=2; a=b<<a + b>>2; printf("%d", a); }

11 Answers   HCL, Vector, Vector India, Vector Solutions, Wipro,


Famous puzzles which are generally asked by companies during interviews ?

1 Answers   3D PLM, Yahoo,


What is const keyword in c?

0 Answers  


can any one tell that i have a variable which is declared as static but i want this variable to be visible to the other files? how?

2 Answers  


what is ur strangth & weekness

0 Answers   Cognizant, LG Soft, NetEnrich,


int array[]={1,2,3,4,5,6,7,8}; #define SIZE (sizeof(array)/sizeof(int)) main() { if(-1<=SIZE) printf("1"); else printf("2"); }

2 Answers   Vector,


HOW DO YOU HANDLE EXCEPTIONS IN C?

2 Answers   AppLabs,


Categories