Queue in generic linked list - c#

I'm new at this and trying to write this code for a queue but it doesn't work. I'm stuck, I wrote queue for a linked list and I can't find where I'm wrong here, any help is great. Thank you in advance.
class Queue
{
public Node<T> head { get; set; }
public Node<T> tail { get; set; }
public int size { get; set; }
public Queue()
{
head = null;
tail = null;
}
public void Enqueue(T value)
{
if (head == null)
{
head = new Node<T>(value);
tail = head;
}
else
{
tail.sljedeci = new Node<T>(value);
tail = tail.next;
}
size++;
}
public T Dequeue()
{
if (size == 0)
{
throw new IndexOutOfRangeException();
}
else if (size == 1)
{
T value = head.value;
size--;
head = tail = null;
return value;
}
else
{
T value = head.value;
head = head.next;
size--;
return value;
}
}
public void Peek()
{
if (size == 0)
{
Console.WriteLine("List is empty");
}
else
{
Node<T> temp = head;
Console.Write("{ ");
while (temp.next != null)
{
Console.Write("{0}, ", temp.value);
temp = temp.next;
}
Console.Write(temp.valuet + " }");
}
}

Related

Why is tail.previous returning a null

Im implementing a stack using a linked list. I am having trouble with my pop method where tail.previous is null. I think this is because im trying to assign to the node after the tail which doesnt exist. if this is the case, how can i fix it.
class DynamicStack<T>
{
private class Node
{
public T element { get; private set; }
public Node Next { get; set; }
public Node Previous { get; set; }
public Node(T element, Node prevNode)
{
this.element = element;
Next = null;
Previous = null;
if (prevNode != null)
prevNode.Next = this;
}
}
private Node head, tail;
public int Count { get; private set; }
public DynamicStack()
{
this.head = null;
this.tail = null;
this.Count = 0;
}
public T Pop()
{
if (Count == 0)
return default(T);
Node temp = tail;
tail = tail.Previous;
Count--;
return temp.element;
}
}
I have tried doing the pop method as below but it doesnt follow LIFO so its not correct.
public T Pop()
{
if (Count == 0)
return default(T);
Node temp = head;
head = head.Next;
Count--;
return temp.element;
}

How to connect leafs and returning a List for nodes around the BST

This is my implementation for BST (Binary Search Tree). Can Anyone help me to create two methods, one to Connect Leafs and one to return a List of Nodes around the Tree, for example: Connecting Leaf Example In this picture it shows how the leafs should be connected, Nodes that should be stored in List and the way the nodes stored in List need to be in this way where the root is the first element and going to the left passing down to leafs going back to root from the right. In my example it should be 8(root), 3, 1, 4, 7, 13, 14, 10, 8(root).
Thank You!
**class Node
{
private int VL;
private int Niv;
public Node Parent, LC, RC;
public Node()
{
this.Parent = this.LC = this.RC = null;
this.Niv = -1;
}
public Node(int x)
{
this.VL = x;
this.Parent = this.LC = this.RC = null;
this.Niv = -1;
}
public int Vlera
{
get { return this.VL; }
set { this.VL = value; }
}
public int Niveli
{
get { return this.Niv; }
set { this.Niv = value; }
}
}
class BSTree
{
public Node Root;
private int MaxNiv;
public BSTree()
{
this.Root = null;
this.MaxNiv = -1;
}
public void Insert(int x)
{
Node tmp = new Node(x);
if (this.Root == null)
{
tmp.Niveli = 0;
this.Root = tmp;
}
else InsertNode(tmp);
if (tmp.Niveli > this.MaxNiv) MaxNiv = tmp.Niveli;
}
public void ConnectLeafs()
{
//TODO
}
public List<T> ReturnNodesAroundTheTree()
{
//TODO
}
public Node GoTo_Node(Node nd)
{
return GoTo_Node_Rec(this.Root, nd);
}
public Node GoTo_Node(int x)
{
return GoTo_Node_Rec(this.Root, x);
}
private Node GoTo_Node_Rec(Node root, Node nd)
{
if (root.Vlera == nd.Vlera) return root;
if (root.Vlera > nd.Vlera) return GoTo_Node_Rec(root.LC, nd);
else return GoTo_Node_Rec(root.RC, nd);
}
private Node GoTo_Node_Rec(Node root, int x)
{
if (root.Vlera == x) return root;
if (root.Vlera > x) return GoTo_Node_Rec(root.LC, x);
else return GoTo_Node_Rec(root.RC, x);
}
private void InsertNode(Node nd)
{
Node tmp = InsertRecNode(this.Root, nd.Vlera);
if (nd.Vlera >= tmp.Vlera) tmp.RC = nd;
else tmp.LC = nd;
nd.Parent = tmp;
nd.Niveli = nd.Parent.Niveli++;
//if (nd.Niveli > this.MaxNiv) MaxNiv = nd.Niveli;
}
private Node InsertRecNode(Node root, int x)
{
if (x >= root.Vlera)
if (root.RC != null) return InsertRecNode(root.RC, x);
else return root;
else
if (root.LC != null) return InsertRecNode(root.LC, x);
else return root;
}
private bool IsRoot(Node nd)
{
if (nd.Parent == null) return true;
return false;
}
private bool IsLeaf(Node nd)
{
if (nd.LC == null && nd.RC == null) return true;
return false;
}**
Here is the easy way to do this. I created a List of all the node. When you create a new node add node to list. See code below
class BSTree
{
public Node Root;
private int MaxNiv;
private List<Node> nodes = new List<Node>();
public BSTree()
{
this.Root = null;
this.MaxNiv = -1;
}
public void Insert(int x)
{
Node tmp = new Node(x);
nodes.Add(tmp);
if (this.Root == null)
{
tmp.Niveli = 0;
this.Root = tmp;
}
else InsertNode(tmp);
if (tmp.Niveli > this.MaxNiv) MaxNiv = tmp.Niveli;
}
public void ConnectLeafs()
{
//TODO
}
public List<Node> ReturnNodesAroundTheTree()
{
return nodes.Where(x => IsLeaf(x)).ToList();
}
}

Copy/duplicate another linked list

basically I'm having a slight problem. I just cannot seem to get this to work. I've looked everywhere. I am trying to copy the list using my Concat method
public void Concat(LinkListGen<T> list2)
{
LinkGen<T> temp = list2.list;
while (temp != null)
{
AppendItem(temp.Data);
temp = temp.Next;
}
}
Which works fine.
However, I'm just not having any luck with my Copy method:
public void Copy(LinkListGen<T> list2)
{
LinkGen<T> temp = list2.list;
while (temp != null)
{
Concat(list2);
AppendItem(list2.list.Data);
temp = temp.Next;
}
}
I know this code is probably stupid, I've just been changing the coding/experimenting and trying.
When I execute the code, nothing is displayed.
list2.AddItem(56);
list2.AddItem(88);
list2.AddItem(17);
list.AddItem(40);
list.AddItem(11);
list.AddItem(77);
list3.Copy(list2);
list3.Copy(list);
System.Console.WriteLine("Copy List" + list3.DisplayItems());
Essentially I want the output like this (may have typed the numbers in the wrong order):
Copy List 56 88 17 40 11 77
Thank you.
EDIT- I'm an idiot, forgot to the AppendItem method.
public void AppendItem(T item)
{
LinkGen<T> temp = list;
if (list == null)
{
new LinkGen<T>(item, null);
}
else
{
while (temp.Next != null)
{
temp = temp.Next;
}
temp.Next = new LinkGen<T>(item, null);
}
}
EDIT --
class LinkGen<T>
{
private T data;
private LinkGen<T> next;
public LinkGen(T item)
{
data = item;
next = null;
}
public LinkGen(T item, LinkGen<T> list)
{
data = item;
next = list;
}
public LinkGen<T> Next
{
set { this.next = value; }
get { return this.next; }
}
public T Data
{
set { this.data = value; }
get { return this.data; }
}
}
}
class LinkListGen<T> where T : IComparable
{
private LinkGen<T> list;
public LinkListGen()
{
this.list = null;
}
public void AddItem(T item)
{
list = new LinkGen<T>(item, list);
}
public string DisplayItems() //write items to string and return
{
LinkGen<T> temp = list;
string buffer = "";
while (temp != null) // move one link and add head to the buffer
{
buffer += temp.Data + "";
temp = temp.Next;
}
return buffer;
}
public int NumberOfItems() // returns number of items in list
{
LinkGen<T> temp = list;
int count = 0;
while (temp != null) // move one link and add 1 to count
{
count++;
temp = temp.Next;
}
return count;
}
public void RemoveItem(int item)
{
LinkGen<T> current = list;
LinkGen<T> previous = null;
while (current != null)
{
if (current.Data.Equals(item))
{
if (previous != null)
{
previous.Next = current.Next;
current = current.Next;
}
else
{
previous = current;
current = current.Next;
list = current;
}
}
else
previous = current;
current = previous.Next;
}
}
public void InsertInOrder(T item)
{
LinkGen<T> temp = list;
if (list == null || list.Data.CompareTo(item) < 0)
{
list = new LinkGen<T>(item, this.list);
}
else
{
while (temp != null)
{
if (list.Data.CompareTo(item) == 0 || list.Data.CompareTo(item) > 0)
{
temp.Next = new LinkGen<T>(item, temp.Next);
temp = null;
}
else
{
temp = temp.Next;
}
}
}
}
public void AppendItem(T item)
{
LinkGen<T> temp = list;
if (list == null)
{
new LinkGen<T>(item, null);
}
else
{
while (temp.Next != null)
{
temp = temp.Next;
}
temp.Next = new LinkGen<T>(item, null);
}
}
public void Concat(LinkListGen<T> list2)
{
LinkGen<T> temp = list2.list;
while (temp != null)
{
AppendItem(temp.Data);
temp = temp.Next;
}
}
Suggested correction
class LinkListGen<T> where T : IComparable
{
private LinkGen<T> list;
public LinkListGen() { }
private LinkGen<T> LastNode ()
{
var temp = list;
while (temp != null && temp.Next != null)
temp = temp.Next;
return temp;
}
public void AppendItem(T item)
{
if (list == null)
list = new LinkGen<T>(item, null);
else
{
LinkGen<T> temp = LastNode();
temp.Next = new LinkGen<T>(item, null);
}
}
public void Concat(LinkListGen<T> list2)
{
if (list2 == null)
return;
LinkGen<T> temp = list2.list;
while (temp != null)
{
AppendItem(temp.Data);
temp = temp.Next;
}
}
public void Copy(LinkListGen<T> list2)
{
Concat(list2);
}
class LinkGen<T2>
{
public LinkGen(T2 item): this(item, null) { }
public LinkGen(T2 item, LinkGen<T2> list)
{
Data = item;
Next = list;
}
public LinkGen<T2> Next { set; get; }
public T2 Data { set ; get ; }
}
}
Answer-
public void Copy(LinkListGen<T> list2)
{
LinkGen<T> temp = list2.list;
while (temp != null)
{
AppendItem(temp.Data);
temp = temp.Next;
}
}
If anyone ever has the same problem I had. They're finally a solution. Yes, I know.... I'm an idiot!

how to delete first IntNode

class IntNode
{
public int value { get; set; }
public IntNode next { get; set; }
public IntNode(int value)
{
this.value = value;
this.next = null;
}
public IntNode(int value, IntNode next)
{
this.value = value;
this.next = next;
}
public bool HasNext()
{
return (next != null);
}
public override string ToString()
{
if (this.HasNext())
return value + "-->" + next;
else
return value + "";
}
}
so if I wanted to remove the 4th node with given list starts with head:
IntNode pos = head.next;
IntNode prev = head;
int counter = 0;
while (pos != null)
{
if (counter == 4) prev.next = pos.next;
prev = pos;
pos = pos.next;
}
But I want to remove the first node (when counter is 0). How can I do this? Thanks.
Something like this should do it for you:
while (pos != null)
{
if ( counter == 0 )
{
head = head.next;
prev = head;
}
else
{
prev.next = pos.next;
}
}
public void Remove(int index) {
if(head != null)
{
if(index == 0)
{
head = head.next;
}
else
{
IntNode pos = head.next;
IntNode prev = head;
while (pos != null)
{
--index;
if (index == 0)
{
prev.next = pos.next;
break;
}
prev = pos;
pos = pos.next;
}
}
}
}

Inserting Into a Generic Linked List

I'm having an issue inserting a new object into a linked list. I use a custom method called Insert that takes the object type and the index where it should be placed. However, when I insert the object, the list doesn't update. Any idea why?
The list:
GenericLinkedList<string> bet = new GenericLinkedList<string>();
bet.Add("a");
bet.Add("b");
bet.Add("c");
bet.Add("d");
//bet.Remove();
bet.Insert("x", 2);
The Generic Linked List Class:
public class GenericLinkedList<T> where T : IComparable<T>
{
public GenericLinkedList()
{
count = 0;
head = null;
}
#region Nested Node Class
private class Node<T>
{
private T data;
public T Data
{
get { return data; }
set { data = value; }
}
private Node<T> next;
public Node<T> Next
{
get { return next; }
set { next = value; }
}
public Node<T> Previous { get; set; }
public Node(T obj_t)
{
next = null;
data = obj_t;
}
public Node()
{
next = null;
}
}
#endregion
private Node<T> head;
private Node<T> tail;
private int count;
public int Count
{
get
{
int num;
if (count < 0)
num = 0;
else {
num = count;
}
return num;
}
}
}
The Insert Method:
public void Insert(T data, int index)
{
Node<T> replacedItem = new Node<T>();
Node<T> newItem = new Node<T>(data);
if (head == null)
{
head = new Node<T>(data);
tail = head;
}
else
{
Node<T> current = head;
Node<T> tempNode = new Node<T>();
if (index > 0 && index <= count + 1)
{
int c = 0;
while (current != null)
{
if (c == index)
{
tempNode = current;
current = newItem;
current.Next = tempNode;
}
else
{
current = current.Next;
}
c++;
}
}
count++;
}
}
The Add Method:
public void Add(T data)
{
count++;
if (head == null)
{
head = new Node<T>(data);
tail = head;
}
else
{
tail.Next = new Node<T>(data);
tail.Next.Previous = tail;
tail = tail.Next;
}
}
Need to add count++ in case head is null. Also count++ should go inside if(index>0&&index<=count+1)
public void Insert(T data, int index)
{
Node<T> replacedItem = new Node<T>();
Node<T> newItem = new Node<T>(data);
if (head == null)
{
head = new Node<T>(data);
tail = head;
count++;
}
else
{
Node<T> current = head;
Node<T> tempNode = new Node<T>();
if (index > 0 && index <= count + 1)
{
int c = 0;
while (current != null)
{
if (c == index)
{
tempNode = current;
current = newItem;
current.Next = tempNode;
}
else
{
current = current.Next;
}
c++;
}
count++;
}
}
}
See if this helps.
You not correctly add new item in your Insert method
in this place
tempNode = current; // save pointer to current item
current = newItem; // save in current new item
current.Next = tempNode; //set next to counter
but you don't change link from previous element, so when you again go from head you just miss your added item.
You need change this code something like
public void Insert(T data, int index)
{
Node<T> replacedItem = new Node<T>();
Node<T> newItem = new Node<T>(data);
if (head == null)
{
head = new Node<T>(data);
tail = head;
count++;
}
else
{
Node<T> current = head;
Node<T> tempNode = new Node<T>();
if (index > 0 && index <= count + 1)
{
int c = 0;
while (current != null)
{
if (c == index)
{
tempNode = current;
current = newItem;
//update link from previous element
if(tempNode.Previous != null)
tempNode.Previous.Next = current;
current.Next = tempNode;
current.Previous = tempNode.Previous;
tempNode.Previous = current;
count++;
break;
}
else
{
current = current.Next;
}
c++;
}
}
}
}

Categories

Resources