Помощь с перестановкой местами элементов списка. C#
Дан односвязный линейный список и целое число n. Элемент списка содержит следующую информацию: фамилия студента, шифр группы. Требуется сдвинуть циклически элементы на n позиций: удалять элементы с конца списка и добавлять их в начало списка. Не могу выполнить последнюю часть задачи, пытался менять значения элементов с помощью дополнительной переменной, но возникли сложности с дальнейшим пониманием действий
using System;
using System.Collections.Generic;
namespace Лабораторная_работа__Односвязные_списки_
{
class Program
{
static void Main(string[] args)
{
student k = null; string d;
Console.WriteLine($"Введите кол-во интераций");
int n = Convert.ToInt32(Console.ReadLine());
List<student> info = new List<student>(2);
Console.WriteLine($"Введите кол-во студентов");
int kol = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < kol; i++)
{
Console.WriteLine($"Введите инфо о студенте");
info.Add(new student() { data = Convert.ToString(Console.ReadLine()) });
k = info[kol];
}
Console.WriteLine(k);
for (int i = 0; i < n; i++)
{
info.Add(k);
info.RemoveAt(kol);
k = info[kol - 1];
}
foreach (student p in info)
{
Console.WriteLine(p.data);
}
}
}
class student
{
public string data { get; set; }
}
}
Ответы (1 шт):
Автор решения: Frehzy
→ Ссылка
public class Node<T>
{
public T Data { get; set; }
public Node<T> Next { get; set; }
public Node(T d)
{
Data = d;
Next = default;
}
}
public class LinkList<T> : IEnumerable<T>
{
private Node<T> _head;
public int Count { get; private set; }
public bool IsEmpty => Count == 0;
public void LastToFirst(int iteration)
{
for (int i = 0; i < iteration; i++)
LastToFirst();
}
public void LastToFirst()
{
if (_head == null || _head.Next == null)
return;
Node<T> secLast = default;
Node<T> last = _head;
while (last.Next != null)
{
secLast = last;
last = last.Next;
}
secLast.Next = null;
last.Next = _head;
_head = last;
}
public bool SaveOnlyLast()
{
if (_head == null)
return false;
var remove = _head;
while (remove.Next != null)
remove = remove.Next;
remove.Next = null;
_head = remove;
Count = 1;
return true;
}
public bool RemoveLast()
{
if (_head == null && _head.Next == null)
return false;
Node<T> second_last = _head;
while (second_last.Next.Next != null)
second_last = second_last.Next;
second_last.Next = null;
Count--;
return true;
}
public Node<T> Add(T item)
{
if (_head == null)
{
_head = CreateNode(item);
Count++;
return _head;
}
if (_head.Next == null)
{
_head.Next = CreateNode(item);
Count++;
return _head.Next;
}
Node<T> second_last = _head;
while (second_last.Next.Next != null)
second_last = second_last.Next;
second_last.Next.Next = CreateNode(item);
Count++;
return second_last.Next.Next;
}
public void PrintList()
{
Node<T> temp = _head;
while (temp != null)
{
Console.Write(temp.Data + " ");
temp = temp.Next;
}
Console.WriteLine();
}
public void Clear()
{
_head = null;
Count = 0;
}
public bool Contains(T item)
{
Node<T> current = _head;
while (current != null)
{
if (current.Data.Equals(item))
return true;
current = current.Next;
}
return false;
}
public Node<T> Push(T item)
{
Node<T> new_node = new Node<T>(item)
{
Next = _head
};
_head = new_node;
Count++;
return new_node;
}
public IEnumerator<T> GetEnumerator()
{
Node<T> current = _head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
IEnumerator IEnumerable.GetEnumerator() =>
((IEnumerable)this).GetEnumerator();
private Node<T> CreateNode(T item)
=> new Node<T>(item) { Next = default };
}
Использование:
LinkList<int> l = new LinkList<int>();
for (int i = 0; i < 5; i++)
l.Add(i);
l.PrintList();
l.LastToFirst();
l.PrintList();
l.LastToFirst(2);
l.PrintList();
l.RemoveLast();
l.PrintList();
l.Push(55);
l.PrintList();
l.RemoveLast();
l.PrintList();
l.Add(101);
l.PrintList();
Console.ReadKey();
