ccvcvbcbvcbvcbvcbvcb vngfg hgjhg gj jgjhgjhgjhg jhgh jhgjhgj gjhgjhgjhgjhgjhgjhgjhg jghgjhgjhgjhgjhg jjghjhgjhgjhgjhgjh hgjhgjhgjhgjhgjhgjhgjgjhgjhgjhg hgfgfhdfhtftfj jgyjyyyugyugyug yjgghgjhgjhf drdfhgfgfgfgj jgjhgjhgjhgjhgjhg jhgghjfgf fxdx gfd hdhfd tftyf yytfy ytftyf ytf ytfty fytftykgh kghjg jhghgh fgfghfgh fgfghfgf gfgfgfgfytf hgfggf ghfgfg gjhghjg cfffgh jgfgfhgffhg hghh yuy ygyyuyutyutyu jgfghfhgfghfh fghfhgfhgf fthfhfhf hfhfhgf hfhgfghf fghfghfghfhhgfghf jgjhgjhgjhgjhgjhgj jgjhgjh jgjhg jgjhgjhgjhg jhgjhgjhg jhgjhg jhgjhgollowing the announcement of Google+ API deprecation scheduled for March 2019, a number of changes will be made to Blogger’s Google+ integration on 4 February 2019. *Google+ widgets:* Support for the “+1 Button”, “Google+ Followers” and “Google+ Badge” widgets in Layout will no longer be available. All instances of these widgets will be removed from all blogs. *+1 buttons:* The +1/G+ buttons and Google+ share links below blog posts and in the navigation bar will be removed. Please note that if you have a custom template that includes Google+ features, you may need to update ... read more ollowing the announcement of Google+ API deprecation scheduled for March 2019, a number of changes will be made to Blogger’s Google+ integration on 4 February 2019. *Google+ widgets:* Support for the “+1 Button”, “Google+ Followers” and “Google+ Badge” widgets in Layout will no longer be available. All instances of these widgets will be removed from all blogs. *+1 buttons:* The +1/G+ buttons and Google+ share links below blog posts and in the navigation bar will be removed. Please note that if you have a custom template that includes Google+ features, you may need to update ... read moreollowing the announcement of Google+ API deprecation scheduled for March 2019, a number of changes will be made to Blogger’s Google+ integration on 4 February 2019. *Google+ widgets:* Support for the “+1 Button”, “Google+ Followers” and “Google+ Badge” widgets in Layout will no longer be available. All instances of these widgets will be removed from all blogs. *+1 buttons:* The +1/G+ buttons and Google+ share links below blog posts and in the navigation bar will be removed. Please note that if you have a custom template that includes Google+ features, you may need to update ... read moreollowing the announcement of Google+ API deprecation scheduled for March 2019, a number of changes will be made to Blogger’s Google+ integration on 4 February 2019. *Google+ widgets:* Support for the “+1 Button”, “Google+ Followers” and “Google+ Badge” widgets in Layout will no longer be available. All instances of these widgets will be removed from all blogs. *+1 buttons:* The +1/G+ buttons and Google+ share links below blog posts and in the navigation bar will be removed. Please note that if you have a custom template that includes Google+ features, you may need to update ... read moreollowing the announcement of Google+ API deprecation scheduled for March 2019, a nu
What is DSA ?
What are different types of data structures ?
Primitive and Non Primitive DSA
Arrays
Queue
Stack
Linked List
Memory representation of Linked Lists
ccvcvbcbvcbvcbvcbvcb vngfg hgjhg gj jgjhgjhgjhg jhgh jhgjhgj gjhgjhgjhgjhgjhgjhgjhg jghgjhgjhgjhgjhg jjghjhgjhgjhgjhgjh hgjhgjhgjhgjhgjhgjhgjgjhgjhgjhg hgfgfhdfhtftfj jgyjyyyugyugyug yjgghgjhgjhf drdfhgfgfgfgj jgjhgjhgjhgjhgjhg jhgghjfgf fxdx gfd hdhfd tftyf yytfy ytftyf ytf ytfty fytftykgh kghjg jhghgh fgfghfgh fgfghfgf gfgfgfgfytf hgfggf ghfgfg gjhghjg cfffgh jgfgfhgffhg hghh yuy ygyyuyutyutyu jgfghfhgfghfh fghfhgfhgf fthfhfhf hfhfhgf hfhgfghf fghfghfghfhhgfghf jgjhgjhgjhgjhgjhgj jgjhgjh jgjhg jgjhgjhgjhg jhgjhgjhg jhgjhg jhgjhgollowing the announcement of Google+ API deprecation scheduled for March 2019, a number of changes will be made to Blogger’s Google+ integration on 4 February 2019. *Google+ widgets:* Support for the “+1 Button”, “Google+ Followers” and “Google+ Badge” widgets in Layout will no longer be available. All instances of these widgets will be removed from all blogs. *+1 buttons:* The +1/G+ buttons and Google+ share links below blog posts and in the navigation bar will be removed. Please note that if you have a custom template that includes Google+ features, you may need to update ... read more ollowing the announcement of Google+ API deprecation scheduled for March 2019, a number of changes will be made to Blogger’s Google+ integration on 4 February 2019. *Google+ widgets:* Support for the “+1 Button”, “Google+ Followers” and “Google+ Badge” widgets in Layout will no longer be available. All instances of these widgets will be removed from all blogs. *+1 buttons:* The +1/G+ buttons and Google+ share links below blog posts and in the navigation bar will be removed. Please note that if you have a custom template that includes Google+ features, you may need to update ... read moreollowing the announcement of Google+ API deprecation scheduled for March 2019, a number of changes will be made to Blogger’s Google+ integration on 4 February 2019. *Google+ widgets:* Support for the “+1 Button”, “Google+ Followers” and “Google+ Badge” widgets in Layout will no longer be available. All instances of these widgets will be removed from all blogs. *+1 buttons:* The +1/G+ buttons and Google+ share links below blog posts and in the navigation bar will be removed. Please note that if you have a custom template that includes Google+ features, you may need to update ... read moreollowing the announcement of Google+ API deprecation scheduled for March 2019, a number of changes will be made to Blogger’s Google+ integration on 4 February 2019. *Google+ widgets:* Support for the “+1 Button”, “Google+ Followers” and “Google+ Badge” widgets in Layout will no longer be available. All instances of these widgets will be removed from all blogs. *+1 buttons:* The +1/G+ buttons and Google+ share links below blog posts and in the navigation bar will be removed. Please note that if you have a custom template that includes Google+ features, you may need to update ... read moreollowing the announcement of Google+ API deprecation scheduled for March 2019, a nu
What is DSA ?
What are different types of data structures ?
Primitive and Non Primitive DSA
Arrays
Queue
Stack
Linked List
Memory representation of Linked Lists
2. Linked list
► Linked list is a linear data structure that includes a series of connected nodes.
► Linked list can be defined as the nodes that are randomly stored in the memory.
► A node in the linked list contains two parts, i.e., first is the data part and second is the
address part.
► The last node of the list contains a pointer to the null.
► After array, a linked list is the second most used data structure.
► In a linked list, every link contains a connection to another link.
4. Why use linked list over array?
• limitations of arrays:
• The size of the array must be known in advance before using it in the program.
• Increasing the size of the array is a time taking process. It is almost impossible to
expand the size of the array at run time.
• All the elements in the array need to be contiguously stored in the memory. Inserting an
element in the array needs the shifting of all its predecessors.
5. ► Linked list is useful because –
• It allocates the memory dynamically.
• All the nodes of the linked list are non-contiguously stored in the memory and linked
together with the help of pointers.
• In the linked list, size is no longer a problem since we do not need to define its size at
the time of declaration.
• The list grows as per the program’s demand and is limited to the available memory
space.
6. How to declare a linked list?
► It is simple to declare an array, as it is of a single type, while the declaration of a linked
list is a bit more typical than array.
► Linked list contains two parts, and both are of different types, i.e., one is the simple
variable, while another is the pointer variable.
► We can declare the linked list by using the user-defined data type structure.
► Syntex:
struct node
{
int data;
struct node *next;
}
7. Types of Linked list
• Singly-linked list - Singly linked list can be defined as the collection of an ordered set
of elements.
• A node in the singly linked list consists of two parts: data part and link part.
• Data part of the node stores actual information that is to be represented by the node,
while the link part of the node stores the address of its immediate successor.
• Doubly linked list - Doubly linked list is a complex type of linked list in which a node
contains a pointer to the previous as well as the next node in the sequence.
• Therefore, in a doubly-linked list, a node consists of three parts: node data, pointer to
the next node in sequence (next pointer), and pointer to the previous node (previous
pointer).
9. • Circular singly linked list - In a circular singly linked list, the last node of the list
contains a pointer to the first node of the list.
• Circular doubly linked list - Circular doubly linked list is a more complex type of data
structure in which a node contains pointers to its previous node as well as the next node.
• Circular doubly linked list doesn't contain NULL in any of the nodes.
• The last node of the list contains the address of the first node of the list. The first node
of the list also contains the address of the last node in its previous pointer.
10.
11. Advantages of Linked list
• Dynamic data structure - The size of the linked list may vary according to the
requirements. The linked list does not have a fixed size.
• Insertion and deletion - Unlike arrays, insertion, and deletion in a linked list is easier.
Array elements are stored in the consecutive location, whereas the elements in the
linked list are stored at a random location.
• To insert or delete an element in an array, we have to shift the elements for creating the
space. Whereas, in a linked list, instead of shifting, we just have to update the address
of the pointer of the node.
• Memory efficient - The size of a linked list can grow or shrink according to the
requirements, so memory consumption in a linked list is efficient.
• Implementation - We can implement both stacks and queues using a linked list.
12. Disadvantages of Linked list
• Memory usage - In a linked list, the node occupies more memory than the array. Each
node of the linked list occupies two types of variables, i.e., one is a simple variable, and
another one is the pointer variable.
• Traversal - Traversal is not easy in the linked list. If we have to access an element in
the linked list, we cannot access it randomly, while in the case of an array we can
randomly access it by index.
• Reverse traversing - Backtracking or reverse traversing is difficult in a linked list. In a
doubly-linked list, it is easier but requires more memory to store the back pointer.
13. Applications of Linked list
► With the help of a linked list, the polynomials can be represented as well as we can
perform the operations on the polynomial.
1st number = 5x2
+ 4x1
+ 2x0
2nd number = -5x1
- 5x0
14. ► A linked list can be used to represent the sparse matrix.
15.
16. ► The various operations like student's details, employee's details, or product details can
be implemented using the linked list as the linked list uses the structure data type that
can hold different data types.
► Using a linked list, we can implement stack, queue, tree, graph, and other various data
structures.
17. Operations performed on Linked list
• Insertion - This operation is performed to add an element into the list.
• Deletion - It is performed to delete an operation from the list.
• Display - It is performed to display the elements of the list.
• Search - It is performed to search an element from the list using the given key.
18. Insertion in singly linked list
► The insertion into a singly linked list can be performed at different positions.
► Insertion at beginning
► Insertion at the end of the list
► Insertion after specific node
20. ► Node Creation
struct node
{
int data;
struct node *next, *head, *ptr;
};
#Allocate the space for the new node and store data into the data part of the node.
ptr = (struct node *)malloc(sizeof(struct node *));
ptr → data = item ;
21. ► #Make the link part of the new node pointing to the existing first node of the list.
ptr->next = head;
At the last, we need to make the new node as the first node of the list
head = ptr;
22. Algorithm
Step 1: IF PTR = NUL
Write overflow and go to step 7
Step 2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR → NEXT
Step 4: SET NEW_NODE → DATA = VAL
Step 5: SET NEW_NODE → NEXT = HEAD
Step 6: SET HEAD = NEW_NODE
Step 7: EXIT
25. Insertion at the end of the list
► In order to insert a node at the last, there are two following scenarios that need to be
mentioned.
1. The node is being added to an empty list
2. The node is being added to the end of the linked list
26. ► in the first case:
► The condition (head == NULL) gets satisfied. Hence, we just need to allocate the space
for the node by using the malloc statement in C.
► Data and the link part of the node are set up by using the following statements.
ptr->data = item;
ptr -> next = NULL;
► Head = ptr
27. ► In the second case:
► The condition Head = NULL would fail, since Head is not null.
► Now, we need to declare a temporary pointer temp in order to traverse through the list.
► Temp is made to point to the first node of the list.
Temp = head
► Then, traverse through the entire linked list using the statements:
while (temp→ next != NULL)
temp = temp → next;
28.
29. ► At the end of the loop, the temp will be pointing to the last node of the list.
► Now, allocate the space for the new node, and assign the item to its data part.
► Since, the new node is going to be the last node of the list hence, the next part of this
node needs to be pointing to the null.
► We need to make the next part of the temp node (which is currently the last node of the
list) point to the new node (ptr).
31. Step 1: IF PTR = NULL Write OVERFLOW and exit
Step 2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR - > NEXT
Step 4: SET NEW_NODE - > DATA = VAL
Step 5: SET NEW_NODE - > NEXT = NULL
Step 6: SET PTR = HEAD
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
32. Insertion in singly linked list after specified
Node
► In order to insert an element after the specified number of nodes into the linked list, we
need to do the desired number of elements in the list to move the pointer at the position
after which the node will be inserted.
► This will be done by using the following statements:
34. ► Allocate the space for the new node and add the item to the data part of it.
► This will be done by using the following statements.
ptr = (struct node *) malloc (sizeof(struct node));
ptr->data = item;
► Now, we just need to make a few more link adjustments and our node will be inserted at
the specified position.
► Since, at the end of the loop, the loop pointer temp would be pointing to the node after
which the new node will be inserted.
► Therefore, the next part of the new node ptr must contain the address of the next part of
the temp.
► This will be done by using the following statements:
35. ► ptr→ next = temp → next
► Now, we just need to make the next part of the temp, point to the new node ptr. This
will insert the new node ptr, at the specified position.
► temp ->next = ptr;
37. Algorithm
STEP 1: IF PTR = NULL
WRITE OVERFLOW and exit
STEP 2: SET NEW_NODE = PTR
STEP 3: NEW_NODE → DATA = VAL
STEP 4: SET TEMP = HEAD
STEP 5: SET I = 0
STEP 6: REPEAT STEP 7 AND 8 UNTIL I <loc
38. STEP 7: TEMP = TEMP → NEXT
STEP 8: IF TEMP = NULL
WRITE "DESIRED NODE NOT PRESENT“ and exit
STEP 9: PTR → NEXT = TEMP → NEXT
STEP 10: TEMP → NEXT = PTR
STEP 12: EXIT
39. Deletion in singly linked list
► The Deletion of a node from a singly linked list can be performed at different positions.
► Based on the position of the node being deleted, the operation is categorized into the
following categories.
► Deletion at beginning
► Deletion at the end of the list
► Deletion after the specified node
40. Deletion in singly linked list at beginning
► Deleting a node from the beginning of the list is the simplest operation of all.
► It just needs a few adjustments in the node pointers.
► Since the first node of the list is to be deleted, therefore, we just need to make the head,
point to the next of the head.
► This will be done by using the following statements.
ptr = head;
head = head->next;
► free(ptr)
42. Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW and exit
Step 2: SET PTR = HEAD
Step 3: SET HEAD = HEAD -> NEXT
Step 4: FREE PTR
Step 5: EXIT
43. Deletion in singly linked list at the end
► There are two scenarios in which, a node is deleted from the end of the linked list.
1. There is only one node in the list that needs to be deleted.
2. There are more than one node in the list and the last node of the list will be deleted.
44. ► In the first scenario:
► the condition head → next = NULL will survive and therefore, the only node head of
the list will be assigned to null.
► This will be done by using the following statements.
ptr = head
head = NULL
free(ptr)
45. In the second scenario
► The condition head → next = NULL would fail and therefore, we have to traverse the
node in order to reach the last node of the list.
► For this purpose, just declare a temporary pointer temp and assign it to the head of the
list.
► We also need to keep track of the second last node of the list.
► For this purpose, two pointers ptr and ptr1 will be used where ptr will point to the last
node and ptr1 will point to the second last node of the list.
47. ► Now, we just need to make the pointer ptr1 point to the NULL and the last node of the
list that is pointed by ptr will become free.
► It will be done by using the following statements.
ptr1->next = NULL;
free(ptr);
48.
49. Algorithm
► Step 1: IF HEAD = NULL write underflow and exit
• Step 2: SET PTR = HEAD
• Step 3: Repeat Steps 4 and 5 while PTR -> NEXT!= NULL
• Step 4: SET PTR1 = PTR
• Step 5: SET PTR = PTR -> NEXT
• [END OF LOOP]
• Step 6: SET PTR1 -> NEXT = NULL
• Step 7: FREE PTR
• Step 8: EXIT
50. Deletion in singly linked list after the specified
node
► In order to delete the node, which is present after the specified node, we need to skip the
desired number of nodes to reach the node after which the node will be deleted.
► We need to keep track of the two nodes.
► The one which is to be deleted and the other one which is present before that node.
► For this purpose, two pointers are used: ptr and ptr1.
52. ► Make the next of ptr1 (points to the specified node) point to the next of ptr (the node
which is to be deleted).
► This will be done by using the following statements.
ptr1 ->next = ptr ->next;
free(ptr);
53.
54. STEP 1: IF HEAD = NULL
WRITE UNDERFLOW
Go to step 10
STEP 2: SET TEMP = HEAD
STEP 3: SET I = 0
STEP 4: REPEAT STEP 5 TO 8 UNTIL I<loc
STEP 5: TEMP1 = TEMP
STEP 6: TEMP = TEMP → NEXT
STEP 7: IF TEMP = NULL
55. WRITE "DESIRED NODE NOT PRESENT“
GOTO STEP 12
End of if
STEP 8: I = I+1
END OF LOOP
STEP 9: TEMP1 → NEXT = TEMP → NEXT
STEP 10: FREE TEMP
STEP 11: EXIT
►
56. Linked list implementation of stack
► Instead of using an array, we can also use a linked list to implement a stack.
► Linked list allocates the memory dynamically.
► In linked list implementation of stack, the nodes are maintained non-contiguously in the
memory.
► Each node contains a pointer to its immediate successor node in the stack.
► Stack is said to be overflown if the space left in the memory is not enough to create a
node.
58. Adding a node to the stack (Push operation)
► Create a node first and allocate memory to it.
► If the list is empty then the item is to be pushed as the start node of the list.
► This includes assigning value to the data part of the node and assigning null to the
address part of the node.
► If there are some nodes in the list already, then we have to add the new element at the
beginning of the list (to not violate the property of the stack).
► For this purpose, assign the address of the starting element to the address field of the
new node and make the new node, the starting node of the list.
60. Program
void push ()
{
int val;
struct node *ptr =(struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("not able to push the element");
}
63. Deleting a node from the stack (POP
operation)
► Deleting a node from the top of the stack is referred to as a pop operation.
1. Check for the underflow condition: The underflow condition occurs when we try to
pop from an already empty stack.
The stack will be empty if the head pointer of the list points to null.
1. Adjust the head pointer accordingly: In the stack, the elements are popped only from
one end, therefore, the value stored in the head pointer must be deleted and the node
must be freed.
2. The next node of the head node now becomes the head node.
66. Display the nodes (Traversing)
► Displaying all the nodes of a stack needs traversing all the nodes of the linked list
organized in the form of a stack.
► For this purpose, we need to follow the following steps.
1. Copy the head pointer into a temporary pointer.
2. Move the temporary pointer through all the nodes of the list and print the value field attached
to every node.
69. Linked List implementation of Queue
► The array implementation can not be used for large-scale applications where the queues
are implemented.
► One of the alternatives of array implementation is linked list implementation of a queue.
► In the linked queue, there are two pointers maintained in the memory i.e. front pointer
and rear pointer.
► The front pointer contains the address of the starting element of the queue while the rear
pointer contains the address of the last element of the queue.
► Insertion and deletions are performed at the rear and front end respectively. If the front
and rear both are NULL, it indicates that the queue is empty.
71. Insert operation
► The insert operation appends the queue by adding an element to the end of the queue.
► The new element will be the last element of the queue.
► Allocate the memory for the new node ptr by using the following statement.
► Ptr = (struct node *) malloc (sizeof(struct node));
► There can be the two scenario of inserting this new node ptr into the linked queue.
72. ► In the first scenario, we insert an element into an empty queue.
► In this case, the condition front = NULL becomes true.
► Now, the new element will be added as the only element of the queue, and the next
pointer of the front and rear pointer both, will point to NULL.
73. ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
74. ► In the second case, the queue contains more than one element.
► The condition front = NULL becomes false.
► In this scenario, we need to update the end pointer rear so that the next pointer of the
rear will point to the new node ptr.
► Since, this is a linked queue, hence we also need to make the rear pointer point to the
newly added node ptr.
► We also need to make the next pointer of the rear point NULL.
76. Algorithm
► Step 1: Allocate the space for the new node PTR
► Step 2: SET PTR -> DATA = VAL
► Step 3: IF FRONT = NULL
► SET FRONT = REAR = PTR
► SET FRONT -> NEXT = REAR -> NEXT = NULL
► ELSE
► SET REAR -> NEXT = PTR
SET REAR = PTR
SET REAR -> NEXT = NULL
[END OF IF]
► Step 4: END
80. Deletion in Queue
► Deletion operation removes the element that is first inserted among all the queue
elements.
► Firstly, we need to check whether the list is empty or not.
► The condition front == NULL becomes true if the list is empty, in this case, we simply
write underflow on the console and make exit.
► Otherwise, we will delete the element that is pointed by the pointer front.
► For this purpose, copy the node pointed by the front pointer into the pointer ptr. Now,
shift the front pointer, point to its next node, and free the node pointed by the node ptr.
► This is done by using the following statements.
82. Algorithm
• Step 1: IF FRONT = NULL Write " Underflow “ and Go to Step 5
• Step 2: SET PTR = FRONT
• Step 3: SET FRONT = FRONT -> NEXT
• Step 4: FREE PTR
• Step 5: END
83. Program
void delete (struct node *ptr)
{
if(front == NULL)
{
printf("nUNDERFLOWn");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}