SlideShare a Scribd company logo
1 of 43
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

What's hot

Introduction of Data Structures and Algorithms by GOWRU BHARATH KUMAR
Introduction of Data Structures and Algorithms by GOWRU BHARATH KUMARIntroduction of Data Structures and Algorithms by GOWRU BHARATH KUMAR
Introduction of Data Structures and Algorithms by GOWRU BHARATH KUMARBHARATH KUMAR
 
Chapter 1( intro &amp; overview)
Chapter 1( intro &amp; overview)Chapter 1( intro &amp; overview)
Chapter 1( intro &amp; overview)MUHAMMAD AAMIR
 
Modern Database Systems - Lecture 01
Modern Database Systems - Lecture 01Modern Database Systems - Lecture 01
Modern Database Systems - Lecture 01Michael Mathioudakis
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structureadeel hamid
 
Dynamic multi level indexing Using B-Trees And B+ Trees
Dynamic multi level indexing Using B-Trees And B+ TreesDynamic multi level indexing Using B-Trees And B+ Trees
Dynamic multi level indexing Using B-Trees And B+ TreesPooja Dixit
 
Modern Database Systems - Lecture 02
Modern Database Systems - Lecture 02Modern Database Systems - Lecture 02
Modern Database Systems - Lecture 02Michael Mathioudakis
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsAakash deep Singhal
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structuresunilchute1
 
Introduction of Data Structure
Introduction of Data StructureIntroduction of Data Structure
Introduction of Data StructureMandavi Classes
 
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Data structure,abstraction,abstract data type,static and dynamic,time and spa...Hassan Ahmed
 
Data Structure & Algorithms | Computer Science
Data Structure & Algorithms | Computer ScienceData Structure & Algorithms | Computer Science
Data Structure & Algorithms | Computer ScienceTransweb Global Inc
 
Data Structure Question Bank(2 marks)
Data Structure Question Bank(2 marks)Data Structure Question Bank(2 marks)
Data Structure Question Bank(2 marks)pushpalathakrishnan
 
DATA STRUCTURES - SHORT NOTES
DATA STRUCTURES - SHORT NOTESDATA STRUCTURES - SHORT NOTES
DATA STRUCTURES - SHORT NOTESsuthi
 
Data Structure # vpmp polytechnic
Data Structure # vpmp polytechnicData Structure # vpmp polytechnic
Data Structure # vpmp polytechniclavparmar007
 
Furnish an Index Using the Works of Tree Structures
Furnish an Index Using the Works of Tree StructuresFurnish an Index Using the Works of Tree Structures
Furnish an Index Using the Works of Tree Structuresijceronline
 
Query Processing and Optimisation - Lecture 10 - Introduction to Databases (1...
Query Processing and Optimisation - Lecture 10 - Introduction to Databases (1...Query Processing and Optimisation - Lecture 10 - Introduction to Databases (1...
Query Processing and Optimisation - Lecture 10 - Introduction to Databases (1...Beat Signer
 
3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
Access Methods - Lecture 9 - Introduction to Databases (1007156ANR)
Access Methods - Lecture 9 - Introduction to Databases (1007156ANR)Access Methods - Lecture 9 - Introduction to Databases (1007156ANR)
Access Methods - Lecture 9 - Introduction to Databases (1007156ANR)Beat Signer
 
Programming & Data Structure Lecture Notes
Programming & Data Structure Lecture NotesProgramming & Data Structure Lecture Notes
Programming & Data Structure Lecture NotesFellowBuddy.com
 

What's hot (20)

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

Similar to Lecture4a dynamic data_structure

Data Structures_Linked List
Data Structures_Linked ListData Structures_Linked List
Data Structures_Linked ListThenmozhiK5
 
Algorithms
AlgorithmsAlgorithms
AlgorithmsDevMix
 
CS8391-DATA-STRUCTURES.pdf
CS8391-DATA-STRUCTURES.pdfCS8391-DATA-STRUCTURES.pdf
CS8391-DATA-STRUCTURES.pdfraji175286
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Balwant Gorad
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)Durga Devi
 
2 marks- DS using python
2 marks- DS using python2 marks- DS using python
2 marks- DS using pythonLavanyaJ28
 
UNIT 3a.pptx
UNIT 3a.pptxUNIT 3a.pptx
UNIT 3a.pptxjack881
 
1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptxssuserd2f031
 
Data structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pdData structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pdNimmi Weeraddana
 
Array linked list.ppt
Array  linked list.pptArray  linked list.ppt
Array linked list.pptWaf1231
 

Similar to Lecture4a dynamic data_structure (20)

Link list
Link listLink list
Link list
 
Data Structures_Linked List
Data Structures_Linked ListData Structures_Linked List
Data Structures_Linked List
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
Data structure
 Data structure Data structure
Data structure
 
Linked List
Linked ListLinked List
Linked List
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
csc211_lecture_21.pptx
csc211_lecture_21.pptxcsc211_lecture_21.pptx
csc211_lecture_21.pptx
 
CS8391-DATA-STRUCTURES.pdf
CS8391-DATA-STRUCTURES.pdfCS8391-DATA-STRUCTURES.pdf
CS8391-DATA-STRUCTURES.pdf
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
Link list assi
Link list assiLink list assi
Link list assi
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
Linked list.docx
Linked list.docxLinked list.docx
Linked list.docx
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
2 marks- DS using python
2 marks- DS using python2 marks- DS using python
2 marks- DS using python
 
UNIT 3a.pptx
UNIT 3a.pptxUNIT 3a.pptx
UNIT 3a.pptx
 
1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptx
 
Data structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pdData structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pd
 
Array linked list.ppt
Array  linked list.pptArray  linked list.ppt
Array linked list.ppt
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 

More from mbadhi barnabas

More from mbadhi barnabas (8)

Lecture3b searching
Lecture3b searchingLecture3b searching
Lecture3b searching
 
Lecture3a sorting
Lecture3a sortingLecture3a sorting
Lecture3a sorting
 
Lecture2b algorithm
Lecture2b algorithmLecture2b algorithm
Lecture2b algorithm
 
Lecture2a algorithm
Lecture2a algorithmLecture2a algorithm
Lecture2a algorithm
 
Lecture1b data types
Lecture1b data typesLecture1b data types
Lecture1b data types
 
Lecture1a data types
Lecture1a data typesLecture1a data types
Lecture1a data types
 
Data struture and aligorism
Data struture and aligorismData struture and aligorism
Data struture and aligorism
 
Data structures and algorithm
Data structures and algorithmData structures and algorithm
Data structures and algorithm
 

Recently uploaded

Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAbdelrhman abooda
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...Suhani Kapoor
 
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...ThinkInnovation
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...Pooja Nehwal
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样vhwb25kk
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDRafezzaman
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfSocial Samosa
 
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一F La
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一F sss
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home ServiceSapana Sha
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 

Recently uploaded (20)

Call Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort ServiceCall Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort Service
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
 
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
 
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 

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

  1. Garbage Collector: daemon process running in the background