0% found this document useful (0 votes)
1 views38 pages

chapter03

The document provides an overview of basic data structures and algorithms, focusing on concepts such as data types, abstract data types (ADT), arrays, lists, stacks, and queues. It includes detailed explanations of various data structures, their operations, and implementations in C and C++. The content is structured into sections that cover definitions, operations, and examples for each data structure.

Uploaded by

22h1120052
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views38 pages

chapter03

The document provides an overview of basic data structures and algorithms, focusing on concepts such as data types, abstract data types (ADT), arrays, lists, stacks, and queues. It includes detailed explanations of various data structures, their operations, and implementations in C and C++. The content is structured into sections that cover definitions, operations, and examples for each data structure.

Uploaded by

22h1120052
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Data structures and Algorithms

Basic 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 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

Built-in data types In C programming language


Type Bits Minimum value Maximum value
byte 8 -128 127
short 16 -32768 32767
char 16 0 65535
int 32 -2147483648 = −2 31 3247483647 = 231 − 1
long 64 -9223372036854775808 9223372036854775807
float 32
double 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 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 }

11 void del ( int 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 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

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
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

Single linked list: Previous


PointerType ∗ prev ( PointerType ∗ p){
2 P o i n t e r T y p e ∗ tmp = f i r s t ;
w h i l e ( tmp != NULL) {
4 i f ( tmp−>n e x t == p )
r e t u r n tmp ;
6 tmp = tmp−>n e x t ;
}
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 P o i n t e r T y p e ∗ l o c a t e ( ElementType x ) {
PointerType ∗ p = f i r s t ;
3 w h i l e ( p != NULL) {
i f ( p−>d a t a == x )
5 return p ;
p = p−>n e x t ;
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∗ 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

Doubly linked list: Insertion (insert an element at a position pointed by


a 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 by


a pointer p)
v o 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−>d a t a = 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)
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

1 int checkMatch ( c h a r x , char y ){


if ( x == ’ ( ’ && y == ’ ) ’ ) return 1;
3 if ( x == ’ [ ’ && y == ’ ] ’ ) return 1;
if ( x == ’ { ’ && y == ’} ’ ) return 1;
5
return 0;
7 }

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++

Stack (manual: http://www.cplusplus.com/reference/stack/stack/)


empty: Test whether the stack is empty
size: Return size
top: Access next element
push: Add element
pop: Remove element
Queue (manual: http://www.cplusplus.com/reference/queue/queue/)

empty: Test whether the queue is empty


size: Return size
front: Access next element
back: Access last element
push: Insert element
pop: 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 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

You might also like