Data structure is the heart of any programming language. Back in the days of C, all complex data structures, such as stack, linked list, queue etc., were hand coded. Writing code not only required in depth study of those data structure but also a lot of time in implementing and testing. Now, with sophisticated programming languages, they are all available ready made.
Nevertheless, I shall describe the intricacy involved in developing these data structures and a few Nunit test cases to validate what has been implemented.
In the list of data structures, first is stack.
Stack:
A stack is a data structure that follows last in first out (LIFO) strategy. The elements entered last would be the first one to come out from the list of elements. There are two operations on stack:
- Push (d): inserts element d in the list.
- Pop: remove /deletes the last element inserted from the list.
You may find several stack implementations on the internet but many of them tried growing the list of elements to double its size when the max limit is reached. The consequences of a growing list are that it gives a user a possibility to call a Pop operation more times than the number of elements in the list. Ideally, Pop should stop when the stack has hit its bottom i.e. the last element (the very first entered).
Let me give some technical details on how can it be achieved:
Push method takes an element and inserts the element in an array and currentIndex; it records the current location of the element. As soon as currentIndex reaches to the length of the array, the size of the array is increased by one.
- /// <summary>
- /// Pushes data on to top of stack
- /// </summary>
- /// <param name="data"></param>
- public void Push(T data)
- {
- if (_currentIndex == _stackCollection.Length)
- {
- Array.Resize(ref _stackCollection, _stackCollection.Length + 1);
- }
- _stackCollection[_currentIndex++] = data;
- }
- /// <summary>
- /// removes an element from array and resizes it
- /// </summary>
- /// <returns></returns>
- public T Pop()
- {
- if (_currentIndex == 0)
- throw new InvalidOperationException("The stack is empty");
- var value = _stackCollection[--_currentIndex];
- Array.Resize(ref _stackCollection, _stackCollection.Length - 1);
- return value;
- }
To validate two methods, I have written two unit test cases. Here they are for your understanding:
- To check if number of elements are correct in thecount even if a pop operation is called in between.
- [TestCase]
- public void TestPushItem()
- {
- IStack < int > stack = new StackImpl < int > (120);
- stack.Push(20);
- stack.Push(30);
- stack.Push(40);
- stack.Pop();
- stack.Push(50); // <== Top Element
- var count = stack.Count();
- Assert.AreEqual(4, count);
- }
- The second test case is to check the top element after executing a couple of operations:
- [TestCase]
- public void TestTopElement()
- {
- IStack < int > stack = new StackImpl < int > (120);
- stack.Push(20);
- stack.Push(30);
- stack.Push(40);
- stack.Pop();
- stack.Push(50); // <== Top Element
- Assert.AreEqual(50, stack.Pop());
- Assert.AreEqual(30, stack.GetTop());
- }
For more details, have a look at the project attached.

Ehsan SajjadPosted Feb 18, 2016, 1:19 PM
Nice work
Santhakumar MunuswamyPosted Feb 12, 2016, 11:27 PM
Thanks for nice share