I have this linked list c# codes. I couldn't figure out the error for not printing any output. I have an error message saying that LinkedList.LinkedList doesn't contain a definition for PrintNodes. Can anyone pointed out why I am getting that error, and where I am doing mistake.
public class Node
{
public object data;
public Node next;
public Node(object data)
{
this.data = data;
}
}
public class LinkedList
{
Node head;
Node current;
public Node Head
{
get { return head; }
}
public void Add(Node n)
{
if (head == null)
{
head = n;
current = head;
}
else
{
current.next = n;
current = current.next;
}
}
public void MergeSortedList(Node first, Node second)
{
if (Convert.ToInt32(first.next.data.ToString())
> Convert.ToInt32(second.data.ToString()))
{
Node t = first;
first = second;
second = t;
}
head = first;
while ((first.next != null) && (second != null))
{
if (Convert.ToInt32(first.next.data.ToString())
< Convert.ToInt32(second.data.ToString()))
{
first = first.next;
}
else
{
Node n = first.next;
Node t = second.next;
first.next = second;
second.next = n;
first = first.next;
second = t;
}
}
if (first.next == null)
first.next = second;
}
static void Main()
{
LinkedList l1 = new LinkedList();
l1.Add(new Node("2"));
l1.Add(new Node("3"));
l1.Add(new Node("4"));
l1.Add(new Node("5"));
l1.Add(new Node("8"));
l1.Add(new Node("100"));
l1.Add(new Node("120"));
LinkedList l2 = new LinkedList();
l2.Add(new Node("10"));
l2.Add(new Node("30"));
l2.Add(new Node("34"));
LinkedList list = new LinkedList();
list.MergeSortedList(l1.Head, l2.Head);
list.PrintNodes();
Console.ReadLine();
}
}
}
In Main you call list.PrintNodes() but your LinkedList class has no such method defined, hence the exception, exactly as it says so:
LinkedList.LinkedList doesn't contain a definition for PrintNodes
Related
I created a simple node class, and solution class with an insert method, a display method, along with Main. I am trying to insert 4 numbers into the linked-list and have it display the entire list. At most I am only able to have it display 2 of the numbers. The issue is most likely in my insert method. I've spent hours trying to figure out what the issue is. What could be wrong with my code?
public static Node insert(Node head, int data)
{
Node newNode = new Node(data);
if (head == null)
{
head = newNode;
}
else
{
while (head.next != null)
{
head = head.next;
}
head.next = newNode;
}
return head;
}
public static void display(Node head)
{
Node start = head;
while (start != null)
{
Console.Write(start.data + " ");
start = start.next;
}
}
static void Main(String[] args)
{
Node head = null;
int[] numbers = new int[]{2, 3, 4, 1};
for (int i = 0; i < numbers.Length; i++)
{
int data = numbers[i];
head = insert(head, data);
}
display(head);
Console.ReadLine();
}
class Node
{
public int data;
public Node next;
public Node(int d)
{
data = d;
next = null;
}
public Node() { }
}
Yes, the problem is in the insert method:
while (head.next != null)
{
// From now on you have the initial head got lost
head = head.next;
}
Quick amendment is to change while into for:
public static Node insert(Node head, int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
}
else {
// we loop on last, keeping head intact
for (Node last = head; ; last = last.next)
if (last.next == null) {
last.next = newNode;
break;
}
}
return head;
}
we can simplify it further:
public static Node insert(Node head, int data) {
Node newNode = new Node(data);
for (Node last = head; last != null; last = last.next)
if (last.next == null) {
last.next = newNode;
return head;
}
return newNode;
}
A list of instructions, and the code I have so far.
The issue I'm running into with this so far is that my Enqueue method or my Dequeue method in C# is not operating correctly, and it's not passing the UnitTest created for the assignment.
The Unit test should be able to take the Enqueue method, and the Dequeue method and put a 5 character array into the stack one at a time.
[]{10,20,30,40,50}. Then take them out in that same order (FIFO).
The code I have written is not able to pass the unit test, and the error says:
Assert.AreEqual failed. Expected<20>.Actual<10>
public int Count { get; private set; }
private Node<T> _head = null;
private Node<T> _tail = null;
public void Enqueue(T data)
{
Node<T> node = new Node<T>();
node.Data = data;
if (Count == 0)
{
_tail = node;
_head = node;
_tail.Next = node;
}
else
{
node.Next = _tail;
_tail = node.Next;
}
}
I think this block of code is my issue here, but I could be wrong. I've tried variations of these over and over with no success, and have resigned that my logic may be off or something very silly is missing.
public T Dequeue()
{
Node<T> position = _head;
if (Count == 0)
{
throw new InvalidOperationException("You've taken it all...");
}
else
{
T temp = _head.Data;
_head = position.Next;
--Count;
if (_head == null)
{
_tail = null;
}
return temp;
}
}
public void Reverse()
{
Node<T> previous, current, next;
previous = null;
current = _head;
next = _head.Next;
while (current != null)
{
next = current.Next;
current.Next = previous;
previous = current;
current = next;
}
_head = previous;
_tail = next;
}
public void EnqueueDequeueTest()
{
int[] testValues = new int[5] { 10, 20, 30, 40, 50 };
PG2Queue<int> testQueue = new PG2Queue<int>();
foreach (var testValue in testValues)
{
testQueue.Enqueue(testValue);
}
for (int i = 0; i < testValues.Length; i++)
{
int itemPopped = testQueue.Dequeue();
Assert.AreEqual(testValues[i], itemPopped);
}
}
I've this program and my tasks are:
To create a method that returns the current end of the list or get the end of the list from the method for appending have it delivered and
To set the value of the end of list instance after appending new elements to the current end of the list and then call the method to append new list items with this value.
class Node
{
string info;
Node next;
public void SetInfo(string InfoNew)
{
info = InfoNew;
next = null;
}
public void Add(string InfoNew)
{
if (next == null)
{
next = new Node();
next.SetInfo(InfoNew);
}
else
next.Add(InfoNew);
}
}
class Program
{
static void Main(string[] args)
{
Node listStart = new Node();
listStart.Add("node 1");
for (int node = 2; node < 4; node++)
listStart.Add("node " + node);
}
}
}
This is my solution. It works. But I'm no sure if this is correct.
class Node
{
string info;
Node next;
public void SetInfo(string InfoNew)
{
info = InfoNew;
next = null;
}
public Node Add(string InfoNew)
{
if (next == null)
{
next = new Node();
next.SetInfo(InfoNew);
}
else
next.Add(InfoNew);
return next;
}
public void Print()
{
Console.WriteLine(info);
if (next != null)
next.Print();
Console.ReadKey();
}
public Node End()
{
Node ptr = next;
while (ptr.next != null)
ptr = ptr.next;
return ptr;
}
}
class Program
{
static void Main(string[] args)
{
Node listStart = new Node();
Node listEnd = new Node();
listStart.Add("node 1");
for (int node = 2; node < 4; node++)
listEnd = listStart.Add("node " + node);
listStart.Print();
}
}
This is probably the right way to do it, isn't it?
class Program
{
static void Main(string[] args)
{
Node listStart = new Node();
Node listEnd = new Node();
listStart.Add("node 1");
listEnd = listStart;
for (int node = 2; node < 4; node++)
listEnd = listEnd.Add("node " + node);
listStart.Print();
}
}
I want to implement simple Linked list and adding items and its seems that my Add function get into endless loop and i don't know why
public class IntNode
{
private int _value;
private IntNode _next;
public IntNode(int val, IntNode n)
{
_value = val;
_next = n;
}
public int getValue()
{
return _value;
}
public IntNode getNext()
{
return _next;
}
public void setValue(int v)
{
_value = v;
}
public void setNext(IntNode next)
{
_next = next;
}
public string ToString()
{
return _value.ToString();
}
}
public class IntList
{
private IntNode _head;
public static int count;
public IntList()
{
_head = null;
count = 0;
}
public IntList(IntNode node)
{
_head = node;
}
public void Add(IntNode node)
{
if (_head == null)
_head = node;
else
{
for (IntNode p = _head; p.getNext() != null; p.getNext()) { }
_head.setNext(node);
count++;
}
}
public void ToString()
{
IntNode cur = _head;
while (cur.getNext() != null)
{
Console.WriteLine(cur.ToString());
cur = cur.getNext();
}
}
}
main
static void Main(string[] args)
{
IntList list = new IntList();
list.Add(new IntNode(5, null));
list.Add(new IntNode(2, null));
list.Add(new IntNode(8, null));
list.Add(new IntNode(1, null));
list.ToString();
}
The problem is the increment step in the for loop. It needs to be p = p.getNext() not simply p.getNext(). The latter just calls the getNext function and does nothing with the return which means p is never modified and hence the loop doesn't make any progress
for (IntNode p = _head; p.getNext() != null; p = p.getNext()) { }
The next problem is you are not actually moving _head or using the p value. Hence you haven't actually found the place to insert. What you need is something like the following
IntNode p = _head;
while (p.getNext() != null) {
p = p.getNext();
}
p.setNext(node);
for (IntNode p = _head; p.getNext() != null; p.getNext()) { }
You're not using p anywhere, and not doing anything in the loop body. Can you spot your problem?
First, you don't assign the result of getNext() anywhere:
for (IntNode p = _head; p.getNext() != null; p.getNext()) { }
Second, you even don't use the last node anywhere. In fact, you even couldn't, because p doesn't exist outside of the for loop…
Advice: Keep a reference to the last node as well and make your life simpler.
Your loop never ends because p is not incremented.
It should be easier for you if you keep a reference to the last inserted item. For example :
private IntNode _lastNode;
public void Add(IntNode node)
{
if (_head == null)
_head = node;
else
{
if (_lastNode == null)
_lastNode = _head;
_lastNode.setNext(node)
_lastNode = node;
}
count++;
}
You won't have to loop through nodes each time you try to add a node.
I'm having problems trying to write a reverse recursive method for a LinkedList class I created in C#.
The LinkedList has 2 pointers in it one for the head and the other for the tail:
public class Node
{
public object data;
public Node next;
public Node(object Data)
{
this.data = Data;
}
}
public class LinkedList
{
Node head;
Node tail;
public void Add(Node n)
{
if (head == null)
{
head = n;
tail = head;
}
else
{
tail.next = n;
tail = tail.next;
}
}
Now, the recursive reverse function goes like this:
public void reverse_recursive()
{
Node temp_head = head;
if (temp_head == tail)
{
return;
}
while (temp_head != null)
{
if (temp_head.next == tail)
{
tail.next = temp_head;
tail = temp_head;
reverse_recursive();
}
temp_head = temp_head.next;
}
}
I'm having 2 troubles with it: first, a logic problem, I know that head doesn't point to the first node after the reverse. The second problem is that i probably do something wrong with the null pointer so the program crashes.
I also give you the main program:
class Program
{
static void Main(string[] args)
{
LinkedList L = new LinkedList();
L.Add(new Node("first"));
L.Add(new Node("second"));
L.Add(new Node("third"));
L.Add(new Node("forth"));
L.PrintNodes();
L.reverse_recursive();
L.PrintNodes();
Console.ReadLine();
}
}
Thank you for helping!!
public void Reverse()
{
this.Reverse(this.head);
}
private void Reverse(Node node)
{
if (node != null && node.next != null)
{
// Create temporary references to the nodes,
// because we will be overwriting the lists references.
Node next = node.next;
Node afterNext = node.next.next;
Node currentHead = this.head;
// Set the head to whatever node is next from the current node.
this.head = next;
// Reset the next node for the new head to be the previous head.
this.head.next = currentHead;
// Set the current nodes next node to be the previous next nodes next node :)
node.next = afterNext;
// Keep on trucking.
this.Reverse(node);
}
else
{
this.tail = node;
}
}
public void reverse()
{
reverse_recursive(tail);
Node tmp = tail;
tail = head;
head = tmp;
}
public void reverse_recursive(Node endNode)
{
Node temp_head = head;
if (temp_head == endNode)
{
return;
}
while (temp_head != null)
{
if (temp_head.next == endNode)
{
break;
}
temp_head = temp_head.next;
}
endNode.next = temp_head;
temp_head.next = null;
reverse_recursive(temp_head);
}
See also this
Another option over here.
class Program{
static void Main(string[] args)
{
LinkedList L = new LinkedList();
L.Add(new Node("first"));
L.Add(new Node("second"));
L.Add(new Node("third"));
L.Add(new Node("forth"));
L.PrintNodes();
L.reverse_recursive();
Console.WriteLine("---------------------");
L.PrintNodes();
Console.ReadLine();
}
}
public class Node
{
public object data;
public Node next;
public Node(object Data)
{
this.data = Data;
}
}
public class LinkedList
{
Node head;
Node tail;
public void Add(Node n)
{
if (head == null)
{
head = n;
tail = head;
}
else
{
tail.next = n;
tail = tail.next;
}
}
public void PrintNodes()
{
Node temp = head;
while (temp != null)
{
Console.WriteLine(temp.data);
temp = temp.next;
}
}
private LinkedList p_reverse_recursive(Node first)
{
LinkedList ret;
if (first.next == null)
{
Node aux = createNode(first.data);
ret = new LinkedList();
ret.Add(aux);
return ret;
}
else
{
ret = p_reverse_recursive(first.next);
ret.Add(createNode(first.data));
return ret;
}
}
private Node createNode(Object data)
{
Node node = new Node(data);
return node;
}
public void reverse_recursive()
{
if (head != null)
{
LinkedList aux = p_reverse_recursive(head);
head = aux.head;
tail = aux.tail;
}
}
}
Hope it helps
A second variant
private void p_reverse_recursive2(Node node)
{
if (node != null)
{
Node aux = node.next;
node.next = null;
p_reverse_recursive2(aux);
if (aux != null)
aux.next = node;
}
}
public void reverse_recursive()
{
if (head != null)
{
Node aux = head;
head = tail;
tail = aux;
p_reverse_recursive2(tail);
}
}
Variation on a theme...
public Node Reverse(Node head)
{
if(head == null)
{
return null;
}
Node reversedHead = null;
ReverseHelper(head, out reversedHead);
return reversedHead;
}
public Node ReverseHelper(Node n, out Node reversedHead)
{
if(n.Next == null)
{
reversedHead = n;
return n;
}
var reversedTail = ReverseHelper(n.Next, out reversedHead);
reversedTail.Next = n;
n.Next = null;
return n;
}
}
I was just playing with similar brain teaser with the only difference that LinkedList class only has a definition for head and all the rest of the nodes are linked there. So here is my quick and dirty recursive solution:
public Node ReverseRecursive(Node root)
{
Node temp = root;
if (root.next == null)
return root;
else
root = ReverseRecursive(root.next);
temp.next = null;
Node tail = root.next;
if (tail == null)
root.next = temp;
else
while (tail != null)
{
if (tail.next == null)
{
tail.next = temp;
break;
}
else
tail = tail.next;
}
return root;
}