chapter03
chapter03
Hanoi, 2012
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 1 / 37
Outline
1 Basic concepts
2 Array
3 Lists
4 Stacks
5 Queues
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 2 / 37
Basic concepts
Data Types
set of values
data representation
set of operations
Abstract Data Types (ADT)
set of values
set of operations
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 3 / 37
Data Types: primitive data types in C
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 4 / 37
ADT
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 5 / 37
Outline
1 Basic concepts
2 Array
3 Lists
4 Stacks
5 Queues
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 6 / 37
Array
Example
1 int a [1000];
int b[100][100];
3 t y p e d e f s t r u c t MyStruct {
int value ;
5 MyStruct ∗ p t r ;
};
7 MyStruct x [ 1 0 ] ;
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 7 / 37
Outline
1 Basic concepts
2 Array
3 Lists
4 Stacks
5 Queues
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 8 / 37
Lists
List ADT implements an ordered collection of values (each value can
appear several times)
Notations
L: list of objects
x: an object
p: position type
END(L): function that returns the position after the position of the
last element of the list
Operations
Insert(x, p, L): insert element x at position p of the list L
Locate(x, L): return the position of x in L
Retrieve(p, L): return the element at position p in L
Delete(p, L): remove element at position p in L
Next(p, L): return the position after the position p in L
Prev(p, L): return the position before the position p in L
MakeNull(L): set L to empty list and return END(L)
First(L): return the first position in L
PrintList(L): print all elements of L in the order they appear in L
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 9 / 37
Lists
List ADT implements an ordered collection of values (each value can
appear several times)
Notations
L: list of objects
x: an object
p: position type
END(L): function that returns the position after the position of the
last element of the list
Operations
Insert(x, p, L): insert element x at position p of the list L
Locate(x, L): return the position of x in L
Retrieve(p, L): return the element at position p in L
Delete(p, L): remove element at position p in L
Next(p, L): return the position after the position p in L
Prev(p, L): return the position before the position p in L
MakeNull(L): set L to empty list and return END(L)
First(L): return the first position in L
PrintList(L): print all elements of L in the order they appear in L
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 9 / 37
List - implementation
Array-based
Elements located in contiguous blocks in memory
Delete and Insert operations are costly
Pointer-based (linked list)
Collection of nodes that not necessarily locate in contiguous blocks
Single linked list: Each node contains the element (data) and a
reference (pointer) to the next node
Doubly linked list: Each node contains the element (data), a reference
to the previous node and a reference to the next node
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 10 / 37
Array-based implementation
Insertion and Deletion
1
int a [10000];
3 i n t n ;// s i z e of the l i s t , elements are a [ 1 ] , a [ 2 ] , ... , a[n]
v o i d i n s e r t ( i n t x , i n t pos ) {
5 f o r ( i n t i = n ; i >= p o s ; i −−)
a [ i +1] = a [ i ] ;
7 a [ pos ] = x ;
n = n + 1;
9 }
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 11 / 37
Array-based implementation
MakeNull and PrintList
1 v o i d makeNull ( ) {
n = 0;
3 }
void p r i n t L i s t () {
5 f o r ( i n t i = 1 ; i <= n ; i ++)
p r i n t f ( ”%d ” , a [ i ] ) ;
7 p r i t n f ( ” \n” ) ;
}
9 void r e t r i e v e ( int k){
return a [ k ] ;
11 }
i n t end ( ) {
13 r e t u r n −1;
}
15 int locate ( int x){
f o r ( i n t i = 1 ; i <= n ; i ++)
17 i f ( a [ i ] == x )
return i ;
19 r e t u r n end ( ) ;
} Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 12 / 37
Pointer-based implementation
Single linked list: representation
t y p e d e f i n t ElementType ;
2
s t r u c t PointerType {
4 ElementType d a t a ;
PointerType ∗ next ;
6 };
8 P o i n t e r T y p e ∗ f i r s t = NULL ;
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 13 / 37
Pointer-based implementation
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 14 / 37
Pointer-based implementation
Single linked list: Insertion
P o i n t e r T y p e ∗ i n s e r t A f t e r ( ElementType x , P o i n t e r T y p e ∗ p ) {
2 // i n s e r t an e l e m e n t x i n t o t h e p o s i t i o n a f t e r p
PointerType ∗ q ;
4 q = new P o i n t e r T y p e ;
q−>d a t a = x ;
6
i f ( f i r s t == NULL) {
8 q−>n e x t = NULL ;
first = q;
10 }else{
q−>n e x t = p−>n e x t ;
12 p−>n e x t = q ;
}
14
return q ;
16 }
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 15 / 37
Pointer-based implementation
Single linked list: Deletion
void del ( PointerType ∗ p){
2 i f ( p == f i r s t ) {
P o i n t e r T y p e ∗ tmp = f i r s t −>n e x t ;
4 delete f i r s t ;
f i r s t = tmp ;
6 }else{
PointerType ∗ pi = f i r s t ;
8 w h i l e ( p i != NULL && p i −>n e x t != p )
p i = p i −>n e x t ;
10 i f ( p i != NULL) {
p i −>n e x t = p−>n e x t ;
12 delete p;
}
14 }
}
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 16 / 37
Pointer-based implementation
Single linked list: PrintList and MakeNull
1 void p r i n t L i s t () {
PointerType ∗ p = f i r s t ;
3 w h i l e ( p != NULL) {
p r i n t f ( ”%d ” , p−>d a t a ) ;
5 p = p−>n e x t ;
}
7 p r i n t f ( ” \n” ) ;
}
9 PointerType ∗ makeNull ( ) {
w h i l e ( f i r s t != NULL) {
11 P o i n t e r T y p e ∗ tmp = f i r s t −>n e x t ;
delete f i r s t ;
13 f i r s t = tmp ;
}
15 r e t u r n NULL ;
}
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 17 / 37
Pointer-based implementation
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 18 / 37
Pointer-based implementation
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 19 / 37
Pointer-based implementation
Doubly linked list: representation
1 s t r u c t Node{
i n t data ;
3 Node∗ left ;
Node∗ r i g h t ;
5 };
7 Node∗ f i r s t = NULL ;
Node∗ l a s t = NULL ;
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 20 / 37
Pointer-based implementation
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 21 / 37
Pointer-based implementation
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 22 / 37
Pointer-based implementation
Doubly linked list: Insertion (insert an element to the end of the list)
void insertToEnd ( i n t x ){
2 Node∗ p = new Node ;
p−>d a t a = x ;
4 i f ( f i r s t == NULL) {
p−> l e f t = NULL ;
6 p−>r i g h t = NULL ;
first = p;
8 last = p;
}else{
10 p−>r i g h t = NULL ;
p−> l e f t = l a s t ;
12 l a s t −>r i g h t = p ;
last = p;
14 }
}
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 23 / 37
Built-in List in C++
Manual: http://www.cplusplus.com/reference/list/list/
Fundamental methods
empty()
size()
push front()
pop front()
push back()
pop back()
insert()
erase()
clear()
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 24 / 37
Outline
1 Basic concepts
2 Array
3 Lists
4 Stacks
5 Queues
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 25 / 37
Stacks
Stack ADT: An ordered list in which all insertions and deletions are
made at one end (called top)
Principle: the last element inserted into the stack must be the first
one to be removed (Last-In-First-Out)
Operations
Push(x, S): push an element x into the stack S
Pop(S): remove an element from the stack S, and return this element
Top(S): return the element at the top of the stack S
Empty(S): return true if the stack S is empty
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 26 / 37
Linked list-based implementation
1 s t r u c t Node{
char i n f o ;
3 Node∗ n e x t ;
};
5
Node∗ t o p = NULL ; / / p o i n t e r t o t h e t o p o f t h e s t a c k
7
9 i n t stackEmpty ( ) {
i f ( t o p == NULL)
11 return 1;
else
13 return 0;
}
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 27 / 37
Linked list-based implementation
1 v o i d push ( c h a r x ) {
Node∗ p ;
3 p = new Node ;
p−>i n f o = x ;
5 p−>n e x t = t o p ;
top = p ;
7 }
9 c h a r pop ( ) {
c h a r x = top−>i n f o ;
11 Node∗ p = t o p ;
t o p = top−>n e x t ;
13 delete p;
return x ;
15 }
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 28 / 37
Application: Parentheses matching
()([]){}: consistent
()()[{): not consistent
[](){[])[]: not consistent
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 29 / 37
Application: Parentheses matching
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 30 / 37
Application: Parentheses matching
i n t check ( char X [ ] , i n t n ) {
2 f o r ( i n t i = 0 ; i < n ; i ++){
i f (X [ i ] == ’ ( ’ | | X [ i ] == ’ [ ’ | | X [ i ] == ’ { ’ )
4 push (X [ i ] ) ;
else{
6 i f (X [ i ] == ’ ) ’ | | X [ i ] == ’ ] ’ | | X [ i ] == ’ } ’ ) {
i f ( stackEmpty ( ) ) r e t u r n 0 ;
8 else{
c h a r x = pop ( ) ;
10 i f ( checkMatch ( x , X [ i ] ) == 0 ) r e t u r n 0 ;
}
12 }
}
14 }
i f ( stackEmpty ( ) ) r e t u r n 1 ; e l s e r e t u r n 0 ;
16 }
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 31 / 37
Outline
1 Basic concepts
2 Array
3 Lists
4 Stacks
5 Queues
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 32 / 37
Queues
Queue ADT: An ordered list in which the insertions are made at one
end (called tail) and the deletions are made at the other end (called
head)
Principle: the first element inserted into the queue must be the first
one to be removed (First-In-First-Out)
Applications: items do not have to be processed immediately but they
have to be processed in FIFO order
Data packets are stored in a queue before being transmitted over the
internet
Data is transferred asynchronously between two processes: IO buffered,
piples, etc.
Printer queues, keystroke queues (as we type at the keyboard), etc.
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 33 / 37
Queues
Operations
Enqueue(x, Q): push an element x into the queue Q
Dequeue(Q): remove an element from the queue Q, and return this
element
Head(Q): return the element at the head of the queue Q
Tail(Q): return the element at the tail of the queue Q
Empty(Q): return true if the queue Q is empty
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 34 / 37
Built-in Stack and Queue in C++
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 35 / 37
Queues: palindrome strings checking
A palindrome string is the string that reads the same forward and
backward
Example: MADAM, NOON, RADAR, etc.
Problem: check whether a given string is palindrome
Use queue and stack
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 36 / 37
Queues: palindrome strings checking
#i n c l u d e < s t d i o . h>
2 #i n c l u d e <queue>
#i n c l u d e <s t a c k >
4 #i n c l u d e < s t r i n g . h>
u s i n g namespace s t d ;
6 i n t main ( i n t a r g c , c h a r ∗∗ a r g v ) {
char ∗ s = argv [ 1 ] ;
8 queue<c h a r > Q;
s t a c k <c h a r > S ;
10 f o r ( i n t i = 0 ; i < s t r l e n ( s ) ; i ++){
Q. push ( s [ i ] ) ; S . push ( s [ i ] ) ;
12 }
w h i l e ( !Q. empty ( ) ) {
14 i f (Q. f r o n t ( ) != S . t o p ( ) ) {
p r i n t f ( ” n o t p a l i n d r o m e \n” ) ;
16 return 0;
}
18 Q. pop ( ) ; S . pop ( ) ;
}
20 p r i n t f ( ” p a l i n d r o m e \n” ) ;
} Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 37 / 37