Professional Documents
Culture Documents
Write a C++ program to create a Circular single linked list and display
the elements.
Description
In circular single linked list, the LINK part of the last node contains address of the
starting node. The diagrammatic representation of a circular single linked list is as follows:
1 2 3
10 2 20 3 30 1
START END
Algorithm creation(): This procedure creates a circular single linked with the specified
number of elements.
Algorithm display(): This procedure is used to display the elements of the circular
single linked list from the first node to the last node.
Program:
#include<iostream>
#include<stdlib.h>
using namespace std;
template<class T>
class CSList
{
typedef struct List
{
T DATA;
struct List *LINK;
}NODE;
NODE *START,*END;
public:CSList()
{
START=END=NULL;
}
void create();
void display();
};
template<class T>
void CSList<T>::create()
{
NODE *NEW;
T x;
while(1)
{
cout<<endl<<"Enter An Element (STOP:-999) =";
cin>>x;
if(x==-999)
return;
else
{
NEW=(NODE*)malloc(sizeof(NODE));
NEW->DATA=x;
NEW->LINK=NULL;
if(START==NULL)
START=END=NEW;
else
{
END->LINK=NEW;
END=NEW;
}
END->LINK=START;
}
}
}
template<class T>
void CSList<T>::display()
{
NODE *Temp;
if(START==NULL)
cout<<endl<<"Circular Single Linked List Empty";
else
{
Temp=START;
cout<<endl<<"List of Elements Are =";
while(Temp->LINK!=START)
{
cout<<" "<<Temp->DATA;
Temp=Temp->LINK;
}
cout<<" "<<Temp->DATA;
}
return;
}
int main()
{
CSList<int> obj;
int ch;
while(1)
{
cout<<endl<<"1:CREATION"<<endl<<"2:DISPLAY"<<endl<<"3:EXIT";
cout<<endl<<"Enter Your Choice = ";
cin>>ch;
switch(ch)
{
case 1:obj.create();
break;
case 2:obj.display();
break;
case 3:exit(0);
}
}
return 0;
}
// 14. Write a C++ program to create a Circular double linked list and display
the elements.
Description
In circular double linked list, the RLINK part of the last node contains address of the
starting node and LLINK part of the first node contains address of the last node respectively.
The diagrammatic representation of a circular double linked list is as follows:
1 2 3
3 10 2 1 20 3 2 30 1
START END
Algorithm creation(): This procedure creates a circular double linked with the specified
number of elements.
ENDIF
EndRepeat
Algorithm display(): This procedure is used to display the elements of the circular
double linked list from the first node to the last node.
Program:
#include<iostream>
#include<stdlib.h>
using namespace std;
template<class T>
class CDList
{
typedef struct List
{
T DATA;
struct List *LLINK,*RLINK;
}NODE;
NODE *START,*END;
public:CDList()
{
START=END=NULL;
}
void create();
void display();
};
template<class T>
void CDList<T>::create()
{
NODE *NEW;
T x;
while(1)
{
cout<<endl<<"Enter An Element (STOP:-999) =";
cin>>x;
if(x==-999)
return;
else
{
NEW=(NODE*)malloc(sizeof(NODE));
NEW->DATA=x;
NEW->LLINK=NULL;
NEW->RLINK=NULL;
if(START==NULL)
START=END=NEW;
else
{
END->RLINK=NEW;
NEW->LLINK=END;
END=NEW;
}
END->RLINK=START;
START->LLINK=END;
}
}
}
template<class T>
void CDList<T>::display()
{
NODE *Temp;
if(START==NULL)
cout<<endl<<"Circular Double Linked List Empty";
else
{
Temp=START;
cout<<endl<<"List of Elements Are = ";
while(Temp->RLINK!=START)
{
cout<<" "<<Temp->DATA;
Temp=Temp->RLINK;
}
cout<<" "<<Temp->DATA;
}
return;
}
int main()
{
CDList<int> obj;
int ch;
while(1)
{
cout<<endl<<"1:CREATION"<<endl<<"2:DISPLAY"<<endl<<"3:EXIT";
cout<<endl<<"Enter Your Choice = ";
cin>>ch;
switch(ch)
{
case 1:obj.create();
break;
case 2:obj.display();
break;
case 3:exit(0);
}
}
return 0;
}