Skip to content

Instantly share code, notes, and snippets.

@000407
Created March 8, 2020 14:32
Show Gist options
  • Select an option

  • Save 000407/4f8527cc7e63a6fff23fa100d308b6a9 to your computer and use it in GitHub Desktop.

Select an option

Save 000407/4f8527cc7e63a6fff23fa100d308b6a9 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
class LinkedListDemo {
static void Main(string[] args) {
LinkedList myList = new LinkedList();
foreach(string arg in args){
int val = 0;
bool success = Int32.TryParse(arg, out val);
if(success) {
myList.AddToTail(val);
}
else {
Console.WriteLine("Could not parse {0}!", arg);
}
}
myList.DisplayList();
Console.WriteLine("List has data {0} @ {1}", 5, myList.Find(5));
while(!myList.IsEmpty()) {
try {
int data = myList.RemoveFromTail();
Console.WriteLine("Data @ tail:{0}", data);
}
catch(Exception e) {
Console.WriteLine(e);
break;
}
}
myList.DisplayList();
}
}
interface ILinkedList {
void AddToTail(int data);
int RemoveFromTail();
bool IsEmpty();
int Find(int data);
}
class ListNode {
public int Data {
get; set;
}
public ListNode Next {
get; set;
}
public ListNode(int data) {
this.Data = data;
this.Next = null;
}
}
class LinkedList : ILinkedList {
private ListNode head;
private ListNode tail;
public LinkedList() {
this.head = null;
this.tail = null;
}
public void AddToTail(int data) {
ListNode node = new ListNode(data);
if(this.head == null) { //head == null means the list is empty.
this.head = node;
this.tail = node; //tail will be the last element.
}
else { // List is not empty. Have to find the tail, which we already know.
this.tail.Next = node; // current tail's Next will be the newnode.
this.tail = node; // new tail will be the newly added node.
}
}
public int RemoveFromTail() {
if(this.IsEmpty()) { // If the list is already empty...
throw new InvalidOperationException("List is empty."); // Throw exception
}
int data = this.head.Data;
if (this.head.Next == null) { // Head is the only element availble. so..
this.head = null; // reset head and tail and...
this.tail = null;
return data; // Return recently detached data element
}
ListNode tmp = this.head;
// tmp should refer to the node before the tail
while(tmp.Next.Next != null) {
tmp = tmp.Next;
}
// Removal
data = tmp.Next.Data;
this.tail = tmp; // New tail should be the one referred by tmp.
this.tail.Next = null; // New tail's Next should be null
return data; // Return recently detached data element
}
public bool IsEmpty() {
return this.head == null;
}
public int Find(int needle) {
ListNode tmp = this.head;
int index = -1; // 0 based index of the node being checked.
// tmp should refer to the node before the tail
while(tmp.Next != null) {
++index;
if(tmp.Data == needle) { // Check if the needle is the current item's data..
return index; // found it. Return it...
}
tmp = tmp.Next;
}
return -1;
}
public void DisplayList() {
if(this.head == null) {
Console.WriteLine("List is empty");
return;
}
ListNode tmp = this.head;
do {
Console.WriteLine("ListNode:{0} Data:{1}", tmp, tmp.Data);
tmp = tmp.Next;
} while(tmp != null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment