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

Experiment Title: 3 (Linked List)

Student Name: Ayush Tiwari UID: 20BCS7611


Branch: BE-CSE Section/Group: 20BCS_WM_905-B
Semester: 5th Date of Performance: 17-08-2022
Subject Name: Competitive Coding I Subject Code: 21CSP-314

1. Aim/Overview of the practical: Given a reference to the head of a doubly-linked list and an
integer, data, create a new DoublyLinkedListNode object having data value data and insert it at
the proper location to maintain the sort.

Example:

Head refers to the list


Data = 3

Return a reference to the new list:


2. Task to be done/ Which logistics used: We can traverse the list from left to right and find a
suitable position for the new node to insert. It is useful to first examine the different positions
that the new node could end up in, and then we can analyse each of those cases independently.

3. Algorithm/Flowchart (For programming-based labs):


 Create a new Node and set the data of new Node as Data.
 Now update the next and previous pointer of new Node.
 If the list is empty, set next and previous pointer as null and return it.
 If the node’s position is in the beginning of the list, set the next pointer of the new node
to point to the current head. Update the prev pointer of the current head to point to the
new node and return the head node.
 Else, find the position of the new node using a temporary current node. Update the next
pointer of current to point to this new node and then return the head node.
4. Steps for experiment/practical/Code:

#include <bits/stdc++.h>

using namespace std;

class DoublyLinkedListNode {
public:
int data;
DoublyLinkedListNode *next;
DoublyLinkedListNode *prev;

DoublyLinkedListNode(int node_data) {
this->data = node_data;
this->next = nullptr;
this->prev = nullptr;
}
};

class DoublyLinkedList {
public:
DoublyLinkedListNode *head;
DoublyLinkedListNode *tail;

DoublyLinkedList() {
this->head = nullptr;
this->tail = nullptr;
}

void insert_node(int node_data) {


DoublyLinkedListNode* node = new DoublyLinkedListNode(node_data);

if (!this->head) {
this->head = node;
} else {
this->tail->next = node;
node->prev = this->tail;
}
this->tail = node;
}
};

void print_doubly_linked_list(DoublyLinkedListNode* node, string sep, ofstream& fout) {


while (node) {
fout << node->data;

node = node->next;

if (node) {
fout << sep;
}
}
}

void free_doubly_linked_list(DoublyLinkedListNode* node) {


while (node) {
DoublyLinkedListNode* temp = node;
node = node->next;

free(temp);
}
}

/*
* Complete the 'sortedInsert' function below.
*
* The function is expected to return an INTEGER_DOUBLY_LINKED_LIST.
* The function accepts following parameters:
* 1. INTEGER_DOUBLY_LINKED_LIST llist
* 2. INTEGER data
*/

/*
* For your reference:
*
* DoublyLinkedListNode {
* int data;
* DoublyLinkedListNode* next;
* DoublyLinkedListNode* prev;
* };
*
*/

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


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

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

int t;
cin >> t;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

for (int t_itr = 0; t_itr < t; t_itr++) {


DoublyLinkedList* llist = new DoublyLinkedList();

int llist_count;
cin >> llist_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

for (int i = 0; i < llist_count; i++) {


int llist_item;
cin >> llist_item;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

llist->insert_node(llist_item);
}

int data;
cin >> data;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

DoublyLinkedListNode* llist1 = sortedInsert(llist->head, data);

print_doubly_linked_list(llist1, " ", fout);


fout << "\n";

free_doubly_linked_list(llist1);
}

fout.close();

return 0;
}
5. Observations/Discussions/ Complexity Analysis:

The complexity of the code which is used to solve the above problem statement is:

 Time Complexity: O(N)


 Space Complexity: O(1)
6. Result/Output/Writing Summary:

Output

Learning outcomes (What I have learnt):

1. Learnt about Doubly Linked List.

2. Learnt about how to traverse inside Linked List.

3. Learnt how to insert in a sorted doubly linked list with given head node.

4. Learnt how to analyze time complexity.

5. Learnt how to build programing logic.

You might also like