Hello I have a stack code and a queue code. I am trying to make a very simple doubly linked list but, I am having a very difficult time understanding how to create a doubly linked list using my stack and queues codes:
this is my stack code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
customer customer1 = new customer()
{
ID = 101,
Name = "Robert",
Gender = "Male"
};
customer customer2 = new customer()
{
ID = 102,
Name = "Paula",
Gender = "Female"
};
customer customer3 = new customer()
{
ID = 103,
Name = "Mike",
Gender = "Male"
};
customer customer4 = new customer()
{
ID = 104,
Name = "Phyllis",
Gender = "Female"
};
Stack stackCustomers = new Stack();
stackCustomers.Push(customer1);
stackCustomers.Push(customer2);
stackCustomers.Push(customer3);
stackCustomers.Push(customer4);
foreach (customer c1 in stackCustomers)
{
Console.WriteLine(c1.ID + " - " + c1.Name);
Console.WriteLine("Items left in stack : " + stackCustomers.Count);
}
customer c1 = stackCustomers.Pop();
Console.WriteLine(c1.ID + " - " + c1.Name);
Console.WriteLine("Total items in the queue = " + stackCustomers.Count);
customer c2 = stackCustomers.Pop();
Console.WriteLine(c2.ID + " - " + c2.Name);
Console.WriteLine("Total items in the queue = " + stackCustomers.Count);
customer c3 = stackCustomers.Pop();
Console.WriteLine(c3.ID + " - " + c3.Name);
Console.WriteLine("Total items in the queue = " + stackCustomers.Count);
customer c4 = stackCustomers.Pop();
Console.WriteLine(c4.ID + " - " + c4.Name);
Console.WriteLine("Total items in the queue = " + stackCustomers.Count);
}
}
public class customer
{
public int ID { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
customer customer1 = new customer()
{
ID = 101,
Name = "Robert",
Gender = "Male"
};
customer customer2 = new customer()
{
ID = 102,
Name = "Paula",
Gender = "Female"
};
customer customer3 = new customer()
{
ID = 103,
Name = "Mike",
Gender = "Male"
};
customer customer4 = new customer()
{
ID = 104,
Name = "Phyllis",
Gender = "Female"
};
Stack
stackCustomers.Push(customer1);
stackCustomers.Push(customer2);
stackCustomers.Push(customer3);
stackCustomers.Push(customer4);
foreach (customer c1 in stackCustomers)
{
Console.WriteLine(c1.ID + " - " + c1.Name);
Console.WriteLine("Items left in stack : " + stackCustomers.Count);
}
customer c1 = stackCustomers.Pop();
Console.WriteLine(c1.ID + " - " + c1.Name);
Console.WriteLine("Total items in the queue = " + stackCustomers.Count);
customer c2 = stackCustomers.Pop();
Console.WriteLine(c2.ID + " - " + c2.Name);
Console.WriteLine("Total items in the queue = " + stackCustomers.Count);
customer c3 = stackCustomers.Pop();
Console.WriteLine(c3.ID + " - " + c3.Name);
Console.WriteLine("Total items in the queue = " + stackCustomers.Count);
customer c4 = stackCustomers.Pop();
Console.WriteLine(c4.ID + " - " + c4.Name);
Console.WriteLine("Total items in the queue = " + stackCustomers.Count);
}
}
public class customer
{
public int ID { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
}
}
This is my queue code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
customer customer1 = new customer()
{
ID = 101,
Name = "Robert",
Gender = "Male"
};
customer customer2 = new customer()
{
ID = 102,
Name = "Paula",
Gender = "Female"
};
customer customer3 = new customer()
{
ID = 103,
Name = "Mike",
Gender = "Male"
};
customer customer4 = new customer()
{
ID = 104,
Name = "Phyllis",
Gender = "Female"
};
Queue queueCustomers = new Queue();
queueCustomers.Enqueue(customer1);
queueCustomers.Enqueue(customer2);
queueCustomers.Enqueue(customer3);
queueCustomers.Enqueue(customer4);
customer c1 = queueCustomers.Dequeue();
Console.WriteLine(c1.ID + " - " + c1.Name);
Console.WriteLine("Total items in the queue = " + queueCustomers.Count);
customer c2 = queueCustomers.Dequeue();
Console.WriteLine(c2.ID + " - " + c2.Name);
Console.WriteLine("Total items in the queue = " + queueCustomers.Count);
customer c3 = queueCustomers.Dequeue();
Console.WriteLine(c3.ID + " - " + c3.Name);
Console.WriteLine("Total items in the queue = " + queueCustomers.Count);
customer c4 = queueCustomers.Dequeue();
Console.WriteLine(c4.ID + " - " + c4.Name);
Console.WriteLine("Total items in the queue = " + queueCustomers.Count);
}
}
public class customer
{
public int ID { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
customer customer1 = new customer()
{
ID = 101,
Name = "Robert",
Gender = "Male"
};
customer customer2 = new customer()
{
ID = 102,
Name = "Paula",
Gender = "Female"
};
customer customer3 = new customer()
{
ID = 103,
Name = "Mike",
Gender = "Male"
};
customer customer4 = new customer()
{
ID = 104,
Name = "Phyllis",
Gender = "Female"
};
Queue
queueCustomers.Enqueue(customer1);
queueCustomers.Enqueue(customer2);
queueCustomers.Enqueue(customer3);
queueCustomers.Enqueue(customer4);
customer c1 = queueCustomers.Dequeue();
Console.WriteLine(c1.ID + " - " + c1.Name);
Console.WriteLine("Total items in the queue = " + queueCustomers.Count);
customer c2 = queueCustomers.Dequeue();
Console.WriteLine(c2.ID + " - " + c2.Name);
Console.WriteLine("Total items in the queue = " + queueCustomers.Count);
customer c3 = queueCustomers.Dequeue();
Console.WriteLine(c3.ID + " - " + c3.Name);
Console.WriteLine("Total items in the queue = " + queueCustomers.Count);
customer c4 = queueCustomers.Dequeue();
Console.WriteLine(c4.ID + " - " + c4.Name);
Console.WriteLine("Total items in the queue = " + queueCustomers.Count);
}
}
public class customer
{
public int ID { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
}
}
is it possible to make a doubly linked list using my stack code and my queue code? if so how exactly do I do it? remember i am just trying to make a very simple doubly linked list using stacks and queues. something very simple that an help me understand it easier. please help. thank you in advance even for just taking the time to look at my question.
VulpesPosted May 18, 2014, 5:57 PM
The LinkedList
Below I've changed your stack program so that it now uses a custom stack class (MyStack
The output is the same as before, namely:
Perhaps I could leave it now to you to implement your own MyQueue class.
Remember that a stack is a LIFO structure whereas a queue is a FIFO structure.
A APosted May 17, 2014, 9:20 PM
VulpesPosted May 17, 2014, 3:08 PM
In fact, on the face of it, it can't be done. The essence of a linked list is that you can insert a node anywhere in the chain. With a stack or queue you can only add a node at one end.
If this is some sort of exercise, are you sure that you've understood it correctly?