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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment - 1.3
Student Name: Anuj Anand UID: 22BCS80050
Branch: General CSE Section/Group: 607 - B
Semester: 5th Date of Performance: 21/08/2023
Subject Name: Advance Programming Subject Code:21CSP-314

1. Aim
Demonstrate the concept of Linked Lists.

Q1. Compare two linked lists


Compare the data in the nodes of the linked lists to check if they are equal . If all the
data attributes are equal and the lists are the same length return 1. Otherwise, return 0.

CODE :

bool compare_lists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2)


{ struct SinglyLinkedListNode *t1;
struct SinglyLinkedListNode *t2;
t1=head1;t2 = head2;
if(t1==NULL && t2==NULL)
return 1;
if(t1 != NULL && t2 == NULL)
return 0;
if(t1 == NULL && t2 != NULL)
return 0; else
{
while(t1->next != NULL && t2->next != NULL)
{
if(t1->data == t2->data)
{
t1 = t1->next;
t2 = t2->next; }
else return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

if(t1->next == NULL && t2->next == NULL)


return 1;
else return 0;
}

OUTPUT

Q2. Reverse a Doubly linked list


Given the pointer to the head node of a doubly linked list, reverse the order of the nodes
in place. That is, change the next and prev pointers of the nodes so that the direction of the
list is reversed. Return a reference to the head node of the reversed list.

CODE

DoublyLinkedListNode* reverse(DoublyLinkedListNode* head) {


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

struct DoublyLinkedListNode *prev,*curr,*next;


curr=head; prev=NULL; while(curr)
{
next=curr->next; curr-
>next=prev; curr->prev=next;
if(next==NULL)break;
prev=curr;curr=next;
}
curr->prev=NULL;return curr;

OUTPUT

Q3. Inserting a Node Into a Sorted Doubly Linked List


Create a new DoublyLinkedListNode object having data value data and insert it at the
proper location to maintain the sort.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

CODE

DoublyLinkedListNode* sortedInsert(DoublyLinkedListNode* head, int data) {

DoublyLinkedListNode *New = create_doubly_linked_list_node(data);


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

if (temp->next != NULL)
{
DoublyLinkedListNode *next = temp->next;
next->prev = New; New->next = next;
}
else
New->next = NULL; temp-

>next = New New->prev = temp;


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

}
return head;
}

OUTPUT

COMPLEXITY:

• For insertion in the linked list, the time complexity is O(1) if done on the head,
O(N) if done at any other location, as we need to reach that location by traversing
the linked list.

• For deletion, the time complexity is O(1), if done on the head, O(N), if done at
any other location, as we need to reach that location by traversing the linked list.

• For searching and accessing any elements, the involved time complexity is O(N)

You might also like