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 |
What is a list in c?
Why ca not I do something like this?
write a function that accepts an array A with n elements and array B with n-1 elements. Find the missing one in array B,with an optimized manner?
What is the difference between null pointer and wild pointer?
Explain what happens if you free a pointer twice?
What is the hardest programming language?
Create a simple code fragment that will swap the values of two variables num1 and num2.
Why c is called procedure oriented language?
Tell about strtok & strstr functions
2 Answers HCL, iFlex, Motorola,
What is the role of this pointer?
What is volatile variable in c?
Suppose I want to write a function that takes a generic pointer as an argument and I want to simulate passing it by reference. Can I give the formal parameter type void **, and do something like this? void f(void **); double *dp; f((void **)&dp);