You are on page 1of 7

Data Structure Control & Systems/ Fourth Class

Lecture 6: Circular Linked Lists

We can change the linear list slightly, making the pointer in the
next member of the last node point back to the first node instead of
containing NULL. Now our list becomes a circular linked list rather than
a linear linked list. We can start at any node in the list and traverse the
entire list.

Circular linked list : A list in which every node has a successor; the
“last” element is succeeded by the “first” element.

We do not need to change any of the declarations in the class


linked queues to make the list circular, rather than linear. After all, the
members in the nodes are the same; only the value of the next member of
the last node has been changed. How does the circular nature of the list
alter the implementations of the list operations? Because an empty
circular list has a NULL pointer, the IsEmpty operation does not change
at all. However, using a circular list requires an obvious change in the
algorithms that traverse the list. We no longer stop when the traversing
pointer becomes NULL. Indeed, unless the list is empty, the pointer
never becomes NULL. Instead , we must look for the external pointer
itself as a stop sign.

H.W
Exercise: Update linked queue to make it a circular queue

41
Data Structure Control & Systems/ Fourth Class

above

42
Data Structure Control & Systems/ Fourth Class

43
Data Structure Control & Systems/ Fourth Class

#include<iostream.h>
#include<assert.h>
typedef int ListElementType;
class List {
public:
List();
void insert(ListElementType elem);
ListElementType Delete(void);
ListElementType DeleteAnyPos(int pos);

bool first(ListElementType elem);


bool next(ListElementType elem);
bool previous(ListElementType &elem);
void printlist(void);

private:
struct NodeType {
ListElementType elem;
NodeType* next;
NodeType * back;
};
NodeType* head;
NodeType* current;

44
Data Structure Control & Systems/ Fourth Class

};
List::List()
{
head=0;
current=0;
}
void List::printlist(void)
{
NodeType *ptr;
assert(head);
ptr=head;
while(ptr !=0)
{
cout<<ptr->elem<<" ";

ptr=ptr->next;
}

}
bool List::previous(ListElementType &elem)
{
assert(current);
if(current->back==0)
return false;
else
{
current=current->back;
elem=current->elem;
return true;
}
}
void List:: insert(ListElementType elem)
{
NodeType *addedNode= new NodeType;
assert(addedNode);
addedNode->elem=elem;
addedNode->next=head;
if(head)
head->back=addedNode;
addedNode->back=0;
head=addedNode;

45
Data Structure Control & Systems/ Fourth Class

ListElementType List::Delete(void)
{
ListElementType x;
NodeType *ptr;
ptr=head;
assert(head);
if(head->next==0)
head=0;
else
{
head=head->next;
head->back=0;
}
x=ptr->elem;
delete ptr;
return x;
}

ListElementType List::DeleteAnyPos(int pos)


{
ListElementType x;
NodeType *ptr;
ptr=head;

assert(head);
int i=0;
while(i<pos )
{ptr=ptr->next;i++;}
if(ptr->next !=NULL) // if the deleted not at the end of list
{
ptr->back->next=ptr->next;
ptr->next->back=ptr->back;
}
else // if deleted node at last of list
{ ptr->back->next=0;
ptr->back=0;
}
x=ptr->elem;
delete ptr;
return x;

46
Data Structure Control & Systems/ Fourth Class

void main()
{
List l;
l.insert(1);
l.insert(2);
l.insert(3);
l.insert(4);
l.printlist();cout<<endl;
l.Delete();
l.printlist();cout<<endl;

l.DeleteAnyPos(3);
l.printlist();
}

47

You might also like