Advertisement
P0ch1ta

ListRand

May 17th, 2025 (edited)
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.62 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Text;
  7.  
  8. namespace ListRandSerialization
  9. {
  10.     class ListNode
  11.     {
  12.         public ListNode Prev;
  13.         public ListNode Next;
  14.         public ListNode Rand;
  15.         public string Data;
  16.     }
  17.  
  18.     class ListRand
  19.     {
  20.         public ListNode Head;
  21.         public ListNode Tail;
  22.         public int Count;
  23.  
  24.         public static ListRand GenerateRandom(int count)
  25.         {
  26.             Random random = new();
  27.             ListRand list = new();
  28.  
  29.             for (int i = 0; i < count; i++)
  30.             {
  31.                 string randomData = new(Enumerable
  32.                                         .Range(0, random.Next(1, 9))
  33.                                         .Select(_ => (char)random.Next(97, 123))
  34.                                         .ToArray()
  35.                                         );
  36.  
  37.                 list.AddToTail(randomData);
  38.             }
  39.  
  40.             return list;
  41.         }
  42.  
  43.         public void Serialize(FileStream s)
  44.         {
  45.             if (Count == 0)
  46.                 return;
  47.  
  48.             Dictionary<ListNode, int> nodesIndexes = new(Count);
  49.  
  50.             int i = 0;
  51.             ListNode tempNode = Head;
  52.  
  53.             while (tempNode != null)
  54.             {
  55.                 nodesIndexes.Add(tempNode, i);
  56.  
  57.                 tempNode = tempNode.Next;
  58.                 i++;
  59.             }
  60.  
  61.             using (BinaryWriter binaryWriter = new(s))
  62.             {
  63.                 binaryWriter.Write(Count);
  64.  
  65.                 tempNode = Head;
  66.  
  67.                 while (tempNode != null)
  68.                 {
  69.                     string nodeData = tempNode.Data;
  70.                     int nodeRandIndex = tempNode.Rand == null ? -1 : nodesIndexes[tempNode.Rand];
  71.  
  72.                     binaryWriter.Write(nodeData);
  73.                     binaryWriter.Write(nodeRandIndex);
  74.  
  75.                     tempNode = tempNode.Next;
  76.                 }
  77.             }
  78.         }
  79.  
  80.         public void Deserialize(FileStream s)
  81.         {
  82.             using (BinaryReader binaryReader = new(s))
  83.             {
  84.                 long baseStreamLength = binaryReader.BaseStream.Length;
  85.  
  86.                 if (binaryReader.BaseStream.Position < baseStreamLength)
  87.                     return;
  88.  
  89.                 int count = binaryReader.ReadInt32();
  90.                 (ListNode, int)[] nodes = new (ListNode, int)[count];
  91.  
  92.                 ListNode tempNode = new();
  93.                 int currentCount = 0;
  94.                 Head = tempNode;
  95.  
  96.                 while (binaryReader.BaseStream.Position < baseStreamLength)
  97.                 {
  98.                     string nodeData = binaryReader.ReadString();
  99.                     int nodeRandIndex = binaryReader.ReadInt32();
  100.  
  101.                     tempNode.Data = nodeData;
  102.  
  103.                     ListNode next = new();
  104.                     tempNode.Next = next;
  105.                     next.Prev = tempNode;
  106.  
  107.                     nodes[currentCount] = (tempNode, nodeRandIndex);
  108.  
  109.                     tempNode = next;
  110.                     currentCount++;
  111.                 }
  112.  
  113.                 Tail = tempNode.Prev;
  114.                 Tail.Next = null;
  115.  
  116.                 Count = currentCount;
  117.  
  118.                 for(int i = 0; i < Count; i++)
  119.                 {
  120.                     ListNode node = nodes[i].Item1;
  121.                     int nodeRandIndex = nodes[i].Item2;
  122.  
  123.                     ListNode nodeRand = nodeRandIndex == -1 ? null : nodes[nodeRandIndex].Item1;
  124.                     node.Rand = nodeRand;
  125.                 }
  126.             }
  127.         }
  128.  
  129.         public void AddToTail(string data)
  130.         {
  131.             ListNode newNode = new();
  132.  
  133.             if(Count == 0)
  134.             {
  135.                 Head = newNode;
  136.                 Tail = newNode;
  137.             }
  138.             else
  139.             {
  140.                 newNode.Prev = Tail;
  141.                 Tail.Next = newNode;
  142.             }
  143.  
  144.             newNode.Next = null;
  145.             newNode.Rand = null;
  146.             newNode.Data = data;
  147.  
  148.             Tail = newNode;
  149.  
  150.             Count++;
  151.         }
  152.  
  153.         public override string ToString()
  154.         {
  155.             if (Count == 0)
  156.             {
  157.                 return "ListRand is empty";
  158.             }
  159.  
  160.             StringBuilder stringBuilder = new();
  161.             stringBuilder.AppendLine("ListRand: " + Count.ToString() + ", Head - " + Head.Data + ", Tail - " + Tail.Data + "\n");
  162.  
  163.             ListNode temp = Head;
  164.             for (int i = 0; i < Count; i++)
  165.             {
  166.                 string nodeInfo = string.Join(", ",
  167.                     $"Data: {temp.Data ?? "null"}",
  168.                     $"Prev: {temp.Prev?.Data ?? "null"}",
  169.                     $"Next: {temp.Next?.Data ?? "null"}",
  170.                     $"Rand: {temp.Rand?.Data ?? "null"}"
  171.                 );
  172.  
  173.                 stringBuilder.AppendLine(nodeInfo);
  174.                 temp = temp.Next;
  175.             }
  176.  
  177.             return stringBuilder.ToString();
  178.         }
  179.     }
  180.  
  181.     class Program
  182.     {
  183.         static void Main(string[] args)
  184.         {
  185.             ListRand firstList = ListRand.GenerateRandom(5);
  186.  
  187.             FileStream fs = new("list.bin", FileMode.OpenOrCreate);
  188.             firstList.Serialize(fs);
  189.             fs.Close();
  190.  
  191.  
  192.             ListRand secondList = new();
  193.  
  194.             fs = new("list.bin", FileMode.Open);
  195.             secondList.Deserialize(fs);
  196.             fs.Close();
  197.  
  198.             Console.WriteLine("First " + firstList);
  199.             Console.WriteLine();
  200.             Console.WriteLine("Second " + firstList);
  201.             Console.ReadKey();
  202.         }
  203.     }
  204. }
  205.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement