0% found this document useful (0 votes)
14 views18 pages

Lists

The document contains code definitions for various operations on linked lists including slicing a list, checking if two lists are equal, finding the union/intersection of lists, finding the maximum/minimum value in a list, sorting lists, and other common list operations. The methods are generic and can operate on linked lists containing different data types.

Uploaded by

nivovadia0101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views18 pages

Lists

The document contains code definitions for various operations on linked lists including slicing a list, checking if two lists are equal, finding the union/intersection of lists, finding the maximum/minimum value in a list, sorting lists, and other common list operations. The methods are generic and can operate on linked lists containing different data types.

Uploaded by

nivovadia0101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

public static Node<char> Slice(Node<char> head, int start, int end)

{ // this methos changes the original list

for (int i = 0; i < start; i++)

head = head.GetNext();

Node<char> p = head;

for (int i = start; i < end; i++)

p = p.GetNext();

p.SetNext(null);

return head;

public static Node<T> Slice<T>(Node<T> head, int start, int end)

{ // this methos changes the original list

for (int i = 0; i < start; i++)

head = head.GetNext();

Node<T> p = head;

for (int i = start; i < end; i++)

p = p.GetNext();

p.SetNext(null);

return head;

public static bool Equal<T>(Node<T> l1, Node<T> l2)

while (l1 != null && l2 != null && l1.GetValue().Equals(l2.GetValue()))


{

l1 = l1.GetNext();

l2 = l2.GetNext();

if (l1 == null && l2 == null)

return true;

return false;

public static Node<T> Union<T>(Node<T> l1, Node<T> l2)

Node<T> p = l1;

while (p.HasNext())

p = p.GetNext();

p.SetNext(l2);

return l1;

public static Node<T> Intersect<T>(Node<T> l1, Node<T> l2)

Node<T> newList = null;

Node<T> l2_copy = l2;

while (l1 != null)

while (l2 != null)

if (l1.GetValue().Equals(l2.GetValue()))

newList = new Node<T>(l1.GetValue(), newList);


break;

l2 = l2.GetNext();

l1 = l1.GetNext();

l2 = l2_copy;

return newList;

public static Node<T> Intersect_Unique<T>(Node<T> l1, Node<T> l2)

Node<T> newList = null;

Node<T> l1_copy = l1;

Node<T> l2_copy = l2;

while (l1 != null)

while (l2 != null)

if (l1.GetValue().Equals(l2.GetValue()))

newList = new Node<T>(l1.GetValue(), newList);

int index = Index(l1_copy, l1.GetValue());

l1_copy = Dell(l1_copy, index);

index = Index(l2_copy, l2.GetValue());

l2_copy = Dell(l2_copy, index);

break;

l2 = l2.GetNext();

l1 = l1.GetNext();

l2 = l2_copy;
}

return newList;

public static void Unique<T>(Node<T> head)

Node<T> p;

Node<T> pre = head;

while (head.HasNext())

p = head.GetNext();

while (p != null)

if (p.GetValue().Equals(head.GetValue()))

pre.SetNext(p.GetNext()); // delete p from list

p = p.GetNext();

else

pre = pre.GetNext();

p = p.GetNext();

head = head.GetNext();

pre = head;

public static void Unique(Node<int> head)

Node<int> p;

Node<int> pre = head;


while (head.HasNext())

p = head.GetNext();

while (p != null)

if (p.GetValue() == head.GetValue())

pre.SetNext(p.GetNext()); // delete p from list

p = p.GetNext();

else

pre = pre.GetNext();

p = p.GetNext();

head = head.GetNext();

pre = head;

public static Node<T> Max<T>(Node<T> head) where T : IComparable<T>

Node<T> max = head;

Node<T> p;

while (head.HasNext())

p = head.GetNext();

if (max.GetValue().CompareTo(p.GetValue()) < 0)

max = p;

}
head = head.GetNext();

return max;

public static Node<int> Max(Node<int> head)

Node<int> max = head;

Node<int> p;

while (head.HasNext())

p = head.GetNext();

if (max.GetValue() < p.GetValue())

max = p;

head = head.GetNext();

return max;

public static Node<T> UnionSort<T>(Node<T> l1, Node<T> l2) where T : IComparable<T>

Node<T> newHead;

if (l1.GetValue().CompareTo(l2.GetValue()) > 0)

newHead = l1;

l1 = l1.GetNext();

else

newHead = l2;

l2 = l2.GetNext();
}

Node<T> tail = newHead;

while (l1 != null && l2 != null)

if (l1.GetValue().CompareTo(l2.GetValue()) > 0)

tail.SetNext(l1);

l1 = l1.GetNext();

tail = tail.GetNext();

else

tail.SetNext(l2);

l2 = l2.GetNext();

tail = tail.GetNext();

if (l1 != null && l2 == null)

tail.SetNext(l1);

else if (l2 != null)

tail.SetNext(l2);

return newHead;

public static Node<double> UnionSort(Node<double> l1, Node<double> l2)

Node<double> newHead;
if (l1.GetValue() > l2.GetValue())

newHead = l1;

l1 = l1.GetNext();

else

newHead = l2;

l2 = l2.GetNext();

Node<double> tail = newHead;

while (l1 != null && l2 != null)

if (l1.GetValue() > l2.GetValue())

tail.SetNext(l1);

l1 = l1.GetNext();

tail = tail.GetNext();

else

tail.SetNext(l2);

l2 = l2.GetNext();

tail = tail.GetNext();

if (l1 != null && l2 == null)

tail.SetNext(l1);

}
else if (l2 != null)

tail.SetNext(l2);

return newHead;

public static int commonValue(Node<int> head)

int[] arr = new int[101];

while (head != null)

arr[head.GetValue()]++;

head = head.GetNext();

int max = arr[0];

int index = 0;

for (int i = 0; i < arr.Length; i++)

if (max < arr[i])

max = arr[i];

index = i;

return index;

public static bool Consistent<T>(Node<T> head) where T : IComparable<T>

Node<T> p1 = head;

Node<T> p2 = head.GetNext();

int compare1 = p1.GetValue().CompareTo(p2.GetValue());


if (compare1 == 0)

return false;

while (p2.HasNext())

p2 = p2.GetNext();

p1 = p1.GetNext();

int compare2 = p1.GetValue().CompareTo(p2.GetValue());

if (compare1 * compare2 <= 0)

return false;

return true;

public static bool Consistent(Node<int> head)

Node<int> p1 = head;

Node<int> p2 = head.GetNext();

int compare1 = p1.GetValue().CompareTo(p2.GetValue());

if (compare1 == 0)

return false;

while (p2.HasNext())

p2 = p2.GetNext();

p1 = p1.GetNext();

int compare2 = p1.GetValue().CompareTo(p2.GetValue());

if (compare1 * compare2 <= 0)

return false;

return true;

public static Node<T> Zipper<T>(Node<T> l1, Node<T> l2)

{
Node<T> head = l1;

Node<T> tail = l1;

l1 = l1.GetNext();

while (l1 != null && l2 != null)

tail.SetNext(l2);

tail = l2;

tail.SetNext(l1);

tail = l1;

l2 = l2.GetNext();

l1 = l1.GetNext();

if (l1 != null)

tail.SetNext(l1);

else if (l2 != null)

tail.SetNext(l2);

return head;

public static Node<int> SumOfsubList(Node<int> L)

Node<int> head = new Node<int>(L.GetValue());

Node<int> tail = head;

int pre = L.GetValue() - 1;

L = L.GetNext();

while (L != null)

if (L.GetValue() > pre)

tail.SetValue(tail.GetValue() + L.GetValue());

else
{

tail.SetNext(new Node<int>(L.GetValue()));

tail = tail.GetNext();

pre = L.GetValue();

L = L.GetNext();

return head;

public static bool isTripleList(Node<int> L)

if (L == null)

return false;

int count = 0;

Node<int> p1 = L;

while (p1 != null)

count++;

p1 = p1.GetNext();

if (count % 3 != 0)

return false;

p1 = L;

Node<int> p2 = L;

for (int i = 0; i < count / 3; i++)

p1 = p1.GetNext();

p2 = p2.GetNext();

p2 = p2.GetNext();

}
for (int i = 0; i < count / 3; i++)

if (L.GetValue() == p1.GetValue() && L.GetValue() == p2.GetValue())

L = L.GetNext();

p1 = p1.GetNext();

p2 = p2.GetNext();

else

return false;

return true;

public static void addSort(Node<int> L, int n)

Node<int> node = new Node<int>(n);

if (L.GetValue() > n) // add node after L and swap values

node.SetNext(L.GetNext());

L.SetNext(node);

node.SetValue(L.GetValue());

L.SetValue(n);

return;

Node<int> pre = L;

L = L.GetNext();

while (L != null)

if (n > L.GetValue())
{

pre = L;

L = L.GetNext();

else

pre.SetNext(node);

node.SetNext(L);

return;

pre.SetNext(node); // add node at last

public static Node<int> sortList(Node<int> L)

Node<int> head = new Node<int>(L.GetValue());

L = L.GetNext();

while (L != null)

addSort(L, L.GetValue());

return head;

public static Node<int> BuildDigit(Node<int> lst)

// make first number

int num = lst.GetValue();

Node<int> head = new Node<int>(lst.GetValue() % 10);

Node<int> tail = head;

num = num / 10;


while (num > 0)

tail.SetNext(new Node<int>(num % 10));

num = num / 10;

tail = tail.GetNext();

tail.SetNext(new Node<int>(-9));

tail = tail.GetNext();

lst = lst.GetNext();

while (lst != null)

num = lst.GetValue();

while (num > 0)

tail.SetNext(new Node<int>(num % 10));

num = num / 10;

tail = tail.GetNext();

lst = lst.GetNext();

tail.SetNext(new Node<int>(-9));

tail = tail.GetNext();

return head;

public static Node<int> BuildDigit2(Node<int> lst)

Node<int> head = new Node<int>(-1); // will be deleted at end

Node<int> tail = head;


while (lst != null)

int num = lst.GetValue();

while (num > 0)

tail.SetNext(new Node<int>(num % 10));

num = num / 10;

tail = tail.GetNext();

lst = lst.GetNext();

Node<int> temp = new Node<int>(-9);

tail.SetNext(new Node<int>(-9));

tail = tail.GetNext();

return head.GetNext(); // return without first node

public static int maxSortedSubList(Node<int> lst)

int max = 1;

int count = 1;

int num = lst.GetValue();

while (lst.HasNext())

lst = lst.GetNext();

if (lst.GetValue() > num)

count++;

if (count > max)

max = count;

}
else

if (count > max)

max = count;

count = 1;

num = lst.GetValue();

return max;

}
public static Node<double> Positive(Node<double> lst)

Node<double> lst2 = lst;

Node<double> head = new Node<double>(0);

Node<double> tail = head;

while (lst2 != null)

if (lst2.GetValue() > 0)

Node<double> temp = new Node<double>(lst2.GetValue());

tail.SetNext(temp);

tail = tail.GetNext(); // tail = temp

lst2 = lst2.GetNext();

head = head.GetNext(); // if (head.hasNext())

PrintList(head);

PrintList(lst);

return head;

You might also like