(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
module 3-.pptx
1. MODULE-
3 LINKED
LIST
Department of ISE Acharya Institute of Technology
Mrs.Jimsha K Mathew
Assistant Professor
Department of ISE
Acharya Institute Of Technology
2. MODULE
3
TOPICS
Linked Lists: Definition, Representation of linked lists in Memory, Memory
allocation; Garbage Collection. Linked list operations: Traversing, Searching,
Insertion, and Deletion. Doubly Linked lists, Circular linked lists, and header linked
lists. Linked Stacks and Queues. Applications of Linked lists – Polynomials, Sparse
matrix representation. Programming Examples
Department of ISE Acharya Institute of Technology
3. LINKED LIST
• Linked List is a linear data structure.
• linked list elements are not stored at a contiguous location; the
elements are linked using pointers.
• Linked list is a collection of nodes linked together in a sequential way.
• Each node contains a data field and an address field which contains the
reference (address)of the next node.
4. Advantages of Linked Lists over Arrays
1.Efficient Memory Utilization: The memory of a linked list is not
pre-allocated. Memory is allocated whenever required and de-
allocated when it is no longer required.
2.Insertion and deletion operations are easier and efficient.
3.Manipulations: Without any prior idea of memory space available
we can perform the computations. Example, there is no “overflow
condition” while implementing stacks or queues.
4.Data can be stored in non-contiguous blocks of memory which
exhibits logical adjacency i.e. they are linked by pointers.
Department of ISE Acharya Institute of Technology
5. Disadvantages of Linked Lists
1.It requires extra space because each node requires to hold the
address of the next node within it (i.e. a pointer field).
2.Accessing a particular node in the list may take more time
compared with arrays because the list needs to be traversed from
the beginning.
Department of ISE Acharya Institute of Technology
6. Singly-linked lists vs. 1D-arrays
ID-array Singly-linked list
Fixed size: Resizing is expensive Dynamic size
Insertions and Deletions are inefficient: Elements
are usually shifted
Insertions and Deletions are efficient: No shifting
Random access i.e., efficient indexing No random access
Not suitable for operations requiring
accessing elements by index such as sorting
No memory waste if the array is full or almost
full; otherwise may result in much memory waste.
Extra storage needed for references; however uses
exactly as much memory as it needs
Sequential access is faster because of greater
locality of references [Reason: Elements in
contiguous memory locations]
Sequential access is slow because of low locality
of references [Reason: Elements not in contiguous
memory locations]
7. Representation
• Each node in a list consists of at least two parts:
1) Data/info
2) Pointer /link (Or Reference) to the next node
• The first node is called the head. If the linked list is empty, then the value
of the head is NULL.
• The last node in the linked list can be identified because its next portion
points to NULL.
8. Linked List Data Structure
A linked list is a linear data structure, in which the elements are not stored at contiguous
memory locations. The elements in a linked list are linked using pointers as shown in the
below image:
9. Representing linked list in C
The following capabilities are needed to make linked representation
possible
•A mechanism to define the nodes structure.
This is done using self-referential structures.
•A way to create new nodes when we need them.
This is done using malloc( ) function.
•A way to remove nodes that we no longer need.
This is done using free( ) function.
Department of ISE Acharya Institute of Technology
10. Types of Linked lists
Following are the various types of linked list.
Singly Linked List − Item Navigation is forward only.
Doubly Linked List − Items can be navigated forward and backward way.
Circular Linked List − Last item contains link of the first element as next and and first
element has link to last element as prev.
11. linked list consists of nodes where each node contains a data field and a
reference(link) to the next node in the list.
Such a structure can be represented as:
struct node
{
int data;
struct node *next;
}
12. In C we can represent a node using a self referential structure.
struct node
{ int data;
struct node* next;
};
Where data is the data you want to store in list. *next is pointer to the
same structure type. The *next will store location of next node if exists
otherwise NULL.
Declare a pointer to node type
struct node *head,*one, *two ,*three ;
Representation
in C
13. • /* Allocate memory */
• one = (struct node *)malloc(sizeof(struct node));
• two = (struct node *)malloc(sizeof(struct node));
• three=(struct node *)malloc(sizeof(struct node));
• All the three pointers will contain address of newly created nodes using
malloc() function
One=100 Two=200
Three=300
16. • /* Save address of first node in head */
head = one;
17. Linked list operations
• Node Creation
• Insertion
• Insertion at beginning
• Insertion after a specified node
• Insertion at the end
• Deletion
• Deletion from beginning
• Deletion after a specified node
• Deletion from the end
• Traversal
• Search
18. Singly Linked Lists
• Linked list is a linear collection of data elements called nodes
and there exists a logical relationship between nodes (i.e. given
the address of first node, any node in that list can be obtained).
• Forward direction only
The pictorial representation of a singly linked list is as shown below.
Department of ISE Acharya Institute of Technology
19. Each and every node has 2 fields.
•Info /data field – Here some useful information can be stored.
•Link /pointer field – This field contains address of the next node. So
this field should be of type pointer.
Note: A chain is a singly linked list that is compromised of zero or
more nodes. When the number of nodes is zero, the chain is empty.
The nodes of a chain are ordered so that the first node links to the
second, second to the third and so on. The last node of a chain has
a zero link (NULL).
Department of ISE Acharya Institute of Technology
20. Note:
Observations:
•head = NULL => List is empty.
•head ->next = NULL => There is one element in the List
•head ->next ! = NULL => There is more than one element in the
List
Department of ISE Acharya Institute of Technology
21. 1. Function to create a n-
node list
void nodecreate()
{
Head=(struct node*)malloc(sizeof(structnode));
Head->data=data;
Head->next=null ;
temp=Head
for(i=2;i<=n;i++)
{
newnode=(struct node*)malloc(sizeof(struct node);
newnode->data=data
newnode->next=null;
temp->next=newnode;
temp=temp->next;
}
}
struct node
{
int data;
struct node *next;
} *Head,*newnode,*temp;
22. 2(a). Insertion In singly linked list-Insertion at beginning
ALGORITHM:
• Allocate memory for new node
• Store data
• Change next of new node to point to head
• Change head to point to recently created node
23. Function to insert an element at the front end
Void insert_front()
{
temp = (struct node*)malloc(sizeof(struct node));
temp->data = item;
temp->next = head;
head=temp;
}
Department of ISE Acharya Institute of Technology
struct node
{
int data;
struct node *next;
} *head, *temp;
24. Insertion at theend
1. Allocate memory for new node
2. Store data
3. Traverse to last node
4. Change next of last node to recently created
node
25. 2.(b) Function to insert an element at the rear(back) end
void insert_rear()
{
newnode=(struct node*)malloc(sizeof(struct node));
newnode ->data = item;
newnode ->next = NULL;
if(head = = NULL) //no node
{
head=newnode;
}
else {
temp = head; // node exists
while(temp->next != NULL)
{
temp= temp->next;
}
temp->next = newnode;
} Department of ISE Acharya Institute of Technology
struct node
{
int data;
struct node *next;
} *head, *temp,
*newnode;
26. 2(c).Insert at a specified position
Algorithm:
• Allocate memory and store data for new node
• Traverse to node just before the required position of new node
• Change next pointers to include new node in between
27. • Step1 : create a new node
• Step 2: Traverse to the n-1th position of the linked list and connect the new
node with the n+1th node.
newNode->next = temp->next , where temp is the n-1th node.
28. • Step 4: Now at last connect the n-1th node with the new node i.e.
the n-1th node will now point to new node.
temp->next = newNode
29. Function to insert an item at a specified position(after a specified node)
void insert_pos( )
{
newnode=(struct node)malloc(sizeof(struct node));
newnode ->data = item;
newnode ->next = NULL;
temp=head;
if(head = = NULL && loc = = 1)// no elements in the list
head=newnode;
If else (loc= = 1) // insert at the beginning
{
newnode->next= head;
head=newnode;
}
}
Department of ISE Acharya Institute of Technology
else
{
for(i=1;i<loc;i++)
{
temp = temp->next;
}
newnode ->next =temp->next;
temp ->next = newnode;
}
}
34. 3(a).Function to delete an element at the front end
void delete_front()
{
if(head= = NULL) // no node
{
printf("list is emptyn");
}
else if(head ->next = = NULL) // only one
node
{
head=NULL;
free(head);
}
else
temp = head;
head= temp->next;
free(temp);
}
Department of ISE Acharya Institute of Technology
struct node
{
int data;
struct node *next;
} *head, *temp,
*newnode;
35. Deleting from the end
Steps
1. Search the Node to be deleted and call it temp
2. Store previous node call is as previous
3. Assign previous->next to temp->next
4. In this case, the temp->next will be Null so no difference from deletion at middle
5. Free(temp)
37. 3(b).Function to delete an element at the rear
end
void delete_rear()
{
if(head = = NULL) // no
node
{ printf("list is emptyn");
}
if(head ->next = = NULL) // only
one node
{
head=NULL;
free(head);
}
else {
temp = head;
while(temp->next != NULL)
}
{
ptr1 = temp;
temp = temp ->next;
}
ptr1->next = NULL;
free(temp);
}
}
Department of ISE Acharya Institute of Technology
struct node
{
int data;
struct node *next;
} struct node *head,
*temp, *ptr1;
38. Department of ISE
3(c).Function to delete a node from a specified location is
given
void delete_specloc( )
{
if(head = = NULL) // empty list
{
printf("the list is emptyn");
}
else
temp=head;
Acharya Institute of
T}echnology
for(i=1;i<loc;i++) {
ptr1=temp;
temp=temp->next;
}
ptr1->next=temp->next;
free(temp);
}
39. Function to display/Traverse the contents of the list
void display( )
{
if(head = = NULL)
{
printf("the list is emptyn");
}
else {
temp = head;
printf("the contents of the list are:n");
while(temp!= NULL)
{
printf("%d",temp->data);
temp= temp->next;
}
}}
Department of ISE Acharya Institute of Technology
40. Note:
1.To implement stacks using linked lists the functions required
are
•Insert_front( ), Delete_front and Display( ) OR
•Insert_rear( ), Delete_rear and Display( )
2.To implement queues using linked lists the function required
are
•Insert_front( ), Delete_rear and Display( ) OR
•Insert_rear( ), Delete_front and Display( )
3.To implement De-queues using linked lists the function
required are Insert_front( ), Delete_front, Insert_rear( ),
Delete_rear and Display( )
Department of ISE Acharya Institute of Technology
41. Other operations of a linked list
•Function to count the number of nodes in a list
int countnodes( )
{
int count = 0;
temp= head;
while(temp!= NULL)
{
count = count + 1;
temp = temp->next;
}
return count;
}
Department of ISE Acharya Institute of Technology
42. • Function to concatenate 2 lists
int concatenate( )
{
if(firsthead = = NULL)
return secondhead;
if(secondhead = = NULL)
return firsthead;
temp = firsthead;
while(temp->next!= NULL)
{
temp = temp->next;
}
temp->next = secondhead;
}
Department of ISE Acharya Institute of Technology
43. Function to search for an item in the list and display the
position if found
void search_item( )
{
int pos;
if(head= = NULL)
{
printf(“list is emptyn”);
return;
}
temp = head;
pos = 1;
while(temp != NULL && item ! =temp->data)
{
temp = temp->next;
pos = pos + 1;
}
if(temp = = NULL)
{
printf(“Unsuccessful searchn”);
return;
}
printf(“Successful search and item found at position %dn”, pos);
}
Department of ISE Acharya Institute of Technology
44. Disadvantages of Singly Linked List
•Given the address of a node in the list, it is difficult to find the
address of previous node.
•Traversing of list is possible only in the forward direction and
hence singly linked list is also termed as one-way list.
Doubly Linked List Representation
To increase the performance and efficiency of algorithms, it is required
to traverse the list in either forward or backward direction. Therefore a
two-way list called as doubly linked list can be made use of, so that
traversing from left to right (forward) or right to left (backward) is
possible.
Department of ISE Acharya Institute of Technology
45. DOUBLY LINKED LIST
Doubly linked lists
Each node points to not only successor but the predecessor
There are two NULL: at the first and last nodes in the list
It is a way of going both directions in a linked list, forward and reverse.
Many applications require a quick access to the predecessor node of some node in
list.
A
Head
B
C
46. Advantages over Singly-linked Lists
Quick update operations:
such as: insertions, deletions at both ends (head and tail), and also at the
middle of the list.
A node in a doubly-linked list store two references:
•A next link; that points to the next node in the list, and
•A prev link; that points to the previous node in the list.
47. REPRESENTATION OF A NODE IN DOUBLY LINKED
LIST
struct Node
{
struct Node *prev;
int data;
struct Node *next;
} * head;
48. Each node in the list consists of
•prev – Contains the address of the left node or previous
node
•data – It is the data to be processed.
•next – Contains the address of the right node or next
node
The structure declaration for each node is as shown
below struct node
{
int data;
struct node
*prev; struct
node *next;
};
Department of ISE Acharya Institute of Technology
49. In a singly linked list, we could traverse only in one direction, because each node
contains address of the next node and it doesn't have any record of its previous nodes.
However, doubly linked list overcome this limitation of singly linked list.
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.
51. Operations on doubly linked list
Node Creation (single node)
Insertion at the beginning
Insertion at the end
Insertion after a specified location
Deletion from beginning
Deletion from end
Deletion from a specified location
Traversal
searching
53. Function to insert an element at the front end of
DLL:
void insertfront ( )
{
newnode=(struct node*)malloc(sizeof(struct
node)); newnode ->data = item;
newnode ->prev = NULL;
newnode->next = NULL;
if(head = = NULL)
return newnode;
newnode ->next = head;
head->prev = newnode;
head=newnode;
return newnode;
}
Department of ISE Acharya Institute of Technology
struct node
{
struct node *prev;
int data;
struct node *next;
} * head,*newnode;
54.
55. Function to insert an element at the rear end
void insertrear( )
{
newnode = (struct node*)malloc(sizeof(struct
node)); newnode ->data = item;
newnode ->prev=NULL;
newnode ->next = NULL;
if(head = = NULL)
return newnode ;
temp= head;
while(temp->next != NULL) {
temp = temp->next;
}
temp->next = newnode ;
newnode ->prev = temp;
}
Department of ISE Acharya Institute of Technology
59. 10 20 30
200 300
300
200
100
NULL
temp=head
NUL
L
temp = head;
head = head -> next;
head -> prev = NULL;
free(temp);
head
NUL
L
200
60. void beginning_delete()
{
if(head == NULL)
{
printf("n List is empty”);
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("nNode Deletedn");
}
else
{
temp = head;
head = head -> next;
head -> prev = NULL;
free(temp);
printf("nNode Deletedn");
} }
DELETION AT THE BEGINNING
61. Deletion of the last node in a doubly linked list needs traversing the
list in order to reach the last node of the list and then make pointer
adjustments at that position.
In order to delete the last node of the list, we need to follow the
following steps.
•If the list is already empty then the condition head == NULL will
become true and therefore the operation can not be carried on.
•If there is only one node in the list then the condition head →
next == NULL become true. In this case, we just need to assign
the head of the list to NULL and free head in order to completely
delete the list
DELETION AT THE END
62. 10 20 30
200 NUL
L
300
200
100
NULL
ptr=head
NUL
L
ptr -> prev -> next = NULL;
free(ptr);
ptr
100 200
63. void last_delete()
{
if(head == NULL)
{
printf("n UNDERFLOWn");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
}
DELETION AT THE END
else
{
ptr = head;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
}
}
66. Suppose you want to delete the second node from the list.
Start traversing the linked list from the head until the position = 2
of the node to be deleted.
Let the node at the position 2 of the list be temp.
Assign the next pointer of temp to temp's previous
node's next pointer.
Assign the temp's prev pointer to temp's next node's prev pointer.
Delete the temp node.
68. Function to Traverse /display the contents of the
list
void display( )
{
if(head = = NULL)
{
printf("the list is emptyn");
return;
}
printf("the contents of the list are:n");
temp = head;
while(temp != NULL)
{
printf("%dn",temp->data);
temp= temp->next;}
}
Department of ISE Acharya Institute of Technology
69. Function to search for an item in the DLL and display the
position if found
void search_item( )
{
int pos;
if(head= = NULL)
{
printf(“list is emptyn”);
return;
}
temp = head;
pos = 1;
while(temp != NULL && item ! =temp->data)
{
temp = temp->next;
pos = pos + 1;
}
if(temp = = NULL)
{
printf(“Unsuccessful searchn”);
return;
}
printf(“Successful search and item found at position %dn”, pos);
}
Department of ISE Acharya Institute of Technology
71. 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.
• We can have circular singly linked list as well as circular doubly linked list.
• We traverse a circular singly linked list until we reach the same node where we
started.
• The circular singly liked list has no beginning and no ending.
• There is no null value present in the next part of any of the nodes.
• The following image shows a circular singly linked list.
72.
73. Circular singly linked list
Circular linked list are mostly used in task maintenance in operating systems.
There are many examples where circular linked list are being used in computer science
including browser surfing where a record of pages visited in the past by the user, is
maintained in the form of circular linked lists and can be accessed again on clicking the
previous button.
74. Circular Singly Linked List
In normal singly linked list,
•The link part of the last node has NULL value, specifying that it is the
last node.
•Given the address of any node ‘x’, it is only possible to reach the nodes
which follow ‘x’ and it is not possible to reach the nodes that precedes
‘x’ (previous nodes). To reach the nodes that precedes ‘x’, it is required
to preserve a pointer variable first, which contains the address of the
first node in the list and use this for traversing.
•Also, given the address of the first node to get the address of the last
node, the list has to be traversed from the beginning till the last node is
reached.
Department of ISE Acharya Institute of Technology
75. These disadvantages can be overcome using circular singly linked
list. In a circular singly linked list the link field of the last node points
to the first node in the list, which makes the list a circular one. The
pictorial representation is as shown below.
Department of ISE Acharya Institute of Technology
76. OPERATIONS OF CIRCULAR SINGLY LINKED LIST(CSLL):
Department of ISE Acharya Institute of Technology
NODE CREATION
INSERTION
DELETION
TRAVERSING/DISPLAY
SEARCHING
77. NODE CREATION:
•Here the link field of the last node contains address of the first node.
•In the following functions we use the structure definition as shown
below. struct node
{
int data;
struct node *next;
}*head;
Department of ISE Acharya Institute of Technology
80. void beg_insert(int item)
{
newnode= (struct node *)malloc(sizeof(struct n
ode));
newnode-> data = item;
if(head == NULL) //no nodes exist
{
head = newnode;
newnode -> next = head;
}
else
{
temp = head;
while(temp->next != head)
{
temp = temp->next;
}
newnode->next = head;
temp -> next = newnode;
head = newnode;
}
}
Insertion into circular singly linked list at beginning
81. Function to insert an element at the rear end-
CSLL
Department of ISE Acharya Institute of Technology
void lastinsert( )
{
newnode = (struct node *)malloc(sizeof(struct nod
e));
newnode->data = item;
if(head == NULL)
{
head = newnode;
newnode-> next = head;
}
else
{
temp = head;
while(temp -> next != head)
{
temp = temp -> next;
}
temp -> next = newnode;
newnode -> next = head;
}
}
82.
83. Function to insert an item at a specified position(after a specified node) -
CSLL
void insert_pos( )
{ newnode=(struct node)malloc(sizeof(struct node));
newnode ->data = item;
temp=head;
if(head = = NULL && loc = = 1)// no elements in the list
head=newnode;
Newnode->next=head
}
Department of ISE Acharya Institute of Technology
else
{
for(i=1;i<loc;i++)
{
temp = temp->next;
}
newnode ->next =temp->next;
temp ->next = newnode;
}
}
86. Function to display/Traverse the contents of the list
IN CSLL
void display( )
{
if(head = = NULL)
{
printf("the list is emptyn");
}
else {
temp = head;
printf("the contents of the list are:n");
while(temp!= head)
{
printf("%d",temp->data);
temp= temp->next;
}
}} Department of ISE Acharya Institute of Technology
87. Function to search for an item in the CSLL and display the
position if found
void search_item( )
{
int pos;
if(head= = NULL)
{
printf(“list is emptyn”);
return;
}
temp = head;
pos = 1;
while(temp ->next != head && item ! =temp->data)
{
temp = temp->next;
pos = pos + 1;
}
if(temp->next = = head)
{
printf(“Unsuccessful searchn”);
return;
}
printf(“Successful search and item found at position %dn”, pos);
}
Department of ISE Acharya Institute of Technology
95. Linked list implementation of stack
Instead of using array, we can also use linked list to implement stack. Linked list allocates the memory
dynamically. However, time complexity in both the scenario is same for all the operations i.e. push, pop
and peek.
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 overflow if the
space left in the memory heap is not enough to create a node.
97. Adding a node/element to the stack (Push operation)
Algorithm
Step 1 - Create a newNode with given value.
Step 2 - Check whether stack is Empty (top == NULL)
Step 3 - If it is Empty, then set newNode → next = NULL.
Step 4 - If it is Not Empty, then set newNode → next = top.
Step 5 - Finally, set top = newNode.
Time Complexity : o(1)
100. Deleting a node/element from the stack (POP operation)
Algorithm:
Step 1 - Check whether stack is Empty (top == NULL).
Step 2 - If it is Empty, then display "Stack is Empty!!! Deletion is not possible!!!" and terminate the function
Step 3 - If it is Not Empty, then define a Node pointer 'temp' and set it to 'top'.
Step 4 – Decrement top.
Step 5 - Finally, delete 'temp'. (free(temp)).
Time Complexity : o(n)
101.
102. void pop()
{
int item;
if (top == NULL)
{
printf("Underflow");
}
else
{
item = top->data;
temp= top;
top = top->next;
free(temp);
}
}
103. 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 stack. For this purpose, we need to follow the following steps.
Copy the head pointer into a temporary pointer.
Move the temporary pointer through all the nodes of the list and print the value field
attached to every node.
Time Complexity : o(n)
105. Queue implementation Using Linked List
Queue using an array is not suitable when we don't know the size of data which we are going to use.
A queue data structure can be implemented using a linked list data structure.
The queue which is implemented using a linked list can work for an unlimited number of values.
In linked list implementation of a queue, the last inserted node is always pointed by 'rear' and the first
node is always pointed by 'front'.
106.
107. enQueue(value) - Inserting an element into the Queue
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
109. deQueue() - Deleting an Element from Queue
Step 1: IF FRONT = NULL
Write " Underflow "
Go to Step 5
[END OF IF]
Step 2: SET PTR = FRONT
Step 3: SET FRONT = FRONT -> NEXT
Step 4: FREE PTR
Step 5: END
112. Polynomial – Using Singly
Linked List Representation
Consider the polynomial 9x3 + 7x2 + 6x + 9. This polynomial can be
represented using a singly linked list as shown below.
Every node has 3
fields cf – coefficient
field
px – power of x
next – contains the address of next
node.
Department of ISE Acharya Institute of Technology
113. Polynomial Representation using Linked List
A Polynomial has mainly two fields. exponent and coefficient.
Node of a Polynomial:
For example 3x^2 + 5x + 7 will represent as follows.
In each node the exponent field will store the corresponding exponent and the coefficient field will store the corresponding coefficient. Link field
points to the next item in the polynomial.
114. A polynomial p(x) is the expression in variable x which is in the form (axn + bxn-1 + …. + jx+
k), where a, b, c …., k fall in the category of real numbers and 'n' is non negative integer,
which is called the degree of polynomial.
An essential characteristic of the polynomial is that each term in the polynomial
expression consists of two parts:
one is the coefficient
other is the exponent
115. • The sign of each coefficient and exponent is stored within the coefficient and the
exponent itself
• Additional terms having equal exponent is possible one
• The storage allocation for each term in the polynomial must be done in ascending and
descending order of their exponent
116. Therefore, the structure declaration to represent this node
will be struct polynode
{
int
cf;
int
px;
struct polynode *next;
}*head;
Department of ISE Acharya Institute of Technology
119. Algorithm:
Step 1: Compare the exponent of P and the corresponding exponent of q.
Step 2:if expo(p) < expo(q) added the terms pointer to q to the resultant list and now
advanced the q pointer.
Step 3:expo(p) > expo(q) added the terms pointer to p to the resultant list and now
advanced the p pointer.
Step 3:If expo(p) = expo(q) Add the coefficients of these two terms and link this to the
resultant list and advance the pointers p and q to their next nodes.
Step 4:If there is no node in the second polynomial to compare with, so the last node in
the first polynomial is added to the end of the resultant linked list.
Step 5:If there is no node in the first polynomial to compare with, so the last node in the
second polynomial is added to the end of the resultant linked list.
124. In the above figure, you will notice that there is no node in the second polynomial to
compare with.
you can understand in a better way after seeing the figure, so the last node in the first
polynomial is added to the end of the resultant linked list.
The next below figure is the output which comes Polynomials Addition of two
polynomials.
126. Note: link field of last node contains the address of head and link field of
header node points to the first node of the list.
Every node has 3 fields
•cf – coefficient field
•px – power of x
•link – contains the address of next node.
Therefore, the structure declaration to represent this node
will be struct node
{
int cf;
int
px;
struct node *link;
};
typedef struct node* NODE;
Program to add two Polynomials using Circular Singly Linked List header
node
Department of ISE Acharya Institute of Technology
127. void sum()
{
node=(struct node*)malloc(sizeof(struct node));
head3=node;
ptr1=head1;
ptr2=head2;
while(ptr1!=NULL && ptr2!=NULL)
{
ptr=node;
if (ptr1->pow < ptr2->pow ) s
{
node->coff=ptr2->coff;
node->pow=ptr2->pow;
ptr2=ptr2->next;
}
function to add 2 polynomials:
130. Sparse matrix-Linked List Representation
In linked list representation, linked list data structure is
used to represent a sparse matrix. In linked list
representation, each node consists of four fields
whereas, in array representation, there are three fields,
i.e., row, column, and value.
The following are the fields in the linked list:
•Row: It is an index of row where a non-zero element is
located.
•Column: It is an index of column where a non-zero
element is located.
•Value: It is the value of the non-zero element which is
located at the index (row, column).
•Next node: It stores the address of the next node.
131. In the above figure, sparse represented in the linked
list form. In the node, first field represents the index of
row, second field represents the index of column, third
field represents the value and fourth field contains the
address of the next node.