Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
SingleLinkedList c# operations
using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Node { public int Value {get; set;} public Node Next {get; set;} } public class LinkedList { public Node head; //print all nodes public void PrintAllNodes() { Node current = head; while(current!=null){ Console.WriteLine(current.Value); current=current.Next; } } //counting number of nodes in a linked list public int getCount() { int count=0; Node Current=head; while(Current!=null) { count++; Current=Current.Next; } return count; } // adding node at the end of singly linked list public void addNodeEnd(int data) { Node toAdd= new Node(); Node temp=new Node(); toAdd.Value=data; toAdd.Next=null; if(head==null) { head=toAdd; } else { temp=head; while(temp.Next!=null) { temp=temp.Next; } temp.Next=toAdd; } } //Adding node at the beginning of singly Linked List public void addFirst(int data) { Node toAdd= new Node(); toAdd.Value=data; toAdd.Next=head; head=toAdd; } //Remove particular element in linked list public void RemoveElements(int val) { Node current = head; if(head != null) { if(current.Value==val&¤t.Next==null) { current=null; return; } while(current !=null && current.Next != null) { if(current.Value == val) { current.Value = current.Next.Value; current.Next = current.Next.Next; } else current = current.Next; } return; } else return; } //Reverse public void ReverseList() { Node current=head,prev=null,next; if (current==null){return;} while(current!=null) { next = current.Next; current.Next = prev; prev = current; current = next; } head=prev; } //Reverse with Recursion public void ReverseRecursion(Node prev, Node current,Node next) { if(current!=null) { ReverseRecursion(current,next,prev); next = current.Next; prev = current; current = next; } else { head=prev; } } } public class Program { public static void Main(string[] args) { //Your code goes here LinkedList myList= new LinkedList(); myList.addNodeEnd(2); myList.addNodeEnd(3); myList.addNodeEnd(6); myList.addNodeEnd(4); myList.addNodeEnd(5); myList.addNodeEnd(6); myList.addNodeEnd(6); myList.addFirst(1); // System.Console.WriteLine(" Removed "+ myList.RemoveElements(2).Value+"****"); myList.RemoveElements(1); System.Console.WriteLine("\nCount "+ myList.getCount()); // myList.addFirst(5); // myList.addFirst(6); myList.PrintAllNodes(); myList.ReverseList(); System.Console.WriteLine("\after reversing "+ myList.getCount()); myList.PrintAllNodes(); myList.ReverseRecursion(null,myList.head,null); System.Console.WriteLine("\after reversing recursion "); myList.PrintAllNodes(); } } }
run
|
edit
|
history
|
help
0
HelloWorld
Add Two String Numbers which may be large - Author MC
testing
book iequatable
asxsdxsd
5
19.05.2020
Pyramid using C#
Next Letter
BEISPEIL- AUGUST 6 -2019