Share 'Unit-III Ds - Docx'
Share 'Unit-III Ds - Docx'
------------------------------------------------------------------------------------------------------------------------------------------
UNIT-III
LIST: Introduction, single linked list, representation of a linked list in memory, Operations-insertion,
deletion, display, search, Circular linked list, Double linked list, applications Advantages and
disadvantages of single linked list, arrays, Implementation of stack, queue using linked list.
LINKED LIST
linked list is a linear data structure. In this the elements can be placed anywhere in the heap
memory unlike array which uses contiguous locations.
Linked list, is a linear collection of data items.The linear order is given by means of POINTERS.
These types of lists are often referred to as linear linked list.
* Each item in the list is called a node.
* Each node of the list has two fields:
1.Data - contains the element being stored in the list.
2.Next address- contains the address of the next node in the list.
* The last node in the list contains NULL pointer to indicate that it is the end of the list.
1
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
In fig: the variable START is used to store the address of the first node. In this example
START=1, so the first data is stored at address 1,which is H. The corresponding next stores the
address of next node ,which is 4. So, we look at address 4 to fetch the next data which is E.
We repeat this procedure until we reach a position where the next entry contains -1 i.e;
NULL.
When we traverse data and next in this manner, we finally see that the list in the above
example stores characters that form HELLO. The empty space in the above fig: contains data for
other applications.
The list appears as:
Ans:
A singly linked list, or simply a linked list, is a linear collection of data items. The linear order is
given by means of POINTERS. These types of lists are often referred to as linear linked list.
* Each item in the list is called a node.
* Each node of the list has two fields:
1. Data - contains the element being stored in the list.
2. Next address- contains the address of the next node in the list.
* The last node in the list contains NULL pointer to indicate that it is the end of the list.
Conceptual view of Singly Linked List
3
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
4
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
a). Inserting a node at the beginning of a linked list involves the following steps:
1. Allocate memory for new node
2. Assign data part of newnode to element
3. Set next field of new node to START
4. Change START pointer point to the new node
5
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
6
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
7
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
8
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
}
}
9
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
scanf("%d",&ele);
printf("\nEnter the key element");
scanf("%d",&key);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
keyptr= search(key);
if(keyptr==NULL)
printf("\nKey is not found");
else
{
ptr=start;
while(ptr->next!=keyptr)
ptr=ptr->next;
ptr->next=newnode;
newnode->next=keyptr;
}
}
10
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
11
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
c). Deleting the node after the given node involves the following steps
1. Search for the given key element by using search function and assign key element address to
keyptr
2. Take pointer variable Aptr which points to next field of keyptr
3. Set next field of keyptr to next field of Aptr
4. Deallocate memory for Aptr node from list
12
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
13
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
d). Deleting the node before the given node involves the following steps
1. Search for the given key element by using search function and assign key element
address to keyptr
2. Take pointer variable ptr which points to START
3. Move ptr such that next field of ptr = keyptr and preptr points to ptr(address of before
node of ptr)
4. Set next field of keyptr to next field of ptr
5. Deallocate memory for ptr node from list
14
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
scanf("%d",&key);
keyptr= search(key);
if(keyptr==NULL)
printf("\nKey is not found");
else
{ ptr=start;
while(ptr->next!=keyptr)
{ preptr=ptr;
ptr=ptr->next;
}
preptr->next=ptr->next;
free(ptr);
}
}
}
15
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
{
if(ptr->data==key)
return ptr;
ptr=ptr->next;
}
return NULL;
}
16
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
else
printf("key is found");
case 6: exit(0);
default: printf("invalid option");
}
}
}
void create()
{
struct node *ptr,*new_node;
int ele;
printf("Enter -1 to end the list \nEnter element");
scanf("%d",&ele);
while(ele!=-1)
{ new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=ele;
new_node->next=NULL;
if(start==NULL)
start=new_node;
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=new_node;
}
printf("Enter -1 to end the list \nEnter element");
scanf("%d",&ele);
}
}
void insert_after()
{
struct node *keyptr,*newnode;
int ele,key;
printf("\nEnter nEnter element");
scanf("%d",&ele);
printf("\nEnter the key element");
scanf("%d",&key);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
keyptr= search(key);
if(keyptr==NULL)
printf("\nKey is not found");
17
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
else
{
newnode->next=keyptr->next;
keyptr->next=newnode;
}
}
struct node* search(int key)
{
struct node *ptr;
ptr=start;
while(ptr!=NULL)
{
if(ptr->data==key)
return ptr;
ptr=ptr->next;
}
return NULL;
}
void display()
{
struct node *ptr;
ptr=start;
if(ptr==NULL)
printf("\n list is empty");
else
{
while(ptr!=NULL)
{
printf("%5d->",ptr->data);
ptr=ptr->next;
}
}
}
void delete_after()
{
struct node *keyptr,*Aptr;
int key;
printf("\nEnter the key element");
scanf("%d",&key);
keyptr= search(key);
if(keyptr==NULL)
printf("\nKey is not found");
else
{
Aptr=keyptr->next;
keyptr->next=Aptr->next;
18
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
free(Aptr);
}
}
Ans:
1. Linked list is Dynamic data structure- It is not necessary to know in advance the number
of elements to be stored in the list . At run time we can allocate as much memory as we
can. Though we can allocate any number of nodes in linked list.
2. Linked list can grow and shrink during run time.
3. Insertion and deletion operations are easier.-We can insert any node at any place easily
and similarly we can remove it easily. In insertion operation we have to just update next
link of node.
4. Efficient memory utilization i.e; no need to pre allocate memory.-Memory is allocated at
run time as per requirement, so that linked list data structure provides us strong command
on memory utilization
5. Faster access time, can be expanded in constant time without memory over head.
6. Linear data structures such as stack, queue can be easily implemented using linked list.
DISADVANTAGES OF LINKED LIST
1. Wastage of memory- Pointer requires extra memory for storage
2. No Random Access- In array we can access nth element easily just by using a[n].
In linked list no random access is given to user, we have to access each node sequentially.
3. Time Complexity-Individual nodes are not stored in the contiguous memory locations.
Access time for individual element is O(n) where as array O(1).
4. Reverse Traversing is difficult- It is very difficult to traverse linked list from end.
5. Heap space restriction- If there is insufficient space in heap then it won’t create any
memory.
5. What are the applications of linked list? Explain in detail.
Ans:
Linked list find applications in many areas. Some of the applications are
1. Polynomial Manipulation
2. Sparse Matrix representation
1. Polynomial Manipulation
We can represent the polynomial with the help of Linked List and the structure is as follows:
19
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
struct node
{ int coeff,exp;
struct node * next;
} *start =NULL;
And the graphical representation is as follows:
Polynomial addition:
We can add the Polynomial with the help of following logic
1. Firstly we have to create a linked list ,first is for first polynomial and second is for
second polynomial
2. Then we have to check the exponent of each node to the rest of the linked list and if the
exponent is match then we have to add the coefficient and so on
Polynomial Multiplication
We can multiply the polynomial with the help of following logic
1. Firstly we have to create two linked lists, first is for first polynomial and second is for
second polynomial
2. Then we multiply each element of the first linked list to each element of the second
linked list
3. Then if the exponents are same then we have to call the addition of polynomial function
20
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
It would be very easy to represent the polynomial using a linked list structure, where each node
can hold information pertaining to a single term of the polynomial. Each node will need to store
the variable x, the exponent and the coefficient for each term.
It often does not matter whether the polynomial is in x or y. This information may not be very
crucial for the intended operations on the polynomial. Thus we need to define a node structure
to hold two integers ie exp and coff.
For the same polynomial if we use array representation we have to keep a slot for each exponent
of x , thus if we have a polynomial of order 50 but containing just 6 terms then a large number of
entries will be zero in the array.
21
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
Eg:
A matrix consumes a lot of space in memory, when the matrix is sparse to store a handful of non-
zero elements in memory, to save storage space we use triple or three tuple representation to
represent each non zero element of sparse matrix.
3 Tuple Representation:
1. Elements in first row represent the number of rows, columns and non-zero values in
sparse matrix.
2. Elements in the other rows give formation about the location (row and column) and value
of nonzero elements.
In this representation each non zero element of the matrix is represented using nod estructure
22
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
Here ROW, COLUMN, DATA fields contains row number column number and value of
non zero elements in the matrix.
RIGHT link points to the node holding the next non zero value in the same row of the
matrix
DOWN link points to the node holding the next non zero value in the same column of the
matrix.
Linked list representation ignores representing zeros in the matrix.
23
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
Ans:
A circularly linked list, or simply circular list, is a linked list in which the last node always
points to the first node. This type of list can be build just by replacing the NULL pointer at the
end of the list with a pointer which points to the first node. There is no first or last node in the
circular list.
Advantages:
Any node can be traversed starting from any other node in the list.
There is no need of NULL pointer to signal the end of the list and hence, all pointers
contain valid addresses.
In contrast to singly linked list, deletion operation in circular list is simplified as the
search for the previous node of an element to be deleted can be started from that item
itself.
Conceptual view of circular linked list
24
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
Linked list provides to insert or delete nodes whenever necessary. First we have to create the
nodes. This is possible by using self referential structure, pointers and dynamic memory
allocation functions like malloc. First we have to declare structure and create memory by using
malloc function.
struct node
{
int data;
struct node *next;
};
struct node *newnode,*start=NULL;
newnode=(struct node *) malloc(sizeof(struct node));
The above statement will allocate a block of memory whose size is equal to size of node of type
structure and its address is assigned to pointer variable newnode. This pointer indicates the
beginning of linked list
25
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
}
}
26
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
void display()
{
struct node *ptr;
ptr=start;
if(ptr==NULL)
printf("\n list is empty");
else
{
while(ptr->next!=start)
{
printf("%5d->",ptr->data);
ptr=ptr->next;
}
printf("%5d",ptr->data);
}
}
a). Inserting a node at the beginning of a circular linked list involves the following steps:
1. Allocate memory for new node
2. Assign data part of newnode to element
3. Set next field of new node to START
4. Take pointer ptr which points to START
5. Move ptr to which points to the next field of ptr! =START
6. Change START pointer point to the new node
7. Set next field of ptr to START
27
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
}
b). Inserting a node at the end of a circular linked list involves the following steps:
1. Allocate memory for new node
2. Assign data part of newnode to element
3. Take pointer variable ptr which points to START
4. Move ptr to which points to next field of ptr!=START
5. Set next field of ptr to newnode
6. Set next field of newnode to START
28
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
29
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
30
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
3. Search for the given key element by using search function and assign key element
address to keyptr
4. Take pointer variable ptr which points to START
5. Move ptr which points to before node of keyptr
6. Set next field of ptr point to newnode
7. Set next field of newnode point to keyptr
31
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
ptr->next=newnode;
newnode->next=keyptr;
}
}
{
struct node *ptr,*temp;
ptr=start;
while(ptr->next!=start)
ptr=ptr->next;
ptr->next=start->next;
free(start);
start=ptr->next;
}
b). Deleting the last node involves the following steps
1. Take pointer variables preptr and ptr which points to START
2. Move ptr such that next field of ptr !=START and preptr points to ptr(address of before
node of ptr)
3. Set next field of preptr points to START
4. Deallocate memory for ptr node from list.
33
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
c). Deleting the node after the given node involves the following steps
1. Search for the given key element by using search function and assign key element address to
keyptr
2. Take pointer variable Aptr which points to next field of keyptr
3. Set next field of keyptr to next field of Aptr
4. Deallocate memory for Aptr node from list
34
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
d). Deleting the node before the given node involves the following steps
1. Search for the given key element by using search function and assign key element
address to keyptr
2. Take pointer variable ptr which points to START
3. Move ptr such that next field of ptr = keyptr and preptr points to ptr(address of before
node of ptr)
4. Set next field of keyptr to next field of ptr
5. Deallocate memory for ptr node from list
35
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
}
preptr->next=ptr->next;
free(ptr);
}
}
}
36
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
return ptr;
ptr=ptr->next;
}
return NULL;
}
37
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
case 6: exit(0);
default: printf("invalid option");
}
}
}
void create()
{
struct node *ptr,*newnode;
int ele;
printf("Enter -1 to end the list \nEnter element");
scanf("%d",&ele);
while(ele!=-1)
{
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
if(start==NULL)
{
start=newnode;
newnode->next=start;
}
else
{
ptr=start;
while(ptr->next!=start)
ptr=ptr->next;
ptr->next=newnode;
newnode->next=start;
}
printf("Enter -1 to end the list \nEnter element");
scanf("%d",&ele);
}
}
void insert_after()
{
struct node *keyptr,*newnode;
int ele,key;
printf("\nEnter nEnter element");
scanf("%d",&ele);
printf("\nEnter the key element");
scanf("%d",&key);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
keyptr= search(key);
if(keyptr==NULL)
38
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
39
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
In a singly linked list, each element contains a pointer to the next element. We have seen this
before. In single linked list, traversing is possible only in one direction. Sometimes, we have to
traverse the list in both directions to improve performance of algorithms. To enable this, we
require links in both the directions, that is, the element should have pointers to the right element
as well as to its left element. This type of list is called doubly linked list.
Doubly linked list is defined as a collection of elements, each element consisting of three fields:
Left link of the leftmost element is set to NULL which means that there is no left element to that.
And, right link of the rightmost element is set to NULL which means that there is no right
element to that.
41
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
}
}
42
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
43
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
The process of insertion involves search for the place of insertion .The search involves in
locating a node after which the new node is to be inserted.
a). Inserting a node at the beginning of a doubly linked list involves the following steps:
1. Allocate memory for new node
2. Assign data part of newnode to element
3. Set prev field of newnode to NULL
4. Set next field of new node to START
5. Set prev field of START to newnode
6. Change START pointer point to the new node
44
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
b). Inserting a node at the end of a linked list involves the following steps:
1. Allocate memory for new node
2. Assign data part of newnode to element and next field to NULL
3. Take pointer variable ptr which points to START
4. Move ptr which points to last node
5. Set next field of ptr to address of newnode
6. Set prev field of newnode to ptr
45
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
3. Search for the given key element by using search function and assign key element
address to keyptr
4. Set prev field of new node points to keyptr
5. Set next field of new node points to next field of Keyptr
6. Set next field of keyptr of prev field to newnode
7. Set next field of keyptr node points to newnode
46
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
if(keyptr==NULL)
printf("\nKey is not found");
else
{
newnode->prev=keyptr;
newnode->next=keyptr->next;
keyptr->next->prev=newnode;
keyptr->next=newnode;
}
}
47
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
48
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
49
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
50
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
51
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
52
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
keyptr->prev=preptr->prev;
preptr->prev->next=keyptr;
free(preptr);
}
}
5.Searching a node in double linked list:-
The search function attempts to locate the requested element in the list. If the node in the list
matches with key, the search returns true if no key matches, it returns false.
The function given below searches through the linked list and returns a pointer the first
occurrence of the search key or returns NULL pointer if the search key is not in the list
54
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
if(start==NULL)
start=newnode;
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=newnode;
newnode->prev=ptr;
}
printf("Enter -1 to end the list \nEnter element");
scanf("%d",&ele);
}
}
void insert_after()
{
struct node *keyptr,*newnode,*aptr;
int ele,key;
printf("\nEnter element");
scanf("%d",&ele);
printf("\nEnter the key element");
scanf("%d",&key);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
keyptr= search(key);
if(keyptr==NULL)
printf("\nKey is not found");
else
{
newnode->prev=keyptr;
newnode->next=keyptr->next;
keyptr->next->prev=newnode;
keyptr->next=newnode;
}
}
struct node* search(int key)
55
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
{
struct node *ptr;
ptr=start;
while(ptr!=NULL)
{
if(ptr->data==key)
return ptr;
ptr=ptr->next;
}
return NULL;
}
void display()
{
struct node *ptr;
ptr=start;
if(ptr==NULL)
printf("\n list is empty");
else
{
while(ptr!=NULL)
{
printf("%5d->",ptr->data);
ptr=ptr->next;
}
}
}
void delete_after()
{
struct node *keyptr,*Aptr;
int key;
printf("\nEnter the key element");
scanf("%d",&key);
keyptr= search(key);
if(keyptr==NULL)
printf("\nKey is not found");
else
{
Aptr=keyptr->next;
keyptr->next=Aptr->next;
Aptr->next->prev=keyptr;
free(Aptr);
}
}
56
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
10. Explain about stack using linked list. Write a program for stack using linked list.
(GRIET/June/2015)
Ans:
STACK USING LINKED LIST:
To implement a stack using linked list we need to define a node which in turn consists of data
and a pointer to the next node. The advantage of representing stacks using linked lists is that
we can decide which end should be the top of the stack.
In our example we will select front end as the top of the stack in which we can add and remove
data.
The working principle of stack is LIFO. Using a linked list is one way to implement a stack so
that it can handle essentially any number of elements. The operations performed on a stack
are push ( ) and pop ( ).When implementing a stack using linked lists, pushing elements on to
the stack and popping elements from the stack is performed at the same end i.e. TOP.
57
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
1. If stack is empty, then display message “stack is empty – no deletion possible” and exit.
2. Otherwise , Take pointer variable ptr which points to TOP
3. set TOP points to the next field of TOP
4. Deallocate memory for ptr node from list
switch(ch)
{
case 1: printf("\nenter the element");
scanf("%d",&ele);
push(ele);
break;
case 2 : pop();
break;
case 3:display();
break;
case 4:exit(0);
}
}
}
void push(int ele)
58
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
if(top==NULL)
{
newnode->next=NULL;
top=newnode;
}
else
{
newnode->next=top;
top=newnode;
}
}
void pop()
{
struct node *ptr;
if(top==NULL)
printf("stack is empty");
else
{
ptr=top;
top=top->next;
printf("\nThe deleted element is :%d",ptr->data);
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr=top;
if(top==NULL)
printf("stack is empty");
else
{
while(ptr!=NULL)
{
printf("%4d->",ptr->data);
ptr=ptr->next;
59
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
}
}
}
11. Explain about queue using linked list. Write a program for queue using linked list.
Ans:
Like stacks when implementing a queue using linked list a node must be defined .Front end of
Queue is used to remove data and rear end is used to add data. The advantage of representing a
queue using linked list is that it allows us to select any end to add and remove data items.
So,we can select the end of the linked list as rear and beginning of the list as front.
The working principle of queue is FIFO. Using a linked list is one way to implement a queue
so that it can handle essentially any number of elements. The operations performed on a
queue are enqueue( ) and dequeue( ).When implementing a queue using linked lists, inserting
elements to the queue is done at one end and deletion of elements from the queue is done
at the other end.
61
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
}
void enqueue(int ele)
{
struct node *newnode;
printf("enter number");
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=ele;
newnode->next=NULL;
if(front==NULL)
{
front=newnode;
rear=newnode;
}
else
{
rear->next=newnode;
rear=newnode;
}
}
void dequeue()
{
struct node *ptr;
if(front==NULL)
printf("queue is empty");
else
{
ptr=front;
front=front->next;
printf("\nThe deleted element is:%d",ptr->data);
free(ptr);
}
}
void display()
{
struct node *ptr;
if(front==NULL)
printf("queue is empty");
else
{
ptr=front;
62
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
while(ptr!=NULL)
{
printf("%5d->",ptr->data);
ptr=ptr->next;
}
}
}
Contiguous memory required for complete list Contiguous memory required for single node
that will be large requirements of list that will be small requirements
List is static in nature i.e created at compile List is dynamic i.e. created and manipulated at
time mostly time of execution
List can’t grow and shrink List can grow and shrink dynamically
Memory allotted to single item of list can’t Memory allotted to single node of list can also
free be free
Random access of ith element is possible Sequential access of ith element i.e all previous
through a[i] i-1 node have to traverse before
Traversal easy since any elements can access Traversal is done node by node hence not as
dynamically and randomly good as in array
Searching can be linear and if sorted than in Searching operation must be linear in case of
array we can also apply binary search of sorted list also. Time complexity is
algorithm time complexity proportional to list length
Insertion and deletion is costly since require Insertion and deletion is performed by simply
shifting of many items pointer exchange.
a) info
b) link
In doubly linked list Each node contains at least three parts:
a) info
63
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
Each element in the singly linked list contains a reference to the next element in the list,
while each element in the doubly linked list contains references to the next element as
well as the previous element in the list. Doubly linked lists require more space for each
element in the list and elementary operations such as insertion and deletion is more
complex since they have to deal with two references. But doubly link lists allow easier
manipulation since it allows traversing the list in forward and backward directions.
Singly linked list uses less memory per node (one pointer) , Doubly linked list uses More
memory per node than Singly Linked list (two pointers)
In Singly linked list, Complexity of Insertion and Deletion at known position is O (n). In
Doubly linked list Complexity of Insertion and Deletion at known position is O (1).
If we know in advance that element to be searched is found near the end of the list(for
example name 'Yogesh' in a telephone directory), even then singly linked list is traversed
sequentially from beginning. In doubly linked list If we know in advance that element to
be searched is found near the end of the list(for example name 'Yogesh' in a telephone
directory), then the list can traversed from the end thereby saving time
14. Give the Representation of a node in a Double linked list using structures.
(GRIET/June/2015)
Ans:
In a singly linked list, each element contains a pointer to the next element. We have seen this
before. In single linked list, traversing is possible only in one direction. Sometimes, we have to
traverse the list in both directions to improve performance of algorithms. To enable this, we
require links in both the directions, that is, the element should have pointers to the right element
as well as to its left element. This type of list is called doubly linked list.
Doubly linked list is defined as a collection of elements, each element consisting of three fields:
64
Subject: Data Structures
------------------------------------------------------------------------------------------------------------------------------------------
Left link of the leftmost element is set to NULL which means that there is no left element to that.
And, right link of the rightmost element is set to NULL which means that there is no right
element to that.
Double Linked list provides to insert or delete nodes whenever necessary. First we have to create
the nodes. This is possible by using self referential structure, pointers and dynamic memory
allocation functions like malloc. First we have to declare structure and create memory by using
malloc function.
struct node
{
struct node *prev;
int data;
struct node *next;
};
struct node *newnode,*start=NULL;
65