Data Structures Notes

mobiprep (6).png

Data Structures Basic

mobiprep (6).png

Stacks

mobiprep (6).png

Queues

mobiprep (6).png

Linked List

mobiprep (6).png

Sorting

mobiprep (6).png

Tree

mobiprep (6).png

Graphs

mobiprep (6).png

Hashing and Searching

Heading

Q

1

Discuss Linked Lists with its advantages and disadvantages.

LRM_EXPORT_207556595493866_20190724_1939

Ans

Linked list is a type of linear data structure used to store value. Unlike many other data structures, the data stored in the linked list does not store in continuous memory location which provides linked list an upper hand in efficient working.
Advantages of linked list:
1.Linked list is a dynamic data structure that grows and shrink as per the allocation of data
2.As linked list do not have continuous memory allocation thus inserting elements and deleting elements from the linked list is easier.
3.As linked-list can shrink during runtime allocation thus there is no memory wastage.
4.Other data structures like Stack, queue can be easily implemented using linked list
Disadvantages of linked list
1.Linked list uses more memory as it has data as well as address of other node present in it
2.We cannot directly access an element from the linked list. For accessing an element, we have to traverse through the list.
3.Reversing a linked list is a cumbersome and tedious work.

LRM_EXPORT_207556595493866_20190724_1939

Q

2

What is Linked Lists? What are different operations on Linked Lists?

LRM_EXPORT_207556595493866_20190724_1939

Ans

Linked list is a type of linear data structure used to store value. Unlike many other data structures, the data stored in the linked list does not store in continuous memory location which provides linked list an upper hand in efficient working.
Operations performed in the linked list are as follow:

Add node
We can add nodes at various positions in the linked list like in the beginning or at the end or even before or after a specific node.

Delete node
Similar to addition of nodes, we can also delete nodes from various positions in a linked list like deleting the first node, last node or a node before or after a specific node.

Traverse list
Traversing a list means moving in the list, visiting various nodes and performing various operations.

Length of list
We can calculate the length of the linked list which can be used for various purposes while dealing with the list.

LRM_EXPORT_207556595493866_20190724_1939

Q

3

Differentiate between Linked List and Array.

LRM_EXPORT_207556595493866_20190724_1939

Ans

1. The element allocation in a list takes place during run time whereas in case of array it happens in compile time.
2. Array has a continuous memory allocation whereas linked lists store the data randomly.
3. Insertion and deletion are more efficiently done in list then array
4. Array uses less memory as compared to list.
5. In array the elements can be accessed directly whereas in linked list it is in sequence

LRM_EXPORT_207556595493866_20190724_1939

Q

4

Why do we need pointers in Linked List?

LRM_EXPORT_207556595493866_20190724_1939

Ans

1. As in list the elements are not stored continuously and every node in a linked list contains the address of the next node. Thus, pointers are required in order to access the next memory location.
2. In singly linked list we need one pointer to access the next memory allocation but in doubly linked we use two pointers as each node holds the address of next as well as previous mode.

Singly Linked list
Singly linked list is the most basic linked data structure. In this the elements can be placed anywhere in the heap memory unlike arrays which use contiguous locations. Nodes in a linked list are linked together using a next field, which stores the address of the next node in the next field of the previous node i.e. each node of the list refers to its successor and the last node contains the NULL reference. It has a dynamic size, which can be determined only at run time.

LRM_EXPORT_207556595493866_20190724_1939

Q

5

Explain Insertion operation at the beginning of singly linked list

LRM_EXPORT_207556595493866_20190724_1939

Ans

Pseudo Code:
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL NEXT
Step 4: SET NEW_NODE🡪 DATA = VAL
Step 5: SET NEW_NODE🡪 NEXT = START
Step 6: SET START = NEW_NODE
Step 7: EXIT

LRM_EXPORT_207556595493866_20190724_1939

Q

6

Explain Insertion operation at nth position before a node of singly linked list

LRM_EXPORT_207556595493866_20190724_1939

Ans

Pseudo code:
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET = AVAIL
Step 3: SET AVAIL = AVAIL 🡪NEXT
Step 4: SET NEW_NODE 🡪DATA = VAL
Step 5: SET PTR = START
Step 6: SET PREPTR = PTR
Step 7: Repeat Steps 8 and 9 while PTR 🡪DATA! = NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR🡪 NEXT
[END OF LOOP]
Step 10: PREPTR🡪 NEXT = NEW_NODE
Step 11: SET NEW_NODE 🡪NEXT = PTR
Step 12: EXIT

LRM_EXPORT_207556595493866_20190724_1939

Q

7

Explain Insertion operation at nth position after a node of singly linked list

LRM_EXPORT_207556595493866_20190724_1939

Ans

Pseudo code:

Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET = AVAIL
Step 3: SET AVAIL = AVAIL🡪 NEXT
Step 4: SET NEW_NODE🡪 DATA = VAL
Step 5: SET PTR = START
Step 6: SET PREPTR = PTR
Step 7: Repeat Steps 8 and 9 while PREPTR🡪 != NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR🡪 NEXT
[END OF LOOP]
Step 10: PREPTR🡪 NEXT = NEW_NODE
Step 11: SET NEW_NODE🡪 NEXT = PTR
Step 12: EXIT

LRM_EXPORT_207556595493866_20190724_1939

Q

8

Explain Insertion operation at the end of singly linked list

LRM_EXPORT_207556595493866_20190724_1939

Ans

Pseudo code:
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 10
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL NEXT
Step 4: SET NEW_NODE DATA = VAL
Step 5: SET NEW_NODE NEXT = NULL
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR NEXT! = NULL
Step 8: SET PTR = PTR NEXT
[END OF LOOP]
Step 9: SET PTR NEXT = NEW_NODE
Step 10: EXIT

LRM_EXPORT_207556595493866_20190724_1939

Q

9

Explain deletion operation at the beginning of singly linked list

LRM_EXPORT_207556595493866_20190724_1939

Ans

Pseudo code:
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 5
[END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START NEXT
Step 4: FREE PTR
Step 5: EXIT

LRM_EXPORT_207556595493866_20190724_1939

Q

10

Explain deletion operation at nth position after a node of singly linked list

LRM_EXPORT_207556595493866_20190724_1939

Ans

Pseudo code:

Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 1
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PREPTR = PTR
Step 4: Repeat Steps 5 and 6 while PREPTR🡪 DATA! = NUM
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR 🡪NEXT
[END OF LOOP]
Step 7: SET TEMP = PTR
Step 8: SET PREPTR 🡪NEXT = PTR🡪 NEXT
Step 9: FREE TEMP
Step 10: EXIT

LRM_EXPORT_207556595493866_20190724_1939