Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Reflection;
- using System.Text;
- namespace ListRandSerialization
- {
- class ListNode
- {
- public ListNode Prev;
- public ListNode Next;
- public ListNode Rand;
- public string Data;
- }
- class ListRand
- {
- public ListNode Head;
- public ListNode Tail;
- public int Count;
- public static ListRand GenerateRandom(int count)
- {
- Random random = new();
- ListRand list = new();
- for (int i = 0; i < count; i++)
- {
- string randomData = new(Enumerable
- .Range(0, random.Next(1, 9))
- .Select(_ => (char)random.Next(97, 123))
- .ToArray()
- );
- list.AddToTail(randomData);
- }
- return list;
- }
- public void Serialize(FileStream s)
- {
- if (Count == 0)
- return;
- Dictionary<ListNode, int> nodesIndexes = new(Count);
- int i = 0;
- ListNode tempNode = Head;
- while (tempNode != null)
- {
- nodesIndexes.Add(tempNode, i);
- tempNode = tempNode.Next;
- i++;
- }
- using (BinaryWriter binaryWriter = new(s))
- {
- binaryWriter.Write(Count);
- tempNode = Head;
- while (tempNode != null)
- {
- string nodeData = tempNode.Data;
- int nodeRandIndex = tempNode.Rand == null ? -1 : nodesIndexes[tempNode.Rand];
- binaryWriter.Write(nodeData);
- binaryWriter.Write(nodeRandIndex);
- tempNode = tempNode.Next;
- }
- }
- }
- public void Deserialize(FileStream s)
- {
- using (BinaryReader binaryReader = new(s))
- {
- long baseStreamLength = binaryReader.BaseStream.Length;
- if (binaryReader.BaseStream.Position < baseStreamLength)
- return;
- int count = binaryReader.ReadInt32();
- (ListNode, int)[] nodes = new (ListNode, int)[count];
- ListNode tempNode = new();
- int currentCount = 0;
- Head = tempNode;
- while (binaryReader.BaseStream.Position < baseStreamLength)
- {
- string nodeData = binaryReader.ReadString();
- int nodeRandIndex = binaryReader.ReadInt32();
- tempNode.Data = nodeData;
- ListNode next = new();
- tempNode.Next = next;
- next.Prev = tempNode;
- nodes[currentCount] = (tempNode, nodeRandIndex);
- tempNode = next;
- currentCount++;
- }
- Tail = tempNode.Prev;
- Tail.Next = null;
- Count = currentCount;
- for(int i = 0; i < Count; i++)
- {
- ListNode node = nodes[i].Item1;
- int nodeRandIndex = nodes[i].Item2;
- ListNode nodeRand = nodeRandIndex == -1 ? null : nodes[nodeRandIndex].Item1;
- node.Rand = nodeRand;
- }
- }
- }
- public void AddToTail(string data)
- {
- ListNode newNode = new();
- if(Count == 0)
- {
- Head = newNode;
- Tail = newNode;
- }
- else
- {
- newNode.Prev = Tail;
- Tail.Next = newNode;
- }
- newNode.Next = null;
- newNode.Rand = null;
- newNode.Data = data;
- Tail = newNode;
- Count++;
- }
- public override string ToString()
- {
- if (Count == 0)
- {
- return "ListRand is empty";
- }
- StringBuilder stringBuilder = new();
- stringBuilder.AppendLine("ListRand: " + Count.ToString() + ", Head - " + Head.Data + ", Tail - " + Tail.Data + "\n");
- ListNode temp = Head;
- for (int i = 0; i < Count; i++)
- {
- string nodeInfo = string.Join(", ",
- $"Data: {temp.Data ?? "null"}",
- $"Prev: {temp.Prev?.Data ?? "null"}",
- $"Next: {temp.Next?.Data ?? "null"}",
- $"Rand: {temp.Rand?.Data ?? "null"}"
- );
- stringBuilder.AppendLine(nodeInfo);
- temp = temp.Next;
- }
- return stringBuilder.ToString();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- ListRand firstList = ListRand.GenerateRandom(5);
- FileStream fs = new("list.bin", FileMode.OpenOrCreate);
- firstList.Serialize(fs);
- fs.Close();
- ListRand secondList = new();
- fs = new("list.bin", FileMode.Open);
- secondList.Deserialize(fs);
- fs.Close();
- Console.WriteLine("First " + firstList);
- Console.WriteLine();
- Console.WriteLine("Second " + firstList);
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement