You are on page 1of 33

UNIT 2

LINKED LIST

Array Vs Linked List – Singly linked list - Representation of a linked list in memory -
Operations on a singly linked list - Merging two singly linked lists into one list - Reversing a
singly linked list – Polynomial Manipulation using List - Advantages and disadvantages of
singly linked list - Circular linked list - Doubly linked list - Circular Doubly Linked List.

2.1 Array Vs Linked List

ARRAY LINKED LIST

Array is a collection of elements of Linked List is an ordered collection of elements of


similar data type. same type, which are connected to each other using
pointers.

Array supports Random Access, which Linked List supports Sequential Access, which


means elements can be accessed directly means to access any element/node in a linked list,
using their index. we have to sequentially traverse the complete
linked list, up to that element.

In an array, elements are stored In a linked list, new elements can be stored
in contiguous memory location or anywhere in the memory. Address of the memory
consecutive manner in the memory. location allocated to the new element is stored in
the previous node of linked list, hence forming a
link between the two nodes/elements.

In array, Insertion and Deletion operation In case of linked list, a new element is stored at the
takes more time, as the memory locations first free and available memory location, with only
are consecutive and fixed. a single overhead step of storing the address of
memory location in the previous node of linked
list.Insertion and Deletion operations are fast in
linked list.
Memory is allocated as soon as the array Memory is allocated at runtime, as and when a new
is declared, at compile time. It's also node is added. It's also known as Dynamic
known as Static Memory Allocation. Memory Allocation.

ARRAY LINKED LIST

In array, each element is independent and In case of a linked list, each node/element points to
can be accessed using it's index value. the next, previous, or maybe both nodes.

Array can be single dimensional, two Linked list can be Linear(Singly) linked


dimensional or multidimensional. list, Doubly linked list or Circular linked list linked
list.

Size of the array must be specified at time Size of a Linked list is variable. It grows at
of array declaration. runtime, as more nodes are added to it.

Array gets memory allocated in Whereas, linked list gets memory allocated
the Stack section. in Heap section.

2.2 Linked List


A Linked list is a collection of elements called nodes, each of which stores two items
called data or info and link or pointer field. Info is an element of the list and a link is a pointer to
the next element. The linked list is also called a chain.
The different types of Linked lists are,
 Singly linked list.
 Doubly linked list
 Circular linked list

Singly Linked List

 The first node is the head node and it points to next node in the sequence.
 The last node’s reference is null indicating the end of the list is shown in Fig.2.1

Fig.2.1 Singly Linked List


Doubly Linked List

 Every node has two pointers, one for pointing to next node and the other for pointing to
the previous node.
 The next pointer of the last node and the previous pointer of the first node (head) are null
is shown in Fig 2.2

Fig.2.2 Doubly Linked List


Circular Linked List

 Circular Linked List is very similar to a singly linked list except that, here the last node
points to the first node making it a circular list as shown in fig 2.3

Fig.2.3 Circular Linked List

2.2.1 Singly Linked List


A singly linked list is a linked list in which each node contains only one link pointing to
the next node in the list.
A Node in a linked list holds the data value and the pointer which points to the location of
the next node in the linked list.
2.2.1.1 Representation of a linked list in memory
A linked list is a linear data structure consisting of a group of nodes where each node
points to the next node by means of a pointer.
Each node is composed of data and a reference to the next node in the sequence. The last node
has a reference to null which indicates the end of the linked list.
Head node is the starting node of the linked list(first node) and it contains the reference to the
next node in the list. The head node will have a null reference when the list is empty. The fig 2.4
gives you an idea of how a Linked List looks.

A linked list is represented by a pointer to the first node of the linked list. The first node is
called the head. If the linked list is empty, then the value of the head is NULL. In C, we
can represent a node using structures.

Fig 2.4 Singly Linked List representation


In the picture above we have a linked list, containing 4 nodes, each node has some data(A, B, C
and D) and a pointer which stores the location of the next node.
In a singly linked list, the first node always pointed by a pointer called HEAD. If the link of the
node points to NULL, then that indicates the end of the list.

Operations of Singly Linked List


The operations that are performed on a linear list are,
 Count the number of elements.
 Add an element at the beginning of the list.
 Add an element at the end of the list.
 Insert an element at the specified position in the list.
 Delete an element from the list.
 Search x in the list.
 Display all the elements of the list.

Add an element at the beginning of the list.

Step 1 - Create a newNode with given value.

Step 2 - Check whether list is Empty (head == NULL)

Step 3 - If it is Empty then, set newNode→next = NULL and head = newNode.

Step 4 - If it is Not Empty then, set newNode→next = head and head = newNode.

Algorithm

Addatbeg()

Begin

Newnode->data=K

Newnode->next=NULL

If(Head==NULL)

Head=Newnode

Else

Newnode->next=Head

Head=Newnode

Endif

End

The fig.2.5 shows how a node is added at the beginning of the linked list.
Fig 2.5 Adding node at the beginning of the linked list

Add an element at the end of the list.

Step 1 - Create a newNode with given value and newNode → next as NULL.

Step 2 - Check whether list is Empty (head == NULL).

Step 3 - If it is Empty then, set head = newNode.

Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.

Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list
(until temp → next is equal to NULL).

Step 6 - Set temp → next = newNode.

Algorithm

Addatend()

Begin

Newnode->data=K
Newnode->next=NULL

If(Head==NULL)

Head=Newnode

Else

Temp=Head

While(temp->next !=NULL)

temp=temp->next

endwhile

temp->next=Newnode

Endif

end

The Fig 2.6 shows how the node is added at the end of the list.

Fig 2.6 Node added at the end of the list

Insert an element at the specified position in the list:


Step 1 - Create a newNode with given value.

Step 2 - Check whether list is Empty (head == NULL)

Step 3 - If it is Empty then, set newNode → next = NULL and head = newNode.

Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.

Step 5 - Keep moving the temp to its next node until it reaches to the node after which
we want to insert the newNode (until temp1 → data is equal to location, here location is
the node value after which we want to insert the newNode).

Step 6 - Every time check whether temp is reached to last node or not. If it is reached to
last node then display 'Given node is not found in the list!!! Insertion not
possible!!!' and terminate the function. Otherwise move the temp to next node.

Step 7 - Finally, Set 'newNode → next = temp → next' and 'temp → next = newNode'

The operation ‘Insert’ inserts the given element x in the k th position. A temporary pointer Temp
th
is created and made to point to Head. Now the Temp pointer is moved to the k – 1 node. A
new node with value x is created and the link of the new node is made to point to the position
where the link of temp is pointing. Then the link of temp is made to point to the new node. Thus
the given element is inserted in the position k is shown in fig.2.7.
Fig 2.7 Inserting node at position

Algorithm
Function insertin_mid()
Begin
Write "Enter the position:"
Read pos;
If head==NULL AND pos=1
then
Insert the node at the beginning
End if
If head->next==NULL AND pos=2
then
insert the node
End if
Else if pos>=2 AND pos<=ct
then
prev=head
for I = 2 to pos - 1 step +1
do
prev=prev->link
END FOR
next=prev->link
temp=new node
Write”Enter the data:"
Read temp->data
temp->link=next
prev->link=temp
ct=ct+1
else
Write "Enter a valid position & try again"
End if
End

Deletion
In a single linked list, the deletion operation can be performed in three ways. They are as
follows...

 Deleting from Beginning of the list


 Deleting from End of the list
 Deleting a Specific Node

Deleting from Beginning of the list

We can use the following steps to delete a node from beginning of the single linked list...

Step 1 - Check whether list is Empty (head == NULL)

Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and


terminate the function.

Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head.

Step 4 - Check whether list is having only one node (temp → next == NULL)

Step 5 - If it is TRUE then set head = NULL and delete temp (Setting Empty list


conditions)

Step 6 - If it is FALSE then set head = temp → next, and delete temp.

Deleting from End of the list


We can use the following steps to delete a node from end of the single linked list...

Step 1 - Check whether list is Empty (head == NULL)


Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.

Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and


initialize 'temp1' with head.

Step 4 - Check whether list has only one Node (temp1 → next == NULL)

Step 5 - If it is TRUE. Then, set head = NULL and delete temp1. And terminate the


function. (Setting Empty list condition)

Step 6 - If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node.
Repeat the same until it reaches to the last node in the list. (until temp1 →
next == NULL)

Step 7 - Finally, Set temp2 → next = NULL and delete temp1.

Deleting a Specific Node from the list

We can use the following steps to delete a specific node from the single linked list...

Step 1 - Check whether list is Empty (head == NULL)

Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and


terminate the function.

Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and
initialize 'temp1' with head.

Step 4 - Keep moving the temp1 until it reaches to the exact node to be deleted or to the
last node. And every time set 'temp2 = temp1' before moving the 'temp1' to its next
node.

Step 5 - If it is reached to the last node then display 'Given node not found in the list!
Deletion not possible!!!'. And terminate the function.

Step 6 - If it is reached to the exact node which we want to delete, then check whether list
is having only one node or not

Step 7 - If list has only one node and that is the node to be deleted, then
set head = NULL and delete temp1 (free(temp1)).

Step 8 - If list contains multiple nodes, then check whether temp1 is the first node in the
list (temp1 == head).
Step 9 - If temp1 is the first node then move the head to the next node (head = head →
next) and delete temp1.

Step 10 - If temp1 is not first node then check whether it is last node in the list (temp1 →
next == NULL).

Step 11 - If temp1 is last node then set temp2 → next = NULL and


delete temp1 (free(temp1)).

Step 12 - If temp1 is not first node and not last node then set temp2 → next = temp1 →
next and delete temp1 (free(temp1)).

Algorithm

Function del()
Begin
if(head= NULL)
then
Write “Empty list"
else
Write “Enter the position:"
Read pos
if(pos==1)
then
next=head->link
head=next
ct=ct-1
else
if((pos>=2)&&(pos<=ct))
prev=head
for(int i=2;i<=pos-1;i++)
Do
prev=prev->link
End FOR
temp=prev->link
next=temp->link
prev->link=next
ct=ct-1
End if
End If
End

Displaying a Single Linked List

We can use the following steps to display the elements of a single linked list...
Step 1 - Check whether list is Empty (head == NULL)

Step 2 - If it is Empty then, display 'List is Empty!!!' and terminate the function.

Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head.

Step 4 - Keep displaying temp → data with an arrow (--->) until temp reaches to the last


node

Step 5 - Finally display temp → data with arrow pointing to NULL (temp → data --->


NULL).

Counting

count()
begin
int co=0;
do
co++;
c=c->link;
while(c!=NULL);
return co;
end

Searching

The operation Search( x ), searches the given value x in the list. If found returns the node
position where it is found. A temporary pointer Temp is created and made to point to the Head.
Now info part of Temp is compared with the value x. If the value matches the node position
number is returned otherwise Temp pointer is moved to the next node. This is repeated till the
end of the list is reached or till the element to be searched is found.
Algorithm
Function search()
Begin
flag=0
if(head=NULL)
then
Write “Empty list"
else
Write ”Enter the element to be searched:"
Read e
cur=head
FOR I = 1 to ct Step +1
Do
if(cur->data= e)
then

pos=I
flag++
break

else

cur=cur->link
End if
ENDFOR
If (flag =1)
then
Write "Element found in position:" pos

else

Write ”Element not found"


End if

End Search

2.3 Merging

Node* MergeLists(Node* list1, Node* list2)


begin
Node* mergedList;
if(list1 == null && list2 ==null)
return null;
if(list1 == null)
return list2;

if(list2 == null){
return list1;
}
if(list1.data < list2.data){//initialize mergedList pointer to list1 if list1's data is lesser
mergedList = list1;
}else{//initialize mergedList pointer to list2 if list2's data is lesser or equal
mergedList = list2;
}
while(list1!=null && list2!=null){
if(list1.data < list2.data){
mergedList->next = list1;
list1 = list1->next;
}else{
mergedList->next = list2;
list2 = list2->next;
}
}
if(list1 == null){//remaining nodes of list2 appended to mergedList when list1 has
reached its end.
mergedList->next = list2;
}else{//remaining nodes of list1 appended to mergedList when list2 has reached its end
mergedList->next = list1;
}
return mergedList;
}

Traversing

The operation Display( ), displays all the value in each node of the list. A temporary pointer is
created and made to point to Head initially. Now info part of Temp is printed and Temp is
moved to the next node. This is repeated till the end of the list is reached.
Algorithm
Function display()
Begin
cur=head
cout<<"\nNo.of nodes is:"<<ct
cout<<"\nThe data is:"
while (cur< >NULL)
Do
Write "["<<cur->data<<"]->"
cur=cur->link
End while
End

2.4 Reverse the Linked List


Algorithm
reverse()
{
struct node *p1,*p2,*p3;
if(start->link==NULL)
return;
p1=start;
p2=p1->link;
p3=p2->link;
p1->link=NULL;
p2->link=p1;
while(p3!=NULL)
{
p1=p2;
p2=p3;
p3=p3->link;
p2->link=p1;
}
start=p2;
}

2.5 Polynomial Manipulation using List


5x2 + 3x1 + 1
12x3 – 4x1
5x4 – 8x3 + 2x2 + 4x1 + 9x0
It is not necessary to write terms of the polynomials in decreasing order of degree. In other words
the two polynomials 1 + x and x + 1 are equivalent.
The computer implementation requires implementing polynomials as a list of pairs of coefficient
and exponent. Each of these pairs will constitute a structure, so a polynomial will be represented
as a list of structures. A linked list structure that represents polynomials 5x4 – 8x3 + 2x2 + 4x1 +
9x0 illustrates in fig 2.8

Fig2.8 Polynomial Manipulation


Algorithm
node * getnode()
{
node *tmp;
tmp =(node *) malloc( sizeof(node) );
printf("\n Enter Coefficient : ");
fflush(stdin);
scanf("%f",&tmp->coef);
printf("\n Enter Exponent : ");
fflush(stdin);
scanf("%d",&tmp->expo);
tmp->next = NULL;
return tmp;
}
node * create_poly (node *p )
{
char ch;
node *temp,*newnode;
while( 1 )
{
printf ("\n Do U Want polynomial node (y/n): ");
ch = getche();
if(ch == 'n')
break;
newnode = getnode();
if( p == NULL )
p= newnode;
else
{
temp = p;
while(temp->next != NULL )
temp = temp->next;
temp->next = newnode;
}
}
return p;
}
void display (node *p)
{
node *t = p;
while (t != NULL)
{
printf("+ %.2f", t -> coef);
printf("X^ %d", t -> expo);
t=t -> next;
}
}

2.6 Advantages and disadvantages of singly linked list

Advantages of Singly Linked List

 it is very easier for the accessibility of a node in the forward direction.

 the insertion and deletion of a node are very easy.

 the Requirement will less memory when compared to doubly, circular or doubly

circular linked list.

 the Singly linked list is the very easy data structure to implement.

 During the execution, we can allocate or deallocate memory easily.

 Insertion and deletion of elements don’t need the movement of all the elements when

compared to an array.

Disadvantages of Singly Linked List

 therefore, Accessing the preceding node of a current node is not possible as there is no

backward traversal.

 the Accessing of a node is very time-consuming.

2.7 Circular linked list


Circular Linked List: Circular linked list is a linked list which consists of collection of nodes
each of which has two parts, namely the data part and the link part. The data part holds the value
of the element and the link part has the address of the next node. The last node of list has the link
pointing to the first node thus making the circular traversal possible in the list is shown in Fig 2.9
Logical representation of the circular linked list:
Fig 2.9 Circular Linked List

The basic operations in a circular single linked list are:


• Creation.
• Insertion.
• Deletion.
• Traversing.
Creating a circular single Linked List with ‘n’ number of nodes:
The following steps are to be followed to create ‘n’ number of nodes:
• Get the new node using getnode().
newnode = getnode();
• If the list is empty, assign new node as start.
start = newnode;
• If the list is not empty, follow the steps given below:
temp = start;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = newnode;
• Repeat the above steps ‘n’ times.
• newnode -> next = start;
Inserting a node at the beginning:
The following steps are to be followed to insert a new node at the beginning of the
circular list:
• Get the new node using getnode().
newnode = getnode();
• If the list is empty, assign new node as start.
start = newnode;
newnode -> next = start;
• If the list is not empty, follow the steps given below:
last = start;
while(last -> next != start)
last= last -> next;
newnode -> next = start;
start = newnode;
last -> next = start;
The function cll_insert_beg(), is used for inserting a node at the beginning. Figure shows
inserting a node into the circular single linked list at the beginning.

Fig 2.10 Inserting a node at the beginning


Inserting a node at the end:
The following steps are followed to insert a new node at the end of the list:
• Get the new node using getnode().
newnode = getnode();
• If the list is empty, assign new node as start.
start = newnode;
newnode -> next = start;
• If the list is not empty follow the steps given below:
temp = start;
while(temp -> next != start)
temp = temp -> next;
temp -> next = newnode;
newnode -> next = start;
The function cll_insert_end(), is used for inserting a node at the end.
Fig 2.11 shows inserting a node into the circular single linked list at the end.

Fig.2.11 Inserting node at the end


Deleting a node at the beginning:
The following steps are followed, to delete a node at the beginning of the list:
• If the list is empty, display a message ‘Empty List’.
• If the list is not empty, follow the steps given below:
last = temp = start;
while(last -> next != start)
last= last -> next;
start = start -> next;
last -> next = start;
• After deleting the node, if the list is empty then start = NULL.
The function cll_delete_beg(), is used for deleting the first node in the list. Fig 2.12
shows deleting a node at the beginning of a circular single linked list.

Fig 2.12 Deleting a node at the beginning

Deleting a node at the end:


The following steps are followed to delete a node at the end of the list:
• If the list is empty, display a message ‘Empty List’.
• If the list is not empty, follow the steps given below:
temp = start;
prev = start;
while(temp -> next != start)
{
prev=temp;
temp = temp -> next;
}
prev -> next = start;
• After deleting the node, if the list is empty then start = NULL.
The function cll_delete_last(), is used for deleting the last node in the list.Fig 2.13 shows
deleting a node at the end of a circular single linked list.

2.13 Deleting the node at the end


Traversing a circular single linked list from left to right:

The following steps are followed, to traverse a list from left to right:
• If list is empty then display ‘Empty List’ message.
• If the list is not empty, follow the steps given below:
temp = start;
do
{
printf("%d", temp -> data);
temp = temp -> next;
} while(temp != start);

2.7 Doubly linked list


Doubly linked list: The Doubly linked list is a collection of nodes each of which consists of
three parts namely the data part, prev pointer and the next pointer. The data part stores the value
of the element, the prev pointer has the address of the previous node and the next pointer has the
value of the next node.

Fig 2.14 Doubly Linked List

In a doubly linked list, the head always points to the first node. The prev pointer of the
first node points to NULL and the next pointer of the last node points to NULL shown in
Fig.2.14

Operations on a Doubly linked list are,


 Count the number of elements.
 Add an element at the beginning of the list.
 Add an element at the end of the list.
 Insert an element at the specified position in the list.
 Delete an element from the list.
 Display all the elements of the list.

Addatbeg(x)
The operation Addatbeg(x) adds a given element x at the beginning of the list. A new
node R is created and the value x is store in the data part of R. The prev pointer of R is made to
point to NULL and the next pointer is made to point to head. Then the prev pointer of head is
made to point to R and head is now moved to point to R making it the first node. Thus the new
node is added at the beginning of the doubly linked list.
Algorithm

Function create()
Begin
temp=new node
Write"Enter the data:"
Read temp->data
End

Function insert_begin()
Begin
Call create()
tmp->flink=head
head=tmp
head->blink=NULL
ct=ct+1
End

Addatend(x)
The Addatend(x) operation adds the given element x at the end of the doubly linked list.
If the given list is empty then create a new node R and store the value of x in the data part of R.
Now make the prev pointer and the next pointer of R point to NULL. Then head is pointed to R.
If the list already contains some elements then, a temporary pointer is created and made to point
to head. The temp pointer is now moved to the last node and then a new node R is created. The
value x is stored in the data part of R and next pointer of R is made to point to NULL. The prev
pointer of R is made to point to temp and next pointer of Temp is made to point to R. Thus the
new node is added at the end of the doubly linked list is shown in fig.2.15.
Algorithm
Function append()
Begin
if(head=NULL)
then

insert_begin()
else
create()
temp->flink=NULL
prev=head
while(prev->flink< >NULL)
Do
prev=prev->flink
End while
prev->flink=temp
temp->blink=prev
ct=ct+1
End if
End

Fig. 2.15 Add at end

Insert(x, k)
The Insert(x) operation inserts a given element x at the specified position k. A temporary
pointer is created and made to point to head. Then it is moved to the k-1 th node. Now a new
node R is created and the data part is stored with value of x. The next pointer of R is made to
point to next(temp) and the prev pointer of next(temp) is made to point to R. Thus the links on
the right side of the new node is established. Now the next of Temp is made to point to R and
the prev pointer of R is made to point to temp thus establishing the links on the left side of the
new node. Now the new node is inserted at the position k is shown in Fig.2.16.
Algorithm
Function insertin_mid()
Begin
Write "Enter the position:"
Read pos;
If head->flink=NULL AND pos=1
then
Call insert_begin()
End if
If head->flink==NULL AND pos=2
then
Call append()
End if
Else if pos>=2 AND pos<=ct
then
prev=head
for I = 2 to pos - 1 step +1
do
prev=prev->flink
END FOR
next=prev->link
temp=new node
Write”Enter the data:"
Read temp->data
temp->flink=next
prev->flink=temp
temp->blink=prev;
next->blink=temp;
ct=ct+1
else
Write "Enter a valid position & try again"
End if
End
Fig.2.16 Insert at mid
Delete(x)

The Delete(x) operation deletes the element x from the doubly linked list. A temporary
pointer is created and made to point to head. Now the data of temp is compared with x and if it
matches that is the node to be deleted otherwise move to the next node and again compare. If the
node to be deleted is first node, then prev(next(temp)) is made to point to NULL and head is
pointed to next(temp). The node pointed by temp is deleted. When the node to be deleted is not
the first node, then next(prev(temp)) is made to point to next(temp) and prev(next(temp)) is
made to point to prev(temp). The node pointed by temp is deleted is shown in Fig.2.17.
Algorithm
Function del()
Begin
if(head= NULL)
then
cout<<"Empty list"
else
Write Enter the position\n"
Read pos
pre=head
if(pos<1 OR pos>ct)
Write Enter a valid position"
else
if(pos==1)
then
pre=pre->flink
head=pre
Write "node gets deleted\n"
Ct=ct -1
else
for(i=2;i<pos;i++)
pre=pre->flink
End For
tmp=pre->flink
nxt=tmp->flink
pre->flink=nxt
nxt->blink=pre
Write”node gets deleted\n"
Ct=ct -1
End if
End if

Fig.2.17 Deletion

2.8 Circular Doubly Linked List.


A circular double linked list has both successor pointer and predecessor pointer in
circular manner. The objective behind considering circular double linked list is to simplify the
insertion and deletion operations performed on double linked list. In circular double linked list
the right link of the right most node points back to the start node and left link of the first node
points to the last node. A circular double linked list is shown in fig 2.18.

Fig 2.18 Circular Doubly Linked List


The basic operations in a circular double linked list are:
• Creation.
• Insertion.
• Deletion.
• Traversing.

Create node
create_node(int info)
begin
new->val = info;
new->next = NULL;
new->prev = NULL;
return new;
end

Add Node at the end


Procedure add_node()
begin
Read info
  if (first == last && first == NULL) // If list is empty
begin
  first = last = new;
first->next = last->next = NULL;
first->prev = last->prev = NULL;
endif
Else // add the new node at the end
begin
last->next = new;
new->prev = last;
last = new;
last->next = first;
first->prev = last;
end
end

  INSERTS ELEMENT AT FIRST


 
insert_at_first()
begin
Read info
  new = create_node(info); // create the new node
  if (first == last && first == NULL) // if the list is empty
begin
first = last = new;
first->next = last->next = NULL;
first->prev = last->prev = NULL;
end
else
begin
new->next = first;
first->prev = new;
first = new;
first->prev = last;
last->next = first;
end
end

INSERTS ELEMNET AT END 

insert_at_end()
begin
  Read info;
  new = create_node(info);
  if (first == last && first == NULL)
begin
first = last = new;
first->next = last->next = NULL;
first->prev = last->prev = NULL;
endif
else
begin
last->next = new;
new->prev = last;
last = new;
first->prev = last;
last->next = first;
endif
end

INSERTS THE ELEMENT AT GIVEN POSITION


 
insert_at_position()
begin
Declare info, pos, len = 0, i;
Node *prevnode;
  Read info and pos
new = create_node(info);
  if (first == last && first == NULL)
begin
if (pos == 1)
begin
first = last = new;
first->next = last->next = NULL;
first->prev = last->prev = NULL;
endif
else
printf " empty linked list you cant insert at that particular position"
endif
else
begin
if (number < pos) // total number of node is stored in the variable number
print “ node cant be inserted as position is exceeding the linkedlist length"
  else

for (ptr = first, i = 1;i <= number;i++)


begin
prevnode = ptr;
ptr = ptr->next;
if (i == pos-1)
begin
prevnode->next = new;
new->prev = prevnode;
new->next = ptr;
ptr->prev = new;
print "inserted at position is succesfully"
break;
end
end
end
end
end
Deletion
delete_node_position()
begin
int pos, count = 0, i;
n *temp, *prevnode;
read the position which u wanted to delete

if (first == last && first == NULL)


print " empty linked list you cant delete"
  else
begin
if (number < pos)
print " node cant be deleted at position as it is exceeding the linkedlist length"
  else
begin
for (ptr = first,i = 1;i <= number;i++)
begin
prevnode = ptr;
ptr = ptr->next;
if (pos == 1)
begin
number--;
last->next = prevnode->next;
ptr->prev = prevnode->prev;
first = ptr;
printf("%d is deleted", prevnode->val);
free(prevnode);
break;
end
else if (i == pos - 1)
begin
number--;
prevnode->next = ptr->next;
ptr->next->prev = prevnode;
printf("%d is deleted", ptr->val);
free(ptr);
break;
end
end
end
end
end

Searching

search()
begin
int count = 0, key, i, f = 0;
  read the value to be searched in the variable key
  if (first == last && first == NULL)
print "list is empty no elemnets in list to search"
else
for (ptr = first,i = 0;i < number;i++,ptr = ptr->next)
begin
count++;
if (ptr->val == key)
begin
Print " the value is found at position count ”
f = 1;
end
end
if (f == 0)
print "the value is not found in linkedlist"
end
end

DISPLAYING IN BEGINNING
 Algorithm

display_from_beg()
begin
int i;
if (first == last && first == NULL)
print "list is empty no elemnts to print"
else
begin
Store total number of node in the variable , number
for (ptr = first, i = 0;i < number;i++,ptr = ptr->next)
print ptr->val
end
end

You might also like