Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Institute of space and technology

KICSIT, KAHUTA CAMPUS (Y- CROSS)

DEPARTMENT OF COMPUTER SCIENCE

Assignment

3
Data Structures & Algorithms
“LAB”

Semester: BSCS 3
Submitted to:
Mam Majda Tareen
Submitted From:
Maria Kabeer Satti
Roll No: 212201007

DATE: 05-December-2022

Question no 1:
Write a complete algorithm for insertion in the circular linked list’s start , middle and end.
Explain its complexity?

Answer:

Doubly Circular Link List

Algorithms:

Insertion at start

Step 1: node *temp=new node;

Step 2: temp->data=val;

Step 3: if head=NULL

Step 4: head=temp; tail=temp; temp->next=temp; temp->prev=temp;

Step 5: else head->prev=temp; temp->next=head; temp->prev=tail; tail->next=temp;


head=temp;

Step 6: end

Pseudocode
void is()

int d;

cout<<"Enter data : ";cin>>d;

node *temp=new node;

temp->data=d;

if(head==NULL)

head=temp;

tail=temp;

temp->next=temp;

temp->prev=temp;
}

else

head->prev=temp;

temp->next=head;

temp->prev=tail;

tail->next=temp;

head=temp;

Insertion at end

Step 1: node *temp=new node;

Step 2: temp->data=val;

Step 3: if head=NULL

Step 4: head=temp; tail=temp; temp->next=temp; temp->prev=temp;

Step 5: else tem->prev=tail; temp->next=head; tail->next=temp; head->prev=temp; tail=temp;

Step 6: end

Pseudocode

void ie()

int d;

cout<<"Enter data : ";cin>>d;

node *temp=new node;

temp->data=d;
if(head==NULL)

head=temp;

tail=temp;

temp->next=temp;

temp->prev=temp;

else

temp->prev=tail;

temp->next=head;

tail->next=temp;

head->prev=temp;

tail=temp;

Insertion at specific position

Step 1: node *temp=new node; node *ptr=new node; node *cptr=new node;

Step 2: ptr=head

Step 3: temp->data=val;

Step 4: Enter position to insert data i.e pos

Step 5: while(pos>0) pos--; ptr=ptr->next;

Step 6: Repeat step 5 until the condition is true

Step 7: cptr=ptr->next; temp->next=ptr->next; temp->prev=ptr; ptr->next=temp; cptr-


>prev=temp;

Step 8: End
Pseudocode
void im()

node *temp=new node;

node *ptr=new node;

node *cptr=new node;

ptr=head;

int d,pos;

cout<<"ENTER DATA : ";cin>>d;

temp->data=d;

cout<<"ENTER POSITION TO INSERT DATA : ";cin>>pos;

if(pos<0)

cout<<"<<<<<NOT A VALID POSITION >>>>>"<<endl;

else if(pos==0)

is();

else{

while(pos>2)

pos--;

ptr=ptr->next;

}
cptr=ptr->next;

temp->next=ptr->next;

temp->prev=ptr;

ptr->next=temp;

cptr->prev=temp;

Time complexities

Insertion at start O(1)


Insertion at end O(1)
Insertion at specified position O(n)

Space complexities

Insertion at start O(1)


Insertion at end O(1)
Insertion at specified position O(1)

Algorithms:

Deletion at start

Step 1: node *temp=new node;

Step 2: temp=head;

Step 3: head=head->next;

Step 4: head->prev=tail;

Step 5: tail->next=head;

Step 6: delete(temp);

Step 7: end

Pseudocode
void ds()

node *temp=new node;

temp=head;

head=head->next;

head->prev=tail;

tail->next=head;

cout<<"DELETED DATA : "<<temp->data<<endl;

delete(temp);

Deletion at end

Step 1: node *temp=new node;

Step 2: temp=tail;

Step 3: tail=tail->prev;

Step 4: head->prev=tail;

Step 5: tail->next=head;

Step 6: delete(temp);

Step 7: end

Pseudocode

void de()

node *temp=new node;

temp=tail;
tail=tail->prev;

head->prev=tail;

tail->next=head;

cout<<"DELETED DATA : "<<temp->data<<endl;

delete(temp);

Deletion at specific position

Step 1: node *temp=new node; node *ptr=new node; node *cptr=new node;

Step 2: ptr=head

Step 3: Enter position to delete data i.e pos

Step 4: while(pos>=0) pos--; ptr=ptr->next;

Step 5: Repeat step 5 until the condition is true

Step 6: temp=ptr; cptr=ptr->next; ptr=ptr->prev; ptr->next=cptr; cptr->prev=ptr;

Step 7: delete(temp);

Step 8: End

Pseudocode
void dm()

node *temp=new node;

node *ptr=new node;

node *cptr=new node;

ptr=head;

int pos;

cout<<"ENTER POSITION TO DELETE DATA : ";cin>>pos;


if(pos<0)

cout<<"<<<<<NOT A VALID POSITION >>>>>"<<endl;

else if(pos==0)

ds();

else{

while(pos>0)

pos--;

ptr=ptr->next;

temp=ptr;

cptr=ptr->next;

ptr=ptr->prev;

ptr->next=cptr;

cptr->prev=ptr;

delete(temp);

Time complexities
Deletion at start O(1)
Deletion at end O(1)
Deletion at specified position O(n)

Space complexities

Deletion at start O(1)


Deletion at end O(1)
Deletion at specified position O(1)

You might also like