Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment -1.3
Student Name: Dhruv UID: 21BCS3484
Branch: BE-CSE Section/Group: 802-B
Semester: 5th Date of Performance: 22-08-23
Subject Name: Advance Programming Lab Subject Code: 21CSP-314

1. Aim: Demonstrate the concept of linked list.

2. Objective:-You’re given the pointer to the head nodes of two linked lists. Compare
the data in the nodes of the linked lists to check if they are equal. If all data attributes are
equal and the lists are the same length, return 1. Otherwise, return 0.

Example :-

The two lists have equal data attributes for the first 3 nodes.llist2 is longer,
though, so the lists are not equal. Return 0.
program and output:
1. Compare List:-
bool compare_lists(SinglyLinkedListNode* head1, SinglyLinkedListNode*
head2) {
int len1 = 0, len2 = 0;
while (head1 != NULL && head2 != NULL) {
if (head1 -> data == head2 -> data) {
head1 = head1 -> next;
head2 = head2 -> next;
len1++;
len2++;
}
else {
return false;
}
1
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
} Source
while (head1 != NULL) {
len1++;
head1 = head1 -> next;
}
while (head2 != NULL) {
len2++;
head2 = head2 -> next;
}

return len1 == len2;


}

Output:-

Task 2: Given a reference to the head of a doubly-linked list and an integer,


data, create a new Doubly Linked List Node object having data value data
and insert it at the proper location to maintain the sort.

2
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

1. Inserting into Doubly Linked List:-


DoublyLinkedListNode* sortedInsert(DoublyLinkedListNode* head, int
data) {
DoublyLinkedListNode* insert = new DoublyLinkedListNode(data);
DoublyLinkedListNode* temp = head;

if (temp == NULL){
head = insert;
}
else if(temp->data > data){
insert->next = temp;
temp->prev = insert;
head = insert;
}
else{
while((temp->next!=NULL)&&(temp->next->data < data)){
temp=temp->next;
}
insert->prev = temp;
if (temp->next!=NULL){
insert->next = temp->next;
temp->next->prev = insert;
}
temp->next = insert;
}
return head;
}

3
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:-

You might also like