0% found this document useful (0 votes)
13 views32 pages

Ds-Unit-I 2019-2020

The document provides an overview of data structures, defining data types, abstract data types (ADTs), and various data structures including primitive and non-primitive types. It explains linear data structures like arrays, linked lists, stacks, and queues, as well as non-linear structures such as trees and graphs. Additionally, it discusses operations performed on data structures and details the representation of linear lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views32 pages

Ds-Unit-I 2019-2020

The document provides an overview of data structures, defining data types, abstract data types (ADTs), and various data structures including primitive and non-primitive types. It explains linear data structures like arrays, linked lists, stacks, and queues, as well as non-linear structures such as trees and graphs. Additionally, it discusses operations performed on data structures and details the representation of linear lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

DATA STRUCTURES

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.

Q2. What is meant by Abstract data type.

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.

The specification of an abstract data type includes:


i) The domain of values.
ii) The operations on the domain.
Objects such as lists, sets, strings and graphs, along with their operations, can be viewed
as abstract data types. The elements of a set are considered the domain of values and
union, intersection, size and complement are considered as the operation on the domain.
Similarly when we implement string as an ADT, the operations are
 String concatenation
 String length
 String compare.
The basic idea is that the implementation of these operations is written once in the
program, and any other part of the program that needs to perform an operation on the
ADT can do so by calling the appropriate function. If for some reason implementation
details need to be changed, it should be easy to do so by merely changing the routines
that perform the ADT operation. This change would be completely transparent to the rest
of the program.
There is no rule telling us which operations must be supported for each ADT; this is a
design decision.
SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 1
Q3. What is data Structures ? Explain various data types. ( 2017)
or
Explain different primitive and non-primitive data structures.(2017,2018)
or
Explain different Linear Data Structures and Non-Linear Data structures.

Definition:- The organized collection of data is called data structure. The programs have
to follow certain rules to access and process the structured data

Data Structure = Organized data + Allowed operation

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:

1. Primitive Data Structure


2. Non-Primitive Data Structure / Derived Structure

Data Structures

Primitive Data Structures Non-Primitive Data Structures

Integer Float Character Boolean Pointer

Linear Data Non-Linear Data


Structures Structures

Arrays Linked Lists Stacks Queues Trees Graphs

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.

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 2


Lists are further sub divided into Linear and Non Linear Data structure.
Linear Data Structures:- A data structure said to be linear if it stores data values
sequentially, then that data structure is called as linear data structure. For example an
array, stack and queue holds the linear relationship between its elements, it is called
linear data structure.
1. Arrays:- An array is defined as a set of finite number of homogeneous elements
or data items. It means an array can contain one type of data only, either all
integers, all floating point numbers, or all characters.

Array Declaration: int a[10];

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

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]

2. Linked Lists:- A list can be defined as a collection of variable number of data


items called nodes. An element of list must contain at least two fields, one for
storing data or information and other for storage address of next element.

Linked List with 5 nodes:

25 3025 35 1026 15 4365 45 235 80 null

625 3025 1026 4365 235

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

Initiation into a Stack

Initially the stack is empty as the elements A , B, C, D,E are inserted into it. The Stack
grows continuously upward.

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 3


D TOP

C C TOP

B B

A A

After deletion two elements of Stack

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

A, B and then C inserted

0 1 2 3 4 5 6 7 8 9

Q= B C

FRONT REAR

Element A is deleted

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 4


0 1 2 3 4 5 6 7 8 9 FRONT = 1
REAR = 5
Q= B C D eE F

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.

The tree structure organizes the data into branches.

B D
C

E F
G H I J

K
L MA N

2. Graph:- Graph is a mathematical non-linear data structure capable of


representing many kinds of physical structures. And graph is a collection of
vertices and edges to connect those edges.

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.

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 5


The Graph structure organization.

V15 V2

V3 V4

Q4. Data Structures Operations

1. Creating:- A Data Structure Say Queue is created from the data


2. Inserting:- New values are added to the data structure
3. Modifying:- the values of the structures are modify by replacing old values of
fields with new values.
4. Traversing:-Each field in the structure is visited for processing purpose.
5. Searching:- Certain value is searched in the structure
6. Deleting:- Deleting is the process of removing a element from the data structure
7. Sorting:- Elements are sorted an ascending or descending order
8. Merging:- the records than more than one sorted file are merged together to
produce a single sorted file.
9. Copying:- Elements in one file are copying to another file.
10. Concatenating:- Elements of the file are appended at the end of another file.
11. Splitting:- Elements in a big file are split into smaller files for processing

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

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 6


ADT for Linear List:

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
}

Array Based Representation of Linear List:

 It uses an array to store the elements of linear list.


 Individual element is located in the array using a mathematical formula.
 typical formula
location(i) = i – 1
à ith element of the list is in position i-1 of the array

element [0] [1] [2] [3] [4] MaxSize


5 2 4 8 1
length=5
Linked Representation of Linear List : In a linked representation a list can be defined
as a collection of variable number of data items. An element of list must contain at least
two fields , one for storing data or information and other for storage address of next
element .

Linked List with 5 nodes:

25 3025 35 1026 15 4365 45 235 80 null

625 3025 1026 4365 235

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 7


Q6. What is a pointer. Discuss the various operations performed in a pointer.

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:

Data type *pointer_name;

Here * before pointer indicate the compiler that variable declared as a pointer.

Examples:.

int *p1; //pointer to integer type


float *p2; //pointer to float type
char *p3; //pointer to character type

Pointer operators :- Two operators are used in the pointer

(a) address operator(&) : Return the address of the variable


(b) Indirection operator or dereference operator (*): Indirection operator gives the
values stored at a particular address.

Pointer Expression :

Pointer assignment: int i=10;


int *p=&i;//value assigning to the pointer.

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.

static x[5] = { 10,20,30,40,50,60,70,80};

Suppose the base address of x is 100 and assuming that each integer requires two bytes,
the five elements will be store as follows :

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]

X= 10 20 30 40 50 60 70 80

100 102 104 106 108 110 112 114

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 8


Q7. What is an array? Mention its types. Discuss the various operations performed
in an array.

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.

Types of arrays: There are three types of arrays. They are


1. One-Dimensional array 2. Two-Dimensional Array 3. Multi-Dimensional Arrays

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:

Data type array-name[size] = new Data type[];

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];

Immediately the computer reserves 5 storage locations.

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

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 9


1. Compile Time Initialization: We can initialize the elements of arrays when they
are declared. The syntax is as follows:
type array-name[size]={list of elements};

Ex: int number[3]={5, 4,1};

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:

Data type array-name[row size][column size] = new Data type[][];

Ex: int a[2][2];

The two-dimensional arrays are stored in memory as follows.

3 8
A =
5 6

Initialization of Two-dimensional Array:


The two-Dimensional arrays may be initialized with a list of values enclosed in braces.

Ex: int a[2][3]={ {1,2,3}, {4,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,

A multi dimensional array is declared using the following syntax:


type array_name[d1][d2][d3][d4]………[dn];
Where each d is a dimension, and dn is the size of final dimension.
SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 10
Examples: int table[5][5][20];
In this Example:
 int designates the array type integer.
 table is the name of our 3D array.
 Our array can hold 500 integer-type elements. This number is reached
by multiplying the value of each dimension. In this case: 5x5x20=500.

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.

Example: void insertArray(int a[], int n, int pos, int x)


{
int i;
if (pos>n)
{
Insertion is not possible
}
for(i=n; i>=pos; i--)
{
a[i]=a[i-1];
}
a[pos-1]=x;
}
SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 11
2. Deletion Operation:- Deletion refers to removing an existing element from the
array and re-organizing all elements of an array.

Example: void deleteArray(int a[], int n, int pos, int key)


{
int i;
if (pos>-n-1)
{
Deletion is not possible
}
for(i=pos-1; i<n-1; i++)
{
a[i]=a[i+1];
}
}
3. Search Operation:-You can perform a search for an array element based on its
value or its index.
Example: void deleteArray(int a[], int n, int key)
{
int i;
for(i=0; i<n; i++)
{
if(a[i]==key)
{
Element is found at (i+1)th location
break;
}
}
if(i==n)
{
Element does not found
}
}

4. Traversing Operation:- This operation is used to visit all the elements in an


array
Example: void TraverseArray(int a[], int n)
{
int i;
for(i=0;i<n;i++)
process(a[i]);

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 12


}
5. Sorting Operation:- This operation will sort the array in a specified order.

Example: void SortArray(int a[], int n)


{
int i,j;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i] > a[j])
swap(a[i],a[j])
}
}
}

Q8. What are spare matrices? Explain them. (2017)

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.

Sparse matrix is a matrix which contains very few non-zero elements.

When a sparse matrix is represented with 2-dimensional array, we waste lot of


space to represent that matrix. For example, consider a matrix of size 100 X 100
containing only 10 non-zero elements. In this matrix only 10 spaces are filled with non-
zero values and remaining spaces of matrix are filled with zero. That means, totally we
allocate 100 X 100 X 2 = 20000 bytes of space to store this integer matrix. And to access
these 10 non-zero elements we have to make scanning for 10000 times.

Sparse Matrix Representations:


A sparse matrix can be represented by using TWO representations, those are as follows:

1. Triplet Representation (Arrays)


2. Linked Representation (Linked Lists)

1. Triplet Representation of Sparse Matrix:


In this representation, we consider only non-zero values along with their row and
column index values. In this representation, the 0th row stores total rows, total columns
and total non-zero values in the matrix.

For example, consider a matrix of size 5 X 6 containing 6 number of non-zero values.


This matrix can be represented as shown in following:
SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 13
Sparse Matrix Triplet Representation

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:

 Row: index of row, where non-zero element is located.


 Column: Index of column, where non-zero element is located.
 Value: Value of the non-zero element located at index- (row , column).
 Next node: Address of the next node.

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.

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 14


Types of Linked Lists:
There are three types of Linked Lists as follows:
1. Single Linked List
2. Double Linked List
3. Circular Linked List

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

625 3025 1026 4365 235

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:

Left Data Right


Double Linked List with 3 nodes:

Null 10 850 350 20 450 850 30 Null

350 850

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 15


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.

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.

25 3025 35 1026 15 4365 45 235 80 625

625 3025 1026 4365 235

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

450 10 850 350 20 450 850 30 350

350 850 450

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:

25 3025 35 1026 15 4365 45 235 80 null

625 3025 1026 4365 235

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 16


Operations on a Single Linked List:

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

3025 1026 4365 235


(Before Insertion)
First
25 3025 35 1026 15 4365 45 235 80 null

625 3025 1026 4365 235


(After Insertion)

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 17


Algorithm: ( Insert At First( ) )

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

625 3025 1026 4365


80 null
(Before Insertion)
235
First
25 3025 35 1026 15 4365 45 235 80 null

625 3025 1026 4365 235


(After Insertion)

Algorithm: (Insert At 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: [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

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 18


Insert at Position()

First Prsent
25 3025 35 4365 45 235 80 null

625 3025 15 null 4365 235

1026
Temp
(Before Insertion)
First
25 3025 35 1026 15 4365 45 235 80 null

625 3025 1026 4365 235


(After Insertion)

Algorithm: (Insert At Position( ) )

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

625 3025 1026 4365 235


(Before Deletion)
First
35 1026 15 4365 45 235 80 null

3025 1026 4365 235


(After Deletion)
SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 19
Algorithm: ( Delete At First( ) )

Step-1 Start
Step-2: Temp=First
Step-3: First=First->link
Step-4: free(Temp)
Step-5: Stop

Delete At Last():

First Prev Last


25 3025 35 1026 15 4365 45 235 80 null

625 3025 1026 4365 235


(Before Deletion)
First Last
25 3025 35 1026 15 4365 45 null

625 3025 1026 4365


(After Deletion)

Algorithm: ( 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 Prev Present (deleted node)


25 3025 35 1026 15 4365 45 235 80 null

625 3025 1026 4365 235


(Before Deletion)

First
25 3025 35 4365 45 235 80 null

625 3025 1026 4365 235


(After Deletion)
SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 20
Algorithm: ( Delete At Position( ) )

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:

Left Data Right

Double Linked List with 3 nodes:

Null 10 850 350 20 450 850 30 Null

350 850 450

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]

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 21


Temp->Left = NULL
Temp->data = d;
Temp->Right=NULL
Step-6: if(First == NULL)
First = Temp
Last=Temp
Else
Last->Right=Temp
Temp->Left=Last
Last = Temp
Step-7: Read data value as d
Step-8: Stop

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

Null 10 850 350 20 450 850 30 Null

350 850 450


(After Insertion)
Algorithm: Insert At First()
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: Temp-Right = First
First->Left = Temp
First = Temp
Step-6: Stop

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 22


Insert At Last()
Last
First Temp

Null 10 850 350 20 null


null 30 Null
350 850
450
(Before Insertion)
First

Null 10 850 350 20 450 850 30 Null

350 850 450

(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

Null 10 450 350 30 Null


Temp
350
null 20 null 450

850
(Before Insertion)
First

Null 10 850 350 20 450 850 30 Null

350 850 450


(After Insertion)

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 23


Algorithm: Insert At Position()

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

Null 10 850 350 20 450 850 30 Null

350 850 450


(Before Deletion)

First

Null 20 450 850 30 Null

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

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 24


Delete At Last()
Last
First

Null 10 850 350 20 450 850 30 Null

350 850 450


(Before Deletion)

Last
First

Null 10 850 350 20 null

350 850
(After Deletion)

Algorithm: Delete At Last()

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

Null 10 850 350 20 450 850 30 Null

350 850 450


(Before Deletion)

First

Null 10 450 350 30 Null

350 450
(After Deletion)

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 25


Algorithm: Delete At Position()

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.

25 3025 35 1026 15 4365 45 235 80 625

625 3025 1026 4365 235

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.

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 26


First

450 10 850 350 20 450 850 30 350

350 850 450

Insert At First()

Temp

25 null
Last
First
625
35 1026 15 4365 45 235 80 3025

3025 1026 4365 235


(Before Insertion)

First Last

25 3025 35 1026 15 4365 45 235 80 625

625 3025 1026 4365 235


(After Insertion)

Algorithm: Insert At First()

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

625 3025 1026 4365


80 null
(Before Insertion)
235
SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 27
First Last
25 3025 35 1026 15 4365 45 235 80 625

625 3025 1026 4365 235


(After Insertion)
Algorithm: Insert At 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: [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()

First Present Last


25 3025 35 4365 45 235 80 625

625 3025 15 null 4365 235

1026
Temp
(Before Insertion)

First Last

25 3025 35 1026 15 4365 45 235 80 625

625 3025 1026 4365 235


(After Insertion)

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 28


Algorithm: Insert At Position()

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

625 3025 1026 4365 235


(Before Deletion)

First Last
35 1026 15 4365 45 235 80 3025

3025 1026 4365 235


(After Deletion)
Algorithm: Delete At First()

Step-1: Start
Step-2: Temp=First
Step-3: First=First->link
Step-4:Last->link=first
Step-5: free(Temp)
Step-6: Stop

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 29


Delete At Last()

First Prev Last


25 3025 35 1026 15 4365 45 235 80 625

625 3025 1026 4365 235


(Before Deletion)

First Last
25 3025 35 1026 15 4365 45 625

625 3025 1026 4365


(After Deletion)

Algorithm: Delete At Last()

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 Prev Present (deleted node) Last


25 3025 35 1026 15 4365 45 235 80 625

625 3025 1026 4365 235


(Before Deletion)

First
25 3025 35 4365 45 235 80 625

625 3025 1026 4365 235


(After Deletion)

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 30


Algorithm: Delete At Position()

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

Q13. What are the Applications of Linked List:

 Linked Lists can be used to implement Stacks , Queues.


 Linked Lists can also be used to implement Graphs. (Adjacency list representation
of Graph).
 Implementing Hash Tables:- Each Bucket of the hash table can itself be a linked
list. (Open chain hashing).
 Undo functionality in Photoshop or Word . Linked list of states.
 A polynomial can be represented in an array or in a linked list by simply storing
the coefficient and exponent of each term.
 However, for any polynomial operation , such as addition or multiplication of
polynomials , linked list representation is more easier to deal with.
 Linked lists are useful for dynamic memory allocation.
 The real life application where the circular linked list is used is our Personal
Computers, where multiple applications are running.
 All the running applications are kept in a circular linked list and the OS gives a
fixed time slot to all for running. The Operating System keeps on iterating over
the linked list until all the applications are completed.

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.

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 31


Q15. Write about Advantages of Double Linked List

 A doubly-linked list is more efficient than a single-linked list as it can be


traversed both ways.
 Insertions and deletions can be done easily from both ends.
 The preceding and succeeding elements can be determined quickly
 We need not point the pointer to the previous element of the m th element to
represent position of m.
 With the use of doubly-linked list, heap can be managed more efficiently.
 It is easier to reverse a list
 It can be used for antiemetic operations on large integer numbers which cannot he
accommodated in one world of memory
 It helps in dynamic memory management algorithms.

Q16. What are the Differences between Linked List and Array?

ARRAY LINKED LIST

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 linked list, elements can be stored at any


In array, elements are stored in consecutive manner
available place as address of nodes stored in
in memory.
previous node.

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.

SVKP & Dr. K.S. RAJU A&S COLLEGE, PENUGONDA Page 32

You might also like