Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

data structure and algoritms lab, Papers of Data Structures and Algorithms

data structure and algoritms lab

Typology: Papers

2024/2025

Uploaded on 04/23/2025

rachit-bansal-2
rachit-bansal-2 🇮🇳

2 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
pf3
pf4
pf5
pf8

Partial preview of the text

Download data structure and algoritms lab and more Papers Data Structures and Algorithms in PDF only on Docsity!

Birla Institute of Technology and Science Pilani, Hyderabad Campus

CS F211: Data Structures and Algorithms

nd

Semester 2024- 25 , Lab Sheet No: 4 (Linked Lists)

General Tips

  • The use of STL is strictly prohibited in this lab. You should not use any inbuilt data-structure/

algorithm to do the tasks listed.

  • Indent your code appropriately and use proper variable names, if not already provided in the

question. Also, use comments wherever necessary.

  • Make sure to debug your code after every task or after every code block to avoid having to debug

your entire file at once in the last minute.

Overview: The goal of this lab is to learn how to implement basic functionalities in a Singly and Doubly linked list. You will be given a piece of code which is incomplete. Based on your learnings in the lecture classes and tutorials, try to complete the code and get the expected output. You should finish your task in the given lab timings.

Program 1 (SinglyLinkedList.cpp attached):

Task 1:

The below search () method is a newly added feature to the GenericSinglyLinkedList implementation discussed in the class. The method basically searches whether there is a node in the list containing the given item. Complete the code highlighted as Task 1. Try to utilize the given isEqual() method to compare two elements of generic type. You should get the output as shown below: SNode *SLinkedList::search(const E &e) { for (SNode *curr = head; curr != NULL; curr = curr->next) { // Complete the missing code } return NULL; // not found } bool SLinkedList::isEqual(const E &a, const E &b) { return ((string)a).compare((string)b) == 0; }

Output: Task 2: Newly added method swap () swaps the nodes containing the two given elements in the singly linked list. Again, you are given an incomplete code. You are free to refer to lecture slides to revise the concept of interchanging node pointers and the technique to swap two nodes. Do a thorough understanding of the code before you jump to coding. You should get the output as shown below: void SLinkedList::swap(const E &a, const E &b) { // first search for element 'a' in the list. SNode *node1 = search(a); if (node1 == NULL) return; // next, search for element 'b' SNode *node2 = search(b);

Output: Task 3: Add a new method reverse () which will reverse the nodes of the linked list. Note that the reversal of linked list should happen in-place i.e. using constant space complexity. Complete the code given below. Hints are given in as comments in the code. template void SLinkedList::reverse() { if (head == NULL || head->next == NULL) return; SNode *curr = head; SNode *prev=NULL; while (curr != NULL) { // Store the next node using a temporary pointer // Update the next pointer of current node to point to previous node // Update the curr and prev pointers in each iteration }

// Update the head pointer of the list } Output: Program 2 (DoublyLinkedList.cpp attached): Task 4: Add a new method isPalindrome() to the DoublyLinkedList class. The method will check if the list is a palindrome or not. A palindrome list is a type of linked list where the elements read the same forwards as they do backwards. Complete the code given below: bool DLinkedList::isPalindrome() { // Complete the initialization of begin and end pointers. // begin is the first node and end is the last node of the list. DNode *begin; DNode *end; while (begin != end) { // Check if the values of begin and end pointers are different. if (begin->next == end) break; //Update the begin and end pointers for the next iteration. }

Task 6 : Using pointer technique, find out if the doubly linked list has odd number of nodes or even number of nodes. Print “Odd” if number of nodes are odd, and “Even” otherwise. (hint: If the fast pointer reaches NULL, it is even, else if it reaches the last node then it is odd.)

Program 3 (Circular Linked List: CircularLL.cpp is given for your reference): Implement a scheduling algorithm which you will read in Operating Systems course later. Round Robin is a CPU scheduling algorithm where each process gets a fixed amount of time (time slice) to execute. If a process doesn't complete within its time slice, it's pre-empted and added to the end of the ready queue. This process repeats until all processes are completed. Implement this algorithm using a Circular linked list as discussed in the class. You may consider the below assumptions:  Each process can be represented as a node in the circular linked list.  The "next" pointer of each node points to the next process in the ready queue.  The CPU scheduler can easily traverse the list in a circular fashion, giving each process its time slice.  When a process completes or is pre-empted, it's simply removed from its current position and added to the end of the list.  Take a set of processes with their CPU burst (time required with CPU) and a time slice (or time quantum) and produce an order of their completion as output. --------------------------