Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
38 views

Chapter 2 - Linear Data Structures

Uploaded by

Fenan Zeinu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Chapter 2 - Linear Data Structures

Uploaded by

Fenan Zeinu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

BIOMEDICAL ENGINEERING

DATA STRUCTURES AND ALGORITHMS


CHAPTER 2
LINEAR DATA STRUCTURES
LEARNING OBJECTIVES

At the end of this chapter, you will be able to:


 Learn basic terminologies on studying data structures.
 Learn classification of data structure.
 Identify the different operations that is possible when utilizing data structures on the program’s
module.
 Understand properties, complexity, advantages, memory allocation, passing to function and accessing
elements of an array.
 Learn linked list’s declaration, advantages, disadvantages and its applications.
 Learn stack’s operations and its related applications.
 Learn queue’s operations and its related applications

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 3


BASIC TERMINOLOGIES

 Data: Elementary value or the collection of values.


 Group Items: Data items which have subordinate data items.
 Record: Defined as the collection of various data items.
 File: A File is a collection of various records of one type of entity, for example, if there are 60
employees in the class, then there will be 20 records in the related file where each record
contains the data about each employee.
 Attribute and Entity: An entity represents the class of certain objects. it contains various
attributes. Each attribute represents the particular property of that entity.
 Field: Field is a single elementary unit of information representing the attribute of an entity.

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 4


ADVANTAGES OF DATA STRUCTURES

 Efficiency: Efficiency of a program depends upon the choice of data structures. There
are better data structures which can make the search process efficient like ordered
array, binary search tree or hash tables.
 Reusability: Data structures are reusable, i.e. once we have implemented a particular
data structure, we can use it at any other place. Implementation of data structures
can be compiled into libraries which can be used by different clients.
 Abstraction: Data structure is specified by the ADT which provides a level of
abstraction. The client program uses the data structure through interface only,
without getting into the implementation details.

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 5


DATA STRUCTURE CLASSIFICATION

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 6


OPERATIONS ON DATA STRUCTURE

 Traversing: Traversing the data structure means visiting each element of the data structure in order to
perform some specific operation like searching or sorting.
 Insertion: Insertion can be defined as the process of adding the elements to the data structure at any
location.
 Deletion:The process of removing an element from the data structure is called Deletion.
 Searching: The process of finding the location of an element within the data structure is called
Searching.
 Sorting:The process of arranging the data structure in a specific order is known as Sorting.
 Merging: When two lists List A and List B of size M and N respectively, of similar type of elements,
clubbed or joined to produce the third list, List C of size (M+N), then this process is called merging.

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 7


ARRAY

 Arrays are defined as the collection of similar type of data items stored at contiguous
memory locations.
 Arrays are the derived data type in C programming language which can store the
primitive type of data such as int, char, double, float, etc.
 Array is the simplest data structure where each data element can be randomly
accessed by using its index number.

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 8


PROPERTIES OF AN ARRAY

 Each element is of same data type and carries a same size i.e. int = 4 bytes.
 Elements of the array are stored at contiguous memory locations where the first
element is stored at the smallest memory location.
 Elements of the array can be randomly accessed since we can calculate the address
of each element of the array with the given base address and the size of data
element.

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 9


ADVANTAGES OF ARRAY

 Array provides the single name for the group of variables of the same type therefore,
it is easy to remember the name of all the elements of an array.
 Traversing an array is a very simple process, we just need to increment the base
address of the array in order to visit each element one by one.
 Any element in the array can be directly accessed by using the index.

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 10


ACCESSING ELEMENTS OF AN ARRAY

 To access any random element of an array we need the following information:


− Base Address of the array.
− Size of an element in bytes.
− Which type of indexing, array follows.

 Address of any element of a 1D array can be calculated by using the following formula:
− A[i] = α + (i) * esize
where:
α - base or starting address
i – element
esize – element size in bytes

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 11


ACCESSING ELEMENTS OF AN ARRAY
CONT…

 To determine the ith element of a 2-dimension array:


− A[i][j] = α + [(i)*(UB2)+(j)] * esize
where:
UB2 – upper bound of the 2nd dimension
α - base or starting address
esize – element size in bytes
 To determine the ith element of a 3-dimension array:
− A[i][j][k]= α +[(i)*(UB2)*(UB3)+(j)*(UB3)+(k)] * esize
where:
UB3 – upper bound of the 3rd dimension
UB2 – upper bound of the 2nd dimension
α - base or starting address
esize – element size in bytes
CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 12
EXERCISES: ARRAY

 Given A[10][3][3][6], α =2000, esize=4 bytes:


− Find the formula to represent an element in a 4-dimensional array.
− Find the total number of elements
− Find the address of A[2][2][0][4]

 Given X[8][3][6][2][3], α =3000, esize=3 bytes:


− Find the total number of elements
− Find the address of X[0][2][5][1][2]

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 13


LINKED LIST

 A linked list is also a collection of elements, but the elements are not stored in a
consecutive location. Suppose a programmer made a request for storing the integer
value then size of 4-byte memory block is assigned to the integer value.
 A linked list can also be defined as the collection of the nodes in which one node is
connected to another node, and node consists of two parts, i.e., one is the data part
and the second one is the address part, as shown in the below figure:

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 14


HOW CAN WE DECLARE THE LINKED LIST?

 The declaration of an array is very simple as it is of single type. But the linked list
contains two parts, which are of two different types, i.e., one is a simple variable, and
the second one is a pointer variable. We can declare the linked list by using the user-
defined data type known as structure.
 The structure of a linked list can be defined as:
struct node
{
int data;
struct node *next;
}
CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 15
BASIC OPERATIONS

 Insertion − Adds an element at the beginning of the list.


 Deletion − Deletes an element at the beginning of the list.
 Display − Displays the complete list.
 Search − Searches an element using the given key.
 Delete − Deletes an element using the given key.

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 16


INSERTION OPERATION EXAMPLE

 Adding a new node in linked list is a more than one step activity. We shall learn this
with diagrams here. First, create a node using the same structure and find the
location where it has to be inserted. Imagine that we are inserting a node B
(NewNode), between A (LeftNode) and C (RightNode).Then point B.next to C −.

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 17


INSERTION OPERATION EXAMPLE
CONT…

 NewNode.next −> RightNode;

 LeftNode.next −> NewNode;

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 18


DELETION OPERATION EXAMPLE

 Deletion is also a more than one step process. We shall learn with pictorial
representation. First, locate the target node to be removed, by using searching
algorithms. The left (previous) node of the target node now should point to the next
node of the target node −

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 19


DELETION OPERATION EXAMPLE
CONT…
 LeftNode.next −> TargetNode.next;

 TargetNode.next −> NULL;

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 20


REVERSE OPERATION EXAMPLE

First, we traverse to the end of the list. It should be pointing to


NULL. Now, we shall make it point to its previous node −

Except the node (first node) pointed by the head node, all
nodes should point to their predecessor, making them their
new successor.The first node will point to NULL.

We'll make the head node point to the new first node by using
the temp node.

The linked list is now reversed.

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 21


STACK

 A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is
named stack as it behaves like a real-world stack, for example – a deck of cards or a pile of
plates, etc.

 A real-world stack allows operations at one end only. For example, we can place or remove
a card or plate from the top of the stack only. Likewise, Stack ADT allows all data operations
at one end only.At any given time, we can only access the top element of a stack.

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 22


STACK REPRESENTATION

 A stack can be implemented by means of Array, Structure, Pointer, and Linked List.
Stack can either be a fixed size one or it may have a sense of dynamic resizing. (Refer
to Laboratory manual for the array and linked list implementation).

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 23


BASIC OPERATIONS

 Stack operations may involve initializing the stack, using it and then de-initializing it. Apart
from these basic stuffs, a stack is used for the following two primary operations −
− push() − Pushing (storing) an element on the stack.
− pop() − Removing (accessing) an element from the stack.
 To use a stack efficiently, we need to check the status of stack as well. For the same purpose,
the following functionality is added to stacks −
− peek() − get the top data element of the stack, without removing it.
− isFull() − check if stack is full.
− isEmpty() − check if stack is empty.

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 24


EVALUATION OF EXPRESSIONS

 Expression is made up of operands, operators and delimiters.


 Operands can be any legal variable names or constants in programming languages.
 Operations are described by operators.
 Consider the order of precedence for basic arithmetic operators:
− Expression inside the parenthesis has the highest order
− Power operator (^) and is evaluated for Right to Left
− Multiplication (*) and Division (/) has the same order and is evaluated from Left to Right
− Addition (+) and Subtraction (-) has the same order and is evaluated from Left to Right
− Unary operator (Negative (-) and Positive (+) sign) has the lowest precedence.

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 25


EVALUATION OF EXPRESSIONS CONT…

 Our concern: how the expressions are evaluated


 The compiler accepts expressions and produce correct result by reworking the
expressions into postfix form. Other forms include infix and prefix.
− Prefix: <Operator> <Operand1> <Operand2>
− Postfix: <Operand1> <Operand2> <Operator>
− Infix: <Operand1> <Operator> <Operand2>

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 26


EXAMPLE: EVALUATION OF EXPRESSION

 Convert the following infix expressions to postfix and prefix:A+B*C/D


 Convert the following prefix expressions to infix: -+-^ABC*D^EFG
 Convert the following postfix expressions to infix:AB+CDE-F+*G-/

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 27


STACK APPLICATION: CONVERSION FROM INFIX
TO POSTFIX USING STACKS

 The order of the operands in both forms is the same whether or not parentheses
are present in the infix expression.
 If the infix expression contains no parentheses, then the order of the operators in
the postfix expression is according to their priority.
 If the infix expression contains parenthesized sub expressions, rule 2 applies for such
sub expression.
 And the following are the priority numbers: 1) icp(x) - priority number when token x
is an incoming symbol (incoming priority) and 2) isp(x) - priority number when token
x is in the stack (in-stack priority)
CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 28
THE ALGORITHM: INFIX TO POSTFIX
CONVERSION

 Step 1. Get the next token x.


 Step 2. If x is an operand, then output x
 Step 3. If x is the ( , then push x onto the stack.
 Step 4. If x is the ), then output stack elements until an ( is encountered. Pop stack
once more to delete the (. If top = 0, the algorithm terminates.
 Step 5. If x is an operator, then while icp(x) < isp(stack(top)), output stack elements;
else, if icp(x) > isp(stack(top)), then push x onto the stack.
 Step 6. Go to step 1.
CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 29
TABLES FOR THE SIMULATION OF
THE ALGORITHM

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 30


EXAMPLE
EXAMPLE: INFIX TO POSTFIX CONVERSION

Convert a + ( b * c + d ) - f / g ^ h into
its postfix form using stack

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 31


QUEUE

 Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both
its ends. One end is always used to insert data (enqueue) and the other is used to remove data
(dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored first will be
accessed first.

 A real-world example of queue can be a single-lane one-way road, where the vehicle enters first, exits
first. More real-world examples can be seen as queues at the ticket windows and bus-stops.
CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 32
QUEUE REPRESENTATION

 As we now understand that in queue, we access both ends for different reasons. The
following diagram given below tries to explain queue representation as data structure −

 As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers and
Structures. For the sake of simplicity, we shall implement queues using one-dimensional array.
(Refer to the Laboratory Manual for the array and linked list implementation of a Queue)
CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 33
BASIC OPERATIONS

 Queue operations may involve initializing or defining the queue, utilizing it, and then
completely erasing it from the memory. Here we shall try to understand the basic operations
associated with queues −
− enqueue() − add (store) an item to the queue.
− dequeue() − remove (access) an item from the queue.
 Few more functions are required to make the above-mentioned queue operation efficient.
These are −
− peek() − Gets the element at the front of the queue without removing it.
− isfull() − Checks if the queue is full.
− isempty() − Checks if the queue is empty.

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 34


TYPES OF QUEUE

 Linear Queue. In Linear Queue, an insertion takes place from one end while the deletion
occurs from another end.
 Circular Queue. In Circular Queue, all the nodes are represented as circular. It is similar to
the linear Queue except that the last element of the queue is connected to the first element.
 Priority Queue. A priority queue is another special type of Queue data structure in which
each element has some priority associated with it.
 Double Ended Queue (Dequeue). Both the Linear Queue and Deque are different as the
linear queue follows the FIFO principle whereas, dequeue does not follow the FIFO principle.
In Dequeue, the insertion and deletion can occur from both ends.
CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 35
QUEUE APPLICATION: TOPOLOGICAL
SORTING

 Topological sorting is problem involving activity networks. It uses both sequential and
link allocation techniques in which the linked queue is embedded in a sequential
vector.
 It is a process applied to partially ordered elements. The input is a set of pairs of
partial ordering and the output is the list of elements, in which there is no element
listed with its predecessor not yet in the output.

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 36


THE ALGORITHM

 Input. A set of number pairs of the form (i, j) for each relation i ≼ j could represent
the partial ordering of elements.The input pairs could be in any order.
 Output. The algorithm will come up with a linear sequence of items such that no
item appears in the sequence before its direct predecessor.

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 37


ALGORITHM PROPER

 A requirement in topological sorting is not to output the items with which the
predecessors are not yet in the output. To do this, there is a need to keep track of
the number of predecessors for every item. A vector could be used for this purpose.
Let's call this vector COUNT. When an item is placed in the output, the count of
every successor of the item is decremented. If the count of an item is zero, or it
becomes zero as a result of putting in the output all of its predecessors, that would
be the time it is considered ready for output. To keep track of the successors, a
linked list named SUC, with structure (INFO, LINK), will be used. INFO contains the
label of the direct successor while LINK points to the next successor, if any.
CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 38
EXAMPLE Draw the Graph:

Given the partial ordering of seven elements, how can Following the procedures of the algorithm, we have below:
they be arranged such that no element appears in the
sequence before its direct predecessor?
(0,1), (0,3), (0,5), (1,2), (1,5), (2,4), (3,2), (3,4), (5,4), (6,5),
(6,7), (7,1), (7,5)

Thus, we have the sorted array: 0, 6, 3, 7, 1, 2, 5, 4


CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 39
SUMMARY

 Basic terminologies on data structures were discussed.


 To understand the essence of data structures in algorithm design, the need of data structures were
discussed, its advantages and classification.
 The operations on data structures includes traversing, insertion, deletion, searching, sorting and
merging.
 Arrays are defined as the collection of similar type of data items stored at contiguous memory
locations. Its property, complexity of the different array operations and its advantages were discussed.
 Accessing elements of array should have the following information: 1) Base Address of the array; 2)
Size of an element in bytes; and 3) Which type of indexing, array follows.
 A linked list is also a collection of elements, but the elements are not stored in a consecutive location.

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 40


SUMMARY CONT…

 Following are the basic operations supported by a list: Insertion, Deletion, Display, Search and Delete.
 Stacks are sometimes called as Last-In-First-Out (LIFO) lists i.e. the element which is inserted first in
the stack, will be deleted last from the stack.
 Evaluation of Expressions includes transforming infix, prefix and postfix expression with each other
 One application of Stack is converting infix to postfix.
 A queue is a data structure in which whatever comes first will go out first. It follows the FIFO (First-
In-First-Out) policy.
 A queue can be a linear queue, circular queue, priority queue and double ended queue
 One application of queue is topological sorting.

CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 41

You might also like