Data Structs
Data Structs
An array is a collection of items stored at contiguous memory locations. The idea is to store
multiple items of the same type together. This makes it easier to calculate the position of each
element by simply adding an offset to a base value, i.e., the memory location of the first element of
the array (generally denoted by the name of the array).
• Arrays are best for storing multiple values in a single variable
• Arrays are better at processing many values easily and quickly
• Sorting and searching the values is easier in arrays
Array in Algorithm :
To declare the array, you need to specify a name and the type of data it will contain. To
create it, you need to specify its length (the number of values), as array is a static data
structure, which means you can not change its size once you declared.
array_name : ARRAY_OF type[length];
// another version
FOR i FROM 0 TO tab.length-1 STEP 1 DO
// here we can replace the static
//number in the max field using the `.length` property
Write(tab[i])
END_FOR
END
tab[pos] := elt;
// remember the tab.length is increased by 1.
END
j := 0;
WHILE (j < tab.length) DO
IF (tab[j] = elt) THEN
BREAK; // element is found let break the loop
END_IF
j := j+1; // update index
END_WHILE
j := pos;
WHILE (j < tab.length) DO
tab[j] := tab[j+1]; // translation from right to left
j := j+1; // update index
END_WHILE
END
example
We refer to such an array as an M-by-N array. By convention, the first dimension is the number of
rows and the second is the number of columns. As with one-dimensional arrays. The initialization
of two-dimensional arrays is useful because it masks more code than for one-dimensional arrays.
The following code is initialization of a matrix of integers with zeros :
m : ARRAY_OF INTEGER[M][N];
FOR i FROM 0 TO M-1 DO
FOR j FROM 0 TO N-1 DO
m[i][j] :=0;
END_FOR
END_FOR
In the above syntax str_name is any name given to the string variable and size is used define the
length of the string, i.e the number of characters strings will store.
Initializing a String
A string can be initialized in different ways. We will explain this with the help of an example.
Below is an example to declare a string with name as str and initialize it with “GoMyCode”.
str : STRING[] := "GoMyCode";
There are many function that we use directly with String such as, Concat, ToLower, ToString,
ToInteger, ToFloat.
String
Now, we are going to practice some of what we've learned about string.
We will make three examples of string manipulation.
The first one is about comparing two string.
We can browse a string like we browse an array, we have simply to call the string identifier, with an
index inside brackets.
Let’s see the code below :
ALGORITHM compare_two_strings
VAR
str1, str2, : STRING[50];
i : INTEGER;
BEGIN
Write("Give the first string to compare");
Read(str1);
Write("Give the second string to compare");
Read(str2);
END
Second, let's suppose that we have the same string but the first one is upper case, the algorithm will
return that the two string are not equal.
To make sure that we escape this corner case, we need to make sure that we convert the two strings
into an uppercase or lowercase.
Let’s see the code below :
ALGORITHM compare_ignore_two_strings
VAR
str1, str2, : STRING[50];
i : INTEGER;
BEGIN
Write("Give the first string to compare");
Read(str1);
Write("Give the second string to compare");
Read(str2);
END
In the third and last algorithm, we are going to remove the blanks from the beginning of a given
string.
Let's take a look at this algorithm:
ALGORITHM delete_blank_begin
VAR
str : STRING[] := " GoMyCode";
i : INTEGER := 0;
j : INTEGER := 0;
BEGIN
WHILE ( str[0]=' ') DO
j := 0;
WHILE (j < str.length) DO
str[j] := str[j+1]; // translation from right to left
j := j+1; // update index
END_WHILE
END_WHILE
END
Write(arr[0].x, arr[0].y);
END
Stacks and Queues
A stack is an abstract structure, commonly used in most programming languages. It is named stack
as it behaves like a real-world stack, for example a pile of plates, we can place or remove a plate
from the top of the stack only.
Likewise, Stack allows all data operations at one end only. At any given time, we can only access
the top element of a stack. This feature makes it LIFO data structure. LIFO stands for Last-in-first-
out. Here, the element which is placed (inserted or added) last, is accessed first.
Basic Operations
• push() − Pushing (storing) an element on the stack.
• pop() − Removing (accessing) an element from the stack.
• peek() − get the top data element of the stack, without removing it.
• isEmpty() − check if stack is empty.
pile/file
END_SWITCH
END_WHILE
END
Hash Tables
Hash Table is a data structure which stores data in an associative manner. In a hash table, data is
stored in an array format, where each data value has its own unique index value. Access of data
becomes very fast if we know the index of the desired data.
Thus, it becomes a data structure in which insertion and search operations are very fast irrespective
of the size of the data. Hash Table uses an array as a storage medium and uses hash technique to
generate an index where an element is to be inserted or is to be located from
Hash Tables
Now, the hash table is one of the most famous data structure among developers due to its ability to
give a super-fast lookup for data.
We can use the hash table in many applications like ( Building dictionaries, checking spells, ...).
We can use the hash table in any situation we want to find items very fast.
The hash tables are implemented in almost every programming language that exists, in different
names.
Hash table is data structure in which insertion and search operations are very fast irrespective of the
size of the data.
Hash Table uses an array as a storage medium and uses a hash technique to generate an index where
an element is to be inserted or is to be located.
Hashing is a technique to convert a range of key values into a range of indexes of an array.
The following are the basic primary operations of a hash table.
• Search
Searches an element in a hash table.
• Insert
Inserts an element in a hash table.
• Delete
Deletes an element from a hash tabl
Hash Tables
Now, it's time to get some practice.
In the first example, we are going to see how to declare a hash table and how to manipulate it.
If we are working with the primitive data type (String, Integer,...), we don't need to implement the
hash function. It’s going to be generated automatically.
Let’s consider the code below:
ALGORITHM hash
VAR
// declaring hash table
htab : HASH_TABLE<INTEGER,STRING>;
// HASH_TABLE<Key_type,Value_type>
BEGIN
htab : HASH_TABLE<INTEGER,STRING>;
htab.insert(0,"zero");
htab.insert(1,"one");
htab.insert(2,"two");
htab.insert(3,"three");
Write(htab);
htab.lookup(0); // TRUE
htab.lookup(5); // FALSE
htab.get(0); // "zero"
htab.delete(0);
// duplicate key
htab.insert(3,"three-a");
htab.insert(3,"three-b");
htab.get(3) // "three-b"
// there is no duplicated key (after the execution this
// instruction will delete the first value, and replace it the second)
// duplicate value
htab.insert(3,"three-a");
htab.insert(4,"three-a");
// hash function
HASH_TABLE<STRING,CHAR>
// this is the way to declare a hash function
// we'll see it more in the function course
Now let’s try to design an algorithm that return the first non repeated character.
array : y is the first non-repeated-character
a=2
r=2
y=1
go my code : g is the first non-repeated-character
ALGORITHM first_non_repeated_char
VAR
htab : HASH_TABLE<CHAR,INTEGER>;
str : STRING[50];
i,count : INTEGER;
ch : CHAR;
BEGIN
Read(str);
// first we browse the string to define the count of each character
FOR i FROM 0 TO str.length -1 STEP 1 DO
IF (htab.lookup(str[i]) = TRUE) THEN
count := htab.get(str[i]);
htab.insert(str[i],count+1);
ELSE
htab.insert(str[i],1);
END_IF
END_FOR
// Now we loop the hash table to extract the first unique character
FOR i FROM 0 TO str.length -1 STEP 1 DO
IF (htab.get(str[i]) = 1) THEN
ch := str[i];
BREAK;
END_IF
END_FOR
Write(ch);
END
Linked List
A linked list is a sequence of data structures, which are connected together via links.
We use Linked List instead of an Array, when we don’t know a priori the number of elements to
store.
Linked List is a sequence of nodes which contains items allocated in memory dynamically. Each
node contains a connection to another node. Linked list is the second most-used data structure after
array. Following are the important terms to understand the concept of Linked List.
• Data − Each node of a linked list can store a data from any type (Simple, Struct or other data
).
• Next − Each link of a linked list contains a link to the next link called Next.
• LinkedList − A LiHow to allocate memory ?
To work with memory address we use a special type called Pointer. Pointers can store a memory
address for a specific type. We use ‘^’ to indicate a Pointer.
For example :
x : INTEGER := 5;
p : ^INTEGER := x;
Here, x is an integer that contain variable 5, and p is the pointer pointing on x, meaning contain the
memory address where x is stored.
Linked List is not a type, we can declared using struct, in the declaration we choose the data type.
List : STRUCT
data : INTEGER;
next : ^List;
END_STRUCT
• nked List contains the connection link to the first link called First.
Key-value
In linked list each node contain minimum of two fields. One field is data field to store the data
second field is?Pointer to node
What are the function used to manipulate memory with a linked list.
Create() oui
delete() non
free() oui
allocate() non , ?!!