Linked Listnotes
Linked Listnotes
A linked list is the most sought-after data structure when it comes to handling dynamic data
elements. A linked list consists of a data element known as a node. And each node consists of
two fields: one field has data, and in the second field, the node has an address that keeps a
reference to the next node.
• A linked list is a linear data structure that stores a collection of data elements
dynamically.
• Nodes represent those data elements, and links or pointers connect each node.
• Each node consists of two fields, the information stored in a linked list and a pointer
that stores the address of its next node.
• The last node contains null in its second field because it will point to no node.
• A linked list can grow and shrink its size, as per the requirement.
This representation of a linked list depicts that each node consists of two fields. The first field
consists of data, and the second field consists of pointers that point to another node.
Here, the start pointer stores the address of the first node, and at the end, there is a null pointer
that states the end of the Linked List.
struct node
int data;
It is a declaration of a node that consists of the first variable as data and the next as a pointer,
which will keep the address of the next node.
Here you need to use the malloc function to allocate memory for the nodes dynamically.
Advantages of an array
1. We can access any element of an array directly means random access is easy
2. It can be used to create other useful data structures (queues, stacks)
3. It is light on memory usage compared to other structures
Disadvantages of an array
1. Its size is fixed
2. It cannot be dynamically resized in most languages
3. It is hard to add/remove elements
4. Size of all elements must be same.
5. Rigid structure (Rigid = Inflexible or not changeable)
A singly linked list is the most common type of linked list. Each node has data and an address
field that contains a reference to the next node.
There are two pointer storage blocks in the doubly linked list. The first pointer block in each
node stores the address of the previous node. Hence, in the doubly linked inventory, there are
three fields that are the previous pointers, that contain a reference to the previous node. Then
there is the data, and last you have the next pointer, which points to the next node. Thus, you can
go in both directions (backward and forward).
III. Circular Linked List
The circular linked list is extremely similar to the singly linked list. The only difference is that
the last node is connected with the first node, forming a circular loop in the circular linked list.
• The next node's next pointer will point to the first node to form a singly linked list.
• The previous pointer of the first node keeps the address of the last node to form a
doubly-linked list.
In this operation, you will display all the nodes in the linked list.
When the temp is null, it means you traversed all the nodes, and you reach the end of the linked
list and get out from the while loop.
}
Insertion
NewNode=malloc(sizeof(struct node));
start= NewNode;
• Change the next pointer of the last node to the newly created node.
NewNode=malloc(sizeof(struct node));
NewNode->next = NULL;
While(i<pos){
i++
}}
• Change the next pointer to exclude the node from the linked list.
While(i<pos-1){
i++
};
while (n != NULL) {
n = n->next;
newnode->next = head;
OR
tail=newnode
}
temp=head;
While(i<pos-1)
i++
void DeleteAtFirst()
free(temp);
void DeleteAtLast()
{ node *temp;
If(tail==0)
Linklist is empty
temp = tail;
tail=tail->next
free temp;
The first part contains data and the second part points to the next node. It contains the address of
the next node in the actual memory.
If(Last==0)
{
Last=newnode
Last->next=newnode;
Else{
newnode->next = Last->next;
If(Last==0)
Last=newnode;
Last->next=newnode;
Else
newnode->next=Last->next;
Last->next=newnode;
Last=newnode;
if(pos<0||pos<length)
invalid position
elseif (pos==1)
Insert atbeg()
Else
{temp=Last->next;
While(i<pos-1)
temp=temp->next;
i++;
}
newnode->next=temp->next;
temp->next=newnode;
void deleteAtFirst()
temp=Last->next
if(Last==NULL)
list is empty
elseif(temp->next==temp)
{Last=0;
Free(temp)
Else
{Last->next=temp->next;
Free(temp)
{ node *temp1,temp2;
temp=Last->next
if(Last==NULL)
list is empty
elseif(temp->next==temp)
{Last=0;
Free(temp)
Else
{ While(temp1->next!=Last->next)
temp2=temp1;
temp1=temp1->next;
temp2->next=Last->next;
Last=temp2;
Free(temp1);
if(pos<0||pos<length)
invalid position
elseif (pos==1)
Insert atbeg()
Else{
While(i<pos-1)
temp1=temp1->next;
i++;
Temp2=temp1->next;
temp1->next=temp2->next;
free(temp2);
o The first part contains the value of the coefficient of the term.
o The second part contains the value of the exponent.
o The third part, LINK points to the next term (next node).
The structure of a node of a linked list that represents a polynomial is shown below:
Consider a polynomial P(x) = 7x2 + 15x3 - 2 x2 + 9. Here 7, 15, -2, and 9 are the coefficients, and
4,3,2,0 are the exponents of the terms in the polynomial. On representing this polynomial using a
linked list, we have
Observe that the number of nodes equals the number of terms in the polynomial. So we have 4
nodes. Moreover, the terms are stored to decrease exponents in the linked list. Such representation
of polynomial using linked lists makes the operations like subtraction, addition, multiplication,
etc., on polynomial very easy.
1. Addition of Polynomials:
To add two polynomials, we traverse the list P and Q. We take corresponding terms of the list P
and Q and compare their exponents. If the two exponents are equal, the coefficients are added to
create a new coefficient. If the new coefficient is equal to 0, then the term is dropped, and if it is
not zero, it is inserted at the end of the new linked list containing the resulting polynomial. If one
of the exponents is larger than the other, the corresponding term is immediately placed into the
new linked list, and the term with the smaller exponent is held to be compared with the next term
from the other list. If one list ends before the other, the rest of the terms of the longer list is inserted
at the end of the new linked list containing the resulting polynomial.
Let us consider an example an example to show how the addition of two polynomials is performed,
Q (x) = 5x3 + 4 x2 - 5
These polynomials are represented using a linked list in order of decreasing exponents as follows:
To generate a new linked list for the resulting polynomials that is formed on the addition of given
polynomials P(x) and Q(x), we perform the following steps,
1. Traverse the two lists P and Q and examine all the nodes.
2. We compare the exponents of the corresponding terms of two polynomials. The first term
of polynomials P and Q contain exponents 4 and 3, respectively. Since the exponent of the
first term of the polynomial P is greater than the other polynomial Q, the term having a
larger exponent is inserted into the new list. The new list initially looks as shown below:
3. We then compare the exponent of the next term of the list P with the exponents of the
present term of list Q. Since the two exponents are equal, so their coefficients are added
and appended to the new list as follows:
4. Then we move to the next term of P and Q lists and compare their exponents. Since
exponents of both these terms are equal and after addition of their coefficients, we get 0,
so the term is dropped, and no node is appended to the new list after this,
5. Moving to the next term of the two lists, P and Q, we find that the corresponding terms
have the same exponents equal to 0. We add their coefficients and append them to the new
list for the resulting polynomial as shown below:
Polynomial Multiplication: