20BCS7358 CC Exp.3

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

DOMAIN: LINKED LIST

Experiment 3

Student Name: ROHIT MOHAN Branch: BE-CSE


UID: 20BCS7358 Section/Group: 604-B
Semester: 5th Subject Code: CSP-312
Subject Name: Competitive Coding

PROBLEM1
1. Aim/Overview of the practical:
To compare two linked lists.

2. Task to be done:
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

Llist1 = 1 -> 2 -> 3 -> NULL

Llist2 = 1 -> 2 -> 3 -> 4-> NULL

The two lists have equal data attributes for the first 3 nodes. llist2 is longer, though, so the lists are not equal.
Return 0.

Function Description

Complete the compare_listsfunction in the editor below.

compare_listshas the following parameters:

• SinglyLinkedListNode llist1: a reference to the head of a list

• SinglyLinkedListNode llist2: a reference to the head of a list

Returns
• int: return 1 if the lists are equal, or 0 otherwise

Input Format

The first line contains an integer t, the number of test cases.

Each of the test cases has the following format:


The first line contains an integer n, the number of nodes in the first linked list. Each of the next n lines contains
an integer, each a value for a data attribute. The next line contains an integer m, the number of nodes in the
second linked list. Each of the next m lines contains an integer, each a value for a data attribute. Constraints

• 1<= t <= 10

• 1 <= n, m <= 1000

• 1 <= llist1[i],llist2[i] <= 1000

Output Format

Compare the two linked lists and return 1 if the lists are equal. Otherwise, return 0. Do NOT print anything to
stdout / console.

The output is handled by the code in the editor and it is as follows:

For each test case, in a new line, print 1 if the two lists are equal, else print 0.

3. Algorithm:
1. Start the code.
2. Declare the function compare_list with two linked list as arguments.
3. Declare an integer flag and use a while loop till head1 and head 2 are not equal.
4. Use if condition and move to next element of linked list.
5. Use if condition (i.e. flag ==1 or any of two linked list head is equal to null) to return that if linked list are
equal or not.
6. End the code.

4. Code:

bool compare_lists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2)


{int flag=0;

while(head1 != NULL && head2 != NULL)


{ if(head1->data != head2->data)
{ flag=1; break; }
head1 = head1->next;
head2 = head2->next;
}

if(flag==1 || head1 != NULL || head2 != NULL) return 0;


else return
1;

5. Observation / Discussion:

In this experiment we have to write a compare_function to compare the elements of linkedlist using pointer
head. Comparing two element of different linked list and returning whther they are equal or not.
6. Output:

Learning outcomes (What I have learnt):


1.) Learnt concept of linked list and its implementation. 2.)
Learnt to code with predefined code and complete it.
3.) Learnt to code with given constraint.

---------------------------------------------------------------------------------------------------------------------------------------

PROBLEM 2

1. Aim/Overview of the practical:


Cycle Detection

2. Task to be done:

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of

a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0.


Example head refers to the list of nodes 1 -> 2 -> 3

-> NULL

The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refers to the list

of nodes 1 -> 2 -> 3 -> 1 -> NULL

There is a cycle where node 3 points back to node 1, so return 1.

Function Description

Complete the has_cyclefunction in the editor below.

It has the following parameter:

 SinglyLinkedListNode pointer head: a reference to the head of the list

Returns

 int: 1 if there is a cycle or if there is not.

Note: If the list is empty, head will be null.

Input Format

The code stub reads from stdin and passes the appropriate argument to your function. The custom test cases

format will not be described for this question due to its complexity. Expand the section for the main function

and review the code if you would like to figure out how to create a custom case.

Constraints

0 <= list size <= 1000

Sample Input

References to each of the following linked lists are passed as arguments to your function:
Sample Output

3. Algorithm:
Step 1: Declare the has_cycle function with head pointer in argument.

Step 2: Declare pointer slowp which is equal to head.

Step 3: Declare pointer fastp which is equal to head.

Step 4: Use while loop to run statement until given condition is true.

Step 5: Use if condition (i.e. if slowp = = fastp) to return if cycle is detected or not.

Step 6: End the code.

4. Code:
int has_cycle(SinglyLinkedListNode* head)
{
SinglyLinkedListNode *slowp = head, *fastp = head;
while (slowp&&fastp&&fastp->next)
{ slowp = slowp->next; fastp =
fastp->next->next; if (slowp
== fastp)
{ return 1;
}}
return 0;
}

5. Observation / Discussion:

In this experiment, we have used the slowp and fastp pointer to detect if cycle is present or not. If slowp is equal
to fastp then the cycle is detected otherwise not.

6. Output:
Learning outcomes (What I have learnt):
1.) Learnt concept of linked list and its implementation. 2.)
Learnt to code with predefined code and complete it.
3.) Learnt to code with given constraint.

You might also like