Download presentation
Presentation is loading. Please wait.
Published byΑνδρομέδη Σκλαβούνος Modified over 6 years ago
1
Data Structures and Database Applications Stacks in C#
2
Stack Overview System.Collections.Generic.Stack
Automatic resizing and homogeneous list Represents a stack of elements
3
The Stack ADT A stack is a list with the restriction
that insertions and deletions can only be performed at the top of the list The other end is called bottom Fundamental operations: Push: Equivalent to an insert Pop: Deletes the most recently inserted element Peek: Examines the most recently inserted element
4
The Stack ADT Stacks are less flexible than normal lists.
But are more efficient and easy to implement Stacks are known as LIFO (Last In, First Out) lists. The last element inserted will be the first to be retrieved Stacks are analogous to a spring-loaded stack of plates in a cafeteria. Clean plates are placed on top of the stack, pushing down any already there. When a plate is removed from the stack, the one below it pops up to become the new top.
5
Push and Pop Primary operations: Push and Pop Push Pop
Add an element to the top of the stack Pop Remove the element at the top of the stack empty stack push an element push another pop top B top top A A A top
6
Creating and Traversing a Stack
Example: Stack<string> numbers = new Stack<string>(); numbers.Push("one"); numbers.Push("two"); numbers.Push("three"); numbers.Push("four"); // A stack can be enumerated without disturbing its contents. Console.Write("There are {0} stack elements",numbers.Count); Console.Write(" in the following order:\n\t"); while (numbers.Peek() != null) { Console.Write(numbers.Pop()); Console.Write((numbers.Peek() != null) ? ", " : "\n")); } This prints the following two lines: There are 4 stack elements in the following order: four, three, two, one
7
Common C# Stack Methods
Push() Pop() Peek() Clear()
8
Stack Database Implementation
To create an efficient Database implementation of a Stack we will continue using the Tables we created in the last assignment which provided us with the Next and Prev pointers and Header fields for the size of the list, and the first and last elements of the list. We shall also continue to use those same methods we developed to get entries, and to get and set the front and rear of the list, and to clear the list, in order to develop the Stack methods that we need.
9
Stack Database Implementation
The following implements the Push operation using the Entry Data Model and some of the get and set operations for the Linked List.
10
Stack Database Implementation
The following implements the Pop operation using the Entry Data Model and some of the get and set operations for the Linked List.
11
Stack Database Implementation
And the following implements the simple Peek operation.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.