Ds-Unit-I 2019-2020
Ds-Unit-I 2019-2020
Unit-I
Q1. What is Data type?
A data type is a set of data values having predefined characteristics. Example data types
would be integer, floats, string and characters. Generally in all programming languages
have a limited number of such data types. The range of values that can be stored in each
of these data types is defined by the language and the computer hardware. These basic
data types are also known as the primitive data types. In addition to this all data types can
be broken up into:
Scalar Data types (Built-in data types):- A simple (scalar) data type consists of a
collection of ordered values and a set of operations that can be performed on those
values. The C, C++ and Java programming languages refer to scalar types as primitive
data types. A scalar variable can hold only one piece of data at a time. So in C, C++ and
Java scalar data types include int, char, float and double, along with others.
Structured data types (Derived data types):- Structured data types hold a collection of
data values. This collection will generally consist of the primitive data types. Examples
of this would include arrays, records (structs), classes, stacks, queues and files. These
data types, which are created by programmers, are extremely important and are the
building block of data structures.
ADT is a useful tool for specifying the logical properties of a data type. A data type
is a collection of values and a set of operations on those values. The collection and
operations form a mathematical construct that may be implemented using a particular
hardware or software data structure. In defining an abstract data type as a mathematical
concept, we are not concerned with space or time efficiency. The definition of ADT is
not concerned with implementation details at all.
Definition:- The organized collection of data is called data structure. The programs have
to follow certain rules to access and process the structured data
Definition:- Data may be organized in many different ways: the logical or mathematical
model of particular organization of data is called a data structure. Data structure are
broadly classified as:
Data Structures
Primitive Data Structures: These are the basic data structures that can be directly
accessed and does not contain any sub-data type.
Examples:
Integer
Character
Float
Boolean
Pointer.
Non-Primitive Data Structure are derived from one or more primitive data structures.
Examples: Arrays, Lists and Files.
Where int specifies the data type or type of elements array stores a is the name of array
and the number specified inside the square brackets is the number of elements an array
can store, this is also called size or length of array.
a= 10 20 30 40 50 60 70 80
3. Stack:-A STACK is a liner data structure in which items may be added or remove
only at one end. An item may be added or removed only from the TOP of the
STACK. This means that the last item to be added to a STACK is the first item
to be removed. Accordingly STACK are also called “Last in First Out” (LIFO)
Lists.
E top
top
D D
C top
C C
B
A top B top B B
top
A A A A
Initially the stack is empty as the elements A , B, C, D,E are inserted into it. The Stack
grows continuously upward.
C C TOP
B B
A A
If two elements are deleted from Stack then the elements E and D are removed from the
stack.
4. Queues:- A Queue is a liner data structure, deletions can takes place only at one
end called the FRONT and insertions can takes place only at the other end called
the REAR. Queues are also called First In First Out (FIFO) lists. Since the first
element in a queue will be the first element out of the queue. In other words the
order in which elements entered into a queue is the order in which they leave.
FRONT = -1 0 1 2 3 4 5 6 7 8 9 MAXQ = 10
REAR = -1
Q=
Empty Queue
In this case “Q” is a Queue during the initialization of Queue FRONT is set to 0(zero)
and REAR is set to -1 (minus one) an each addition of a Queue is the variable REAR is
incremented by 1 , on other hand in each deletion operation the variable FRONT is
incremented by 1.
0 1 2 3 4 5 6 7 8 9
Q= A B C
FRONT REAR
0 1 2 3 4 5 6 7 8 9
Q= B C
FRONT REAR
Element A is deleted
Non-Linear Data Structure:- In case of non-linear data structure, they do not maintain
any linear relationship between their elements. For example tree and graph.
1. Trees:- A tree can be defined as finite set of data items. Tree is non-linear type of
data structure in which data items are arranged or stored in a sorted sequence.
Trees represent the hierarchical relationship between various elements. In Trees:
(a) There is a special data item at the top of hierarchy called the root of the
tree.
(b) The remaining data items are partitioned into number of disjoint subsets,
each of which is itself, a tree which is called the sub tree.
(c) The tree always grows in length towards bottom in data structures, unlike
natural trees which grows upwards.
B D
C
E F
G H I J
K
L MA N
Defining Graph:- A graph G(V,E) is a set of vertices ‘v’ and a set of edges ‘e’. Vertices
on the graph are shown as point or circles and edges are drawn as arcs or line segment.
Thus an edge can be represented as e=(v,w) where ‘v’ and ‘w’ are pair of vertices. The
vertices ‘v’ and ‘w’ are pair of vertices. The vertices ‘v’ and ‘w’ lie on e. Vertices may
be considered as cities and edges, arcs , line segment as roads.
V15 V2
V3 V4
Q5. What is Linear List ? Write it ADT. Explain various representations in Linear
List.
Definitions
– Linear list is a data object whose instances are of the form (e1,e2,…,en)
– ei is an element of the list.
– e1 is the first element, and en is the last element.
– n is the length of the list.
– When n = 0, it is called an empty list.
– e1 comes before e2, e2 comes before e3, and so on.
Examples
– student names order by their alphabets
– a list of exam scores sorted by descending order
AbstractDataType LinearList
{
Domain: Instances ordered finite collections of zero or more elements
Operations:
Create(): create an empty linear list
Destroy(): erase the list
IsEmpty(): return true if empty, false otherwise
Length(): return the list size
Find(k,x): return the kth element of the list in x
Search(x): return the position of x in the list
Delete(k,x): delete the kth element and return it in x
Insert(k,x): insert x just after the kth element
Output(out): put the list into the output stream out
}
A pointer is a variable that store memory address or that contains address of another
variable where addresses are the location number always contains whole number.
Syntax:
Here * before pointer indicate the compiler that variable declared as a pointer.
Examples:.
Pointer Expression :
Here declaration tells the compiler that P will be used to store the address of integer
value or in other word P is a pointer to an integer and *p reads the value at the address
contain in p.
Pointers and Arrays: When an array is declared, the compiler allocates a base address
and sufficient amount of storage to contain all the elements of the array in contiguous
memory locations. The base address is the location of the first element of the array.
Suppose the base address of x is 100 and assuming that each integer requires two bytes,
the five elements will be store as follows :
X= 10 20 30 40 50 60 70 80
Definition: An Array is a group of related data items that share a common name.
Ex: int number [5]
A particular value is indicated by writing a number is called index or subscript
after the array name.
1 2 3 4 5
number=
5 4 1 2 6
The complete set of values is referred to as an array, the individual values are called
elements.
One-Dimensional array: A list of items can be given to one variable name using one
subscript, such a variable is called single subscripted-variable or one dimensional array.
Declaration of One-Dimensional Array: Arrays must be declared before they are used.
The general form of array creation is:
Here type specifies the type of elements that will be contained in the array such as int,
float, char etc. The size indicates the maximum no. of elements can be stored inside the
array and the new operation is for memory allocation
Ex: int number [5]=new int[5];
number[1]
5
number[2] 4
number[3] 1
2
number[4]
6
number[5]
Initialization of Arrays:
An array can be initialized in two ways. 1. Compile Time 2. Run Time
2. Run Time Initialization: Arrays can be initialized at run time using a loop with
the help of array index.
for(i=1;i<3;i++)
a[i]= Integer.parseInt(in.readLine());
Two-Dimensional Array:
In two-dimensional array the values are represented in different rows and different
columns. A two – dimensional array is called as a matrix. The syntax is as follows:
3 8
A =
5 6
Run Time Initialization of Two Dimensional Arrays: Two dimenstional Arrays can be
initialized at run time using two loops with the help of array index.
for(i=1;i<3;i++)
for(j=1;j<=3;j++)
a[i][j]= Integer.parseInt(in.readLine());
Multi-Dimensional Array: Programming languages can have two, three, or even ten or
more dimensions. The maximum dimensions can have depends on which compiler is
being used. More dimensions in an array means more data be held,
Initializing a 3D Array:
int arr[3][3][3]=
{
{
{11, 12, 13},
{14, 15, 16},
{17, 18, 19}
},
{
{21, 22, 23},
{24, 25, 26},
{27, 28, 29}
},
{
{31, 32, 33},
{34, 35, 36},
{37, 38, 39}
}
};
Operations on Arrays:
1. Insertion Operation:- Insert operation is to insert one or more data elements into an
array. Based on the requirement, a new element can be added at the beginning,
end, or any given index of array.
Sparse Matrix:
In computer programming, a matrix can be defined with a 2-dimensional array.
Any array with ‘m’ rows and ‘n’ columns represents a m * n matrix. There may be a
situation in which a matrix contains more number of ZERO values than NON-ZERO
values. Such matrix is known as sparse matrix.
In above example matrix, there are only 6 non-zero elements and matrix size is 5 X 6.
We represent his matrix as show in the above. Here the first row in the right side table is
filled with value 5, 6, 6 which indicates that it is a spare matrix with 5 rows, and 6
columns and 6 non-zero values. Second row is filled with 0, 4 and 9 which indicates the
value in the matrix at 0th row , 4th column is 9. In the same way the remaining non-zero
values also follows the similar pattern.
2. Linked Representation:
In Linked list, each node has four fields. These four fields are defined as:
Q9. What is linked list? Explain different types of linked list in data structure (2018)
Definition: Linked list is a collection of elements known as nodes, which contains two
fields of information. One contains the data item and other contains the link or address
of the successor list element.
A linked list is a linear data structure, in which the elements are not stored at
contiguous memory locations. Linked list is a dynamic data structure. The elements in a
linked list are linked using pointers.
Single Linked Lists:- Single Linked List or one-way list, is a linear collection of data
elements, called nodes, where the linear order is given by links. Each individual element
is called as “Node”. Every node contains two fields. The first field is the data, which
store actual value of that node and the second field is the link, which stores the address of
the next node in the sequence.
Structure of node:
Link Field / Address Field
Data
Field
Linked List with 5 nodes:
25 3025 35 1026 15 4365 45 235 80 null
Double Linked List:- In a single link one can move starting from the first node to any
node in one direction only (from left to right) on the other hand a double linked list is a
two way list because one can move in either direction, either from left to right or from
right to left.
In two way list is a linear collection of data elements called nodes where each
node is divided three parts.
1. A pointer field Left which contains the location of the preceding node in the list
2. An information filed data which contains the data value.
3. A pointer field Right which contains the location of the next node in the list
Node Structure:
350 850
Single circular list:- In case of a single linked list, the last node points back to the first
node instead of containing the NULL pointer , is called single circular list.
Double circular list:- In double linked list the right pointer filed of the last node points to
the first node instead of containing NULL pointer. Similarly the left pointer filed of first
node points to the last node instead of containing NULL pointer. Such list is called
double circular list.
First
Q10. What is Single linked list? Write an algorithm to explain the concept of Single
linked list.
Single Linked List or one-way list, is a linear collection of data elements, called
nodes, where the linear order is given by links. Each individual element is called as
“Node”. Every node contains two fields. The first field is the data, which store actual
value of that node and the second field is the link, which stores the address of the next
node in the sequence.
Structure of node:
Link Field
Data Field
Linked List with 5 nodes:
Create(): The variable First points to the first node of the list. The variable Last points to
the last node of the list. The variable Temp points to the newly created node.
Step-1: Start
Step-2: First = NULL
Last = NULL
Step-3: Read data value as d
Step-4: Repeat Step-4 to Step-6 until d==0
Step-5: [create new node as Temp]
Temp->data = d
Temp->link = NULL
Step-6: if(First = NULL)
First = Temp
Last = Temp
Else
Last->link = Temp
Last = Temp
Step-7: Read Data value as d
Step-8: Stop
Traversing() : The variable First points to the first node of the list. The variable Present
points to the node currently being process.
Step-1: Start
Step-2: Present = First
Step-3: Repeat Step-3 to Step-4 Until Present <> NULL
Step-4: Print Present->data
Step-5: Present=Present->link
Step-6: Stop
Insert at First()
Temp
25 null
First
625
35 1026 15 4365 45 235 80 null
Step-1: Start
Step-2: Create new node named as Temp
Step-3: Read data value as d
Step-4: Temp->data = d
Temp->link = NULL
Step-5: Temp->link = First
First = Temp
Step-6: Stop.
Insert at Last()
First Last
25 3025 35 1026 15 4365 45 null Temp
Step-1: Start
Step-2: Create a new node named as Temp
Step-3: Read data value as d
Step-4: Temp->data = d
Temp->link = NULL
Step-5: [Traversing Last Node]
Present=first
Repeat Step-5 until Last->link = NULL
Step-6: present = present->link
Step-7: Last->link = Temp
Last = Temp
Step-8: Stop
First Prsent
25 3025 35 4365 45 235 80 null
1026
Temp
(Before Insertion)
First
25 3025 35 1026 15 4365 45 235 80 null
Step-1: Start
Step-2: Create a new node named as Temp
Step-3: Read data value as d
Step-3: Temp->data = d
Temp->link = NULL
Step-5: Read Inserted Position as P
Present=First
Step-6: i=1
Step-7: Repeat Step-7 while (i<P-1)
Step-8: Present = Present->link
Step-9: Temp->link = Present->link
Present->link = Temp
Step-10: Stop
Delete At First():
First
Temp
25 3025 35 1026 15 4365 45 235 80 null
Step-1 Start
Step-2: Temp=First
Step-3: First=First->link
Step-4: free(Temp)
Step-5: Stop
Delete At Last():
Step-1: Start
Step-2: [Traversing to Last node]
Present=first
Step-3: Repeat Step-3 until Present->link =NULL
Step-4: Present=Present->link
Step-5: Prev->link = NULL
free(Last)
Step-6: Stop
Delete At Position():
First
25 3025 35 4365 45 235 80 null
Step-1:Start
Step-2: Read deleted position as P
Step-3: Present=First
i=1
Repeat Step-3 while(i<p)
Step-4: Prev = Present
i=i+1
Step-5: Prev->link = Present->link
Step-6: free(Present)
Step-7: Stop
Q11. What is double linked list? Write an algorithm to explain the concept of double
linked list.
In a single link one can move starting from the first node to any node in one direction
only (from left to right) on the other hand a double linked list is a two way list because
one can move in either direction, either from left to right or from right to left.
In two way list is a linear collection of data elements called nodes where each node is
divided three parts.
1. A pointer field Left which contains the location of the preceding node in the list
2. An information filed data which contains the data value.
3. A pointer field Right which contains the location of the next node in the list
Node Structure:
Create()
Step-1: Start
Step-2: First = NULL
Last = NULL
Step-3: Read data value as d
Step-4: Repeat Step-3 to Step-6 until d==0
Step-5: [Create new node as Temp]
Traversing()
Step-1:Start
Step-2: Present = First
Step-3: Repeat Step-3 to Step-4 until Present <> NULL
Step-4: Print Present->data
Step-5: Present = Present->Right
Step-6: Stop
Insert At First()
Temp
null 10 null
First
Null 20 450 850 30 null
350
850 450
(Before Insertion)
First
(After Insertion)
Algorithm: Insert At Last()
Step-1: Start
Step-2: Create new node as Temp
Step-3: Read data value as d
Step-4: Temp->Left = NULL
Temp->data = d
Temp->Right = NULL
Step-5: [Traversing Last Node]
present = First
Repeat Step-5 until present->Right <> NULL
Step-6: present = present->Right
Step-7: Last->Right = Temp
Temp->Left = Last
Last = Temp
Step-8: Stop
Insert At Position()
Present
850
(Before Insertion)
First
Step-1:
Step-2: Create new node as Temp
Step-3: Read data value as d
Step-4: Temp->Left = NULL
Temp->data = d
Temp->Right = NULL
Step-5:Read Inserted Position as P
Step-6: [Traversing at Position P-1]
i=1; Present = First
Repeat Step-6 while(I < P-1)
Step-7: Present = Present -> Right
Step-8: Temp->Right = Present->Right
Temp->Left = Present
Present->Right->Left=Temp
Present -> Right = Temp
Step-9: Stop
Delete At First()
First
First
850 450
(After Deletion)
Algorithm: Delete At First()
Step-1:Start
Step-2: Temp = First
Step-3: First=First->Right
Step-4: First->Left = NULL
Step-5: free(Temp)
Step-6: Stop
Last
First
350 850
(After Deletion)
Step-1:Start
Step-2: present = First
Step-3: [Traversing At last node]
Repeat Step-3 until Last->Right <> NULL
Step-4: present = present->Right
Step-5: Temp=Last
Last->Left->Right = NULL
Last=Last->Left
Free(temp)
Step-6: Stop
Delete At Position()
Present
First
First
350 450
(After Deletion)
Step-1:Start
Step-2: Read Deleted Position as P
Step-3: [Traversing At Position P]
i=1; Present = First
Repeat Step-3 while(I<P)
Step-4: Present = Present->Right
i=i+1
Step-5: Present->Right->Left = Present->Left
Present->Left->Right = Present->Right
Step-6: free(Present)
Step-7: Stop
Q12. What is circular linked list? Write a program to explain the concept of
circular linked list. (2017)
or
What is circular linked list? Write an algorithm to explain the concept of
circular linked list. (2018)
Circular Linked List:- Suppose that a small change is made to the structure of a liner
list, so that the next pointer filed of the last node contains the address of the first node
rather than the NULL pointer. Such a list is called circular list.
Circular linked list is sequence of elements in which every element has link to its next element in
the sequence and the last element has a link to the first element in the sequence.
That means circular linked list is similar to the single linked list except that the last node
points to the first node in the list.
Single circular list:- In case of a single linked list, the last node points back to the first
node instead of containing the NULL pointer , is called single circular list.
Double circular list:- In double linked list the right pointer filed of the last node points to
the first node instead of containing NULL pointer. Similarly the left pointer filed of first
node points to the last node instead of containing NULL pointer . Such list is called
double circular list.
Insert At First()
Temp
25 null
Last
First
625
35 1026 15 4365 45 235 80 3025
First Last
Step-1:Start
Step-2: Create new node named as Temp
Step-3: Read data value as d
Step-4: Temp->data = d
Temp->link = NULL
Step-5: Temp->link = First
First = Temp
Step-6: Last->link=temp;
Step-7: Stop
Insert At Last()
First Last
25 3025 35 1026 15 4365 45 625 Temp
Step-1: Start
Step-2: Create a new node named as Temp
Step-3: Read data value as d
Step-4: Temp->data = d
Temp->link = NULL
Step-5: [Traversing Last Node]
Last=First
Repeat Step-5 until Last->link != first
Step-6: Last=Last->link
Step-7: Last->link = Temp
Last = Temp
Last->link=first
Step-8: Stop
Insert At Position()
1026
Temp
(Before Insertion)
First Last
Step-1: Start
Step-2: Create a new node named as Temp
Step-3: Read data value as d
Step-4: Temp->data = d
Temp->link = NULL
Step-5: Read Inserted Position as P
Present=First
Step-6: i=1
Step-7: Repeat Step-7 while (i<P-1)
Step-8: Present = Present->link
Step-9: Temp->link = Present->link
Present->link = Temp
Step-10: Stop
Delete At First()
First Last
Temp
25 3025 35 1026 15 4365 45 235 80 625
First Last
35 1026 15 4365 45 235 80 3025
Step-1: Start
Step-2: Temp=First
Step-3: First=First->link
Step-4:Last->link=first
Step-5: free(Temp)
Step-6: Stop
First Last
25 3025 35 1026 15 4365 45 625
Step-1 : Start
Step-2: [Traversing to Last node]
present=First
Step-3: Repeat Step-3 until present->link = first
Step-4: present=present->link
Step-5: Prev->link = first
free(Last)
prev=last
Step-6: Stop
Delete At Position()
First
25 3025 35 4365 45 235 80 625
Step-1: Start
Step-2: Read deleted position as P
Step-3: Present=First
i=1
Repeat Step-3 while(i<p)
Step-4: Prev = Present
i=i+1
Step-5: Prev->link = Present->link
Step-6: free(Present)
Step-7: Stop
Q14. What are the Advantages and Disadvantages of Single linked List ?
A linked list is a dynamic data structure. They can grow or shrink in size during
the execution of a program
They does not waste memory space. It uses the memory that is just needed for the
list at any point of time. This is because it is not necessary to specify the number
at nodes to be used in the list.
The linked lists provide flexibility in allowing the items to be rearranged
efficiently. It is easier to insert or delete items by rearranging the links.
Disadvantages:
In linked lists the access to any arbitrary item is little cumbersome and time
consuming.
Whenever we deal with a fixed length list, it would be better to use an array rather
than a linked list. A linked list will use more storage than an array with the same
number of items. This is because each item has an additional link field.
Q16. What are the Differences between Linked List and Array?
Array is a collection of elements having same data Linked List is an ordered collection of elements
type with common name. which are connected by links / pointers.
In array, elements can be accessed using In linked list, elements can’t be accessed randomly
index/subscript value, i.e. elements can be randomly but can be accessed only sequentially and accessing
accessed like a[0], a[1], a[2], etc. element takes 0(n) time.
In array, memory is allocated at compile time i.e. In linked list, memory is allocated at run time i.e.
Static Memory Allocation. Dynamic Memory Allocation.
Array can be single dimensional, two dimension or Linked list can be singly, doubly or circular linked
multidimensional. list.