Stack project

25
Data Structures Stack Dr. Mohammed Belal Submitted to :

description

Stack

Transcript of Stack project

Page 1: Stack project

Data Structures

Stack

Dr. Mohammed Belal

Submitted to :

Page 2: Stack project

Data Structures & Algorithms

A data structure is an arrangement of data in a computer’s memory (or sometimes on a disk). Data structures include arrays, linked lists, stacks, binary trees, and hash tables, among others.

Algorithms manipulate the data in these structures in various ways, such as searching for a particular data item and sorting the data.

Page 3: Stack project

Characteristics of Data Structures

Disadvantag

es

Advantages Data

Structure Slow search, slow deletion, fixed size.

Quick insertion, very search,Fast access if index known.

Array

Slow insertion and deletion, fixed size .

Quicker search than unsorted array .

Ordered array

Slow access to other items.

Provides last-in, first-out access. Stack

Slow access to other items.

Provides first-in, first-out access. Queue

Slow search. Quick insertion ,quick

deletion.

Linked list

Page 4: Stack project

Characteristics of Data Structures (cont’d)

Disadvantage

s

Advantages Data

Structure Deletion algorithm is complex.

Quick search, insertion, deletion (if tree is complex.remains balanced).

Binary tree

Complex. Quick search, insertion, deletion. Tree alwaysbalanced.

Red-black tree

Complex. Quick search, insertion, deletion. Tree alwaysbalanced. Similar treesgood for disk storage.

2-3-4 tree

Slow deletion,nefficientmemory usage.

Quick insertion ,quick

deletion. Hash table

Page 5: Stack project

Characteristics of Data Structures (cont’d)

Disadvantag

es

Advantages Data

Structure Slow access to other items.

Fast insertion, deletion, Slow access to largest item.

Heap

Some algorithms are slow and complex .

Models real-worldsituations. Graph

The data structures shown in Table , except the arrays, can be thought of asAbstract Data Types, or ADTs.

Page 6: Stack project

A stack is a list like a structure in which all insertions and deletions are made at one end, called the top. The last element to be inserted into the stack will be the first to be removed. Thus stacks are sometimes referred to as Last In First Out (LIFO) lists.

Stack

Basic operations are push and pop.

Often top and isEmpty are available, too.

Also known as "last-in, first-out" or LIFOATop

Page 7: Stack project

A stack is a list like a structure in which all insertions and deletions are made at one end, called the top. The last element to be inserted into the stack will be the first to be removed. Thus stacks are sometimes referred to as Last In First Out (LIFO) lists.

Stack

Basic operations are push and pop.

Often top and isEmpty are available, too.

Also known as "last-in, first-out" or LIFOABTop

Page 8: Stack project

A stack is a list like a structure in which all insertions and deletions are made at one end, called the top. The last element to be inserted into the stack will be the first to be removed. Thus stacks are sometimes referred to as Last In First Out (LIFO) lists.

Stack

Basic operations are push and pop.

Often top and isEmpty are available, too.

Also known as "last-in, first-out" or LIFOAB

Top C

Page 9: Stack project

A stack is a list like a structure in which all insertions and deletions are made at one end, called the top. The last element to be inserted into the stack will be the first to be removed. Thus stacks are sometimes referred to as Last In First Out (LIFO) lists.

Stack

Basic operations are push and pop.

Often top and isEmpty are available, too.

Also known as "last-in, first-out" or LIFOAB

TopCD

Page 10: Stack project

A stack is a list like a structure in which all insertions and deletions are made at one end, called the top. The last element to be inserted into the stack will be the first to be removed. Thus stacks are sometimes referred to as Last In First Out (LIFO) lists.

Stack

Basic operations are push and pop.

Often top and isEmpty are available, too.

Also known as "last-in, first-out" or LIFOA

DCB

ETop

Page 11: Stack project

Real life

Stack Applications

Pile of books Plate trays

More applications related to computer science

Program execution stack

Evaluating expressions

Page 12: Stack project

Stack Operations• Push(x)insert the element x to the top of the stack• Pop()remove element from the top• Top()check the next element to be removed (do not remove)• IsEmpty()check whether the stack is empty

Page 13: Stack project

Push Function :

A

CB

Top

(1)

CBA

Top

(2)

D

A

Top

B

DC

(3)

Push

Algorithm push(o)if t = S.length − 1 thenthrow FullStackExceptionelset ← t + 1S[t] ← o

Page 14: Stack project

Pop Function :

A

CB

Top

(1)

A

B

BTopABTop

(3)(2)

Pop

Algorithm pop( )if isEmpty( ) thenthrow EmptyStackExceptionelset ← t − 1return S[t + 1]

Page 15: Stack project

Top Function :DCBA

Top

Is Empty Function :

Top 0

Algorithm Top(){Return s [ Top] ;}

Algorithm Is Empty()

If S null{Return true ;elseReturn false ;}

Page 16: Stack project

Stack Code For Javaclass Stack X

{Private int maxSize ; // size of stack arrayPrivate long [] stackArray ;Private int top ; // top of stack//--------------------------------------------------------------Public Stack X ( int s ) // constructor

{maxSize = s ; // set array sizestackAr ray = new long [ maxSize ] ; // create array top = -1 ; // no items yet

{//--------------------------------------------------------------

public void push ( long j ) // put item on top of stack

{stackArray [ ++ top ] = j ; // increment top, insert item

{

Page 17: Stack project

Stack Code For Java (cont’d)

public long pop( ) // take item from top of stack}Return stackArray [ top-- ] ; // access item, decrement top{//--------------------------------------------------------------public long top( ) // top of stack}Return stackArray [ top ] ; {//--------------------------------------------------------------public boolean isEmpty( ) // true if stack is empty}return ( top == -1 ) ; {//--------------------------------------------------------------public boolean isFull( ) // true if stack is full}return ( top == maxSize-1 ) ;{} // end class StackX

Page 18: Stack project

Array-based Stack

Page 19: Stack project

Array-based Stack Implementation

Allocate an array of some size (pre-defined)

Maximum N elements in stack

Bottom stack element stored at element 0

last index in the array is the top

Increment top when one element is pushed, decrement after pop

Page 20: Stack project

Array-based Stack Implementation

#include<stdio.h>#include<conio.h>#define MAXSTACK 10

typedef char StackEntry;typedef struct stack {

int top;StackEntry entry[MAXSTACK];

} Stack;void CreateStack ( Stack *s ) }

s->top=0; }

Page 21: Stack project

Array-based Stack Implementation (cont’d)

Int StackFull ( Stack *s ) }

if( s->top>=MAXSTACK )return 1;

elsereturn 0;

{ int StackEmpty( Stack *s ) }

if(s->top<=0)return 1;

else return 0;

{

Page 22: Stack project

Array-based Stack Implementation (cont’d)

void Push ( StackEntry e, Stack *s ) }

if(StackFull(s))printf("Stack is full");

elses->entry[s->top++]=e;

}void Pop(StackEntry *e,Stack *s) }

if(StackEmpty(s))printf("Stack is empty");

else*e=s->entry[--s->top];

}

Page 23: Stack project

Array-based Stack Implementation (cont’d)

void ClearStack(Stack *s) }

s->top=0; { int StackSize(Stack *s) }

return s->top; { void StackTop(StackEntry *e,Stack *s) }

Pop(e,s);Push(*e,s);

{

Page 24: Stack project

Array-based Stack Implementation (cont’d) void TraverseStack(Stack *s,void(*visit)(StackEntry *e)) }

if(StackEmpty(s))printf("Stack is empty");

for(int i=s->top;i>=0;i--)(*visit)(&s->entry[i]);

{

void ChangeStack(StackEntry *e) }

*e=++*e; {

Page 25: Stack project

Amr Mohammed Abou El-Good Hanan Abd-el azim abd-el galil Asmaa hussien korani Walaa hussien hassan naglaa mohammed mohammed

Team Members :