Brandon Chatham Brandon Chatham - 3 months ago 16
C# Question

Custom linked list c# for practice - not sure why my insertion is't working

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
class node
{
public int data;
public node next;

public node(int value)
{
next = null;
data = value;
}

public void addFront(node head, int value)
{
var tempNode = new node(value);
tempNode.next = head;
head = tempNode;
}

public void addBack(node head, int value)
{
var insertNode = new node(value);
var traverseNode = head;
while (traverseNode != null)
{
if (traverseNode.next == null)
{
traverseNode.next = insertNode;
insertNode.data = value;
}
traverseNode = traverseNode.next;
}
}

public void printList(node head)
{
var counter = 0;
var traverseNode = head;
while (traverseNode != null)
{
Console.WriteLine("LL item {0}: {1}", counter, traverseNode.data);
traverseNode = traverseNode.next;
counter++;
}
}

public int deleteFront(node head)
{
//Don't need to delete the memory because the garbage collector handles that in c# (at least by my understanding). If this were C, I'd have to use free, and in C++ I'd use keyword Delete
var returnValue = head.data;
head = head.next;
return returnValue;
}

public int deleteBack(node head)
{
node traverseNode = head;
node traverseNodeTrailer = head;
if (traverseNode == null)
{
return 0;
}

while (traverseNode != null)
{
if (traverseNode.next == null)
{
traverseNodeTrailer.next = null;
break;
}
traverseNodeTrailer = traverseNode;
traverseNode = traverseNode.next;
}
return traverseNode.data;
}

public void deleteMiddle(node middleNode)
{
var traverseNode = middleNode;
while (traverseNode != null)
{
if (traverseNode.next.next == null)
{
traverseNode.data = traverseNode.next.data;
traverseNode.next = null;
break;
}
traverseNode.data = traverseNode.next.data;
traverseNode = traverseNode.next;
}
}

public node search(node head, int value)
{
var traverseNode = head;
while (traverseNode != null)
{
if (traverseNode.data == value)
return traverseNode;

traverseNode = traverseNode.next;
}

return null;//failed to find the value in the linked list
}
}

static void Main(string[] args)
{
Int32 userMenuInput = 0;
var running = true;
int userNodeInput = 0;
node head = null;
while (running)
{
Console.WriteLine("Enter a number depending on the operation you would like to perform.");
Console.WriteLine("1 - Insert node of specified value; 2 - Delete a node and return the value; 3 - Search for a node with a specified value; 4 - Print the list values. *All other values exit");
userMenuInput = Convert.ToInt32(Console.ReadLine());//get user input for what they want to do
switch (userMenuInput)
{
case 1:
Console.WriteLine("Enter a number you'd like to insert into the linked list.");
userNodeInput = Convert.ToInt32(Console.ReadLine());//get user input for value to insert
if (head == null)
{
head = new node(userNodeInput);
break;
}
head.addFront(head, userNodeInput);
break;
case 2:
if (head == null)
{
Console.WriteLine("The list is empty - cannot delete.");
break;
}
userNodeInput = head.deleteFront(head);
Console.WriteLine("{0} is the value that was deleted from the front of the list.", userNodeInput);
break;
case 3:
Console.WriteLine("What is the value you'd like to search for in the list?");
userNodeInput = Convert.ToInt32(Console.ReadLine());
if (head == null)
{
//do nothing
}
else if (head.search(head, userNodeInput) != null)
{
Console.WriteLine("That value was found");
break;
}
Console.WriteLine("That value was not found.");
break;
case 4:
if (head == null)
{
Console.WriteLine("The list has nothing to print.");
break;
}
head.printList(head);
break;
default:
running = false;
break;
}
}
}
}
}


DISCLAIMER: I'm very new to c# - just been using it for the past week or so. I will appreciate any optimization hints you have, but for now, I'm just trying to get this running.

So, this is correctly inserting 1 element, but nothing more. It is finding the element if it is in the first link. The delete doesn't seem to be working at all. It doesn't throw any errors but it also doesn't delete the first link. Note: A few of my functions in there are unused so far so don't worry about mentioning that - I'm aware.

Thanks everyone!

Answer

You can use ref if you want to modify argument passed to addFront method:

public void addFront(ref node head, int value)
        {
            var tempNode = new node(value);
            tempNode.next = head;
            head = tempNode;
        }

.

head.addFront(ref head, userNodeInput);