You are on page 1of 6

Name: Atitebi Ayoola Emmanuel

Department: Mathematical and Computing Sciences

Matric Number: KDU/FAPS/19/017

Course Code: CSC 204 Fundamental of Data Structures (3 Units)

Assignment 3

1. Define a data structure. What are the different types of data structures? Explain each of
them with suitable example.
Solution

Data Structures are a specialized means of organizing and storing data in computers in such a way
that we can perform operations on the stored data more efficiently. A data structure is a specialized
format for organizing, processing, retrieving and storing data. There are several basic and advanced
types of data structures, all designed to arrange data to suit a specific purpose.

The different types of data structures are:

a. Arrays: An array is a structure of fixed-size, which can hold items of the same data type. It can be
an array of integers, an array of floating-point numbers, an array of strings or even an array of
arrays (such as 2-dimensional arrays). Arrays are indexed, meaning that random access is
possible.
b. Linked lists: A linked list is a sequential structure that consists of a sequence of items in linear
order which are linked to each other. Hence, you have to access data sequentially and random
access is not possible. Linked lists provide a simple and flexible representation of dynamic sets.
c. Stacks: A stack is a LIFO (Last In First Out — the element placed at last can be accessed at first)
structure which can be commonly found in many programming languages. This structure is
named as “stack” because it resembles a real-world stack — a stack of plates.
d. Queues: A queue is a FIFO (First In First Out — the element placed at first can be accessed at
first) structure which can be commonly found in many programming languages. This structure is
named as “queue” because it resembles a real-world queue — people waiting in a queue.
e. Hash tables: A Hash Table is a data structure that stores values which have keys associated with
each of them. Furthermore, it supports lookup efficiently if we know the key associated with the
value. Hence it is very efficient in inserting and searching, irrespective of the size of the data.
f. Trees: A tree is a hierarchical structure where data is organized hierarchically and are linked
together. This structure is different from a linked list whereas, in a linked list, items are linked in a
linear order.
g. Heaps: A Heap is a special case of a binary tree where the parent nodes are compared to their
children with their values and are arranged accordingly.
h. Graphs: A graph consists of a finite set of vertices or nodes and a set of edges connecting these
vertices. The order of a graph is the number of vertices in the graph. The size of a graph is the
number of edges in the graph.
2. What is an algorithm? What are the characteristics of a good algorithm?

Solution

An algorithm is a procedure or formula for solving a problem, based on conducting a sequence of


specified actions. A computer program can be viewed as an elaborate algorithm.

An algorithm is a finite sequence of well-defined computer-implementable instructions, typically to


solve a class of specific problems or to perform a computation. Algorithms are always unambiguous
and are used as specifications for performing calculations, data processing, automated reasoning,
and other tasks. In contrast, a heuristic is a technique used in problem solving that uses practical
methods and/or various estimates in order to produce solutions that may not be optimal but are
sufficient given the circumstances.

 Finiteness – An algorithm must always terminate after a finite number of steps i.e.
the algorithm stops after a finite number of instructions are executed.
 Definiteness – Each step of an algorithm must be precisely defined; the actions to be carried out
must be rigorously and unambiguously specified for each case.
 Precision – the steps are precisely stated(defined).
 Uniqueness – results of each step are uniquely defined and only depend on the input and the
result of the preceding steps.
 The input must be specified.
 The output must be specified.
 Effectiveness.

3. How does an array differ from an ordinary variable?

Solution

An array is different from ordinary variable because array holds multiple values, whereas
an ordinary variable holds a single value. It is true when the elements of the array are treated as
individual entities, and when the variables such as an int. It is not generally right to distinguish
between a variable and an array.

4. Explain a Stack structure and its applications.

Solution

A Stack structure is a simple data structure that allows adding and removing elements in a particular
order. Every time an element is added, it goes on the top of the stack and the only element
that can be removed is the element that is at the top of the stack, just like a pile of objects.
An application of stack structure is:

o Stacks can be used for expression evaluation: Stack data structure is used for evaluating the
given expression.

For example, consider the following expression

5 * (6 + 2) - 12 / 4
Since parenthesis has the highest precedence among the arithmetic operators, (6+2) = 8 will be
evaluated first. Now, the expression becomes
5 * 8 - 12 / 4
* and / have equal precedence and their associativity is from left-to-right. So, start evaluating the
expression from left-to-right.
5 * 8 = 40 and 12 / 4 = 3
Now, the expression becomes
40 - 3
And the value returned after the subtraction operation is 37.

5. Write C++ structure for implementing stack using an array. Using this structure, write
functions for push and pop operations.
Solution
We already discussed arrays. Arrays are used to store similar type of data; and if you have ever
thought if there is any way to store dissimilar data, the answer is yes.
We use structures to store different types of data. For example, you are a student. Your name is a
string and your phone number and roll.no are integers. So, here, Name, Address and Phone number
are those different types of data. Here, structure comes in the picture.
6. Differentiate between using Arrays and Linked Lists for implementation of Linear Lists.
Solution
Arrays and Linked lists are both linear data structures, but both of them have some advantages and
disadvantages over each other. For example, an array is a datatype which is widely implemented as
a default type, in most of the modern programming languages, which are used to store data of
similar type. But there are many cases, like the one where we don't know the quantity of data to be
stored, for which some other data structures like Linked Lists can be used.

Linked List is an ordered collection of elements of


An array is a collection of elements of a
the same type in which each element is connected
similar data type.
to the next using pointers.
Array elements can be accessed randomly Random accessing is not possible in linked lists. The
using the array index. elements will have to be accessed sequentially.

New elements can be stored anywhere and a


Data elements are stored in contiguous
reference is created for the new element using
locations in memory.
pointers.

Insertion and Deletion operations are costlier


Insertion and Deletion operations are fast and easy
since the memory locations are consecutive
in a linked list.
and fixed.

Memory is allocated during the compile time Memory is allocated during the run-time (Dynamic
(Static memory allocation). memory allocation).

Size of the array must be specified at the time Size of a Linked list grows/shrinks as and when new
of array declaration/initialization. elements are inserted/deleted.

Advantages of Linked Lists

 Size of linked lists is not fixed; they can expand and shrink during run time.
 Insertion and Deletion Operations are fast and easier in Linked Lists.
 Memory allocation is done during run-time (no need to allocate any fixed memory).
 Data Structures like Stacks, Queues, and trees can be easily implemented using Linked list.

Disadvantages of Linked Lists

 Memory consumption is more in Linked Lists when compared to arrays. Because each node
contains a pointer in linked list and it requires extra memory.
 Elements cannot be accessed at random in linked lists.
 Traversing from reverse is not possible in singly linked lists.

7. What are the advantages and limitation of an Array?

Solution

The advantages of arrays go thus:

 Arrays represent multiple data items of the same type using a single name.
 In arrays, the elements can be accessed randomly by using the index number.
 Arrays allocate memory in contiguous memory locations for all its elements. Hence there is no
chance of extra memory being allocated in case of arrays. This avoids memory overflow or
shortage of memory in arrays.
These are the limitations of arrays:

 The number of elements to be stored in an array should be known in advance.


 An array is a static structure (which means the array is of fixed size). Once declared the size of
the array cannot be modified. The memory which is allocated to it cannot be increased
or decreased.
 Insertion and deletion are quite difficult in an array as the elements are stored in consecutive
memory locations and the shifting operation is costly.
 Allocating more memory than the requirement leads to wastage of memory space and less
allocation of memory also leads to a problem.

8. Write Structure for implementing Linked List of integers.

Solution

There are multiple functions that can be implemented on the linked list in C. Let’s try to understand
them with the help of an example program. First, we create a list, display it, insert at any location,
and delete a location. The following code will show you how to perform operations on the list

 create()
 display()
 insert_begin()
 insert_end()
 ]insert_pos()
 delete_begin()
 delete_end()
 delete_pos()

9. Define tree and binary tree. Explain in detail.

Solution

A tree is a widely used abstract data type that simulates a hierarchical tree structure, with a root
value and sub trees of children with a parent node, represented as a set of linked nodes.

A binary tree is a tree data structure in which each node has at most two children, which are
referred to as the left child and the right child. A recursive definition using just set theory notions is
that a (non-empty) binary tree is a tuple (L, S, R), where L and R are binary trees or the empty
set and S is a singleton set containing the root. Some authors allow the binary tree to be the empty
set as well.
10. Explain operations of Queue.

Solution

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.

 enqueue() − add (store) an item to the queue.


 dequeue() − remove (access) an item from the queue.

You might also like