You are on page 1of 7

// 13.

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.

Step 1: Repeat WHILE TRUE


READ an element as x
IF x = -999 THEN
RETURN
ELSE
Allocate memory for a NEW node
DATA(NEW) ← x
LINK(NEW) ← NULL
IF START = NULL THEN
START ← END ← NEW
ELSE
LINK(END) ← NEW
END ← NEW
ENDIF
LINK(END) ← START
ENDIF
EndRepeat

Algorithm display(): This procedure is used to display the elements of the circular
single linked list from the first node to the last node.

Step 1: IF START = NULL THEN


WRITE ‘Circular Single Linked List Empty’
ELSE
TEMP ← START
Repeat WHILE LINK(TEMP) ≠ START
WRITE DATA(TEMP)
TEMP ← LINK(TEMP)
EndRepeat
WRITE DATA(TEMP)
ENDIF
Step 2: RETURN

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.

Step 1: Repeat WHILE TRUE


READ an element as x
IF x = -999 THEN
RETURN
ELSE
Allocate memory for a NEW node
DATA(NEW) ← x
LLINK(NEW) ← NULL
RLINK(NEW) ← NULL
IF START = NULL THEN
START ← END ← NEW
ELSE
RLINK(END) ← NEW
LLINK(NEW) ← END
END ← NEW
ENDIF
RLINK(END) ← START
LLINK(START) ← END

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.

Step 1: IF START = NULL THEN


WRITE ‘Circular Double Linked List Empty’
ELSE
TEMP ← START
Repeat WHILE RLINK(TEMP) ≠ START
WRITE DATA(TEMP)
TEMP ← RLINK(TEMP)
EndRepeat
WRITE DATA(TEMP)
ENDIF
Step 2: RETURN

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;
}

You might also like