Data Structures and Algorithms

Define Data structures. Classify the data structures.

Data Structure can be defined as the group of data elements which provides an efficient way of storing and organising data in the computer so that it can be used efficiently. Some examples of Data Structures are arrays, Linked List, Stack, Queue, etc. Data Structures are widely used in almost every aspect of Computer Science i.e. Operating System, Compiler Design, Artifical intelligence, Graphics and many more.

 

Data Structures are the main part of many computer science algorithms as they enable the programmers to handle the data in an efficient way. It plays a vital role in enhancing the performance of a software or a program as the main function of the software is to store and retrieve the user's data as fast as possible.

Data Structure Classification

  • Primitive Data Structure
  • Non Primitive Data Structure
    • Linear
      • Static
        • Array
      • Dynamic
        • Linked list
        • Stack
        • Queue
    • Non Linear
      • Tree
      • Graph

 

Linear Data Structures: A data structure is called linear if all of its elements are arranged in the linear order. In linear data structures, the elements are stored in non-hierarchical way where each element has the successors and predecessors except the first and last element.

  • Types of Linear Data Structures are given below:

Arrays: An array is a collection of similar type of data items and each data item is called an element of the array. The data type of the element may be any valid data type like char, int, float or double.

The elements of array share the same variable name but each one carries a different index number known as subscript. The array can be one dimensional, two dimensional or multidimensional.

The individual elements of the array age are:

age[0], age[1], age[2], age[3],......... age[98], age[99].

Linked List: Linked list is a linear data structure which is used to maintain a list in the memory. It can be seen as the collection of nodes stored at non-contiguous memory locations. Each node of the list contains a pointer to its adjacent node.

Stack: Stack is a linear list in which insertion and deletions are allowed only at one end, called top.

A stack is an abstract data type (ADT), can be implemented in most of the programming languages. It is named as stack because it behaves like a real-world stack, for example: - piles of plates or deck of cards etc.

Queue: Queue is a linear list in which elements can be inserted only at one end called rear and deleted only at the other end called front.

It is an abstract data structure, similar to stack. Queue is opened at both end therefore it follows First-In-First-Out (FIFO) methodology for storing the data items.

 

 

Non Linear Data Structures : This data structure does not form a sequence i.e. each item or element is connected with two or more other items in a non-linear arrangement. The data elements are not arranged in sequential structure.

  • Types of Non Linear Data Structures are given below:

Trees: Trees are multilevel data structures with a hierarchical relationship among its elements known as nodes. The bottommost nodes in the herierchy are called leaf node while the topmost node is called root node. Each node contains pointers to point adjacent nodes.

Tree data structure is based on the parent-child relationship among the nodes. Each node in the tree can have more than one children except the leaf nodes whereas each node can have atmost one parent except the root node. Trees can be classfied into many categories which will be discussed later in this tutorial.

Graphs: Graphs can be defined as the pictorial representation of the set of elements (represented by vertices) connected by the links known as edges. A graph is different from tree in the sense that a graph can have cycle while the tree can not have the one.

QUESTION----       Define data structure ? Explain Needs and   classification of data structures.

  • Data can be organized in many different ways. The logical or mathematical model of a organization of data is called a Data Structure
  •  A data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data,

 

NEEDS OF DATA  STRUCTURE--

  • The computer are electronic data processing machine. In order to solve particular problem we need to know:

             1-- How to represent data in computers?

             2-- How to access them?

             3-- What are the steps we need to perform to get the needed output

     These task can be achieved with the knowledge of data structure and algorithm.

 

CLASSIFICATION OF DATA STRUCTURE---

  • Data structure are classified into PRIMITIVE and NON-PRIMITIVE data structures

               1-> PRIMITIVE DATA STRUCTURE  

                      --   These are the fundamentals standards data types.

                      --  These are used to represent single values .

                      example- int, float, char, double

               2->  NON-PRIMITIVE DATA STRUCTURE

                      -- These are derived from primitive data types

                      --  Used to store group of values

                      example- arrays, stacks, queues, trees etc.

  • Based on the structures and arrangement of data, non-primitive data structures are further classified into linear and non-linear.                   

            LINEAR DATA STRUCTURE--

           >  A data structure is said to be linear if its elements form a sequence or a linear list

           > In linear data structures, the data is arranged in a linear fashion although the way they are stored in memory need not be sequential

            example- Arrays, linked list etc

            NON-PRIMITIVE DATA STRUCTURE--

           > A data structure is said to be a non-linear if the data is not arranged in sequence. 

           > The insertion and deletion of data is therefore not possible in a linear fashion

           example- trees, graphs

Why do we need dynamic memory allocation techniques? Explain the functions available for allocating memory dynamically.

DYNAMIC MEMORY ALLOCATION----

  •  It refers to performing manual management for dynamic memory allocation using standard library functions such as malloc, realoc, calloc and free.
  • The size of array initially declared can be sometimes insufficient or sometimes more than required but dynamic memory allocation allows a program to obtain more memory space, while running or to release space when not required.

FUNCTIONS OF DYNAMIC MEMORY ALLOCATION----

  • There are four standard library functions under “stdlib.h” for dynamic memory allocation. 
  1. malloc()
  2. calloc()
  3. realloc()
  4. free()             

=>   malloc() – It allocates requested size of bytes and returns a pointer first byte of allocated space.

                      Syntax : ptr=(data_type *)malloc(bysize);

                      Ex : (int*)malloc(100*sizeof(int)) 

=>   calloc() – Allocates spaces for array elements, initializes to zero and then returns a pointer to memory.

                      Syntax : ptr=(data_type*)calloc(n,element_size);

                      Ex : ptr=(float*)calloc(25,sizeof(float))

=>   realloc() – Changes the size of the previously allocated space according to the requirement.

                      Syntax : ptr=realloc(ptr,newsize);

=>   free() – It deallocates the previously allocated space.

                     Syntax – free(ptr);

Explain declaration, initialization of One dimensional and Two dimensional arrays.

==> One Dimensional Array---

               Declaration::

                      Syntax: data_type array_name[array_size];

                                   where, data_type can be int, float or char.

                                   array_name is the name of the array.

                                   array_size indicates number of elements in the array.

                                   For ex: int age[5];

             Initialization::

                          Syntax: data_type array_name[array_size]={v1,v2,v3};

                                       where, v1, v2, v3 are the values

                                       For ex: int age[5]={2,4,34,25,18};

==> Two Dimensional Array----

             Declaration::

                            Syntax: data_type array_name[array_size][array_size];

                                          where, first index shows the row number of the element and

                                                         second index shows the coulmn number of the element

            Initialisation::

                             Syntax: data_type array_name[array_size][array_size]={V1,V2,V3,.....Vn};

                                           where, V1, V2, V3,......,Vn are the values

                             For ex: int matrix[2][3]={2,4,56,3,6};