8 Replies - 4485 Views - Last Post: 21 September 2009 - 09:12 AM Rate Topic: -----

#1 panman1560  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-September 09

C# adding a stack to a linked list

Posted 17 September 2009 - 11:41 AM

I was given these instructions in my C# class
"You should implement your stack using a LinkedList<T> from the dll you downloaded. This class contains a no-arguments constructor and the following properties:
•Data, which gets or sets the data (of type T) at the front of the list; and
•Next, which gets or sets the LinkedList<T> containing the remainder of the list.
Though you shouldn't need it for this project, the source code for this class can be found on the Examples page on K-State Online. Maintain the elements of the stack in a LinkedList<T> so that the top of the stack is at the front of the list.
Your Stack<T> class should not contain any loops or recursion. These constructs are not needed for this project, and if used, they only make the code less efficient.

Testing Your Code
You can compile your Stack<T> using the "Start Debugging" button or menu entry. However, when it compiles without error, it will display the message, "A project with an Output Type of Class Library cannot be started directly." When you see this message, you will know that there are no syntax errors, and that a dll containing your class has been created. However, you will still need to test your code to ensure that it is working correctly. "

I have my generic stack implemented but do not understand how to implement the linked list in my stack.

This is my Stack Code
using System;
using RodHowell.Cis300.LinkedList;
using System.Linq;
using System.Text;

namespace DavidPankey.Cis300.StackClass
{
	public class Stack<T>
	{
		/// The elements of the stack.
		///
		private T[] _Stack = new T[10];

		/// The number of elements in the stack.
		///
		private int _count = 0;

		/// Gets the number of elements in the stack.
		///
		public int Count
		{
			get
			{
				return _count;
			}
		}

		/// Adds the given item at the top of the stack.
		///
		public void Push(T item)
		{
			if (_count == _Stack.Length)
			{
				T[] el = new T[2 * _Stack.Length];
				Array.Copy(_Stack, el, _Stack.Length);
				_Stack = el;
			}
			_Stack[_count++] = item;
			
		}

		/// Removes and returns the element at the top of the stack.
		///
		public T Pop()
		{
			if (_count == 0)
			{
				throw new InvalidOperationException();
			}
			return _Stack[--_count];
		}

		/// Returns the element at the top of the stack.
		///
		public T Peek()
		{
			if (_count == 0)
			{
				throw new InvalidOperationException();
			}
			return _Stack[_count - 1];
		}

		/// Clears the stack.
		///
		public void Clear()
		{
			_Stack = new T[10];
			_count = 0;
		}
	}
}


This is the code for the linked list
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RodHowell.Cis300.LinkedList
{
	/// A class implementing a generic linked list.
	///
	public class LinkedList<T>
	{
		/// The data.
		///
		private T _data = default(T);

		/// Gets or sets the data.
		///
		public T Data
		{
			get
			{
				return _data;
			}
			set
			{
				_data = value;
			}
		}

		/// The next element.
		///
		private LinkedList<T> _next = null;

		/// Gets or sets the next element.
		///
		public LinkedList<T> Next
		{
			get
			{
				return _next;
			}
			set
			{
				_next = value;
			}
		}
	}
}



If you could please explain it perhaps a little better then my instructor I would be very grateful. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: C# adding a stack to a linked list

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9392
  • View blog
  • Posts: 35,271
  • Joined: 12-June 08

Re: C# adding a stack to a linked list

Posted 17 September 2009 - 11:51 AM

Using the link list code you got make a stack. Basically how do you enforce the FILO (first in last out) constraint via a linked list?
Was This Post Helpful? 0
  • +
  • -

#3 Aeternalis  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: C# adding a stack to a linked list

Posted 17 September 2009 - 12:31 PM

Yeah, what you have done is created a generic stack class that uses a traditional representation to store the data. (A templated Array). Instead of using your array.. he wants you to use his Linked list class as the storage structure for the data.

Change your member methods of Stack to create and use the linked list to store your data. Modify the functions to maintain proper Stack behavior, you should be golden.

Aet
Was This Post Helpful? 0
  • +
  • -

#4 simy307  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-September 09

Re: C# adding a stack to a linked list

Posted 17 September 2009 - 12:52 PM

View PostAeternalis, on 17 Sep, 2009 - 11:31 AM, said:

Yeah, what you have done is created a generic stack class that uses a traditional representation to store the data. (A templated Array). Instead of using your array.. he wants you to use his Linked list class as the storage structure for the data.

Change your member methods of Stack to create and use the linked list to store your data. Modify the functions to maintain proper Stack behavior, you should be golden.

Aet



Any chance you could please give a small example Aeternalis?




-

This post has been edited by simy307: 17 September 2009 - 01:01 PM

Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9392
  • View blog
  • Posts: 35,271
  • Joined: 12-June 08

Re: C# adding a stack to a linked list

Posted 17 September 2009 - 01:28 PM

View Postsimy307, on 17 Sep, 2009 - 01:52 PM, said:

View PostAeternalis, on 17 Sep, 2009 - 11:31 AM, said:

Yeah, what you have done is created a generic stack class that uses a traditional representation to store the data. (A templated Array). Instead of using your array.. he wants you to use his Linked list class as the storage structure for the data.

Change your member methods of Stack to create and use the linked list to store your data. Modify the functions to maintain proper Stack behavior, you should be golden.

Aet



Any chance you could please give a small example Aeternalis?




-



Ah.. any "small example" would be the structure you are looking for! :P
Was This Post Helpful? 0
  • +
  • -

#6 simy307  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-September 09

Re: C# adding a stack to a linked list

Posted 17 September 2009 - 02:15 PM

This is what i have so far on mine, i am completly lost, i guess i just dont know what im really suposed to do, any help?

using System;
using RodHowell.Cis300.LinkedList;
using System.Linq;
using System.Text;

namespace BrianSimonson.Cis300.Stack
{
	public class Stack<T>
	{  
		private LinkedList<T> _list = new LinkedList<T>();

		private int _count = 0;

		public int Count
		{
			get
			{
				return _count;
			}
		}

		public void Push(T item)
		{
			if (_list.Data == null)
			{
				_list.Data = item;
			}
			else
			{
				//adding to next isnt working here
			}
		}

		public T Pop()
		{
		 return _list.Data;
		}

		public T Peek()
		{
		   ///returning next isnt working here

		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9392
  • View blog
  • Posts: 35,271
  • Joined: 12-June 08

Re: C# adding a stack to a linked list

Posted 17 September 2009 - 05:42 PM

How would you control the location of the first object in so it's the last object out?
Was This Post Helpful? 0
  • +
  • -

#8 simy307  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-September 09

Re: C# adding a stack to a linked list

Posted 17 September 2009 - 05:52 PM

I have no idea thats why im so lost, I know how to make a stack but i dont know how to intergrate them. Like im trying to call the set method in LinkedList Next but it wont work.
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9392
  • View blog
  • Posts: 35,271
  • Joined: 12-June 08

Re: C# adding a stack to a linked list

Posted 21 September 2009 - 09:12 AM

You can always think of your linked list as a storage mechanism for the values.. and combine that with an array for proper indexing you can figure out what to insert and remove in what order..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1