DS Unit 1
DS Unit 1
DS Unit 1
tu
re
fe
ren
Unit-1
ce
.c
om
Q.1 What is data type?
Ans. A Data type refers to a named group of data which share similar properties or characteristics
and which have common behavior among them. Three fundamental data types used in C
programming are int for integer values, float for floating-point numbers and char for character
values.
or
om
Data structure is representation of the logical relationship existing between individual elements
of data. In other words, a Data structure is a way of organizing all data items that considers not
only the elements stored but also their relationship to each other.
.c
or
Data structures are the building blocks of a program. And hence the selection of particular data
structure stresses on the following two things :
ce
1. The data structures must be rich enough in structure to reflect the relationship existing
between the data.
en
2. And the structure should be simple so that we can process data effectively whenever
required.
instructions written to carry out certain tasks and the data structure is the way of organizing the
data with their logical relationship maintained.
Ans. Data structures are normally divided into two broad categories.
tu
The data appearing in our data structures are processed by means of certain operations. In
fact, the particular data structure that one chooses for a given situation depends largely on
the frequency with which specific operations are performed.
om
Ans.
.c
ce
r en
fe
re
Ans. The basic operations that are performed on data structures are as follows :
tu
1. Traversing : Accessing each record exactly once so that certain items in the record may be
processed. (This accessing and processing is sometimes called “visiting” the record).
ak
2. Searching : Searching operation finds the presence of the desired data item in the list of
data item. It may also find the locations of all elements that satisfy certain conditions.
3. Inserting : Inserting means addition of a new data element in a data structure.
The following two operations, which are used in special situations, will also be considered :
(1) Sorting : Sorting is the process of arranging all data items in a data structure in a
particular order say for example, either in ascending order or in descending order.
(2) Merging : Combining the records of two different sorted files into a single sorted file.
Q.9 State the difference between array and link list .
Ans.
om
.c
Q.10 Explain time and space complexity?
ce
Ans. Complexity of algorithm is a function of size of input of a given problem instance which
determines how much running time/memory space is needed by the algorithm in order to run to
completion.
en
Time Complexity: Time complexity of an algorithm is the amount of time it needs in order to run
to completion.
Space Complexity: Space Complexity of an algorithm is the amount of space it needs in order to
r
run to completion.
There are two points which we should consider about computer programming:-
fe
It provides possibly asymptotically tight upper bound for f(n) and it does not give best case
complexity but can give worst case complexity.
ak
Let f be a nonnegative function. Then we define the three most common asymptotic bounds as
follows.
We say that f(n) is Big-O of g(n), written as f(n) = O(g(n)), iff there are positive constants c and
n0 such that
om
Ans. Big-Omega Notation (Ω):
It provides possibly asymptotically tight lower bound for f(n) and it does not give worst case
.c
complexity but can give best case complexity
f(n) is said to be Big-Omega of g(n), written as f(n) = Ω(g(n)), iff there are positive constants c
and n0 such that
ce
0 ≤ c g(n) ≤ f(n) for all n ≥ n0
Ans. We say that f(n) is Big-Theta of g(n), written as f(n) = Θ(g(n)), iff there are positive
constants c1, c2 and n0 such that
Equivalently, f(n) = Θ(g(n)) if and only if f(n) = O(g(n)) and f(n) = Ω(g(n)). If f(n) = Θ(g(n)), we
say that g(n) is a tight bound on f(n).
om
Q.15 What is an array ?
Ans. 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
.c
all characters. Declaration of arrays is as follows :
INT A[10];
ce
where int specifies the data type or type of elements array stores. “A” is the name of the 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.
en
Some important concepts of arrays are :
(1) The individual element of an array can be accessed by specifying name of the array,
followed by index or subscript (which is an integer number specifying the location of
r
element in the array) inside square brackets. For example to access the fifth element of array
a, we have to give the following statement :
fe
A[4];
(2) The first element of the array has index zero [0]. It means the first element and last element
of the above array will be specified as :
A[0], and A[9] respectively.
re
(3) The elements of array will always be stored in consecutive memory locations.
tu
(4) The number of elements that can be stored in an array i.e., the size of array or its length is
given by the following equation :
(upperbound – lowerbound) + 1
ak
For the above array, it would be (9-0) + 1 = 10. Where 0 is the lower bound of array, and 9
is the upper bound of array.
(5) Arrays can always be read or written through loop. If we read a one-dimensional array, it
requires one loop for reading and other for writing (printing) the array. For example :
If we are reading or writing two-dimensional array it would require two loops. And similarly the
array of n dimension would required n loops.
om
1. Creation of an array.
2. Traversing an array (accessing array elements).
3. Insertion of new elements.
.c
4. Deletion of required element.
5. Modification of an element.
6. Merging of arrays.
Ans. ce
en
A linked list is a linear collection of data elements, called node pointing to the next nodes
by means of pointers.
Each node is divided into two parts : the first part containing the information of the
r
element, and the second part called the link or next pointer containing the address of the
next node in the list.
fe
Technically, each such element is referred to as a node, therefore a list can be defined as a
collection of nodes as shown in Fig. below :
Eg. A common model of a stack is plates in a marriage party. Fresh plates are “pushed” onto the
top and “popped” off the top.
Ans. Queue is a linear data structure that permits insertion of an element at one end and deletion
of an element at the other end.
om
The end at which the deletion of an element take place is called front, and the end at
which insertion of a new element can take place is called rear.
The deletion or insertion of elements can take place only at the front and rear end of the
list respectively.
.c
The first element that gets added into the queue is the first one to get removed from the
list.
Queue is also referred to as First-In-First-Out (FIFO) list. .
ce
Fig. shows the pictorial representation of a Queue.
en
10 20 30 40 50 60 70 80
r
Front Rear
fe
re
In Fig, 10 is the first element and 80 is the last element added to the Queue. Similarly, 10 would
be the first element to get removed and 80 would be the last element to get removed.
Ans. A Tree can be defined as a finite set of data items (nodes). Tree is a non-linear type of data
structure in which data items are arranged of stored in a sorted sequence. Trees represent the
hierarchical relationship between various elements. In trees :
ak
1. There is a special data item at the top of hierarchy called the Root of the Tree.
2. The remaining data items are partitioned into number of mutually exclusive (i.e. disjoint)
subsets, each of which is itself, a tree, which is called the subtree.
3. 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, which relate the information. A tree is shown
in Fig.
om
.c
Q.20 Define array and the type of array ?
Ans. “An array is a collection of variables of the same type that are referenced by a common
name.”
ce
Arrays are a way to group a number of items into a larger unit. In C, array elements are stored in
contiguous (consecutive) memory locations. The lowest address corresponds to the first element
and the highest address to the last element. Arrays can have data items of simple types like int or
en
float or even of user-defined types like structure or objects.
Types of Arrays
r
Arrays are of different types:
fe
allows arrays of more than two dimensions. The exact limit (of dimensions), if any, is
determined by the compiler you use.
tu
data_type array-name[size];
where data_type declares the base type of array, which is the type of each element in the array.
The array-name specifies the name with which the array will be referenced and size defines how
many elements the array will hold. The size must be an integer value or integer constant without
any sign
For e.g.
int marks[10];
The above statement declared array marks with 10 elements, marks[0] to marks[9].
Initialization of array :
data_type array-name[size]={element-1,element-2,……..,element-n};
or
om
data_type array-name[ ]={element-1,element-2,……..,element-n};
For example,
.c
int marks[5]={50,25,72,45,30};
Marks[0]=50;
Marks[1]=25;
Marks[2]=72;
Marks[3]=45;
Marks[4]=30; ce
en
Or int marks[ ]={50,25,72,45,30};
For example, to assign a value to second location of array, we give the following statement
tu
marks[1]=90;
2. For reading the value of fourth element in array_name marks, we give the following
statement :
ak
scanf(“%d”,&marks[3]);
3. For writing the value of second element in array_name marks, we give the following
statement :
printf(“%d\t”,marks[1]);
Arrays can always be read or written through loop. If we read a one-dimensional array, it requires
one loop for reading and other for writing (printing) the array. For example :
om
Implementation of one-dimensional array in memory :
.c
Address of element a[k] = B+ W * k
where B is the base address of the array, W is the size of each element in array, and k is the
number of required element in the array (index of element) which should be a integer quantity.
ce
For example :
Let the base address of first element of the array is 2000 (i.e. base address B is = 2000), and each
element of the array occupies four bytes in the memory, then address of fifth element of a one-
dimensional array a[4] will be given as :
en
Address of element a[4] = 2000 + 4 * 4
= 2000 + 16
=2016
r
#include<stdio.h>
#include<conio.h>
void main( )
ak
{
clrscr( );
//Array declaration and initialization
int marks[ ]={50,60,70,80,90},i;
//Array output
printf("\nMarks of 5 students are : \n");
for(i=0;i<5;i++)
printf("%d\t",marks[i]);
getch( );
}
OUTPUT :
#include<stdio.h>
#include<conio.h>
void main( )
{
om
clrscr( );
//Array declaration
int marks[5],i;
//Array input
.c
printf("Enter marks of 5 students : \n");
for(i=0;i<5;i++)
scanf("%d",&marks[i]);
// Array output
printf("\nMarks of 5 students are : \n");
for(i=0;i<5;i++)
printf("%d\t",marks[i]); ce
en
getch( );
}
OUTPUT :
r
Enter marks of 5 students :
50 60 70 80 90
fe
#include<stdio.h>
#include<conio.h>
tu
void main( )
{
clrscr( );
int A[5],i;
ak
float sum=0,avg;
printf("Enter 5 elements of an array : \n");
for(i=0;i<5;i++)
scanf("%d",&A[i]);
for(i=0;i<5;i++)
sum=sum+A[i];
avg=sum/5;
printf("\nSum : %.2f",sum);
printf("\nAverage : %.2f",avg);
getch( );
}
OUTPUT :
2 4 6 8 10
Sum : 30.00
Average : 6.00
om
#include<stdio.h>
#include<conio.h>
void main( )
.c
{
clrscr( );
int A[5],i,max,min;
printf("Enter 5 elements of an array : \n");
for(i=0;i<5;i++)
scanf("%d",&A[i]);
max=min=A[0]; ce
en
for(i=0;i<5;i++)
{
if(max<A[i])
max=A[i];
r
if(min>A[i])
min=A[i];
fe
}
printf("\nLargest element in array : %d",max);
printf("\nSmallest element in array : %d",min);
re
getch( );
}
tu
OUTPUT :
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
int len;
printf("Enter size of array (max. 10) : ");
scanf("%d",&len);
int A[10],num,i,pos;
printf("\nEnter %d elements of array : \n",len);
for(i=0;i<len;i++)
scanf("%d",&A[i]);
printf("\nOriginal array is : \n");
for(i=0;i<len;i++)
printf("%d\t",A[i]);
printf("\n\nEnter the element to be inserted : ");
scanf("%d",&num);
printf("Enter the position of insertion : ");
om
scanf("%d",&pos);
pos--;
for(i=len-1;i>=pos;i--) // Shifts down one position
A[i+1]=A[i];
.c
A[pos]=num;
if(pos>len)
printf("\nInsertion outside the array");
else
{
printf("\nNew array after insertion : \n");
len++; ce
en
for(i=0;i<len;i++)
printf("%d\t",A[i]);
}
getch( );
r
}
fe
OUTPUT :
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
int A[10],i,len,num,f=0;
printf("Enter the size of array (max. 10) : ");
scanf("%d",&len);
printf("\nEnter %d elements of an array : \n",len);
for(i=0;i<len;i++)
scanf("%d",&A[i]);
printf("\nOriginal array is : \n");
for(i=0;i<len;i++)
printf("%d\t",A[i]);
printf("\n\nEnter the element to delete : ");
scanf("%d",&num);
for(i=0;i<len;i++)
{
if(num==A[i])
{
om
f=1;
for(;i<len-1;i++)
A[i]=A[i+1];
len--;
.c
break;
}
}
if(f==0)
printf("\nNumber not found in array");
else
{ ce
en
printf("\nNew Array after deletion : \n");
for(i=0;i<len;i++)
printf("%d\t",A[i]);
}
r
getch( );
}
fe
OUTPUT :
Enter the size of array (max. 10) : 5
re
Ans
A double dimensional array is an array in which each element is itself an array. For
example, an array A[R][C] is an R by C table with R rows and C columns containing R *
C elements.
While storing the elements of a 2-D array in memory, these are allocated contiguous memory
locations. A two-dimensional array can be implemented in a programming language in two ways :
1. Row-major implementation
2. Column-major implementation
1. Row-major implementation :
om
stored and so on. For example, an array A [3] [3] is stored in the memory as shown in Fig.(1)
below :
.c
A A A A A A A A A
00 01 02 10 11 12 20 21 22
a01 a02 ce
The storage can be clearly understood by arranging array as matrix as shown below :
a00 Row1
en
a = a10 a11 a12 Row2
a20 a21 a22 Row3
r
Column-major implementation :
fe
second column is stored and so on. For example, an array a[3][3] is stored in the memory as
shown in Fig.(1) below :
tu
The storage can be clearly understood by arranging array as matrix as shown below :
datatype arrayvariablename[rowsize][col.size];
For e.g.
int A[4][3];
om
datatype arrayname[rowsize][col. size]={{1st row elements},{2nd row elements},………};
For e.g.
.c
int A[4][3]={{1,2,3}, {4,5,6,}, {7,8,9}, {10,11,12}};
for(r=0;r<4;r++) ce
en
for(c=0;c<3;c++)
scanf("%d",&A[r][c]);
r
Column major form
fe
for(c=0;c<3;c++)
for(r=0;r<4;r++)
scanf("%d",&A[r][c]);
re
Array output :
for(r=0;r<4;r++)
tu
{
for(c=0;c<3;c++)
printf("%d\t",A[r][c]);
printf("\n");
ak
}
Q.24 Write down the program for double dimension array.
Program 1 : Double Dimensional array (Matrix) Initialization & Output
Program 2 : Double Dimensional array(Matrix) input & Output
Program 3 : Addition of Two 3 * 3 Matrices
Program 4 : WAP to display the transpose of a 3 * 3 matrix
Program 5 : WAP to multiply two 3 * 3 matrices
Ans.
Program 1 : Double Dimensional array (Matrix) Initialization & Output
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
// Double Dim. Array declaration and initialization int
A[4][3]={{1,2,3},{4,5,6},{7,8,9},{10,11,12}}; int r,c;
// Double Dim. Array Output
printf("\nGiven 4 * 3 matrix is : \n");
for(r=0;r<4;r++)
om
{
for(c=0;c<3;c++)
printf("%d\t",A[r][c]);
printf("\n");
.c
}
getch( );
}
OUTPUT :
Given 4 * 3 matrix is :
ce
en
1 2 3
4 5 6
7 8 9
10 11 12
r
fe
#include<stdio.h>
#include<conio.h>
re
void main( )
{
clrscr( );
// Matrix declaration
tu
int A[4][3],r,c;
// Matrix input
printf("\nEnter elements of a 4 * 3 matrix : \n");
for(r=0;r<4;r++)
ak
for(c=0;c<3;c++)
scanf("%d",&A[r][c]);
// Matrix output
printf("\nGiven 4 * 3 matrix is : \n");
for(r=0;r<4;r++)
{
for(c=0;c<3;c++)
printf("%d\t",A[r][c]);
printf("\n");
}
getch( );
}
OUTPUT :
Enter elements of a 4 * 3 matrix :
1 2 3
4 5 6
7 8 9
10 11 12
om
Given 4 * 3 matrix is :
1 2 3
4 5 6
.c
7 8 9
10 11 12
ce
r en
fe
re
tu
ak
Program 3 : Addition of Two 3 * 3 Matrices
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
// Matrix declaration
int A[3][3],B[3][3],C[3][3],r,c;
// Matrix input
printf("\nEnter elements of first 3 * 3 matrix : \n");
om
for(r=0;r<3;r++)
for(c=0;c<3;c++)
scanf("%d",&A[r][c]);
printf("\nEnter elements of second 3 * 3 matrix : \n");
.c
for(r=0;r<3;r++)
for(c=0;c<3;c++)
scanf("%d",&B[r][c]);
// Matrix addition
ce
printf("\nAddition of first two matrices : \n");
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
en
{
C[r][c]=A[r][c]+B[r][c];
printf("%d\t",C[r][c]);
r
}
printf("\n");
fe
}
getch( );
}
OUTPUT :
re
1 2 3
4 5 6
7 8 9
ak
2 3 4
5 6 7
8 9 10
3 5 7
9 11 13
15 17 19
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
int A[3][3],r,c;
printf("\nEnter elements of a 3 * 3 matrix :\n");
om
for(r=0;r<3;r++)
for(c=0;c<3;c++)
scanf("%d",&A[r][c]);
printf("\nOriginal matrix is :\n");
.c
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
printf("%d\t",A[r][c]);
ce
printf("\n");
}
printf("\nTranspose of given matrix is :\n");
en
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
printf("%d\t",A[c][r]);
r
printf("\n");
}
fe
getch( );
}
OUTPUT :
re
1 2 3
4 5 6
7 8 9
Original matrix is :
ak
1 2 3
4 5 6
7 8 9
1 4 7
2 5 8
3 6 9
Program 5 : WAP to multiply two 3 * 3 matrices
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
int A[3][3],B[3][3],C[3][3],r,c,k;
om
printf("Enter elements of first 3 * 3 matrix : \n");
for(r=0;r<3;r++)
for(c=0;c<3;c++)
scanf("%d",&A[r][c]);
.c
printf("\nEnter elements of second 3 * 3 matrix : \n");
for(r=0;r<3;r++)
for(c=0;c<3;c++)
scanf("%d",&B[r][c]);
ce
printf("\nProduct of first two 3 * 3 matrices :\n");
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
en
{
C[r][c]=0;
for(k=0;k<3;k++)
r
C[r][c]=C[r][c]+(A[r][k]*B[k][c]);
printf("%d\t",C[r][c]);
fe
}
printf("\n");
}
getch( );
re
}
OUTPUT :
tu
1 2 3
4 5 6
ak
7 8 9
1 2 3
4 5 6
7 8 9
datatype arrayname[size-1][size-2]………[size-n];
om
For example :
int A[5][2][3];
.c
float B[2][5][3];
The simplest form of a multidimensional array is a two-dimensional array, which is also known
as array of an array.
through which we can store only the non-zero elements and keep intact the functionality of the matrix
can save a lot of memory space. Fig. (3) shows sparse matrix 5 * 6 with 5 non zero elements.
re
0 0 0 6 0 0
0 0 3 0 0 0
0 8 0 0 2 0
tu
0 0 0 0 0 0
0 0 0 0 9 0
ak
The concept of Dynamic or Run-time memory allocation helps us to overcome this problem in arrays,
as well as allows us to be able to get the required portion of memory at run-time (or we say as the need
om
arises). This is best suited type of allocation where we do not know the memory requirement in advance,
which is the case with most of real-life problems. In other words, dynamic memory allocation gives
flexibility for programmer. As well as it makes efficient use of memory by allocating the required
amount of memory whenever needed, unlike static allocation where we declare the amount of memory
.c
to be allocated statically.
MALLOC ( ) FUNCTION
malloc (number of elements * size of each element);
ce
For example,
int *ptr;
en
ptr= malloc (10 * size of each element);
FREE ( ) FUNCTION
The free ( ) function is used to de-allocate the previously allocated memory using malloc ()
r
free (ptr_var);
Ans.
The simplest one of data structures i.e. an array can be used only when their numbers of elements
along with elements sizes are predetermined, since the memory is reserved before processing.
tu
For this reason, arrays are called static data structures. Now, consider a situation where exact
numbers of elements are not known. In such a case, estimates might go wrong. Also during
processing, i.e., either more memory is required or extra free spaces lie in the array. Another
problem associated with arrays is complexity involved in insertions and deletions of elements.
ak
Linked lists(Dynamic data structures) overcome the drawbacks of arrays as in linked lists number
of elements need not be predetermined, more memory can be allocated or released during the
processing as and when required, making insertions and deletions much easier and simpler.
Dynamic memory allocation technique facilitates allocation of memory during the program
execution itself using malloc( ) function as and when required. Dynamic memory allocation also
facilitates release of memory using free( ) function, if memory is not required any more. Data
structures like linked lists and trees use this technique for their memory allocation
Q.29 Explain link list in detail.
Ans.
“A linked list is a linear collection of data elements, called node pointing to the next nodes by
means of pointers.”
Each node is divided into two parts: the first part containing the information of the element, and
om
the second part called the link or next pointer containing the address of the next node in the list.
Start
1000
.c
Info Link Info Link Info Link
ce
10 2000 20 3000 30 0
Insertion: This operation is used to insert a new node in the linked list at the specified position. A new
node may be inserted :
Deletion:This operation is used to delete a node from the linked list. A node may be deleted from
Beginning of the link list
End of the link list
At the specific position of the link list
Traversing:It is a process of going through all the nodes of a linked list from one end to the other end.
It we start traversing from the very first node towards the last node, it is called forward
traversing.
It we start traversing from the very last node towards the first node, it is called reverse traversing
Searching:If the desired element is found, we signal operation “SUCCESSFULL”. Otherwise, we signal
it as “UNSUCCESSFULL”.
Concatenation:It is a process of appending (joining) the second list to the end of the first list consisting
of m nodes. When we concatenate two lists, the second list has n nodes, then the concatenated list will
be having (m + n) nodes. The last node of the first linked list is modified so that it is now pointing to the
first node in the second list.
Display:This operation is used to print each and every node’s information. We access each node from
om
the beginning (or the specified position) of the list and output the data stored there.
.c
Ans.
TYPES OF LINKED LISTS
Basically, we can put linked lists into the following four types:
A singly linked list is one in which all nodes are linked together in some sequential manner. Hence, it is
fe
also called linear linked list. Clearly, it has the beginning and the end. The problem with this list is that
we cannot access the predecessor node (previous node) from the current node. This can be overcome by
doubly linked lists. A linear linked list is shown in Fig. (1).
re
tu
ak
om
.c
Circular linked list :A circular linked list is one which has no beginning and no end. A singly linked
list can be made circular linked list by simply storing the address of the very first node in the link field of
ce
the last node. A circular linked list is shown in Fig. (3).
en
r
fe
re
tu
Circular doubly linked list: A circular doubly linked list is one which has both the successor pointer
and predecessor pointer in circular manner. A Circular doubly linked list is shown in Fig. (4).
ak
ak
tu
re
fe
ren
ce
.c
om
Q.31 Write down the algorithm for the following:
i) Algorithm to insert node at the beginning of the singly linked list
ii) Algorithm to insert node at the end of the singly linked list
iii) Algorithm to insert node at a specific location in the singly linked list
iv) Algorithm to delete node from the beginning of the singly linked list
v) Algorithm to delete node from the end of the singly linked list
vi) Algorithm to delete node from a specific location in the singly linked list
Ans.
om
INSERTION
Let PTR is the structure pointer which allocates memory for the new node at the beginning of the
.c
singly linked list & NUM is the element to be inserted into the linked list, INFO represents the
information part of the new node and LINK represents the link or next pointer of the new node
pointing to the address of next node. START represents the address of first node. Initially, before
ce
inserting first node in the singly linked list, START=NULL
(I) Algorithm to insert node at the beginning of the singly linked list
en
Step 1 : Allocate memory for the new node using PTR.
Step 2 : Read NUM to be inserted into singly linked list.
Step 3 : Set PTR->INFO = NUM
Step 4 : Set PTR->LINK=START;
r
Step 5 : Set START:=PTR
Step 6 : Exit
fe
(II) Algorithm to insert node at the end of the singly linked list
re
Step 1 : Read the location LOC where you want to insert the node.
Step 2 : If LOC > 1 : then
Set TEMP := START.
Repeat loop for I = 1 to LOC – 1
om
If TEMP := NULL : then
Set F :=1 and Exit from loop.
[End of If Statement]
[End of loop]
.c
If (F=1 OR (START=NULL AND LOC>1)) : then
Write : “Total nodes in list are lesser than this position.”
Write : “So Node cannot be inserted.” and return.
ce
[End of If structure]
[End of Step 2 If Structure]
Step 3 : Allocate memory for the new node using PTR.
Step 4 : Read NUM to be inserted into singly linked list.
Step 5 : Set PTR->INFO = NUM
en
Step 6 : If LOC :=1 : then
Set PTR->LINK := START.
Set START := PTR.
r
Else
Set PTR->LINK := TEMP->LINK.
fe
TEMP->LINK := PTR.
[End of If Else Structure].
Step 7 : Exit
re
(B) DELETION
Let PTR is the structure pointer which deallocates memory of the node at the beginning of the
tu
singly linked list & NUM is the element to be deleted from the linked list, INFO represents the
information part of the deleted node and LINK represents the link or next pointer of the deleted
node pointing to the address of next node. START represents the address of first node
ak
(IV) Algorithm to delete node from the beginning of the singly linked list
(V) Algorithm to delete node from the end of the singly linked list
om
Set PTR := START.
Set START := NULL.
Else
.c
Set TEMP := START.
Set PTR := PTR->LINK.
Repeat loop while PTR->LINK!=NULL
Set TEMP := PTR.
ce
Set PTR := PTR->LINK.
[End of loop]
Set TEMP->LINK := NULL.
[End of Step 2 If Else Structure]
Step 3 : Set NUM := PTR->INFO.
en
Step 4 : Write : “Deleted element from the end of the singly linked list : “,NUM.
Step 5 : Deallocate memory of the node at the end of singly linked list.
r
Step 6 : Exit
fe
(vi) Algorithm to delete node from a specific location in the singly linked list
START := PTR->LINK
Else
Repeat loop for I = 1 to LOC.
Set TEMP := PTR.
ak
om
.c
ce
r en
fe
re
tu
ak