0% found this document useful (0 votes)
33 views

Basic Data Structures: University of Technology and Engineering Vietnam National University Hanoi

The document provides an overview of basic data structures including arrays, linked lists, queues, priority queues, and stacks. It describes key properties and operations for each data structure. For arrays, it covers one-dimensional and two-dimensional arrays and provides examples of array implementations and operations in C++ using vectors. For linked lists, it describes singly linked lists and doubly linked lists as well as common operations like insertion and deletion. It also provides examples of queue and priority queue operations and applications.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Basic Data Structures: University of Technology and Engineering Vietnam National University Hanoi

The document provides an overview of basic data structures including arrays, linked lists, queues, priority queues, and stacks. It describes key properties and operations for each data structure. For arrays, it covers one-dimensional and two-dimensional arrays and provides examples of array implementations and operations in C++ using vectors. For linked lists, it describes singly linked lists and doubly linked lists as well as common operations like insertion and deletion. It also provides examples of queue and priority queue operations and applications.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Basic data structures

University of Technology and Engineering


Vietnam National University Hanoi
Outline

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

➢ Important property: Elements are consecutively located


on the memory, therefore getting and changing values
of elements are easy and efficient.

3
Array

one two
dimension dimensions

4
One-dimensional array
➢ element (p) : return the element at
position p arr[0] 73

➢ insert (p, x): insert x into position arr[1] 98

p, elements from p are moved arr[2] 86


backward one position.
arr[3] 61
➢ delete (p): delete the element at
arr[4] 96
position p, elements after p are
moved forward one position.

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

5. Given a matrix A of m rows and n columns containing integer


numbers, write two functions to do the following tasks:
❖ Find the rows whose sums are positive
❖ Find the columns whose sums are negative

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;

std::cout << ”Size: " << myints.size() << '\n';


for (int i=0; i<10; i++)
myints.push_back(i);
std::cout << “Size: " << myints.size() << '\n';

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;

Method element (head, p):


current ← head;
index ← 0;
while (index < p) do
index ← index + 1;
current ← current → next;
return current;
13
Singly linked list operations
delete (head, p): delete element at position p

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

Compare the element operations on array and


linked list structures.

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

Given a singly linked list Head containing n integer numbers,


write 2 functions to do the following tasks:
❖Find the greatest number of Head
❖Calculate the sum of the list of Head

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.

Example: Queue to buy movie tickets

delete/dequeue insert/enqueue

B C D

A FIFO (First In First Out) E

23
Queue operations
enqueue (Q, x): insert element x at the tail of the queue Q

enqueue

A B C D

FIFO (First In First Out) E

24
Queue operations
dequeue (Q): remove the element at the head of queue Q

dequeue

B C D E

A FIFO (First In First Out)

25
Exercises on Queue
1. Implement enqueue() and dequeue() operations of the
queue structure using an array.

2. Implement enqueue() and dequeue() operations of the


queue structure using a linked list.

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.

2. Implement push() and top() operations of the stack


structure using a linked list.

32
Queue container in C++

Queue structure is a standard container in C++ to store and


handle data.
#include <iostream>
#include <queue>

using namespace std;

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

q.pop() ; // removes 2 from the stack , front = 3


}

33
Stack container in C++

Stack structure is a standard container in C++ to store and


handle data.

#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

You might also like