Unit 1 - Data Structure - WWW - Rgpvnotes.in
Unit 1 - Data Structure - WWW - Rgpvnotes.in
Tech
Subject Name: Data Structure
Subject Code: CS-303
Semester: 3rd
Downloaded from be.rgpvnotes.in
Subject Notes
CS-302 – Data Structure
Unit -1
INTRODUCTION:
Computer Science is the study of data, its representation and transformation by Computer. For every data
object, we consider the class of operations to be performed and then the way to represent the object so that
These are several terms involved above which we need to know carefully before we proceed. These include
data structure, data type and data representation.
A data type is a term which refers to the kinds of data that variables may hold. With every programming
language there is a set of built-in data types. This means that the language allows variables to name data of
that type and provides a set of operations which meaningfully manipulates these variables. Some data types
are easy to provide because they are built-i i to the o puter s a hi e la guage i stru tio set, such as
integer, character etc. Other data types require considerably more efficient to implement. In some languages,
these are features which allow one to construct combinations of the built-in types like stru tures i C .
However, it is necessary to have such mechanism to create the new complex data types which are not
provided by the programming language. The new type also must be meaningful for manipulations. Such
meaningful data types are referred as abstract data type.
DATA STRUCTURE:
Data Structures are the programmatic way of storing data so that data can be used efficiently. Almost every
enterprise application uses various types of data structures in one or the other way.
Thus, a data structure is the portion of memory allotted for a model, in which the required data can
be arranged in a proper fashion.:Data Structure is a way of collecting and organising data in such a way that
we can perform operations on these data in an effective way. Data Structures is about rendering data
elements in terms of some relationship, for better organization and storage. For example, we have data
player's name "Virat" and age 26. Here "Virat" is of String data type and 26 is of integer data type.
Programming: Programming is the process of taking an algorithm and encoding it into a notation, a
programming language, so that it can be executed by a computer. Although many programming languages and
many different types of computers exist, the important first step is the need to have the solution. Without an
algorithm there can be no program.Computer science is not the study of programming. Programming,
however, is an important part of what a computer scientist does. Programming is often the way that we create
a representation for our solutions. Therefore, this language representation and the process of creating it
becomes a fundamental part of the discipline.
Single linked list: - A single linked list is used to traverse among the nodes in one direction.
type of list is also called a linked list. A linked list may be a single list or double linked list.
Double linked list: - A double linked list is used to traverse among the nodes in both the directions.
A linked list is normally used to represent any data used in word-processing applications, also applied in
different DBMS packages.
Stack: - It is also called as last-in-first-out (LIFO) system. It is a linear list in which insertion and deletion take
A list has two subsets. They are: -
Queue: - It is also called as first-in-first-out (FIFO) system. It is a linear list in which insertion takes place at
place only at one end. It is used to evaluate different expressions.
once end and deletion takes place at other end. It is generally used to schedule a job in operating systems
and networks.
Non-linear data structure:-
The frequently used non-linear data structures are
Trees: - It maintains hierarchical relationship between various elements
Graphs: - It maintains random relationship or point-to-point relationship between various elements.
The data structures can also be classified on the basis of the following characteristics:
ABSTRACT DATA TYPES:
Abstract Data type (ADT) is a type or class for objects whose behavior is defined by a set of value and a set of
operations.ADT only mentions what operations are to be performed but not how these operations will be
implemented. It does not specify how data will be organized in memory and what algorithms will be used for
i ple e ti g the operatio s. It is alled a stra t e ause it gi es a i ple e tatio i depe de t ie . The
process of providing only the essentials and hiding the details is known as abstraction.
Example: Stack, Queue etc.
IMPLEMENTATION ASPECTS:
MEMORY REPRESENTATION:
There are 2 types of memory allocations possible in C:
Compile time or static allocation
Runtime or Dynamic allocation
1. Compile time or static allocation: Compile time memory allocation means reserving memory for variables,
constants during the compilation process. So you must exactly know how many bytes you require? This
type of allocation is done with the help of Array.The biggest disadvantage of compile time memory
allocation, we do not have control on allocated memory. You cannot increase, decrease or free memory,
the compiler takes care of memory management. We can also refer compile time memory allocation as
static or stack memory allocation.
programmers prefer dynamic memory allocation more over static memory allocation. Since, we have full
control over the allocated memory. Which means we can allocate, de-allocate and can also reallocate
memory when needed.
a. malloc():allocates requested size of bytes and return a void pointer pointing to the first byte the
allocated space.
Syntax: malloc (no. of elements* size of each element);
For example:
int *ptr;
ptr=(int*)malloc(10*sizeof(int));
b. calloc(): allocate space for an array of element and initialize them to zero and then return a void
pointer to memory.
Syntax: malloc (no. of elements, size of data type);
For example:
int *ptr;
ptr=(int*)calloc(10,2);
c. realloc(): modify the size of previously allocated space.
Syntax: ptr=realloc(ptr,newsize);
d. free(): releases previously allocated memory.
Global variable, static variable and program instruction get their memory in permanent storage and
Memory Allocation Process:
The memory space between 2 regions is known as Heap area. This region is used for dynamic memory
where local variable are stored in memory area called stack.
Insertion: - Insertion means adding new details or new node into the data structure.
The four major operations performed on data structures are:
Searching: - Searching means finding the location of node for a given key value.
can be processed. Traversing is also called as visiting.
Apart from the four operations mentioned above, there are two more operations occasionally performed on
1 Dimensional Arrays:A one-dimensional array (or single dimension array) is a type of linear array. Accessing
its elements involves a single subscript which can either represent a row or column index.
As an example consider the C declaration intanArrayName[10];
Syntax :datatypeArrayname[sizeofArray];
LINKED LIST:
A linked list is a linked representation of the ordered list. It is a linear collection of data elements termed as
nodes whose linear order is given by means of link or pointer. Every node consists of two parts. The first part is
called INFO, contains information of the data and second part is called LINK, contains the address of the next
node in the list. A variable called START, always points to the first node of the list and the link part of the last
node always contains null value. A null value in the START variable denotes that the list is empty.
C 1 6
2 3
4 3 5
START A 4 9
5 7
D 6 Null
2 7 8
AVAIL 8 10
B 9 1
10 Null
START
A B C D NULL
malloc() is used to dynamically allocate a single block of memory in C, it is available in the header file
stdlib.h.
sizeof() is used to determine size in bytes of an element in C. Here it is used to determine size of each
node and sent as a parameter to malloc.
Linked Dictionary:
An important part of any compiler is the construction and maintenance of a dictionary containing names and
their associated values. Such dictionary is also called Symbol Table. There may be several symbols
corresponding to variable names, labels, literals, etc.
The constraints, which must be considered in the design of the symbol tables, are processing time and
memory space. There are many phases associated with the construction of symbol tables. The main phases
are building and referencing.
It is very easy to construct a very fast symbol table system, provided that a large section of memory is
available. In such case a unique address is assigned to each name. The most straightforward method of
accessing a symbol table is linear search technique. This method involves arranging the symbols sequentially in
memory via an array or by using a simple linked list. An insertion can be easily handled by adding new element
to the end of the list. When it is desired to access a particular symbol, the table is searched sequentially from
its beginning until it is found. It will take n/2 comparisons to find a particular symbol.