File tree Expand file tree Collapse file tree 2 files changed +20
-11
lines changed
Expand file tree Collapse file tree 2 files changed +20
-11
lines changed Original file line number Diff line number Diff line change 11# Stacks
22
3- A stack is a basic data structure that can be logically thought as linear structure represented by a real physical
4- stack or pile, a structure where insertion and deletion of items takes place at one end called top of the stack. The
5- basic concept can be illustrated by thinking of your data set as a stack of plates or books where you can only take the
6- top item off the stack in order to remove things from it.
3+ A stack is a data structure that can be logically thought of as a real physical stack or pile, a structure where
4+ insertion and deletion of items takes place at one end only called the " top" of the stack. The basic concept can be
5+ illustrated by thinking of your data set as a stack of plates or books where you can only take the top item off the
6+ stack in order to remove things from it.
77
88![ ] ( http://i.imgur.com/dax54C9.jpg )
Original file line number Diff line number Diff line change 33function Stack ( ) {
44 let items = [ ] ;
55
6- // remove all items in the stack
6+ // remove all items
77 this . clear = function ( ) {
88 items = [ ] ;
99 } ;
@@ -13,25 +13,30 @@ function Stack() {
1313 return items . length == 0 ;
1414 } ;
1515
16- // return last item added to stack
16+ // return last item added
1717 this . peek = function ( ) {
1818 return items [ items . length - 1 ] ;
1919 } ;
2020
21+ // remove last item from end
22+ this . pop = function ( ) {
23+ return items . pop ( ) ;
24+ } ;
25+
2126 // display all items
2227 this . print = function ( ) {
2328 console . log ( items . toString ( ) ) ;
2429 } ;
2530
26- // add item to end of stack
31+ // add item to end
2732 this . push = function ( element ) {
2833 items . push ( element ) ;
2934 } ;
3035
31- // remove last item from stack
32- this . pop = function ( ) {
33- return items . pop ( ) ;
34- }
36+ // get size of stack
37+ this . size = function ( ) {
38+ return items . length ;
39+ } ;
3540
3641}
3742
@@ -42,7 +47,11 @@ buckysStack.push(87);
4247buckysStack . push ( 29 ) ;
4348buckysStack . push ( 71 ) ;
4449buckysStack . print ( ) ;
50+
51+ console . log ( '--------------------' ) ;
52+
4553console . log ( buckysStack . peek ( ) ) ;
54+ console . log ( buckysStack . size ( ) ) ;
4655
4756console . log ( '--------------------' ) ;
4857
You can’t perform that action at this time.
0 commit comments