You are on page 1of 11

EC-233 Data Structures and Algorithms

LAB REPORT#05
  
           

  Submitted by:
Mahnoor inam
18-ce-009

Department of Computer Engineering


HITEC University Taxila

Lab.Instructor
Tauqeer Anjum
Experiment # 05

Operations on Circular Linked List

Objectives:

The objective of this lab is to understand the various operations on Circular linked list in C++.

• Creation of a Circular link list

• Displaying a Circular link list

• Insertion

• Deletion

Software Tools:

• Microsoft Visual Studio

Theory:

A circular linked list is a variation of the linked list. It is a linked list whose nodes are connected
in such a way that it forms a circle.In the circular linked list, the next pointer of the last node is
not set to null but it contains the address of the first node thus forming a circle.

Applications of Circular Linked List


• Real-time application of circular linked list can be a multi-programming operating
system wherein it schedules multiple programs. Each program is given a dedicated timestamp
to execute after which the resources are passed to another program. This goes on continuously
in a cycle. This representation can be efficiently achieved using a circular linked list.

• Games that are played with multiple players can also be represented using a circular
linked list in which each player is a node that is given a chance to play.

• We can use a circular linked list to represent a circular queue. We can remove the two
pointers front and rear that is used for the queue. Instead, we can use only one pointer.
Task
Perform the following operations in circular doubly linked list
 Insertion at the Start
 Insertion at the Middle
 Insertion at the End
 Deletion at the start
 Deletion at the Middle
 Deletion at the end
 Display linked list
 Search item in the list
Code:
#include<iostream>

using namespace std;

struct node

int data;

node *next;

node *prev;

};

class list

private:
node *head, *tail;

public:

list();

head = NULL;

tail = NULL;

void insertend(int value)

{ node *temp = new node;

temp->data = value;

if(head == NULL)

head = temp;

tail = temp;

temp = NULL;

else

tail->next= temp;

temp->prev=tail;

tail = temp;

tail->next=head;

head->prev+tail;

}
void display()

bode *current= new node;

current = head;

while(current->next !=tail)

cout<<current->data"t";

current = current->prev;

void insertstart(int value)

node *temp = new node;

temp->data = value;

temp-> = head;

heas->prev= temp;

head=temp;

tail->next=head;

head->prev->tail;

void insertpos(int pos, int value)

node *pre = new node;

node *cur = new node;

node *temp = new node;


cur = head;

for(int i=1;i<pos;i++)

pre = cur;

cur = cur->next;

temp->data = value;

pre->next = temp;

temp->prev=pre;

temp->next=cur;

cur->prev=temp;

void deletestart()

node *temp = new node;

temp = head;

head = head->next;

temp->next=NULL;

temp->prev=NULL;

delete temp;

head->prev=tail;

tail->next=head;

void deleteend()

{
node *current = new node;

current=head;

while(current->next!=tail)

current=current->next;

tail->next=NULL;

tail->prev=NULL;

delete tail;

current->next=head;

head->prev=current;

tail=current;

void deletepos(int pos)

node *current = new node;

node *previous = new node;

current = head;

for(int i=1,i<pos;i++)

previous = current;

current = current->next;

previous->next = current->next;

current->next->prev=previous;
current->next-NULL;

current->prev=NULL;

delete current;

void searchitem(int B)

int index =1;

node *current= new node;

current = head;

while(current->next !=rail)

if(current->data==B)

cout<<"item found at position is "<<index<<endl;

index++;

current = current->next;

cout<<"item not found in list "<<endl;

~list()

cout << "\n\n delete end linklist";

node *nodeptr, *nextnode;

nodeptr = head;
while (nodeptr !=NULL)

nextnode = nodeptr->next;

delete nodeptr;

nodeptr = nextnode;

cout << "\n dlete";

};

int main()

list obj;

obj.insertedend(1);

obj.insertedend(2);

obj.insertedend(3);

obj.insertedend(4);

obj.insertedend(5);

cout << "\ninserting at end: \"n;

obj.display();

cout << endl;

obj.insertedstart(7);

obj.insertedstart(8);

cout << "\ninserting at start: \n";

obj.display();

cout << endl;


obj,insertpos(12,15);

cout << "\ninserting at position 12: \n";

obj.display();

cout << endl << endl;

obj.deletestart();

cout << "\ndelete at start: \n";

obj.display();

cout << endl;

obj.deleteend();

cout << "\ndelete at end: \n";

obj.display();

cout << endl;

obj.deletepos(12);

cout << "\ndelete at position 3: \n";

obj.display();

cout << endl;

cout<<"forward display:\n";

obj.display();

cout<<endl;

cout<<"backward display:\n";

obj.displayrev();
cout<<endl;

int n;

cout<<"enter item to search:";

cin>>n;

obj-searchitem(n);

return 0;

Conclusion:
In this lab I learned about the circular linklist and its implementation.

END

You might also like