-
Notifications
You must be signed in to change notification settings - Fork 0
UniqueList #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kamendov-maxim
wants to merge
2
commits into
master
Choose a base branch
from
UniqueList
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
UniqueList #6
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace Lists; | ||
|
|
||
| public class ElementExistsException: Exception | ||
| { | ||
| public ElementExistsException() { } | ||
| public ElementExistsException(string message) : base(message) { } | ||
|
|
||
| public ElementExistsException(string message, Exception innerException) : base(message, innerException) { } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| namespace Lists; | ||
|
|
||
| public interface IList | ||
| { | ||
| void Add(int value); | ||
| void Insert(int index, int value); | ||
| int[] ToArray(); | ||
| int RemoveAt(int index); | ||
| int FindValue(int index); | ||
| int Count { get; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| using System.Dynamic; | ||
| using System.Reflection.Metadata.Ecma335; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Threading.Tasks.Dataflow; | ||
| using System.Xml; | ||
|
|
||
| namespace Lists; | ||
|
|
||
| /// <summary> | ||
| /// An implementation of linked list | ||
| /// </summary> | ||
| public class List: IList | ||
| { | ||
| protected class ListElement | ||
| { | ||
| public ListElement? Next; | ||
| public ListElement? Previous; | ||
| public int Value; | ||
| public ListElement(int value) | ||
| { | ||
| Value = value; | ||
|
|
||
| Next = null; | ||
| Previous = null; | ||
| } | ||
| } | ||
|
|
||
| public List() | ||
| { | ||
| Count = 0; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Method to insert value at given index | ||
| /// </summary> | ||
| /// <param name="index">Where to insert value</param> | ||
| /// <param name="value">Value to insert</param> | ||
| /// <exception cref="OutOfRangeException">Exception is thrown if index is less than 0 or | ||
| /// bigger than list.Count and it is impossible to insert value</exception> | ||
| public virtual void Insert(int index, int value) | ||
| { | ||
| if (index > Count || index < 0) | ||
| { | ||
| throw new OutOfRangeException(); | ||
| } | ||
|
|
||
| var newElement = new ListElement(value); | ||
|
|
||
| if (index == Count) | ||
| { | ||
| if (Count == 0) | ||
| { | ||
| head = newElement; | ||
| } | ||
| if (bottom is not null) | ||
| { | ||
| newElement.Previous = bottom; | ||
| bottom.Next = newElement; | ||
| } | ||
| bottom = newElement; | ||
| ++Count; | ||
| return; | ||
| } | ||
|
|
||
| if (index == 0) | ||
| { | ||
| newElement = new ListElement(value); | ||
| head.Previous = newElement; | ||
| newElement.Next = head; | ||
| head = newElement; | ||
| ++Count; | ||
| return; | ||
| } | ||
|
|
||
| var currenElement = FindNode(index); | ||
| newElement.Next = currenElement; | ||
| newElement.Previous = currenElement.Previous; | ||
| currenElement.Previous.Next = newElement; | ||
| currenElement.Previous = newElement; | ||
| ++Count; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds value to the end of list | ||
| /// </summary> | ||
| /// <param name="value">Value to add</param> | ||
| public virtual void Add(int value) | ||
| { | ||
| var newElement = new ListElement(value); | ||
| if (Count == 0) | ||
| { | ||
| head = newElement; | ||
| bottom = newElement; | ||
| } | ||
| else if (Count == 1) | ||
| { | ||
| head.Next = newElement; | ||
| newElement.Previous = head; | ||
| } | ||
| else | ||
| { | ||
| bottom.Next = newElement; | ||
| newElement.Previous = bottom; | ||
| } | ||
| bottom = newElement; | ||
| ++Count; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Method to get an int array containing all the values from list in the same order | ||
| /// </summary> | ||
| /// <returns>Array with values</returns> | ||
| public int[] ToArray() | ||
| { | ||
| var output = new int[Count]; | ||
| var currenElement = head; | ||
| for (int i = 0; i < Count; ++i) | ||
| { | ||
| output[i] = currenElement.Value; | ||
| currenElement = currenElement.Next; | ||
| } | ||
|
|
||
| return output; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Method allowing to remove value at given index | ||
| /// </summary> | ||
| /// <param name="index">Index of element to remove</param> | ||
| /// <returns>Value of this element</returns> | ||
| /// <exception cref="OutOfRangeException">Thrown if there is no element with such index in the list</exception> | ||
| public int RemoveAt(int index) | ||
| { | ||
| if (index > Count - 1 || index < 0 || Count == 0) | ||
| { | ||
| throw new OutOfRangeException(); | ||
| } | ||
|
|
||
| if (index == Count - 1) | ||
| { | ||
| if (Count == 0) | ||
| { | ||
| head = null; | ||
| bottom = null; | ||
| } | ||
| else | ||
| { | ||
| bottom = bottom.Previous; | ||
| } | ||
| } | ||
|
|
||
| int value; | ||
|
|
||
| if (index == 0) | ||
| { | ||
| value = head.Value; | ||
| head = head.Next; | ||
| --Count; | ||
| return value; | ||
| } | ||
|
|
||
| var element = FindNode(index); | ||
| value = element.Value; | ||
| element.Next.Previous = element.Previous; | ||
| element.Previous.Next = element.Next; | ||
| --Count; | ||
| return value; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Method to get a value of an element at a givent index | ||
| /// </summary> | ||
| /// <param name="index">Index of an element to find</param> | ||
| /// <returns>Value of an element</returns> | ||
| /// <exception cref="OutOfRangeException">Thrown if there is no element with such index in the list</exception> | ||
| public int FindValue(int index) | ||
| { | ||
| if (index > Count - 1 || index < 0) | ||
| { | ||
| throw new OutOfRangeException("Index is out of range"); | ||
| } | ||
|
|
||
| var element = FindNode(index); | ||
|
|
||
| return element.Value; | ||
| } | ||
|
|
||
| protected ListElement FindNode(int index) | ||
| { | ||
| ListElement? currenElement; | ||
| if (index < Count / 2) | ||
| { | ||
| currenElement = head; | ||
| for (int i = 0; i < index; ++i) | ||
| { | ||
| currenElement = currenElement.Next; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| currenElement = bottom; | ||
| for (int i = Count - 1; i > index; --i) | ||
| { | ||
| currenElement = currenElement.Previous; | ||
| } | ||
| } | ||
|
|
||
| return currenElement; | ||
| } | ||
|
|
||
| public int Count {get; private set; } | ||
| protected ListElement? head = null; | ||
| protected ListElement? bottom = null; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace Lists; | ||
|
|
||
| public class OutOfRangeException : Exception | ||
| { | ||
| public OutOfRangeException() { } | ||
| public OutOfRangeException(string message) : base(message) { } | ||
|
|
||
| public OutOfRangeException(string message, Exception innerException) : base(message, innerException) { } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| using Lists; | ||
|
|
||
|
|
||
|
|
||
| class Program | ||
| { | ||
| public static void Main() | ||
| { | ||
| string options = """ | ||
| What kind of list do you want to create? Write number to console to choose | ||
| 0 - List | ||
| 1 - UniqueList | ||
| """; | ||
|
|
||
| Console.WriteLine(options); | ||
| int listType; | ||
| while (!int.TryParse(Console.ReadLine(), out listType) || listType > 1 || listType < 0) | ||
| { | ||
| Console.WriteLine("Incorrect input"); | ||
| Console.Write(options); | ||
| } | ||
| Console.WriteLine(); | ||
|
|
||
| IList list; | ||
| if (listType == 0) | ||
| { | ||
| list = new List(); | ||
| } | ||
| else | ||
| { | ||
| list = new UniqueList(); | ||
| } | ||
|
|
||
| bool running = true; | ||
| while (running) | ||
| { | ||
| options = """ | ||
| 0 - Exit | ||
| 1 - Add element to list | ||
| 2 - Insert element to list | ||
| 3 - Remove element from list | ||
| 4 - Print list | ||
| Write number to console to choose option | ||
| """; | ||
|
|
||
| Console.WriteLine(options); | ||
|
|
||
| int option; | ||
| while (!int.TryParse(Console.ReadLine(), out option) || option > 4 || option < 0) | ||
| { | ||
| Console.WriteLine("Incorrect input"); | ||
| Console.WriteLine(options); | ||
| } | ||
|
|
||
| switch (option) | ||
| { | ||
| case 0: | ||
| { | ||
| running = false; | ||
| break; | ||
| } | ||
| case 1: | ||
| { | ||
| var value = UserInputTryParse("Write value you want to add"); | ||
| try | ||
| { | ||
| list.Add(value); | ||
| } | ||
| catch (ElementExistsException) | ||
| { | ||
| Console.WriteLine("You have already added this element earlier"); | ||
| } | ||
| break; | ||
| } | ||
| case 2: | ||
| { | ||
| var index = UserInputTryParse("Write an index where you want to insert an element"); | ||
| var value = UserInputTryParse("Write value you want to insert"); | ||
|
|
||
| try | ||
| { | ||
| list.Insert(index, value); | ||
| } | ||
| catch (OutOfRangeException) | ||
| { | ||
| Console.WriteLine("Index is out of range"); | ||
| } | ||
| catch (ElementExistsException) | ||
| { | ||
| Console.WriteLine("You have already added this element earlier"); | ||
| } | ||
| break; | ||
| } | ||
| case 3: | ||
| { | ||
| var index = UserInputTryParse("Write an index where you want to delete an element"); | ||
| try | ||
| { | ||
| list.RemoveAt(index); | ||
| } | ||
| catch (OutOfRangeException) | ||
| { | ||
| Console.WriteLine("Index is out of range"); | ||
| } | ||
| break; | ||
| } | ||
| case 4: | ||
| { | ||
| Console.Write("["); | ||
| for (int i = 0; i < list.Count; ++i) | ||
| { | ||
| Console.Write(list.FindValue(i)); | ||
| if (i != list.Count - 1) | ||
| { | ||
| Console.Write(", "); | ||
| } | ||
| } | ||
| Console.Write("]\n"); | ||
| break; | ||
| } | ||
| } | ||
| Console.WriteLine(); | ||
| } | ||
| } | ||
|
|
||
| private static int UserInputTryParse(string message) | ||
| { | ||
| Console.WriteLine(message); | ||
| int userInput; | ||
| while (!int.TryParse(Console.ReadLine(), out userInput)) | ||
| { | ||
| Console.WriteLine("Incorrect input"); | ||
| Console.WriteLine(message); | ||
| } | ||
|
|
||
| return userInput; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Уже есть вот такое хорошее исключение