Sei sulla pagina 1di 47

DATASTRUCTURE

## DATRUCTURE IS THE WAY OF

ORGANIZING ALL DATA ITEMS IN
ORDER THAT NOT ONLY ELEMENTS TO
BE STORED BUT ALSO THE RELATION
BETWEEN THE ELEMENTS
DATASTRUCTURE

LINEAR NONLINEAR

ARRAY
TREES&
QUEUE
GRAPH
What are the problems with Arrays
- Size is fixed
-Array Items are stored contiguously
-Insertions and deletion at particular position is
complex
-Size is not fixed
-Data can be stored at any place
-Insertions and deletions are simple and faster
Dynamic Memory Allocation : Linked list are dynamic
data structure i.e. they can grow or shrink during the
execution of a program
Efficient memory utilization: In linked list (or
dynamic) representation, memory is not pre-allocated
i.e. Memory is allocated whenever it is required and it is
deallocated (or removed) when it is not needed
Insertion and deletion are easier and efficient:
Linked list provides flexibility in inserting a data item at
a specified position and deletion of a data item from the
given position
Definition: The linked list is a linear data
structure consists of nodes which is created
dynamically.
Each node of the linked list is having two parts,
one is info part and another is link part.
info: It contains information or data element.
link: It is a pointer variable which holds the
In the linked list there exists a special pointer
called as start, which holds the address of the
What does a Linked List look like ?
A linked list is a collection of nodes with various fields

Pointer to the
first node

Info field
Fig : Node

## Fig : Linked List representation in Memory

Suppose we want to store a list of integer numbers using
linked list. Then it can be schematically represented as

1. More memory: to store an integer number, a node
with integer data and address field is allocated That
is more memory space is needed
cumbersome and also time consuming
Memory Requirement : Array Vs Linked List
What is the amount of memory required to store
five floating point numbers in an (i)array

## If we want to store 5 floating point numbers

in a linked list, then the total number of bytes
required is 5 6 + 2 = 32 bytes Whereas we
store the same in an array, it will require 5 x 4
= 20 bytes which is less than 32 bytes

## For representing the above linked list LIST in memory, two

linear arrays are required such that
INFO INFO[K] contains the information part of a node
of LIST
of LIST
Node declaration in a Link List with one information part
struct node
{
int info; 2 Bytes
};
typedef struct node *NODE;
Meaning of this: A node is of the type struct and
contains info field and link field
Node declaration in a Link List with more than one
information part
A node may have more than one information part, for example:

struct node
{ int age;
char name[20];
float sal;
Graphical Representation : Singly Linked List

First

10 1000 15 20 NULL
2000

## 4000 1000 2000

Graphical Representation : Circular Singly
First

## Last node contains the address of the

first node
Graphical Representation : Doubly Linked List
First

## Contains the address of previous node

and next node
Graphical Representation : Circular Doubly
First

## Contains the address of first node and

last node
The basic operations on linked list are as follows:
Creation: Creation of a linked list
Insertion:
At the beginning of a linked list
At the end of a linked list
At the specified position
Deletion:
Specified position in the list
Traversing: Visiting each node exactly
once, it may be for displaying, counting,
searching
Concatenation: It is the processing of
appending (joining) the second list to the
end of the first list consisting of m nodes
When we concatenate 2-lists, the 2nd list
having n nodes, then the concatenated list
will have (m + n) nodes
Reverse: Accessing all elements in a
linked list in reversed order i.e. Last
element will be accessed 1st and 1st element
will be accessed at the end
Searching: Finding weather a given item is
found in the given list
Sorting: Arranging all the elements in the
linked list in a required sequence i.e. in a
ascending or descending order

## start: It is a pointer pointing to the 1st node in the

list
curr: It is a pointer which keeps the address of
newly created node
temp: It is a pointer always pointing to last node
Dynamic Allocation of Memory
malloc ( ):
malloc() allocates single block of memory
The name malloc stands for "memory allocation The
function malloc( ) reserves a block of memory of specified size
and return a pointer of specified form
By default garbage value
Syntax:
ptr = (cast-type *) malloc (byte-size);
Example:
ptr = (int *) malloc (100*sizeof (int) );
curr = (struct node *) malloc(sizeof(struct node));
Dynamic Allocation of Memory
calloc ( ):
The name calloc stands for "contiguous allocation"
calloc() allocates multiple blocks of memory each of same size
By default the allocated memory will be initialized to zero
Syntax:
ptr = (cast-type*) calloc (n, element-size);
Example:
ptr = (float*) calloc (25, sizeof (float) );
curr = (struct node *) calloc (10, sizeof (struct node) );

## The above picture denotes a situation where the first node is

created for an empty link list
That means before creation of this node the linked list was
empty i.e. start = curr = temp = NULL
struct node
{ int info; 2 Bytes
struct node *link; 2 Bytes };
typedef struct node *NODE;

## Allocation of memory to newnode

newnode =( NODE ) malloc (sizeof(struct node));
It returns address of the location with piece
of memory of the size struct i.e. for information

Here the new node created is appended to the linked list i.e.
the new node is added to the end of the linked list
Inserting a new node to the end
info: information part of a node
item: item to be stored in the info part of
node
start: It is a pointer pointing to the 1st node
temp: pointer pointing to the last node
curr: It is a pointer which keeps the address
of newly created node
Step 1: create a new node and store its address in curr
Step 2: [check for overflow]
if ( curr = = NULL)
print Overflow Step 6: if you wish to continue
goto step 1
exit
Step 3: a) curr info = item Step 7: exit
b ) curr link = NULL
Step 4: if (start = = NULL)
start = curr
temp = curr
else
temp = curr
Inserting a node as a 1st node
Inserting a node as a 1st node in the linked list
follows the steps:
Creating a node.
Checking for overflow.
Putting the information in the information
part of the newly created node.
curr info = item;
or scanf (%d, &curr info);
Algorithm : insert_node as a first node (list, link, info, curr, item)
info: Information part of the node curr: Newly created node.
item: Item to be inserted at 1st position of a linked list.
Step 1: [checking for overflow]
if (curr = = NULL)
print Overflow
goto step 4
[end if ]
Step 2:
curr info = item curr link = NULL
Step 3: curr link = start
start = curr
Step 4: exit
Inserting a node as a last node
insert_lastnode (link, list, info, curr, temp, item)
info: Information part of the node
curr: Newly created node
temp: Temporary pointer variable, which will be
used for traversing and at last will hold address of
the last node
item: Item to be inserted as a last node of the
Step 1: if (curr = = NULL) print overflow
goto step 6
Step 2: curr info = item
Step 3: if(start = = NULL)
start = curr
go to step 6
Step 4: temp = start
while (temp link ! = NULL)
Step 5: temp link = curr
Step 6: exit.
list: The given linked list start: Has address of the 1st node
Step 1: [Check for empty list]
if (start = = NULL)
print List is empty
go to step 4.
Step 2: temp = start
Step 3: while (temp ! = NULL)
print temp info
[end while]
Step 4: exit.
Reversing a Singly Linked Linear List
Physically we cannot traverse a single linked
list in reverse order, but we can display the
elements in reverse order by storing the
values of each node in an array at the time of
forward traversing

## After storing the elements in the array,

display the array in reverse order
Algorithm:reverse_traverse ( list , start, temp , info, link, a)
info: Information part of the node
start: Has address of the 1st node
temp: Pointer variable used for traversing
a: A linear array
Step 1: If (start = = NULL)
print List is empty
goto step 6
Step 2: temp = start and set i = 0
Step 3: for (temp = start; temp ! = NULL; temp = temp link)
a [ i ] = temp info
i++
Step 4: i--
Step 5: while ( i > = 0)
print a [ i ]
i=i1
Step 6: exit.
Sorting is a process in which the data elements of a
list are arranged either in ascending or
descending order sequentially in the node of that
list.
Algorithm: Sort (list, start, link, info, num, temp1, temp
Step 1: set temp1 = start and temp2 = temp1->link
Step 2: while temp1 NULL repeat step (3) to step (5)
Step 3: A. while temp2 NULL repeat step (a) and (b)
Step 4: if temp1 info > temp2 info then :
set num = temp1 info
set temp1 info = temp2 info
temp2 info = num.
B. set temp2 = temp2 link
[End of step 4 while loop]
Step 5: set temp1 = temp 1 link
temp2=temp1 link [End of step2 while loop]
Step 6: Exit.
Concatenation of two Single Linked Lists

start1

start2

## 315 112 415

200
Algorithm : Concatenate (list1, list2, start1, start2, temp, link, info)