//returning data from top element and deleting it public int Pop() { if (top != null) { int result = top.data; top = top.next; stackSize--; return(result); } else { throw new InvalidOperationException(); } }
//making possible to use foreach cycle for our class private IEnumerable <int> GetData(MyStackCell toFindData) { if (toFindData.next != null) { yield return(toFindData.data); foreach (var i in GetData(toFindData.next)) { yield return(i); } } else { yield return(toFindData.data); } }
//adding data to stack public void Push(int data) { //creating new variable for stack cell MyStackCell cell = new MyStackCell(data); //if stack is empty top becomes holder of data, also it's next field points on nothing if (top == null) { top = cell; } //if stack has some data we just write top cell in next field of new cell, and save new cell in top field else { cell.next = top; top = cell; } //just counting data in stack stackSize++; }
public MyStackCell(int Data) { data = Data; next = null; }