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;
}
}
}
}
}
免责声明:我对c#很陌生 - 过去一周左右一直在使用它。我会感谢您提供的任何优化提示,但就目前而言,我只是想让它运行起来。
所以,这是正确插入1个元素,但仅此而已。它是在第一个链接中找到元素。删除似乎根本不起作用。它不会抛出任何错误,但它也不会删除第一个链接。注意:到目前为止,我的一些功能尚未使用,所以不要担心提到 - 我知道。
谢谢大家!
答案 0 :(得分:1)
如果要修改传递给addFront方法的参数,可以使用ref:
public void addFront(ref node head, int value)
{
var tempNode = new node(value);
tempNode.next = head;
head = tempNode;
}
head.addFront(ref head, userNodeInput);