April 3, 2010
Copyright
1 Stacks
Stack definition
Stack implementation: Static implementation
Stack implementation: Dynamic implementation
2 Queues
SBE201 Data Structures and Algorithms
Stacks
Outline
1 Stacks
Stack definition
Stack implementation: Static implementation
Stack implementation: Dynamic implementation
2 Queues
SBE201 Data Structures and Algorithms
Stacks
Stack definition
Stack definition
Stack Operations
Here is how the stack will look like after he following operation:
Example
push(16); push(28);
SBE201 Data Structures and Algorithms
Stacks
Stack implementation: Static implementation
If you know the maximum size of the stack before runtime, you
can implement the stack statically using arrays
Steps
First, you need to declare the data type of the item you will carry
in the array
Example
struct stack_item
int x;
char y;
;
SBE201 Data Structures and Algorithms
Stacks
Stack implementation: Static implementation
Example
maxstacksize=100;
stack_item stackarray[maxstacksize];
Example
int top=-1;
push function
pop function
Example
SBE201 Data Structures and Algorithms
Stacks
Stack implementation: Static implementation
ISempty function
Isempty function
char isempty(){
if (top==-1){return (1);} else{return (0);}
}
SBE201 Data Structures and Algorithms
Stacks
Stack implementation: Dynamic implementation
You can let the stack size grows during runtime by implementing
it dynamically using linked lists
Steps:
First we will declare the structure of the elements contained in
the stack:
Example
struct elem{
int dataitem1; char dataitem2;
struct elem* next;
};
SBE201 Data Structures and Algorithms
Stacks
Stack implementation: Dynamic implementation
Example
push function
pop function
pop function
pop function
char isempty(){
if (top==NULL)return (1); elsereturn (0);
}
SBE201 Data Structures and Algorithms
Queues
Outline
1 Stacks
Stack definition
Stack implementation: Static implementation
Stack implementation: Dynamic implementation
2 Queues
SBE201 Data Structures and Algorithms
Queues
Introduction
Queue Implementation
Queue initialization
First step, we need to declare the array that will hold the
elements of the queue
We will initialize the front and the rear of the queue to -1
Queue initialization
If we are inserting the first element in the queue, the rear= 0 and
the front is still at -1, so the front will always be set to the array
index just before the index of the first element in the queue
SBE201 Data Structures and Algorithms
Queues
Dequeue function
int dequeue(){
if (front==rear)//testing if the queue is empty!
return(0);//return 0 if the queue is empty
int data=queue[front+1];
front++;
return(data);
}
SBE201 Data Structures and Algorithms
Queues
Examples
Consider a queue of length=5, let’s see how it will look like after
some enqueue and dequeue operations:
SBE201 Data Structures and Algorithms
Queues
Circular Queue
Let’s consider an array of size =5,to implement our circular
queue using the this linear array : instead iof letting the front and
the rear move in a linear direction from left to right, we will let the
two variables wrap around to the beginning of the array when
they reach its ending!
SBE201 Data Structures and Algorithms
Queues
Exercise
Further Reading
Robert Lafore.
C Programming Using Turbo C++.
Charles F. Bowman.
Algorithms and Data Structures.
Aaron M. Tenenbaum, Yedidyah Langsam, Moshe J. Augenstein.