SlideShare a Scribd company logo
Lecture 4
Dynamic Data Structure Part 1
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 1
Content Lecture 4 Dynamic Data
Part 1
 Pointer
 Linear Lists
 Linked List
 Circular List
 Doubly-Linked List
 Stack
 Queues
 Sorted List
 Linked Lists vs. Dynamic Arrays
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 2
Pointers
 Pointers are an essential instrument in dynamical data
structures
 But the use of pointers is not trivial
 That is because of:
//important
 Technics working with pointer are hard to read
 The chances for errors are higher (especially for
beginners)
 Undefined pointers are causing the most program
crashes
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 3
Pointers
Address space
 When a program is executed the data are placed in the
memory
 You can see the memory as an array which elements are
memory cells
 The index area of this array is called address space
 The single memory cell is in general one byte (8 bits)
 Each active program has its own virtual address space
 This space consists of a heap and a stack
 Local variable or constants are stored in the stack
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 4
Pointers
Dynamical Data/Heap
 If you need more memory during run time the memory has to
be allocated in the free parts of the address space (heap)
 In opposite to global variables the addresses of the memory
locations are not constant
 Therefore to address this memory you need so called
pointers
 Pointers are variables whose values refers directly to (point
to) another value by using its address
 This address refers to a memory location in which the data are
written
 Therefore a pointer points to a memory address
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 5
Pointers
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 6
Storage for dynamical data
created or initialized at runtime
Storage for local variables
and parameters
declared and initialized
before runtime
Pointers
Example in C
int *myptr = &myvar;
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 7
Pointers
 More than one pointer can refer to the same memory
location
 In program language pointers are often used to realize a
call-by-reference function call, also for lists, strings, lookup
tables, control tables and trees
 You can still change the data behind the pointer
 If a pointer references to a location in memory to obtain
the value at this location this is called dereferencing
 To release the memory you have to deallocate the memory
 In some program language the deallocation of the memory
is done by a so called Garbage Collector (for example Java)
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 8
Pointers
 In other language the programmer has to take care of the
deallocation by himself (for example C/C++)
 That leads to following problems:
 The deallocation can be forgotten (memory leaks)
 It is not always trivial to calculate what and when
something has to be deallocated
 Pointers are still used even when the deallocation has
taken place (dangling reference)
 All this problems can lead to a mostly unexpected behavior
of the program execution
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 9
Linear Lists
 To one of the simplest data structure belongs the Linear
List
 A number of elements ai are represented in an order form:
a1 a2 … ai-1 ai ai+1 … an
 Typical operations are:
 Putting a new element at the beginning or at the end of
the list
 Deleting an element from the list
 Getting an element at position i (especially when i=1 or
i=n)
 Getting the next or previous element of an element ai
(therefore ai+1 or ai-1)
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 10
Linear Lists
Examples for lists
 Orders in a shop: Each element is equal to an order. The
order tells which article has to be send to a costumer. Each
new order is put at the end of the list. After finishing the
order the element is deleted in the list.
 Moves: Each element is equal to a move in a game. A new
move is put at the end of the list. You can restore an old score
by removing the last element.
 Timetable: Each element is equal to a new appointment or
short information. All elements are sorted by time. New
entries are therefore put in the list according to their time.
Expired entries are deleted from the list.
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 11
Linear Lists
 An implementation of a list data structure may require some
of the following operations:
 An operation to creating an empty list (init)
 An operation to test whether or not a list is empty
(isempty)
 An operation for adding an entity to a list at the beginning
 An operation for appending an entity to a list
 An operation for receiving the first component element
(head) of a list or the last element
 An operation for referring to the list consisting of all the
components of a list except for its first (this is called the
"tail" of the list)
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 12
Linked List
 A Linked List is a data structure that consists of a sequence
of data records such that in each record there is a field that
contains a reference (a link) to the next record in the
sequence
 Each record of a Linked List is often called an element or
node
 The field of each node that contains the address of the next
node is usually called the next link or next pointer
 The remaining fields are known as the data, information
or value
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 13
Linked List
struct Node
{
data; //The data/value being stored in the node
Node next; //reference to the next node, null for last
//node
};
struct List
{
Node first; //Points to first node of list; null for empty
//list
};
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 14
Linked List
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 15
5 14 44 67 X
node/element next link/pointer
data/value null/end of the list
list
first link/pointer
Linked List
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 16
5 14 44 67 X
31
5 14 44 67 X
31
Insert a new node
5 14 44 67 X
31
newNode.next
newNode.next = node.next
node.next = newNode
newNode
node
node.next
Linked List
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 17
5 14 44
3list newNode
5 14 44
3list
newNode.next = list.first;
Special case if you insert a new
node at the beginning
5 14 44
3list list.first = newNode;
Linked List
Removing a node
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 18
5 14 44 67
5 14 44 67
node
obsoleteNode
node.next
5 14 44 67
node.next node.next.next
destroy obsoleteNode
node.next = node.next.next;
Linked List
23/10/2018 Lecture 4 Dynamic Data Structure Part 1
19
5 14 44
list
5 14 44
list
5 14 44
list
obsoleteNode = list.first
list.first = list.first.next
list.first.next
destroy obsoleteNode
Removing a node in front of the list
Circular List
 In the most cases the last node of a list contains a null
value
 Means that there is no next node in the list
 In some cases a pointer to the first node of the list is made
 This is called a Circular List
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 20
5 14 44 67 struct List
{
Node first;
//Points to first node of list;
Node last;
//Points to first node of list;
}
Circular List
Insert an element at the
beginning
newNode.next = list.first
list.first = newNode
list.last = newNode;
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 21
5 14 44 67
1
5 14 44 67
1
Doubly-Linked List
 A Doubly-Linked List is a Linked List that contains a
number of elements, each having two special fields
referencing to the next and previous element in the list
 You can view it as two Linked Lists formed from the same
data items, in two opposite directions
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 22
5 14 44 67 XX
Doubly-Linked List
struct Node {
data; // The data being stored in the node
Node next; // A reference to the next node; null for last node
Node prev; // A reference to the previous node; null for first
// node
};
struct List {
Node firstNode; // points to first node of list;
Node lastNode; // points to last node of list;
};
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 23
Doubly-Linked List
 Iterating through a Doubly Linked List can be done in
either direction
 In fact, direction can change many times, if desired
Forwards
node = list.firstNode
while (node != null) {
//do something with the
//data
node = node.next;
}
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 24
Backwards
node = list.lastNode
while (node != null) {
//do something with the
//data
node = node.prev;
}
Doubly-Linked List
Inserting a node
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 25
5 14 44 67 XX
31
5 14 44 67 XX
31
newNode
newNode.prev = node;
newNode.next = node.next;
node
Doubly-Linked List
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 26
5 14 44 67 XX
31
5 14 44 67 XX
31
node.next = newNode;
node.next.prev = newNode
Doubly-Linked List
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 27
Removing a node
5 14 44 67 XX
5 14 44 67 XX
5 14 67 XX
destroy node;
obsoleteNode
obsoleteNode.prev.next=obsoleteNode.next;
44
obsoleteNode.next.prev=obsoleteNode.prev;
Stack
 A Stack is also a Linear List with the difference that only at
one end elements could be added or removed
 In a Stack you are mostly interested in the element on the
top
 Elements below are only appearing again if all elements on
the top of it are removed
 In computer science, a stack is a Last In, First Out (LIFO)
abstract data type and data structure
 Examples for Stacks: Game Tic-Tac-Toe
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 28
Stack
 In the most program language the implementation of Stack
can be done with arrays
 The C++ Standard Template Library provides a Stack
Template Class which is restricted to only push/pop
operations
 Java's library contains a Stack class that is a specialization
of the class Vector
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 29
Stack
 An implementation of a Stack may require some of the
following operations:
 An operation to creating an empty Stack (init)
 An operation to add an element on the top of the Stack
(push)
 An operation to remove an element on the top of the
Stack (pop)
 An operation to receive the current element on the top
of the Stack (top)
 An operation to receive the length of the stack; the
number of elements in the Stack (length)
 An operation showing that the maximal capacity of the
Stack is reached (full)
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 30
Stack
Example
X = {1, 3, 5, 7, 11, 13} a Stack
length() = 6
pop() = 13  X = {1, 3, 5, 7, 11}
top() = 11
length() = 5
push(17);  X = {1, 3, 5, 7, 11, 17}
length() = 6
top() = 17
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 31
13
11
7
5
3
1
11
7
5
3
1
17
11
7
5
3
1
Stack
Example
X = {b, d, f, h, j, l, n, p} a Stack
What is:
pop()
length()
top()
push(r)
length()
top()
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 32
Stack
Solution
X = {b, d, f, h, j, l, n, p} a Stack
What is:
pop()  X = {b, d, f, h, j, l, n}
length() = 7
top() = n
push(r)  X = {b, d, f, h, j, l, n, r}
length() = 8
top() = r
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 33
p
n
l
j
h
f
d
b
r
n
l
j
h
f
d
b
n
l
j
h
f
d
b
Queues
 A Queue or FIFO (First-In-First-Out) is a Linear List
where at one end elements are added and on the other end
elements are removed
 The inner elements are not considered
 The elements will be processed in exactly the same order in
which they are original placed
 The C++ Standard Template Library provides a Queue
Template Class.
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 34
Queues
 The following operations exists in general for queues
 init() initialize an empty queue
 isempty() true if the queue is empty, otherwise false
 pop() removes the item at the front of the queue
 push() insert an item at the back of the queue
 size() return the number of elements in the
queue
 front() returns a reference to the value at the
front of a non-empty queue
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 35
Queues
Example
Y = {1, 3, 5, 7, 11, 13} a Queue
isempty() = false
pop() = 13  Y = {1, 3, 5, 7, 11}
push(17)  Y = {17, 1, 3, 5, 7, 11}
size() = 6
front() = 11
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 36
1 3 5 7 11 13
17 1 3 5 7 11
1 3 5 7 11
Queues
Example
Y = {g, f, e, d, c, b, a} a Queue
What is
pop()
size()
front()
push(h)
isempty()
size()
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 37
g f e d c b a
Queues
Solution
Y = {g, f, e, d, c, b, a} a Queue
What is
pop() = a  Y = {g, f, e, d, c, b}
size() = 6
front() = b
push(h)  Y = {h, g, f, e, d, c, b}
isempty() = false
size() = 7
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 38
g f e d c b a
g f e d c b
h g f e d c b
Sorted List
 In a Sorted List each element has a key
 For this key a complete order relation ≤ exists with:
 a ≤ a (reflexivity)
 a ≤ b and b ≤ a  a = b (anti symmetry)
 a ≤ b and b ≤ c  a ≤ c (transitivity)
 With this order relation you can decided if an element is
smaller or bigger than another one
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 39
Sorted List
 The following operations exist for Sorted List:
 init() initialize an empty Sorted List
 insert() insert an element in the list so that the
is still sorted
 removefirst() removes the first element in the list (the
element with the lowest key)
 getfirst() get the first element from the list
 search(key) search for an element with given key
 delete(key) delete an element with given key
 length() the size of the Sorted List
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 40
Sorted List
Insert in a Sorted List
 Different from other list variants the value of the new element
decided in which place of the list it will be insert
 If the list is empty or if the value of the first element is greater
than the value of the new element, the new element is to be
inserted at beginning of the list
 Otherwise you have to pass through the list until you find a
value greater than the value of the new element
 For this you use a pointer passing from element to element
and comparing the values
 Traversing is also necessary if you print out an element or if
you remove an element of the list
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 41
Sorted List
Example
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 42
5 14 44 67 XX
22
5 14 44 67 XX 22
1. 5 < 22
2. 14 < 22 3. 44 > 22
Any
questions?
23/10/2018 Lecture 4 Dynamic Data Structure Part 1 43

More Related Content

PPTX
Lecture4b dynamic data_structure
PPTX
Introduction to data structure ppt
PDF
Data and File Structure Lecture Notes
PPT
Data structure
PDF
Introduction to Data Structure
PPT
Indexing Data Structure
DOCX
Chapter 1
PPT
Introductiont To Aray,Tree,Stack, Queue
Lecture4b dynamic data_structure
Introduction to data structure ppt
Data and File Structure Lecture Notes
Data structure
Introduction to Data Structure
Indexing Data Structure
Chapter 1
Introductiont To Aray,Tree,Stack, Queue

What's hot (20)

PDF
Introduction of Data Structures and Algorithms by GOWRU BHARATH KUMAR
PPT
Chapter 1( intro &amp; overview)
PDF
Modern Database Systems - Lecture 01
PPT
Introduction to data structure
PPTX
Dynamic multi level indexing Using B-Trees And B+ Trees
PDF
Modern Database Systems - Lecture 02
PPT
Lecture 1 data structures and algorithms
PPTX
Introduction to data structure
PPTX
Introduction of Data Structure
PPTX
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
PPTX
Data Structure & Algorithms | Computer Science
DOCX
Data Structure Question Bank(2 marks)
DOC
DATA STRUCTURES - SHORT NOTES
PPSX
Data Structure # vpmp polytechnic
PDF
104333 sri vidhya eng notes
PDF
Furnish an Index Using the Works of Tree Structures
PDF
Query Processing and Optimisation - Lecture 10 - Introduction to Databases (1...
PPTX
3. Stack - Data Structures using C++ by Varsha Patil
PDF
Access Methods - Lecture 9 - Introduction to Databases (1007156ANR)
PDF
Programming & Data Structure Lecture Notes
Introduction of Data Structures and Algorithms by GOWRU BHARATH KUMAR
Chapter 1( intro &amp; overview)
Modern Database Systems - Lecture 01
Introduction to data structure
Dynamic multi level indexing Using B-Trees And B+ Trees
Modern Database Systems - Lecture 02
Lecture 1 data structures and algorithms
Introduction to data structure
Introduction of Data Structure
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data Structure & Algorithms | Computer Science
Data Structure Question Bank(2 marks)
DATA STRUCTURES - SHORT NOTES
Data Structure # vpmp polytechnic
104333 sri vidhya eng notes
Furnish an Index Using the Works of Tree Structures
Query Processing and Optimisation - Lecture 10 - Introduction to Databases (1...
3. Stack - Data Structures using C++ by Varsha Patil
Access Methods - Lecture 9 - Introduction to Databases (1007156ANR)
Programming & Data Structure Lecture Notes
Ad

Similar to Lecture4a dynamic data_structure (20)

PDF
Link list
PPT
Introduction to Data structures and Trees.ppt
PPTX
Data Structures_Linked List
PPTX
Data Structures-UNIT Four_Linked_List.pptx
PPTX
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
PPT
Chapter 5 ds
PDF
Notes of bca Question paper for exams and tests
PDF
Data structure
PDF
LinkedList1LinkedList1LinkedList1111.pdf
PPTX
RPT_03_A_Linked List presentation for FE
PPT
Linked List
PPTX
Algorithms
PPTX
csc211_lecture_21.pptx
PDF
CS8391-DATA-STRUCTURES.pdf
PDF
CS8391-DATA STRUCTURE.pdf1111111111111111
PDF
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
PPT
Unit ii(dsc++)
PPTX
Lecture ............ 3 - Linked Lists.pptx
PPTX
Unit – III.pptx Data Structures and Algorithms
DOCX
Link list assi
Link list
Introduction to Data structures and Trees.ppt
Data Structures_Linked List
Data Structures-UNIT Four_Linked_List.pptx
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
Chapter 5 ds
Notes of bca Question paper for exams and tests
Data structure
LinkedList1LinkedList1LinkedList1111.pdf
RPT_03_A_Linked List presentation for FE
Linked List
Algorithms
csc211_lecture_21.pptx
CS8391-DATA-STRUCTURES.pdf
CS8391-DATA STRUCTURE.pdf1111111111111111
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Unit ii(dsc++)
Lecture ............ 3 - Linked Lists.pptx
Unit – III.pptx Data Structures and Algorithms
Link list assi
Ad

More from mbadhi barnabas (8)

PPTX
Lecture3b searching
PPTX
Lecture3a sorting
PPTX
Lecture2b algorithm
PPTX
Lecture2a algorithm
PPTX
Lecture1b data types
PPTX
Lecture1a data types
PDF
Data struture and aligorism
PDF
Data structures and algorithm
Lecture3b searching
Lecture3a sorting
Lecture2b algorithm
Lecture2a algorithm
Lecture1b data types
Lecture1a data types
Data struture and aligorism
Data structures and algorithm

Recently uploaded (20)

PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
PDF
.pdf is not working space design for the following data for the following dat...
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
PPT
Reliability_Chapter_ presentation 1221.5784
PDF
Mega Projects Data Mega Projects Data
PDF
annual-report-2024-2025 original latest.
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PPTX
Introduction to machine learning and Linear Models
PPTX
Database Infoormation System (DBIS).pptx
PDF
Introduction to Data Science and Data Analysis
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PPTX
Supervised vs unsupervised machine learning algorithms
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PPTX
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
PPTX
STERILIZATION AND DISINFECTION-1.ppthhhbx
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
.pdf is not working space design for the following data for the following dat...
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
Reliability_Chapter_ presentation 1221.5784
Mega Projects Data Mega Projects Data
annual-report-2024-2025 original latest.
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
Introduction to machine learning and Linear Models
Database Infoormation System (DBIS).pptx
Introduction to Data Science and Data Analysis
Galatica Smart Energy Infrastructure Startup Pitch Deck
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
Supervised vs unsupervised machine learning algorithms
Miokarditis (Inflamasi pada Otot Jantung)
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
STERILIZATION AND DISINFECTION-1.ppthhhbx

Lecture4a dynamic data_structure

  • 1. Lecture 4 Dynamic Data Structure Part 1 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 1
  • 2. Content Lecture 4 Dynamic Data Part 1  Pointer  Linear Lists  Linked List  Circular List  Doubly-Linked List  Stack  Queues  Sorted List  Linked Lists vs. Dynamic Arrays 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 2
  • 3. Pointers  Pointers are an essential instrument in dynamical data structures  But the use of pointers is not trivial  That is because of: //important  Technics working with pointer are hard to read  The chances for errors are higher (especially for beginners)  Undefined pointers are causing the most program crashes 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 3
  • 4. Pointers Address space  When a program is executed the data are placed in the memory  You can see the memory as an array which elements are memory cells  The index area of this array is called address space  The single memory cell is in general one byte (8 bits)  Each active program has its own virtual address space  This space consists of a heap and a stack  Local variable or constants are stored in the stack 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 4
  • 5. Pointers Dynamical Data/Heap  If you need more memory during run time the memory has to be allocated in the free parts of the address space (heap)  In opposite to global variables the addresses of the memory locations are not constant  Therefore to address this memory you need so called pointers  Pointers are variables whose values refers directly to (point to) another value by using its address  This address refers to a memory location in which the data are written  Therefore a pointer points to a memory address 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 5
  • 6. Pointers 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 6 Storage for dynamical data created or initialized at runtime Storage for local variables and parameters declared and initialized before runtime
  • 7. Pointers Example in C int *myptr = &myvar; 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 7
  • 8. Pointers  More than one pointer can refer to the same memory location  In program language pointers are often used to realize a call-by-reference function call, also for lists, strings, lookup tables, control tables and trees  You can still change the data behind the pointer  If a pointer references to a location in memory to obtain the value at this location this is called dereferencing  To release the memory you have to deallocate the memory  In some program language the deallocation of the memory is done by a so called Garbage Collector (for example Java) 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 8
  • 9. Pointers  In other language the programmer has to take care of the deallocation by himself (for example C/C++)  That leads to following problems:  The deallocation can be forgotten (memory leaks)  It is not always trivial to calculate what and when something has to be deallocated  Pointers are still used even when the deallocation has taken place (dangling reference)  All this problems can lead to a mostly unexpected behavior of the program execution 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 9
  • 10. Linear Lists  To one of the simplest data structure belongs the Linear List  A number of elements ai are represented in an order form: a1 a2 … ai-1 ai ai+1 … an  Typical operations are:  Putting a new element at the beginning or at the end of the list  Deleting an element from the list  Getting an element at position i (especially when i=1 or i=n)  Getting the next or previous element of an element ai (therefore ai+1 or ai-1) 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 10
  • 11. Linear Lists Examples for lists  Orders in a shop: Each element is equal to an order. The order tells which article has to be send to a costumer. Each new order is put at the end of the list. After finishing the order the element is deleted in the list.  Moves: Each element is equal to a move in a game. A new move is put at the end of the list. You can restore an old score by removing the last element.  Timetable: Each element is equal to a new appointment or short information. All elements are sorted by time. New entries are therefore put in the list according to their time. Expired entries are deleted from the list. 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 11
  • 12. Linear Lists  An implementation of a list data structure may require some of the following operations:  An operation to creating an empty list (init)  An operation to test whether or not a list is empty (isempty)  An operation for adding an entity to a list at the beginning  An operation for appending an entity to a list  An operation for receiving the first component element (head) of a list or the last element  An operation for referring to the list consisting of all the components of a list except for its first (this is called the "tail" of the list) 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 12
  • 13. Linked List  A Linked List is a data structure that consists of a sequence of data records such that in each record there is a field that contains a reference (a link) to the next record in the sequence  Each record of a Linked List is often called an element or node  The field of each node that contains the address of the next node is usually called the next link or next pointer  The remaining fields are known as the data, information or value 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 13
  • 14. Linked List struct Node { data; //The data/value being stored in the node Node next; //reference to the next node, null for last //node }; struct List { Node first; //Points to first node of list; null for empty //list }; 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 14
  • 15. Linked List 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 15 5 14 44 67 X node/element next link/pointer data/value null/end of the list list first link/pointer
  • 16. Linked List 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 16 5 14 44 67 X 31 5 14 44 67 X 31 Insert a new node 5 14 44 67 X 31 newNode.next newNode.next = node.next node.next = newNode newNode node node.next
  • 17. Linked List 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 17 5 14 44 3list newNode 5 14 44 3list newNode.next = list.first; Special case if you insert a new node at the beginning 5 14 44 3list list.first = newNode;
  • 18. Linked List Removing a node 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 18 5 14 44 67 5 14 44 67 node obsoleteNode node.next 5 14 44 67 node.next node.next.next destroy obsoleteNode node.next = node.next.next;
  • 19. Linked List 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 19 5 14 44 list 5 14 44 list 5 14 44 list obsoleteNode = list.first list.first = list.first.next list.first.next destroy obsoleteNode Removing a node in front of the list
  • 20. Circular List  In the most cases the last node of a list contains a null value  Means that there is no next node in the list  In some cases a pointer to the first node of the list is made  This is called a Circular List 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 20 5 14 44 67 struct List { Node first; //Points to first node of list; Node last; //Points to first node of list; }
  • 21. Circular List Insert an element at the beginning newNode.next = list.first list.first = newNode list.last = newNode; 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 21 5 14 44 67 1 5 14 44 67 1
  • 22. Doubly-Linked List  A Doubly-Linked List is a Linked List that contains a number of elements, each having two special fields referencing to the next and previous element in the list  You can view it as two Linked Lists formed from the same data items, in two opposite directions 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 22 5 14 44 67 XX
  • 23. Doubly-Linked List struct Node { data; // The data being stored in the node Node next; // A reference to the next node; null for last node Node prev; // A reference to the previous node; null for first // node }; struct List { Node firstNode; // points to first node of list; Node lastNode; // points to last node of list; }; 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 23
  • 24. Doubly-Linked List  Iterating through a Doubly Linked List can be done in either direction  In fact, direction can change many times, if desired Forwards node = list.firstNode while (node != null) { //do something with the //data node = node.next; } 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 24 Backwards node = list.lastNode while (node != null) { //do something with the //data node = node.prev; }
  • 25. Doubly-Linked List Inserting a node 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 25 5 14 44 67 XX 31 5 14 44 67 XX 31 newNode newNode.prev = node; newNode.next = node.next; node
  • 26. Doubly-Linked List 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 26 5 14 44 67 XX 31 5 14 44 67 XX 31 node.next = newNode; node.next.prev = newNode
  • 27. Doubly-Linked List 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 27 Removing a node 5 14 44 67 XX 5 14 44 67 XX 5 14 67 XX destroy node; obsoleteNode obsoleteNode.prev.next=obsoleteNode.next; 44 obsoleteNode.next.prev=obsoleteNode.prev;
  • 28. Stack  A Stack is also a Linear List with the difference that only at one end elements could be added or removed  In a Stack you are mostly interested in the element on the top  Elements below are only appearing again if all elements on the top of it are removed  In computer science, a stack is a Last In, First Out (LIFO) abstract data type and data structure  Examples for Stacks: Game Tic-Tac-Toe 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 28
  • 29. Stack  In the most program language the implementation of Stack can be done with arrays  The C++ Standard Template Library provides a Stack Template Class which is restricted to only push/pop operations  Java's library contains a Stack class that is a specialization of the class Vector 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 29
  • 30. Stack  An implementation of a Stack may require some of the following operations:  An operation to creating an empty Stack (init)  An operation to add an element on the top of the Stack (push)  An operation to remove an element on the top of the Stack (pop)  An operation to receive the current element on the top of the Stack (top)  An operation to receive the length of the stack; the number of elements in the Stack (length)  An operation showing that the maximal capacity of the Stack is reached (full) 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 30
  • 31. Stack Example X = {1, 3, 5, 7, 11, 13} a Stack length() = 6 pop() = 13  X = {1, 3, 5, 7, 11} top() = 11 length() = 5 push(17);  X = {1, 3, 5, 7, 11, 17} length() = 6 top() = 17 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 31 13 11 7 5 3 1 11 7 5 3 1 17 11 7 5 3 1
  • 32. Stack Example X = {b, d, f, h, j, l, n, p} a Stack What is: pop() length() top() push(r) length() top() 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 32
  • 33. Stack Solution X = {b, d, f, h, j, l, n, p} a Stack What is: pop()  X = {b, d, f, h, j, l, n} length() = 7 top() = n push(r)  X = {b, d, f, h, j, l, n, r} length() = 8 top() = r 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 33 p n l j h f d b r n l j h f d b n l j h f d b
  • 34. Queues  A Queue or FIFO (First-In-First-Out) is a Linear List where at one end elements are added and on the other end elements are removed  The inner elements are not considered  The elements will be processed in exactly the same order in which they are original placed  The C++ Standard Template Library provides a Queue Template Class. 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 34
  • 35. Queues  The following operations exists in general for queues  init() initialize an empty queue  isempty() true if the queue is empty, otherwise false  pop() removes the item at the front of the queue  push() insert an item at the back of the queue  size() return the number of elements in the queue  front() returns a reference to the value at the front of a non-empty queue 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 35
  • 36. Queues Example Y = {1, 3, 5, 7, 11, 13} a Queue isempty() = false pop() = 13  Y = {1, 3, 5, 7, 11} push(17)  Y = {17, 1, 3, 5, 7, 11} size() = 6 front() = 11 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 36 1 3 5 7 11 13 17 1 3 5 7 11 1 3 5 7 11
  • 37. Queues Example Y = {g, f, e, d, c, b, a} a Queue What is pop() size() front() push(h) isempty() size() 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 37 g f e d c b a
  • 38. Queues Solution Y = {g, f, e, d, c, b, a} a Queue What is pop() = a  Y = {g, f, e, d, c, b} size() = 6 front() = b push(h)  Y = {h, g, f, e, d, c, b} isempty() = false size() = 7 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 38 g f e d c b a g f e d c b h g f e d c b
  • 39. Sorted List  In a Sorted List each element has a key  For this key a complete order relation ≤ exists with:  a ≤ a (reflexivity)  a ≤ b and b ≤ a  a = b (anti symmetry)  a ≤ b and b ≤ c  a ≤ c (transitivity)  With this order relation you can decided if an element is smaller or bigger than another one 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 39
  • 40. Sorted List  The following operations exist for Sorted List:  init() initialize an empty Sorted List  insert() insert an element in the list so that the is still sorted  removefirst() removes the first element in the list (the element with the lowest key)  getfirst() get the first element from the list  search(key) search for an element with given key  delete(key) delete an element with given key  length() the size of the Sorted List 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 40
  • 41. Sorted List Insert in a Sorted List  Different from other list variants the value of the new element decided in which place of the list it will be insert  If the list is empty or if the value of the first element is greater than the value of the new element, the new element is to be inserted at beginning of the list  Otherwise you have to pass through the list until you find a value greater than the value of the new element  For this you use a pointer passing from element to element and comparing the values  Traversing is also necessary if you print out an element or if you remove an element of the list 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 41
  • 42. Sorted List Example 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 42 5 14 44 67 XX 22 5 14 44 67 XX 22 1. 5 < 22 2. 14 < 22 3. 44 > 22
  • 43. Any questions? 23/10/2018 Lecture 4 Dynamic Data Structure Part 1 43

Editor's Notes

  • #10: Garbage Collector: daemon process running in the background