Data Structure Concept Notes Gunjan Mathur Data Structure Data

advertisement
Data Structure Concept Notes
Gunjan Mathur
Data Structure
Data structure is a way of organizing all data items that considers not only the elements stored but also
their relationships to each other.
A data structure can be broadly classified into
(i) Primitive data structure – int, char, float, double etc
Linear – array, linked list, stack, queue.
(ii) Non-primitive data structure
Non linear – tree and graph.
Operations on Data structures
1. Traversing 2. Searching 3. Inserting 4. Deleting 5. Sorting 6. Merging
Structured Programming
Structured Programming is a computer programming in which the statements are organized in a specific
manner to minimize error or misinterpretation. In structured programming any logic problem can be
solved using just three types of statements:
1) Sequential statements
2) Conditional statements
3) Looping statements
Top down and Bottom up Design
It is an approach to a problem that begins at the highest conceptual level and works down to the details
i.e. to design with main or top level module and progresses downwards to the lowest level or function.
It is an approach to a problem that begins with details and works up to the highest conceptual level i.e. to
design with the lowest level module or function, and progress upward to the main program.
Abstract Data Type (ADT) structures
An abstract data type can be assumed as a mathematical model with a collection of operations defined
on that model i.e. an ADT is a new data type derived or created from basic or built in data type based on
a particular logical or mathematical model
Examples of ADT: array, link list, stack, queue, tree, graph
Memory management in c
There are two types of memory allocations in c
(1) Compile time or static allocation and (2) Run time or dynamic allocation
When we need to allocate memory dynamically then we can use some functions which are,
Malloc() – Allocates one block of memory in bytes and returns a pointer to that allocated memory
Syntax: malloc (number of elements * size of each element)
Ex: ptr = malloc (10 * sizeof (int))
Calloc() – Allocates number of blocks in bytes and returns a pointer to first byte of memory
Syntax: calloc (number of elements, size of each element)
Ex: ptr = calloc (10, sizeof (int))
Free() – It is used to deallocate the previously allocated memory using malloc or calloc function.
Syntax: free (ptr)
Where, ptr is the pointer which holds the address of memory to be free.
Realloc() – It is used to resize the size of already allocated memory block.
Syntax: realloc (ptr, newsize)
Single dimension Array
Array is a collection of elements of similar data type. It is a list of homogeneous elements.
Declaration: <data type> <array name> [size of array] (ex – int arr[10])
Initialization: int arr[5] = {1,2,3,4,5} or at run time
Address calculation of nth element in single dimension array
α + w(n-L1)
where α is base address of array, w is size of every element of array, n is the index of element whose
address is to be find, L1 is the lower bound of array.
Multi dimension Array
Multi dim array is an array with more than one dimension which is used to store data in a tabular format.
Double dim array: int arr[5][2] (array with 5 rows and 2 columns in each row)
Triple dim array: int arr[3][2][3]
Multi dim array: int arr[d1][d2][d3]…..[dn]
Address calculation of [i,j]th element in double dimension array
Row major: α + W [(i - L1)(U2 - L2 + 1) + ( j - L2 )]
Column major: α + W [(j - L2)(U1 - L1 + 1) + (i - L1)]
α is base address of array, W is the size of element of array
i, j are row and column of element whose address is to be find,
L1,U1 are lower and upper bound of row
L2,U2 are lower and upper bound of column
Linear search – compare one by one all elements of array to the value to be searched.
Compare arr[loop] = = search
Binary search – divide array into half and check whether the value belongs to upper part of array or
lower part of array.
Compare arr[mid] = = search
Id arr[mid]<search => low=mid+1
If arr[mid]>search => high=mid-1
Bubble Sort - In bubble sort, each element is compared with its adjacent element. If the first element is
larger than the second one, then the positions of the elements are interchanged, otherwise not changed.
Then next element is compared with its adjacent element and the same process is repeated for all the
elements in the array until we get a sorted array.
Selection Sort - Insertion sort algorithm sorts a set of values by inserting values into an existing sorted
file. Compare every element with its previous elements in array and place it to its correct place, follow
the same for all the elements of array till the end.
Insertion sort - Selection sort algorithm finds the smallest element of the array and interchanges it with
the element in the first position of the array. Then it finds the second smallest element from the
remaining elements in the array and places it in the second position of the array and so on.
Pointers
Pointer is a variable which holds the address of another variable.
Declaration: <data type> <*pointer name> (ex – int *ptr; or char *ptr;)
Data type tells the type of variable whose address pointer will hold.
int a=5, *ptr;
ptr = &a;
Printf(“%d”,*ptr); // this will print 5
Structures
Structure is a user defined data type. Structure is an ADT structure which holds data of different types.
A structure is the data structure that allows values of different data types to be stored together as a
structure.
Declaration:
struct <StructureName>
{
<datatype> member1;
<datatype> member 2;
….. so on
};
Stacks
A stack is a linear ADT structure in which an element may be inserted or deleted only at one end called
the top end of the stack i.e. the elements are removed from a stack in the reverse order of that in which
they were inserted into the stack. A stack follows the principle of last-in-first-out (LIFO) system. Stack
can be implemented using array and link list.
Basic Operations:
PUSH – insert element to stack
POP – delete element from stack.
Empty, Full, display, count – can be done by only using push and pop operations
Queues
Queue is a linear ADT structure in which insertion can take place at only one end called rear end and
deletion can take place at other end called top end. Queue is also called First in First out (FIFO) system
since the first element in queue will be the first element out of the queue. Queue can be implemented
using array and link list.
Basic Operations:
Insert, Delete, Empty, Full, Display, Count
Linked list
Link list is a collection of nodes, in which each node contains a data item and a pointer to the next node.
It is a sequence of nodes that can only be accessed in order (that is, from first to last).
START
31
32
33
34
35
Types of linked list
Singly linked list – list in which each node contains data item and one pointer to next node
Doubly link list – list in which each node contains data item and pointers to previous and next node
Singly Circular linked list – singly link list with last node containing a pointer to first node in the list
Doubly Circular linked list – Doubly link list with last node containing a pointer to first node and first
node containing a pointer to last node in the list
Recursion
It is a technique of dividing problem into smaller problems in terms of same problem.
Recursion is a process of calling itself. A function which calls itself is called a recursive function. It can
be used in place of iteration (loop). Recursion can be categorized into two types:
Direct recursion: function calls itself
Indirect recursion: function A calls to function B which then calls A function.
recursivefunction(parameter)
{
if (stopping case /base condition)
solve it
else
reduce the problem using recursion(call the function with new parameter)
}
Download