stack%2badt

4
JBIET M.T ech. CSE Subject : APS Abstract data type In computing, an abstract data type or abstract data structure is a mathematical model for a certain class of data structures that have similar behavior; or for certain data types of one or more programming languages that have similar semantics. An abstract data type is defined indirectly, only by the operations that may be performed on it and by mathematical constraints on the effects (and possibly cost) of those operations. For example, an abstract stack data structure could be defined by to operations! push, that inserts some data item into the structure, and pop, that extracts an item from it; ith the constraint that each pop alays returns the most recently pushed item that has not been popped yet. "hen analy#ing the efficiency of algorithms that use stacks, one may also specify that both operations take the same time no matter ho many items have been pushed into the stack, and that the stack uses a constant amount of storage for each element. Abstract data types are purely theoretical entities, used to simplify the description of abstract algorithms, to classify and evaluate data structures. $oever, an A%& may be implemented by specific data types or data structures, i n many ays and in many programming languages. A%&s are often implemented as modules! the module's interface declares procedures that correspond to the A%& operations, sometimes ith comments that describe the constraints. &his information hiding strategy allos the implementation of the module to be changed ithout disturbing the client programs. Abs tract data t ypes are also an important conceptual tool in obectoriented programming and design by contract methodologies for softare development. &he name *abstract data type* apparently as coined by researchers in softare engineering and programming language design; hile *abstract data structure* as coined by researchers in data structures and algorithms. Array implementation of the Stack ADT &he instance variables for this implementation are an array of +bects, hich ill contain the items on the stack, and an integer index hich ill keep track of the next available space in the array. Initially , the array is empty and the index is . &o add an element to the stack (push), e'll copy a reference to it onto the stack and increment the index. &o remove an element (pop) e have to decrement the index first and then copy the element out. $ere is the class definition! public class -tack +bect/0 array; int index; public -tack () this.array 1 ne +bect/2340; this.index 1 ;  5 5  KVR Rao Page 1 of4

Upload: vam-si

Post on 03-Jun-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Stack%2Badt

 

JBIET M.Tech. CSE Subject : APS

Abstract data typeIn computing, an abstract data type or abstract data structure is a mathematical model for a certain

class of data structures that have similar behavior; or for certain data types of one or moreprogramming languages that have similar semantics. An abstract data type is defined indirectly,

only by the operations that may be performed on it and by mathematical constraints on the effects

(and possibly cost) of those operations.

For example, an abstract stack data structure could be defined by to operations! push, that insertssome data item into the structure, and pop, that extracts an item from it; ith the constraint that each

pop alays returns the most recently pushed item that has not been popped yet. "hen analy#ing theefficiency of algorithms that use stacks, one may also specify that both operations take the same

time no matter ho many items have been pushed into the stack, and that the stack uses a constant

amount of storage for each element.

Abstract data types are purely theoretical entities, used to simplify the description of abstractalgorithms, to classify and evaluate data structures. $oever, an A%& may be implemented by

specific data types or data structures, in many ays and in many programming languages. A%&s are

often implemented as modules! the module's interface declares procedures that correspond to theA%& operations, sometimes ith comments that describe the constraints. &his information hiding

strategy allos the implementation of the module to be changed ithout disturbing the clientprograms. Abstract data types are also an important conceptual tool in obectoriented programming

and design by contract methodologies for softare development.

&he name *abstract data type* apparently as coined by researchers in softare engineering and

programming language design; hile *abstract data structure* as coined by researchers in datastructures and algorithms.

Array implementation of the Stack ADT&he instance variables for this implementation are an array of +bects, hich ill contain the items

on the stack, and an integer index hich ill keep track of the next available space in the array.Initially, the array is empty and the index is .

&o add an element to the stack (push), e'll copy a reference to it onto the stack and increment the

index. &o remove an element (pop) e have to decrement the index first and then copy the elementout.

$ere is the class definition!

public class -tack +bect/0 array;

int index;public -tack ()

this.array 1 ne +bect/2340;

this.index 1 ;

  5

5

 KVR Rao Page 1 of4

Page 2: Stack%2Badt

 

JBIET M.Tech. CSE Subject : APS

As usual, once e have chosen the instance variables, it is a mechanical process to rite a

constructor. For no, the default si#e is 234 items. 6ater e ill consider better ays of handling

this.

7hecking for an empty stack is trivial.

public boolean is8mpty () return index 11 ;

5

It is important to remember, though, that the number of elements in the stack is not the same as the

si#e of the array. Initially the si#e is 234, but the number of elements is .

&he implementations of push and pop follo naturally from the specification.

public void push (+bect item)

array/index0 1 item;index99;

5public +bect pop ()

index;

return array/index0;5

&o test these methods, e can take advantage of the client code e used to exercise the builtin

-tack. All e have to do is comment out the line import ava.util.-tack. &hen, instead of using thestack implementation from ava.util the program ill use the implementation e ust rote.

If everything goes according to plan, the program should ork ithout any additional changes.

Again, one of the strengths of using an A%& is that you can change implementations ithout

changing client code.

:o e go for the Algorithms to present these operations in a -tack.

Alogorithm!ush(-t,t,x)! &his algorithm inserts an element 'x' onto the top of the stack '-t'. &hestack pointer is the 't'.

-tep 2). /7heck the stack overflo 0

If t 1 <ax

then riteln('-tack overflo on =-$')

return

-tep 3). t > t92

step ?). -t/t0 > x

step @). eturn

Alogorithm!&op(-t,t)! &his algorithm returns the top element of the stack '-t'. &he stack pointer is

the 't'.

-tep 2). If t 1

then riteln('-tack =nderflo on &op')

return :=66

else eturn -t/t0

 KVR Rao Page 2 of4

Page 3: Stack%2Badt

 

JBIET M.Tech. CSE Subject : APS

Alogorithm!op(-t,t)! &his algorithm deletes an element from the top of the stack '-t'. &he stack

pointer is the 't'.

-tep 2). /7heck the stack =nderflo 0

If t 1

then riteln('-tack =nderflo on op')

return :=66

-tep 3). x > -t/t0

step ?). t > t2

step @). eturn x

Linked Lists&he 6inked 6ist uses %ynamic memory Allocation, unlike Array, "here it uses static

memory allocation. &he 6inked lists are generally of to types A -ingle 6inked 6ist and a %oubly6inked 6ist. 8ach item of a linked list is sored in a speciali#ed data structure called :+%8. A :ode

of the singly 6inked 6ist consists of to parts of memory namely an Item and a next pointer; theItem stores the data part and the next pointer gives address of another :ode, the next :ode of the

linked 6ist. A double 6inked list :ode contains an additional pointer called the previous pointer to

point to the revious :ode.

A linked list is headed by a $ead :ode that points the First :ode of the list. &he linked list:odes provide fe operations to set and get the fields of the :ode. &hey are listed belo

• getItem() !"ill return %ata part of the :ode

• setItem() ! &o set the %ata part of the :ode

• get:ext() ! &o return the -ucceeding :ode address

• set:ext() ! &o set as the -uccessor :ode

• getrev() ! &o return the predecessor :ode

• setrev() ! &o set as the predecessor :ode

Linked Implementation of the Stack&he 6inked -tack implementation uses a '&op' pointer as the $ead of the 6ist. "hich pooints

to the top elemet of the -tack, "here from ush, pop and &op operations ill originate. All these

operations use an Inbuilt 6inked 6ist of Free :odes, ABAI6. &his ABAI6 is to be updated afterevery operation. &his is also called the ABAI6 stack due to its behaviour.

Alogorithm!ush(&op,x)! &his algorithm adds a :ode containing item 'x' at the front of the list

pointed by the &op.

-tep 2). /7heck the Availability of a free :ode0

If ABAI6 1 :=66

 KVR Rao Page 3 of4

Page 4: Stack%2Badt

 

JBIET M.Tech. CSE Subject : APS

then riteln('<emory +ut for =-$')

return

-tep3 ). / Cet a free :ode from the ABAI6 0

 :e > ABAI6

ABAI6 > ABAI6.next

-tep?). :e.next > &op

 :e.item > x

 &op > :e

-tep@). eturn

Alogorithm!&+(&op)! &his algorithm returns item at the top element of the stack '-t'. &he stack

pointer is the '&op'.

-tep 2). if &op 1 :=66

then "riteln('-tack is 8mpty')

  eturn(:=66)

-tep 3). eturn(getItem(get:ext(&op)))

Alogorithm!op(&op)! &his algorithm returns an element from the top of the stack after deleting it

from the 6inked list of stack and updates the ABAI6. &he stack pointer is the '&op'.

-tep 2). /7heck the stack =nderflo 0

If &op 1 :=66

then riteln('-tack =nderflo on op')

return :=66

-tep 3). :e > &op

-tep?). &op > get:ext(:e)

-tep@). D > getItem(:e)

-tepE). :e.next > ABAI6

-tep). ABAI6 > :e

-tepG). eturn x

 KVR Rao Page 4 of4