Skip to content

Commit

Permalink
Merge pull request #31 from SakshamSharma07/main
Browse files Browse the repository at this point in the history
Spliting a circular linked list in 2 equal halves
  • Loading branch information
TechVine authored Oct 13, 2022
2 parents ef2ee35 + 0360f21 commit 79139d0
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Python/array/maxMin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Function to find minimum and maximum position in list
def maxminposition(A, n):
# inbuilt function to find the position of minimum
minposition = A.index(min(A))
# inbuilt function to find the position of maximum
maxposition = A.index(max(A))
print ("The maximum is at position::", maxposition + 1)
print ("The minimum is at position::", minposition + 1)
# Driver code
A=list()
n=int(input("Enter the size of the List ::"))
print("Enter the Element ::")
for i in range(int(n)):
k=int(input(""))
A.append(k)
maxminposition(A,n)

# Output
# Enter the size of the List ::4
# Enter the Element::
# 12
# 34
# 1
# 66
# The maximum is at position:: 4
# The minimum is at position:: 3
122 changes: 122 additions & 0 deletions Splitting_Cirular_List.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Program to split a circular linked list
// into two halves
#include <bits/stdc++.h>
using namespace std;

/* structure for a node */
class Node
{
public:
int data;
Node *next;
};

void splitList(Node *head, Node **head1_ref,
Node **head2_ref)
{
Node *slow_ptr = head;
Node *fast_ptr = head;

if(head == NULL)
return;

while(fast_ptr->next != head &&
fast_ptr->next->next != head)
{
fast_ptr = fast_ptr->next->next;
slow_ptr = slow_ptr->next;
}

/* If there are even elements in list
then move fast_ptr */
if(fast_ptr->next->next == head)
fast_ptr = fast_ptr->next;

/* Set the head pointer of first half */
*head1_ref = head;

/* Set the head pointer of second half */
if(head->next != head)
*head2_ref = slow_ptr->next;

/* Make second half circular */
fast_ptr->next = slow_ptr->next;

/* Make first half circular */
slow_ptr->next = head;
}

void push(Node **head_ref, int data)
{
Node *ptr1 = new Node();
Node *temp = *head_ref;
ptr1->data = data;
ptr1->next = *head_ref;

/* If linked list is not NULL then
set the next of last node */
if(*head_ref != NULL)
{
while(temp->next != *head_ref)
temp = temp->next;
temp->next = ptr1;
}
else
ptr1->next = ptr1; /*For the first node */

*head_ref = ptr1;
}

/* Function to print nodes in
a given Circular linked list */
void printList(Node *head)
{
Node *temp = head;
if(head != NULL)
{
cout << endl;
do {
cout << temp->data << " ";
temp = temp->next;
} while(temp != head);
}
}

// Driver Code
int main()
{
int list_size, i;

/* Initialize lists as empty */
Node *head = NULL;
Node *head1 = NULL;
Node *head2 = NULL;

/* Created linked list will be 12->56->2->11 */
push(&head, 12);
push(&head, 56);
push(&head, 2);
push(&head, 11);

cout << "Original Circular Linked List";
printList(head);

/* Split the list */
splitList(head, &head1, &head2);

cout << "\nFirst Circular Linked List";
printList(head1);

cout << "\nSecond Circular Linked List";
printList(head2);
return 0;
}

//output:

// Original Circular Linked List
// 11 2 56 12
// First Circular Linked List
// 11 2
// Second Circular Linked List
// 56 12

0 comments on commit 79139d0

Please sign in to comment.