Data structures and Algorithms Basic Data structures
-
Upload
independent -
Category
Documents
-
view
9 -
download
0
Transcript of Data structures and Algorithms Basic Data structures
Data structures and AlgorithmsBasic Data structures
Pham Quang Dung
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 valuesdata representationset of operations
Abstract Data Types (ADT)
set of valuesset of operations
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 3 / 37
Data Types: primitive data types in C
Built-in data types In C programming languageType Bits Minimum value Maximum value
byte 8 -128 127short 16 -32768 32767char 16 0 65535int 32 -2147483648 = −231 3247483647 = 231 − 1long 64 -9223372036854775808 9223372036854775807float 32double 64
Operations on primitive data types: +, -, *, /, ...
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 4 / 37
ADT
ADT Object Operations
List nodes insert, remove, find,...Graphs nodes, edges findPath, Search,...Stack elements push, pop, isEmpty,...Queue elements enqueue, dequeue, isEmpty,...Binary tree nodes traversal, find, ...
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
Collection of elements of similar data type
Elements are stored sequentially
Each elements of the array is accessed via its index
An array can have one or more dimensions
Example
1 i n t a [ 1 0 0 0 ] ;i n t b [ 1 0 0 ] [ 1 0 0 ] ;
3 t y p ed e f s t r u c t MyStruct{i n t v a l u e ;
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 canappear several times)Notations
L: list of objectsx : an objectp: position typeEND(L): function that returns the position after the position of thelast element of the list
OperationsInsert(x , p, L): insert element x at position p of the list LLocate(x , L): return the position of x in LRetrieve(p, L): return the element at position p in LDelete(p, L): remove element at position p in LNext(p, L): return the position after the position p in LPrev(p, L): return the position before the position p in LMakeNull(L): set L to empty list and return END(L)First(L): return the first position in LPrintList(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 canappear several times)Notations
L: list of objectsx : an objectp: position typeEND(L): function that returns the position after the position of thelast element of the list
OperationsInsert(x , p, L): insert element x at position p of the list LLocate(x , L): return the position of x in LRetrieve(p, L): return the element at position p in LDelete(p, L): remove element at position p in LNext(p, L): return the position after the position p in LPrev(p, L): return the position before the position p in LMakeNull(L): set L to empty list and return END(L)First(L): return the first position in LPrintList(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 memoryDelete and Insert operations are costly
Pointer-based (linked list)
Collection of nodes that not necessarily locate in contiguous blocksSingle linked list: Each node contains the element (data) and areference (pointer) to the next nodeDoubly linked list: Each node contains the element (data), a referenceto 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
i n t a [ 1 0 0 0 0 ] ;3 i n t n ; // s i z e o f the l i s t , e l ement s a r e 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 >= pos ; i−−)
a [ i +1] = a [ i ] ;7 a [ pos ] = x ;
n = n + 1 ;9 }
11 vo i d d e l ( i n t k ) {f o r ( i n t i = k ; i <= n−1; i++)
13 a [ i ] = a [ i +1] ;n = n − 1 ;
15 }
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 11 / 37
Array-based implementation
MakeNull and PrintList
1 vo i d makeNul l ( ) {n = 0 ;
3 }vo i d 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 vo i d r e t r i e v e ( i n t k ) {r e t u r n a [ k ] ;
11 }i n t end ( ) {
13 r e t u r n −1;}
15 i n t l o c a t e ( i n t x ) {f o r ( i n t i = 1 ; i <= n ; i++)
17 i f ( a [ i ] == x )r e t u r n 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 ed e f i n t ElementType ;2
s t r u c t Po in te rType {4 ElementType data ;
Po in te rType ∗ next ;6 } ;
8 Po inte rType ∗ f i r s t = NULL ;
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 13 / 37
Pointer-based implementation
Single linked list: Insertion
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 14 / 37
Pointer-based implementation
Single linked list: Insertion
Po inte rType ∗ i n s e r t A f t e r ( ElementType x , Po inte rType ∗ p ) {2 // i n s e r t an e l ement x i n t o the p o s i t i o n a f t e r p
Po in te rType ∗ q ;4 q = new Po inte rType ;
q−>data = x ;6
i f ( f i r s t == NULL) {8 q−>next = NULL ;
f i r s t = q ;10 } e l s e {
q−>next = p−>next ;12 p−>next = q ;
}14
r e t u r n q ;16 }
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 15 / 37
Pointer-based implementation
Single linked list: Deletion
vo i d d e l ( Po in te rType ∗ p ) {2 i f ( p == f i r s t ) {
Po inte rType ∗ tmp = f i r s t −>next ;4 d e l e t e f i r s t ;
f i r s t = tmp ;6 } e l s e {
Po inte rType ∗ p i = f i r s t ;8 wh i l e ( p i != NULL && pi−>next != p )
p i = pi−>next ;10 i f ( p i != NULL) {
pi−>next = p−>next ;12 d e l e t e 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 vo i d p r i n t L i s t ( ) {Po inte rType ∗ p = f i r s t ;
3 wh i l e ( p != NULL) {p r i n t f ( ”%d ” ,p−>data ) ;
5 p = p−>next ;}
7 p r i n t f ( ”\n” ) ;}
9 Po inte rType ∗ makeNul l ( ) {wh i l e ( f i r s t != NULL) {
11 Po inte rType ∗ tmp = f i r s t −>next ;d e l e t e 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
Single linked list: Previous
Po inte rType ∗ prev ( Po inte rType ∗ p ) {2 Po inte rType ∗ tmp = f i r s t ;
wh i l e ( tmp != NULL) {4 i f ( tmp−>next == p)
r e t u r n tmp ;6 tmp = tmp−>next ;
}8 r e t u r n NULL ;}
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 18 / 37
Pointer-based implementation
Single linked list: Locate
1 Po inte rType ∗ l o c a t e ( ElementType x ) {Po inte rType ∗ p = f i r s t ;
3 wh i l e ( p != NULL) {i f ( p−>data == x )
5 r e t u r n p ;p = p−>next ;
7 }r e t u r n NULL ;
9 }
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∗ l e f t ;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
Doubly linked list: Insertion (insert an element at a position pointed bya pointer p)
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 21 / 37
Pointer-based implementation
Doubly linked list: Insertion (insert an element at a position pointed bya pointer p)
vo i d i n s e r t A t ( i n t x , Node∗ p ) {2 Node∗ q = new Node ;
q−>r i g h t = p ;4 q−>data = x ;
Node∗ p1 = p−> l e f t ;6 i f ( p1 != NULL)
p1−>r i g h t = q ;8 q−> l e f t = p1 ;
p−> l e f t = q ;10 }
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)
vo i d in se r tToEnd ( i n t x ) {2 Node∗ p = new Node ;
p−>data = x ;4 i f ( f i r s t == NULL) {
p−> l e f t = NULL ;6 p−>r i g h t = NULL ;
f i r s t = p ;8 l a s t = p ;
} e l s e {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 ;
l a s t = 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 aremade at one end (called top)
Principle: the last element inserted into the stack must be the firstone to be removed (Last-In-First-Out)
Operations
Push(x ,S): push an element x into the stack SPop(S): remove an element from the stack S , and return this elementTop(S): return the element at the top of the stack SEmpty(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∗ next ;} ;
5
Node∗ top = NULL;// p o i n t e r to the top o f the s t a c k7
9 i n t stackEmpty ( ) {i f ( top == NULL)
11 r e t u r n 1 ;e l s e
13 r e t u r n 0 ;}
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 27 / 37
Linked list-based implementation
1 vo i d push ( cha r x ) {Node∗ p ;
3 p = new Node ;p−> i n f o = x ;
5 p−>next = top ;top = p ;
7 }
9 char pop ( ) {char x = top−> i n f o ;
11 Node∗ p = top ;top = top−>next ;
13 d e l e t e p ;r e t u r n 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
1 i n t checkMatch ( cha r x , cha r y ) {i f ( x == ’ ( ’ && y == ’ ) ’ ) r e t u r n 1 ;
3 i f ( x == ’ [ ’ && y == ’ ] ’ ) r e t u r n 1 ;i f ( x == ’ { ’ && y == ’ } ’ ) r e t u r n 1 ;
5
r e t u r n 0 ;7 }
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 30 / 37
Application: Parentheses matching
i n t check ( cha r 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 ] ) ;
e l s e {6 i f (X [ i ] == ’ ) ’ | | X[ i ] == ’ ] ’ | | X[ i ] == ’ } ’ ) {
i f ( stackEmpty ( ) ) r e t u r n 0 ;8 e l s e {
char 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 oneend (called tail) and the deletions are made at the other end (calledhead)
Principle: the first element inserted into the queue must be the firstone to be removed (First-In-First-Out)
Applications: items do not have to be processed immediately but theyhave to be processed in FIFO order
Data packets are stored in a queue before being transmitted over theinternetData 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 QDequeue(Q): remove an element from the queue Q, and return thiselementHead(Q): return the element at the head of the queue QTail(Q): return the element at the tail of the queue QEmpty(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++
Stack (manual: http://www.cplusplus.com/reference/stack/stack/)
empty: Test whether the stack is emptysize: Return sizetop: Access next elementpush: Add elementpop: Remove element
Queue (manual: http://www.cplusplus.com/reference/queue/queue/)
empty: Test whether the queue is emptysize: Return sizefront: Access next elementback: Access last elementpush: Insert elementpop: Delete next element
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 andbackward
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 tack>
4 #i n c l u d e < s t r i n g . h>u s i n g namespace s td ;
6 i n t main ( i n t argc , cha r ∗∗ a rgv ) {char ∗ s = argv [ 1 ] ;
8 queue<char> Q;stack<char> 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 }wh i l e ( !Q. empty ( ) ) {
14 i f (Q. f r o n t ( ) != S . top ( ) ) {p r i n t f ( ” not pa l i nd rome \n” ) ;
16 r e t u r n 0 ;}
18 Q. pop ( ) ; S . pop ( ) ;}
20 p r i n t f ( ” pa l i nd rome \n” ) ;}
Pham Quang Dung () Data structures and Algorithms Basic Data structures Hanoi, 2012 37 / 37