Upload
amr-aboelgood
View
2.144
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Stack
Citation preview
Data Structures
Stack
Dr. Mohammed Belal
Submitted to :
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.
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
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
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.
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
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
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
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
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
Real life
Stack Applications
Pile of books Plate trays
More applications related to computer science
Program execution stack
Evaluating expressions
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
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
Pop Function :
A
CB
Top
(1)
A
B
BTopABTop
(3)(2)
Pop
Algorithm pop( )if isEmpty( ) thenthrow EmptyStackExceptionelset ← t − 1return S[t + 1]
Top Function :DCBA
Top
Is Empty Function :
Top 0
Algorithm Top(){Return s [ Top] ;}
Algorithm Is Empty()
If S null{Return true ;elseReturn false ;}
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
{
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
Array-based Stack
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
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; }
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;
{
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];
}
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);
{
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; {
Amr Mohammed Abou El-Good Hanan Abd-el azim abd-el galil Asmaa hussien korani Walaa hussien hassan naglaa mohammed mohammed
Team Members :