Linked List DSA
Linked List DSA
1. struct node
2. {
3. struct node *prev;
4. int data;
5. struct node *next;
6. } ;
The prev part of the first node and the next part of the last
node will always contain null indicating end in each direction.
Due to the fact that, each node of the list contains the address
of its previous node, we can find all the details about the
previous node as well by using the previous address stored
inside the previous part of each node.
Node Creation
1. struct node
2. {
3. struct node *prev;
4. int data;
5. struct node *next;
6. };
7. struct node *head, *last;
head=last=NULL;
S Operation Description
N
2 Insertion at end Adding the node into the linked list to the end.
3 Insertion after Adding the node into the linked list after the specifi
specified node node.
5 Deletion at the end Removing the node from end of the list.
6 Deletion of the node Removing the node which is present just after the n
having given data containing the given data.
Explanation :
o Allocate the space for the new node in the memory. This
will be done by using the following statement.
1. ptr->next = NULL;
2. ptr->prev=NULL;
3. ptr->data=item;
4. head=ptr;
o In the second scenario, the condition head ==
NULL become false and the node will be inserted in
beginning. The next pointer of the node will point to the
existing head pointer of the node. The prev pointer of the
existing head will point to the new node being inserted.
o This will be done by using the following statements.
1. ptr->next = head;
2. head→prev=ptr;
Since, the node being inserted is the first node of the list and
therefore it must contain NULL in its prev pointer. Hence assign
null to its previous part and make the head point to this node.
1. ptr→prev =NULL
2. head = ptr
3. tail = ptr
Algorithm :
Write OVERFLOW
Go to Step 9
[END OF IF]
1. struct node
2. {
3. int data;
4. struct node *next;
5. struct node *prev;
6. };
7. struct node *head, *tail;
8. head=NULL;
9. tail=NULL;
10. void insertbeginning(int item)
11. {
12.
13. struct node *ptr = (struct node *)malloc(sizeof(struct n
ode));
14. if(ptr == NULL)
15. {
16. printf("\nOVERFLOW");
17. }
18. else
19. {
20.
21.
22. if(head==NULL)
23. {
24. ptr->next = NULL;
25. ptr->prev=NULL;
26. ptr->data=item;
27. head=ptr;
28. tail=ptr;
29. }
30. else
31. {
32. ptr->data=item;
33. ptr->prev=NULL;
34. ptr->next = head;
35. head->prev=ptr;
36. head=ptr;
37.
38. }
39. }
40.
41. }
o Allocate the memory for the new node. Make the pointer ptr point to the new
node being inserted.
o Check whether the list is empty or not. The list is empty if the condition head
== NULL holds. In that case, the node will be inserted as the only node of the
list and therefore the prev and the next pointer of the node will point to NULL
and the head pointer will point to this node.
1. ptr->next = NULL;
2. ptr->prev=NULL;
3. ptr->data=item;
4. head=ptr;
5. tail=ptr;
o In the second scenario, the condition head == NULL become false. The new
node will be inserted as the last node of the list..
o tail->next=ptr;
o ptr->next=NULL;
o ptr->prev=tail;
o tail=ptr;
1. struct node
2. {
3. int data;
4. struct node *next;
5. struct node *prev;
6. };
7. struct node *head,*tail;
8. void insertlast(int item)
9. {
10.
11. struct node *ptr = (struct node *) malloc(sizeof(struct node));
12. struct node *temp;
13. if(ptr == NULL)
14. {
15. printf("\nOVERFLOW");
16.
17. }
18. else
19. {
20.
21. ptr->data=item;
22. if(head == NULL)
23. {
24. ptr->next = NULL;
25. ptr->prev = NULL;
26. head = ptr;
tail = ptr;
27. }
28. else
29. { tail->next=ptr;
30. ptr->next=NULL;
31. ptr->prev=tail;
32.
33. tail=ptr;
34.
35. }
36.
37. printf("\nNode Inserted\n");
38.
39. }
40. }
41. Traverse to N-1 node in the list. Where N is the position to insert.
Say temp now points to N-1 node.
th
42. Create a newNode that is to be inserted and assign some data to its data field.
43. Connect the next address field of newNode with the node pointed by next
address field of temp node.
44. Connect the previous address field of newNode with the temp node.
45. Check if temp->next is not NULL then, connect the previous address field of
node pointed by temp.next to newNode.
46. Connect the next address field of temp node to newNode i.e. temp->next will
now point to newNode.
struct node
{
struct node *prev;
int data;
struct node *next;
};
struct node *head, *last;
head=last=NULL;
void insertion_specified()
{
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
temp=head;
printf("Enter the location");
scanf("%d",&loc);
if (head==NULL )
printf("create the list first");
for(i=1;i<=loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\n There are less than %d elements", loc);
return;
}
}
printf("Enter value");
scanf("%d",&item);
ptr->data = item;
ptr->next = temp->next;
ptr -> prev = temp;
temp->next->prev=ptr;
temp->next = ptr;
printf("\nnode inserted\n");
}
}
Deletion at beginning
Deletion in doubly linked list at the beginning is the simplest operation. We just need
to copy the head pointer to pointer ptr and shift the head pointer to its next.
1. Ptr = head;
2. head = head → next;
now make the prev of this new head node point to NULL. This will be done by using
the following statements.
1. free(ptr)
Algorithm
o STEP 1: IF HEAD = NULL
WRITE UNDERFLOW
GOTO STEP 6
o STEP 6: EXIT
1. struct node
2. {
3. int data;
4. struct node *next;
5. struct node *prev;
6. };
7. struct node *head, *tail;
8.
9.
10. void beginning_delete()
11. {
12. struct node *ptr;
13. if(head == NULL)
14. {
15. printf("\n UNDERFLOW\n");
16. }
17. else if(head->next == NULL)
18. {
19.
20. free(head);
21. head = NULL;
22. tail=NULL;
23.
24. printf("\nNode Deleted\n");
25. }
26. else
27. {
28. ptr = head; head=head->next;
29. head = head -> next; free(head->prev);
30. head -> prev = NULL; head->prev=NULL;
31. free(ptr);
32. printf("\nNode Deleted\n");
33. }
34. }
Deletion in doubly linked list at the end
1. struct node
2. {
3. int data;
4. struct node *next;
5. struct node *prev;
6. };
7. struct node *head,*tail,*temp;
8. void last_delete()
9. {
10. struct node *ptr;
11. if(head == NULL)
12. {
13. printf("\n UNDERFLOW\n");
14. }
15. else if(head->next == NULL)
16. { free(head);
17. head = NULL;
18. tail=NULL;
19.
20. printf("\nNode Deleted\n");
21. }
22. else
23. {
24. ptr=tail; tail=tail->prev;
25. tail=tail->prev; free(tail->next);
26. tail->next=NULL; tail->next=NULL;
27. free(ptr);
28.
29. printf("\nNode Deleted\n");
30. }
31. }
Deletion at middle
Steps
create a node called the previous storing previous node of the target node
Assign previous node’s next pointer to the next node of the target node
For the next node of the target node, its previous pointer is assigned to the targets
The major problem with the stack implemented using an array is, it works only for a
fixed number of data values. That means the amount of data must be specified at
the beginning of the implementation itself. Stack implemented using an array is not
suitable, when we don't know the size of data which we are going to use. A stack
data structure can be implemented by using a linked list data structure. The stack
implemented using linked list can work for an unlimited number of values. That
means, stack implemented using linked list works for the variable size of data. So,
there is no need to fix the size at the beginning of the implementation. The Stack
implemented using linked list can organize as many data values as we want.
element. That means every newly inserted element is pointed by 'top'. Whenever we
want to remove an element from the stack, simply remove the node which is pointed
by 'top' by moving 'top' to its previous node in the list. The next field of the first
In the above example, the last inserted node is 99 and the first inserted node is 25.
To implement a stack using a linked list, we need to set the following things before
Step 1 - Include all the header files which are used in the program. And
Step 2 - Define a 'Node' structure with two members data and next.
We can use the following steps to insert a new node into the stack...
We can use the following steps to delete a node from the stack...
Step 3 - If it is Not Empty, then define a Node pointer 'temp' and set it to
'top'.
We can use the following steps to display the elements (nodes) of a stack...
function.
Step 3 - If it is Not Empty, then define a Node pointer 'temp' and initialize
with top.
Step 4 - Display 'temp → data --->' and move it to the next node. Repeat the
same until temp reaches to the first node in the stack. (temp → next !
= NULL).
In a linked queue, each node of the queue consists of two parts i.e. data part and the
link part. Each element of the queue points to its immediate next element in the
memory.
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 rear and front end respectively. If front and
rear both are NULL, it indicates that the queue is empty.
1.
struct node
2. {
3. int data;
4. struct node *next;
5. };
6. struct node *front;
7. struct node *rear;
front=rear=NULL
Operation on Linked Queue
There are two basic operations which can be implemented on the linked queues. The
operations are Insertion and Deletion.
Insert operation
The insert operation append the queue by adding an element to the end of the
queue. The new element will be the last element of the queue.
Firstly, allocate the memory for the new node ptr by using the following statement.
There can be the two scenario of inserting this new node ptr into the linked queue.
In the first scenario, we insert 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 front and rear pointer both, will
point to NULL.
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 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 rear point to NULL.
o Step 4: END
Algorithm
Deletion
Deletion operation removes the element that is first inserted among all the queue
elements. Firstly, we need to check either 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.
1. ptr = front;
2. front = front -> next;
3. free(ptr);
o Step 5: END
Algorithm
1. void delete ()
2. { struct node *ptr;
3. if(front == NULL)
4. {
5. printf("\nUNDERFLOW\n");
6. return;
7. }
8. else
9. {
10. ptr = front;
11. printf( " the deleted data is %d", front->data);
12. front = front -> next;
13. free(ptr);
14. }
15. }
Algorithm
8. void display()
9. {
10. struct node *ptr;
11. ptr = front;
12. if(front == NULL)
13. {
14. printf("\nEmpty queue\n");
15. }
16. else
17. { printf("\nprinting values .....\n");
18. while(ptr != NULL)
19. {
20. printf("\n%d\n",ptr -> data);
21. ptr = ptr -> next;
22. }
23. }
24. }