0% found this document useful (0 votes)
15 views

Linked List - Note

Uploaded by

sayanpal854
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Linked List - Note

Uploaded by

sayanpal854
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1. What is a structure? How is it different from an array?

When different types of elements have to be put in a single unit, a structure


needs to be used. For eg. a structure of student type can be defined as follows:
struct student{ int rno;
char name[30];
float marks;
};
struct student s1;
Here student is the structure-tag and rno, name, marks are structure elements
or members. s1 is a struct student type of variable with the size of 36 bytes,
comprising of rno, name and marks in a sequential order. One structure variable
can be assigned to another structure variable of the same type using assignment
operator.
Arrays Structures
1. An array is a collection of related data 1. Structure can have elements of
elements of the same type. different types
2. An array is a derived data type 2. A structure is a programmer-
defined data type
3. Any array behaves like a built-in data 3. But in the case of structure, first,
types. All we have to do is to declare an we have to design and declare a
array variable and use it. data structure before the variable of
that type are declared and used.
4. Structures allocate dynamic
4. Array allocates static memory and uses
memory and uses (.) operator for
index/subscript for accessing elements of
accessing the member of a
the array.
structure.
5. An array is a pointer to the first
5. Structure is not a pointer
element of it
6. Element access takes relatively less 6. Property access takes relatively
time. large time.

2. What is a self-referential structure? Explain with example.


A self-referential structure is a structure which has atleast one member which
is of the same type as the structure itself. It is different from an ordinary
structure because it has atleast one structure pointer of its own type as one of its
members.
Self referential structures are needed in situations when the exact number of
records to be input is not known. With the help of these structures, linked records
can be created. Unlike arrays, linked records are stored in non-contiguous
locations but are linked to each other with the help of pointers. For eg. a self-
referential structure can be declared in the following way:
struct student{ int rno;
char name[30];
struct student *next;
};
The above-mentioned structure declaration contains next pointer which is of the
same type as the structure itself which is used to point to a record of the same
type.

3.What is the static and dynamic memory allocation?


Static memory allocation refers to memory allocation at compile-time. For
example an array is allocated memory at compile time. That is, the size of the
array can not be changed at run-time.
Dynamic memory allocation means allocating memory at run time from the
heap portion of RAM. At run-time, depending upon the user’s requirement,
memory can be allocated, resulting in a better utilization of memory.
The functions generally used for this are:
a)malloc
malloc requires one argument - the number of bytes to be allocated dynamically.
If the memory allocation is successful, malloc returns a void pointer – this can
be assigned to a pointer variable, which will store the address of the allocated
memory.
If memory allocation fails , malloc returns a NULL pointer.
Example:
int *ptr;
ptr = (int *)malloc(sizeof(int));
b)calloc
calloc is similar to malloc, but the main difference is that the values stored in the
allocated memory space is zero by default. With malloc, the allocated memory
could have any value.
calloc requires two arguments. The first is the number of blocks to be allocated
memory for. The second is the size of each block. calloc is used to allocate
multiple memory blocks.
Like malloc, calloc returns a void pointer if the memory allocation was successful,
else it returns a NULL pointer.
int *ptr;
ptr =(int *) calloc(3, sizeof(int));
c)realloc
Realloc deallocates the old object pointed to by ptr and returns a pointer to a
new object that has the size specified by size. The contents of the new object is
identical to that of the old object prior to deallocation, up to the lesser of the new
and old sizes. Any bytes in the new object beyond the size of the old object have
indeterminate values.
The point to note is that realloc() should only be used for dynamically allocated
memory. If the memory is not dynamically allocated, then behavior is undefined.
void *realloc(void *ptr, size_t size);
4.What is the difference between static and dynamic memory allocation?
Static Memory Allocation Dynamic Memory Allocation

In this case, variables get allocated In this case, variables get allocated only
permanently if your program unit gets active

Allocation is done before program Allocation is done during program


execution execution

It uses the data structure called It uses the data structure called heap
stack for implementing static for implementing dynamic allocation
allocation

Less efficient More efficient

There is no memory reusability There is memory reusability and


memory can be freed when not required

Static memory allocation is faster \Dynamic memory allocation is Slower


than dynamic than static

5.What is Free () and what is memory leak


Free()This function is used to de-allocate the memory block which is no
longer needed.

free(ptr);

A memory leak occurs when memory is allocated using malloc() function and
not released using free() function. This results in an orphaned block which is not
referenced by anything.
Memory leak arises due to the following reasons:
 Memory blocks allocated are not released even when not required.
 The free statement is bypassed at the time of execution of the particular
function.
 The memory allocated by malloc function is stored in a pointer variable which
was already pointing to another memory location.
6. What is Linked list? Advantage and Disadvantage of Linked list
Linked lists- It is a dynamic data-structure in which memory for each node is
allocated at run-time from the heap portion of RAM. The memory allocation for
the nodes need not be continuous as in arrays. It can be defined as a list of nodes
where each node contains the information part and the address of the next node.
Insertion/Deletion can take place from the beginning, middle or end.
The various types of linked list are:-
 Single-linked list
 Double-linked list
 Circular linked list
 Circular Double linked list
Advantages of linked lists over arrays:
a) As memory allocation is done at run-time, using a linked-list, results in
better memory utilization. This is because we end-up creating only as many
nodes as required by the user. Neither there is a wastage of memory nor a
shortage which might happen incase of arrays.
b) As in a linked list the nodes are not allocated continuous memory, again this
results in better memory utilization.
c) Insertion/Deletion is simpler and less time consuming as this requires only
the adjustment of some pointers. In an array, this process takes time as it
requires a shifting of elements.
Drawbacks:
a) Random access is not allowed. We have to access elements sequentially
starting from the first node. So we cannot do binary search with linked lists.
b) Extra memory space for a pointer is required with each element of the list.
c) Not cache friendly. Since array elements are contiguous locations, there is
locality of reference which is not there in case of linked lists.

7. What are operation which can be performed on linked list?


a. Traversing- This refers to accessing all the nodes in the linked list one by
one.
b. Searching- It is the process of finding the location of a given data element
in the linked list. If Binary search has to be performed, linked lists are not
suitable for this searching process.
c. Insertion- It means adding a new data element to the linked list. This could
be done in sorted or unsorted order.
d. Deletion- It means removing any existing data element from the linked list.
e. Sorting- It is the process of arranging all the elements in some logical
order such as ascending or descending order.
f. Merging- It is the process of combining the elements of two linked lists into
a single linked list.

8. What is a single linked list? How is it implemented?


A linked list whose node contains two parts i.e. information part & link part &
null value in the link part signifies last node. As it has a single link which is to the
next node, it is known as single linked list.

Nodes are created for the linked list at run-time using memory allocation function,
malloc(). The nodes are created from the memory area known as heap.
The representation of a node in the linked list is done using the following self-
referential structure:
struct node{ int info;
struct node *next;
};

Each element (we will call it a node) of a list is comprising of two items - the data and a
reference to the next node. The last node has a reference to null. The entry point into a
linked list is called the head of the list. It should be noted that head is not a separate node,
but the reference to the first node. If the list is empty then the head is a null reference.
9. What are the application of single 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.

10. What are the advantages and disadvantages of single linked list?
ADVANTAGE :-
1) Insertions and Deletions can be done easily.
2) It does not need movement of elements for insertion and deletion.
3) It space is not wasted as we can get space according to our requirements.
4) Its size is not fixed.
5) It can be extended or reduced according to requirements.
6) Elements may or may not be stored in consecutive memory available,even
then we can store the data in computer.
7) It is less expensive.
*DISADVANTAGE :-
1) It requires more space as pointers are also stored with information.
2) Different amount of time is required to access each element.
3) If we have to go to a particular element then we have to go through all those
elements that come before that element.
4) we can not traverse it from last & only from the beginning.
5) It is not easy to sort the elements stored in the linear linked list.

11.What is a double linked list?


In a double linked list. each node contains information part and two pointers ,
one containing the address of the next node and the other containing the
address of the previous node. Rather than a single pointer pointing to the next
node in a single linked list, it has two pointers, hence it is known as double
linked list.
prev info next

A node in a double linked list is represented using the following self-referential


structure:
struct node{int info;
struct node*prev;
struct node *next;
};
It has a pointer start pointing to the beginning of the list and a pointer last
pointing to the last node of the list.
12.What are the advantages/ disadvantages of Doubly linked list?
Advantages of Doubly linked list
Doubly linked list is one of the important data structures. Here are various
advantages of doubly linked list.
 As like singly linked list it is the easiest data structures to implement.
 Allows traversal of nodes in both direction which is not possible in singly linked
list.
 Deletion of nodes is easy when compared to singly linked list, as in singly linked
list deletion requires a pointer to the node and previous node to be deleted.
Which is not in case of doubly linked list we only need the pointer which is to be
deleted.
 Reversing the list is simple and straightforward.
 Can allocate or de-allocate memory easily when required during its execution.
 It is one of most efficient data structure to implement when traversing in both
direction is required.
Disadvantages of Doubly linked list
Not many but doubly linked list has few disadvantages also which can be listed
below:
 It uses extra memory when compared to array and singly linked list.
 Since elements in memory are stored randomly, hence elements are accessed
sequentially no direct access is allowed.

13. What are Applications/Uses of doubly linked list in real life?

There are various application of doubly linked list in the real world. Some of them
can be listed as:

 Doubly linked list can be used in navigation systems where both front and back
navigation is required.
 It is used by browsers to implement backward and forward navigation of visited
web pages i.e. backand forward button.
 It is also used by various application to implement Undo and Redo functionality.
 It can also be used to represent deck of cards in games.
 It is also used to represent various states of a game.

14.What is a circular linked list?


A circular linked list is one in which the last node of the list rather than containing
a NULL, contains the address of the starting node. Thus the last node points back
to the start of the linked list.
15.How is it advantageous over a single linked list? What are
disadvantage of Circular
Advantages of Circular Linked Lists:
a) Any node can be a starting point. We can traverse the whole list by starting
from any point. We just need to stop when the first visited node is visited again.
b) Useful for implementation of queue. Unlike this implementation, we don’t need
to maintain two pointers for front and rear if we use circular linked list. We can
maintain a pointer to the last inserted node and front can always be obtained as
next of last.
c) Circular lists are useful in applications to repeatedly go around the list. For
example, when multiple applications are running on a PC, it is common for the
operating system to put the running applications on a list and then to cycle
through them, giving each of them a slice of time to execute, and then making
them wait while the CPU is given to another application. It is convenient for the
operating system to use a circular list so that when it reaches the end of the list it
can cycle around to the front of the list.
d) Circular Doubly Linked Lists are used for implementation of advanced data
structures like
Disadvantages:
1. It is not easy to reverse the linked list.
2. If proper care is not taken, then the problem of infinite loop can occur.
3. If we at a node and go back to the previous node, then we can not do it in
single step. Instead we have to complete the entire circle by going through the in
between nodes and then we will reach the required node.
16.What are the application of Circular Linked List

 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.
 Another example can be Multiplayer games. All the Players are kept in a
Circular Linked List and the pointer keeps on moving forward as a player's
chance ends.
 Circular Linked List can also be used to create Circular Queue. In a Queue we
have to keep two pointers, FRONT and REAR in memory all the time, where as
in Circular Linked List, only one pointer is required.

17. What is Doubly Circular Linked List


Circular Doubly Linked List has properties of both doubly linked list and circular
linked list in which two consecutive elements are linked or connected by previous
and next pointer and the last node points to first node by next pointer and also
the first node points to last node by previous pointer.
Following is representation of a Circular doubly linked list node in C
// Structure of the node
struct node
{
int data;
struct node *next; // Pointer to next node
struct node *prev; // Pointer to previous node
};

Insertion at the end of list or in an empty list


 Empty List (start = NULL): A node(Say N) is inserted with data = 5, so
previous pointer of N points to N and next pointer of N also points to N. But
now start pointer points to the first node the list.

 List initially contain some nodes, start points to first node of the
List: A node(Say M) is inserted with data = 7, so previous pointer of M points
to last node, next pointer of M points to first node and last node’s next
pointer points to this M node and first node’s previous pointer points to this M
node.
Insertion at the beginning of the list: To insert a node at the beginning of the
list, create a node(Say T) with data = 5, T next pointer points to first node of the
list, T previous pointer points to last node the list, last node’s next pointer points
to this T node, first node’s previous pointer also points this T node and at last
don’t forget to shift ‘Start’ pointer to this T node.

18. What are the advantages and disadvantages of Circular Doubly Linked
List

1. List can be traversed bothways from head to tail as well as tail to head
2. Being a circular linked list tail can be reached with one operation from head
node

Disadvantages

1. It takes slightly extra memory in each node to accomodate previous pointer

19.What are the Applications of Circular Doubly Linked List

1. Managing songs playlist in media player applications


2. Managing shopping cart in online shopping

You might also like