Professional Documents
Culture Documents
Assignment DSA
Assignment DSA
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:
Algorithms:
Insertion at start
Step 2: temp->data=val;
Step 3: if head=NULL
Step 6: end
Pseudocode
void is()
int d;
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 2: temp->data=val;
Step 3: if head=NULL
Step 6: end
Pseudocode
void ie()
int d;
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;
Step 1: node *temp=new node; node *ptr=new node; node *cptr=new node;
Step 2: ptr=head
Step 3: temp->data=val;
Step 8: End
Pseudocode
void im()
ptr=head;
int d,pos;
temp->data=d;
if(pos<0)
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
Space complexities
Algorithms:
Deletion at start
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()
temp=head;
head=head->next;
head->prev=tail;
tail->next=head;
delete(temp);
Deletion at end
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()
temp=tail;
tail=tail->prev;
head->prev=tail;
tail->next=head;
delete(temp);
Step 1: node *temp=new node; node *ptr=new node; node *cptr=new node;
Step 2: ptr=head
Step 7: delete(temp);
Step 8: End
Pseudocode
void dm()
ptr=head;
int pos;
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