Basic Data Structures: University of Technology and Engineering Vietnam National University Hanoi
Basic Data Structures: University of Technology and Engineering Vietnam National University Hanoi
➢ Array
➢ Linked list
➢Queue
➢ Priority Queue
➢Stack
2
Array
➢ Array is the most basic and common data structure to
store elements of the same data type.
3
Array
one two
dimension dimensions
4
One-dimensional array
➢ element (p) : return the element at
position p arr[0] 73
5
Two-dimensional array
Column Index
0 1 2 3
0 8 6 5 4
Row Index
1 2 1 9 7
2 3 6 4 2
Two-dimensional array
6
Array in C++
➢ One-dimensional array:
DataType arrayName [Max];
Example:
int scoreArr[100];
Student studentArr[100];
➢ Two-dimensional array:
DataType arrayName[MAX1][MAX2];
Example:
int chessBoard[8][8];
7
Exercises on Array
1. Why getting and changing values of elements on an array
are easy and sufficient?
2. Write function insert (p, x) to insert element x into position
p of array A, elements from p are moved backward one
position.
3. Write function delete (p, x) to delete the element at
position p of array A, elements after p are moved forward
one position.
8
Exercises on Array
4. Given an array A containing n integer numbers, write 4 functions to do
following tasks:
❖ Calculate the sum of these numbers
❖ Find the smallest number
❖ Find the greatest number
❖ Delete odd numbers from the array
9
Vector container in C++
➢ Vector is a standard container in C++ to handle dynamic
size array.
Example:
// vector::size
#include <iostream>
#include <vector>
int main () {
std::vector<int> myints;
myints.pop_back();
std::cout << “Size: " << myints.size() << '\n';
return 0;
}
10
Singly Linked List
➢ A singly linked list is a next
concrete data structure
consisting of a sequence of
nodes
➢ Each node stores Data nod
❖ Data e
❖ Link to the next node
A B C D null
Head 11
Singly Linked List
next
Data nod
e
A B C D null
Head
Singly linked list operations
Method move (Node head):
Node current ← head;
while (current != NULL) do
current ← current → next;
A B C D null
Head
14
Singly linked list operations
insert (head, p, x): insert element x into position p
A B C D null
Head
X
15
Doubly Linked List
➢ A doubly linked list is a concrete previous next
data structure consisting of a
sequence of nodes
➢ Each node stores
❖ Data
❖ link to the next node Data nod
❖ link to the previous node e
null A B C D null
head tail
16
Doubly linked List
append (tail, x): append element x at the end of the list
null null
A B C D null X
head
tail
17
Quiz
18
Pointer in C++ overview
Pointer is a variable whose value is a memory address
➢ Declare pointer
DataType *pointer_variable;
Example: int *p;
➢ Allocate memory
pointer_variable = new DataType;
Example: p = new int;
➢ Delete pointer
delete pointer_variable;
Example: delete p;
19
Array by pointer
➢ Declare and locate memory
pointer_array *DataType;
pointer_array = new DataType[SIZE];
Example:
int* arr;
arr = new int[10];
➢ Delete array
delete [] pointer_array;
Example: delete [] arr;
20
Exercises on Linked List
21
Linked list container in C++
➢ Introduction: In C++, lists are containers which are
implemented as doubly-linked lists.
#include <iostream>
#include <list>
int main () {
std::list<int> mylist{2};
mylist.push_front(1)
mylist.push_back (3)
std::cout << "mylist contains:";
for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); it++)
std::cout << ' ' << *it;
return 0;
}
22
Queue
Queue is a data structure to organize a list of elements where
operations occur only at two ends: head and tail.
delete/dequeue insert/enqueue
B C D
23
Queue operations
enqueue (Q, x): insert element x at the tail of the queue Q
enqueue
A B C D
24
Queue operations
dequeue (Q): remove the element at the head of queue Q
dequeue
B C D E
25
Exercises on Queue
1. Implement enqueue() and dequeue() operations of the
queue structure using an array.
26
Priority Queue
➢ A priority queue stores a collection
of entries
➢ Each entry is a pair
(key, value)
➢ Main methods of the Priority Queue
ADT
❖ insert(k, x): inserts an entry with
key k and value x
❖ removeMax(): removes and
returns the entry with largest
key
27
Applications of priority queue
➢Standby flyers
➢Auctions
➢Stock market
28
Stack
➢ A data structure to
Dat
organize a list of Elem
a
ta e nt
elements where Da men
t
Ele Last In – First Out
operations are at the
“top” of the stack. Push Data Data
Pop
Element Element
Data Data
Element Element
Example: Data
Element
Data
Element
❖Page-visited history in a Web Data Data
Element Element
browser Data Data
❖Undo content in a text editor Element Element
Stack Stack
29
Stack operations
Push (S, x): push element x on top of the stack S.
ta t
Da men
Ele
Push Data
Element
Data
Element
Data
Element
Data
Element
Data
Element
Stack
30
Stack operations
Pop (S): Delete the element at the top of stack S
Dat
a
Elem
ent
Data
Pop
Element
Data
Element
Data
Element
Data
Element
Data
Element
Stack
31
Exercises on Stack
1. Implement push() and top() operations of the stack
structure using an array.
32
Queue container in C++
int main ()
{
queue <int> q; // creates an empty queue of integer q
q.push>(2); // pushes 2 in the queue , now front = back = 2
q.push(3); // pushes 3 in the queue , now front = 2 , and back = 3
33
Stack container in C++
#include <iostream>
#include <stack>
using namespace std;
int main ()
{
stack<int> s;
// pushing elements into stack
s.push(2);
s.push(3);
s.push(4);
cout << s.top(); // prints 4, as 4 is the topmost element
cout << s.size(); // prints 3, as there are 3 elements in
}
34