Professional Documents
Culture Documents
20BCS7358 CC Exp.3
20BCS7358 CC Exp.3
20BCS7358 CC Exp.3
Experiment 3
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
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
Returns
• int: return 1 if the lists are equal, or 0 otherwise
Input Format
• 1<= t <= 10
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.
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:
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:
---------------------------------------------------------------------------------------------------------------------------------------
PROBLEM 2
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
-> 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
Function Description
Returns
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
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 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.
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.