0% found this document useful (0 votes)
5 views

module-3 linked-list final

Uploaded by

tharunsanatani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

module-3 linked-list final

Uploaded by

tharunsanatani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

DATA STRUCTURES AND APPLICATIONS

MODULE -3
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

3.1 Linked List: “A Linked list is a linear collection of data elements called nodes, where each
node is divided into two parts: the first part called info field, contains the information of the element
and second part called link field or next pointer field, contains the address of the next node in the
list”.
Node: Info-field Link-field

Example: first
10 20 30 40 \0

100 200 300 400


Where,
 first is a variable, contains the address of the first node.
 This list contains 4 nodes and each node consists of two fields, info and link.
 The info field of each node contains the data. 10, 20, 30 and 40.
 The numbers 100,200,300 and 400 represent the address of each node in memory.
 The link field of the last node contains \0 (NULL) indicates it is last node.

Arrays–drawbacks:
(1)The size of the arrays is fixed: So we must know the upper limit on the number of elements in
advance. (Static in nature)
(2) Inserting and a new element in an array of elements is expensive, because room has to be
created for the new elements and to create room existing elements have to shifted. Same holds good
for deletion also.

Advantages of linked list:


1. Efficient memory utilization: The memory of a linked list is not pre-allocated. Memory can be
allocated whenever required. And it is de-allocated when it is no longer required.
2. Insertion and deletion operations are easier and efficient: Linked list provide flexibility in
inserting a data item at a specified position and deletion of a data item from the given position.
3. Extensive manipulation: We can perform any number of complex manipulations without any
prior idea of the memory space available. (i.e. in stacks and queues we sometimes get overflow
conditions. Here no such problem arises.)
4. Arbitrary memory locations: Here the memory locations need not be consecutive. They may
be any arbitrary values. But even then the accessing of these items is easier as each data item
contains within itself the address to the next data item.

Prof. Shrikant Pujar, Dept. of CS&E 1


DATA STRUCTURES AND APPLICATIONS
Disadvantages of linked lists
 They have a tendency to use more memory due to pointers requiring extra storage space.
 Nodes in a linked list must be read in order from the beginning as linked lists are inherently
sequentially accessed.(cannot be randomly accessed)
 Nodes are stored in contiguously, greatly increasing the time required to access individual
elements within the list.
 Difficulties arise in linked lists when it comes to reverse traversing.

3.2 Representation of linked lists in Memory


 List requires 2 linear arrays- INFO and LINK – such that INFO[K] and LINK [K] contain,
information part and next pointer field of a node of LIST.
 LSIT also requires a variable name – START – which contains the location of the beginning of
the list.
 NULL –which indicates the end of the list.

Prof. Shrikant Pujar, Dept. of CS&E 2


DATA STRUCTURES AND APPLICATIONS
3.3 Memory allocation; Garbage Collection
 The maintenance of linked lists in memory assumes the possibilities of inserting new nodes into
the lists and hence requires some mechanism which provides unused memory space for new
nodes. With the linked list in memory, a special list is maintained which consist of unused
memory cells. This list which has its own pointer is called the list of available spaces or the free
storage list.
 When we use parallel array then for the use of unused memory cells in array we will also be
linked together to form a linked list using avail as its list pointer variable. Such structure will be
denoted by
LIST (INFO, LINK, START, AVAIL)

 Garbage Collection: Whenever a node is deleted, some memory space becomes reusable. This
memory space should be available for future use. One way to do this is called „Garbage
Collection‟.
 The operating system of computer may periodically collect all the deleted space into the free
space list. Any technique which does this collection is called garbage collection.
 It takes place in two steps.
1. First computer runs through all lists, tagging those cells which are currently in use.
2. Then computer runs through the memory collecting the untagged space on to free storage list.
 The garbage collection may take place when there is only some minimum amount of space or no
space at all left in the Free storage list or when CPU is idle and has time to do the collection.

Over flow & under flow


 Sometimes new data are to be inserted into a data structure but there is no available space i.e the
free storage list is empty. This situation is usually called overflow. The over flow may handle by
printing the message of over flow.
 Under flow refers to the situation where one wants to delete data from a data structure that is
empty. This situation is usually called underflow. The over flow may handle by printing the
message of under flow.

3.4 Types of Linked List:


 There are mainly 4 different types of linked lists.
1. Singly Linked List (SLL)
2. Circular Singly Linked List (CSLL)
3. Doubly Linked List (DLL)
4. Circular Doubly Linked List (CDLL)
5. Header Linked List (HLL)

Prof. Shrikant Pujar, Dept. of CS&E 3


DATA STRUCTURES AND APPLICATIONS
3.4.1 Singly Linked List (SLL): It is a collection of zero or more nodes where each node has two
or more fields but only one link field which contains the address of next node.

Example: first
10 20 30 40 \0

100 200 300 400


Where,
 first is a variable, contains the address of the first node.
 This list contains 4 nodes and each node consists of two fields, info and link.
 The info field of each node contains the data. 10, 20, 30 and 40.
 The numbers 100,200,300 and 400 represent the address of each node in memory.
 The link field of the last node contains \0 (NULL) indicates it is last node.

Representing a node of Linked List:

Struct node
{
int info; //info-integer field contains information
struct node *link; //link- holds address of next node
};
typedef struct node *NODE; //giving alternative name to strcut node to NODE
NODE first; // first- self-referential structure variable
First = NULL; // Empty list by name first is created with NULL value

Create a node:
X = (data_type *) malloc (size);
 Malloc(): it is used to allocate memory explicitly as and when required and exact amount of
memory space needed during execution.
X = (NODE) malloc (sizeof(struct node)); // allocate the memory space

NODE getnode()
{
NODE X;
X = (NODE) malloc (sizeof(struct node));
if ( X = = NULL) //nodes don‟t exist
{
Printf(“out of memory”);
}
return X; // allocation successful
}

Prof. Shrikant Pujar, Dept. of CS&E 4


DATA STRUCTURES AND APPLICATIONS
Operations on Singly Lined List:
1. Inserting a node into list : a) insert front b) insert rear
2. Deleting a node from list: a) delete front b) delete rear
3. Searching data in a list
4. Display the content of list

3.4.1.1 Inserting a node at front:


Step 1: create a node using getnode() function. temp
temp = getnode ();

Step 2: copy the item 50 into info field of temp. temp


temp -> info = item; 50

Step 3: copy the address of the first node which is stored in the variable first into link field of
temp. temp -> link = first;
temp first
50 10 20 30 40 \0

Step 4: temp is inserted and then temp becomes the first node.
return temp;
first

50 10 20 30 40 \0

C Function to insert an item at the front end of the list.

NODE insert_front (int item, NODE first )


{
NODE temp;
temp = getnode(); //obtain the new temp node
temp -> info = item; //insert item to temp info
temp -> link = first; // insert new node at front
return temp; // return the new first node
}

3.4.1.2 Inserting a node at rear:


Step 1: create a node using getnode() function. temp
temp = getnode ();

Step 2: copy item 50 into info field and NULL to link. temp
temp -> info = item; 50 \0
temp -> link = NULL;

Prof. Shrikant Pujar, Dept. of CS&E 5


DATA STRUCTURES AND APPLICATIONS
Step 3: If list is empty, the above node can be returned as the first node of the list.
if (first = = NULL) return temp;

Step 4: if the list is existing, then we have to insert temp at the end of list. To insert at the end,
we have to find the address of the last node.
cur = first;
while( cur -> link != NULL)
{
cur = cur ->link;
}
After executing the above code , we will get the address of last node in a list.
First cur temp

10 20 30 40 \0 50 \0

Step 5: copy the address of the temp node to cur -> link, NULL is replaced by temp address.
cur -> link = temp;
first cur temp
10 20 30 40 50 \0

Step 6: first contains the address of the first node of list. So we have to return first.
return first;
first

10 20 30 40 50 \0

C Function to insert an item at the rear end of the list.


NODE insert_rear ( int item, NODE first)
{
NODE temp, cur;
temp = getnode(); //obtain the new temp node
temp -> info = item; //insert item to temp info
temp -> link = NULL; // insert NULL to link
if (first = = NULL)
return temp;
cur = first;
while( cur -> link != NULL) //checking last node of list
{
cur = cur ->link;
}
cur -> link = temp; // insert new node at end
return first; // return the first node
}

Prof. Shrikant Pujar, Dept. of CS&E 6


DATA STRUCTURES AND APPLICATIONS
3.4.1.3 Delete a node from the front end:
Step 1: if the list is empty, it is not possible to delete a node from list.
if ( first = = NULL)
{
printf(“List is empty”);
}
Step 2: if list is existing, first contains the address of the first node. We have to take pointer
variable temp and store the address of first node.
temp = first;
first temp

10 20 30 40 50 \0

Step 3: update the pointer temp so that it contains address of the second node.
temp = temp -> link;
first temp
10 20 30 40 50 \0

Step 4: now, first points to first node and temp points to second node of the list. De-allocate the
memory of the first node.
free( first);

Step 5: once first node is deleted, and then second node temp is the first node. So, return temp as
first node. return temp;
first
20 30 40 50 \0

C Function to delete an item from front end of the list.

NODE delete_front ( NODE first)


{
NODE temp;
if ( first = = NULL)
{
printf(“List is empty”);
}
temp = first;
temp = temp -> link;
printf(“item delted =%d”, first -> info);
free (first);
return temp;
}

Prof. Shrikant Pujar, Dept. of CS&E 7


DATA STRUCTURES AND APPLICATIONS
3.4.1.4 Delete a node from the rear end:
Step 1: if the list is empty, it is not possible to delete a node from list.
if ( first = = NULL)
{
printf(“List is empty”);
}

Step 2: if the list is existing, then we have to delete the last node of list. To delete at the raer end,
we have to find the address of the last node by using variable cur and prev.
cur = first;
prev= NULL;
while( cur -> link != NULL)
{
prev = cur;
cur = cur ->link;
}

After executing the above code , we will get the address of last node and prev get the address of
last but one node in a list.
First prev cur

10 20 30 40 50 \0

Step 3: delete the last node pointed by cur.


free ( cur );
first prev cur
10 20 30 40 50 \0

Step 4: once last node is deleted, and then prev node should be the last node. So, copy NULL to
link field of prev node.
prev -> link = NULL;
first prev

10 20 30 40 \0

Step 5: finally return the address of the first node..


return first;
first

10 20 30 40 \0

Prof. Shrikant Pujar, Dept. of CS&E 8


DATA STRUCTURES AND APPLICATIONS
C Function to delete an item from rear end of the list.

NODE delete_rear ( NODE first)


{
NODE cur,prev;
if ( first = = NULL)
{
printf(“List is empty”);
}
cur = first;
prev= NULL;
while( cur -> link != NULL)
{
prev = cur;
cur = cur ->link;
}
printf(“item delted =%d”, cur -> info);
free (cur);
return first;
}

3.4.1.5 Searching for key item in the list:


 Here we have to search key by comparing info field of each node in a list.
 if item found, then return pointer to the node.
 if item not found, return NULL.

C Function to Search for key item in the list.


NODE search ( int key, NODE first)
{
NODE cur;

if (first = = NULL) // search for empty list


return NULL;

/* compare one after the other */


cur = first;
while( cur ! = NULL)
{
if ( key = = cur -> info)
return cur; //if key found
cur = cur ->link; // point cur to next node
}
return NULL; // key not found
}

Prof. Shrikant Pujar, Dept. of CS&E 9


DATA STRUCTURES AND APPLICATIONS
3.4.1.6 Display the items in the list
C Function to display the contents of linked list.

Void display ( NODE first)


{
NODE cur;
if ( first = = NULL)
{
printf(“List is empty”);
return;
}

printf(“the contents of linked list”);


cur = first; //holds address of first node
while( cur != NULL)
{
Printf(“%d”, cur ->info); //display info field of node
cur = cur ->link; //points to the next node
}
printf(“\n”);
}

3.4.1.7 Concatenate two lists:


 Concatenation of two lists means combining the second list at the end of the first list.
Step 1: concatenation of two lists is possible only if two lists exist. If one of the lists is empty,
return the address of the first node of the non-empty list.
Step 2: if two lists are exist, obtain the address of the last node of first list. Pointer cur contains
the address of the last node of the first list.
cur = first;
while( cur ->link ! = NULL)
{
cur = cur ->link;
}
First cur second

10 20 30 \0 40 50 \0

Step 3: now, attach address of the first node of the second list to cur node.
cur -> link = second;
First cur second
10 20 30 40 50 \0

Prof. Shrikant Pujar, Dept. of CS&E 10


DATA STRUCTURES AND APPLICATIONS
C Function to concatenate two list.

NODE concate ( NODE first, NODE second)


{
NODE cur;

if (first = = NULL)
return second;
if (second = = NULL)
return first;

cur = first;
while( cur ->link ! = NULL)
{
cur = cur ->link; // point cur to next node
}
Cur -> link =second;
return first;
}

3.4.1.8 Reverse a list without creating new nodes.

C Function to reverse list.

NODE reverse ( NODE first )


{
NODE cur,temp;
cur = NULL; //initial reversed list

while (first ! = NULL)


{
temp = first -> link; //obtain address of second node
first-> link = cur; // attach first node of list to be reversed
// at the beginning of partially reversed list
cur = first; // point cur to point to newly
// partially reversed list
first = temp;
}
return cur; //contains address of reversed list
}

Prof. Shrikant Pujar, Dept. of CS&E 11


DATA STRUCTURES AND APPLICATIONS
3.4.2 Circular Singly Linked List (CSLL)
 A circular singly linked list is a singly linked list where the link field of last node of the list
contains address of the first node.
first

10 20 30 40

Representing a node of Circular Linked List:


Struct node
{
int info; //info-integer field contains information
struct node *link; //link- holds address of next node
};
typedef struct node *NODE; //giving alternative name to strcut node to NODE
NODE last; // last- self-referential structure variable
last = NULL; // Empty list by name last is created with NULL value
if (last = = NULL)
{
printf(“list is empty\n”);
}

Operations on Circular Singly Lined List:


1. Inserting a node into list : a) insert front b) insert rear
2. Deleting a node from list: a) delete front b) delete rear
3. Display the content of list

3.4.2.1 Inserting a node at front:


Step 1: create a node using getnode() function. temp
temp = getnode ();

Step 2: copy the item 40 into info field of temp. temp


temp -> info = item; 40

Step 3: copy the address of the first node(i.e last -> link ) into link field of temp.
if(last != NULL)
temp -> link = last ->link;
else
temp = last;
temp first last

40 10 20 30

Prof. Shrikant Pujar, Dept. of CS&E 12


DATA STRUCTURES AND APPLICATIONS
Step 4: make temp as the first node by copying temp into link field of last node.
last->link = temp;
first last

40 10 20 30

C Function to insert an item at the front end of the list.


NODE insert_front (int item, NODE first )
{
NODE temp;
temp = getnode(); //obtain the new temp node
temp -> info = item; //insert item to temp info
if(last != NULL)
temp -> link = last ->link; //insert at front
else
last = temp;
last -> link = temp; // link last node to first node
return last; // return the last node
}

3.4.2.2 Inserting a node at rear:


Step 1: create a node using getnode() function. temp
temp = getnode ();

Step 2: copy the item 50 into info field of temp. temp


temp -> info = item; 50

Step 3: copy the address of the first node(i.e last -> link ) into link field of temp.
if(last != NULL)
temp -> link = last ->link;
else
temp = last;
first last temp

10 20 30 50

Step 4: make temp as the last node by copying temp into link field of last node.
last->link = temp;
first temp

10 20 30 50

Prof. Shrikant Pujar, Dept. of CS&E 13


DATA STRUCTURES AND APPLICATIONS
C Function to insert an item at the rear end of the list.
NODE insert_rear (int item, NODE first )
{
NODE temp;
temp = getnode(); //obtain the new temp node
temp -> info = item; //insert item to temp info
if(last != NULL)
temp -> link = last ->link; //insert at front
else
last = temp ;
last -> link = temp; // link last node to first node
return temp; // make new node as last node
}

3.4.2.3 Delete a node from the front end:


Step 1: if the list is empty, it is not possible to delete a node from list.
if ( first = = NULL)
{
printf(“List is empty”);
}

Step 2: if list is existing, obtain the address of the first node.


first = last -> link;
first last

10 20 30 50

Step 3: Make second node as first node.


last-> link = first -> link;
first last

10 20 30 50

Step 4: Now remove the first node. De-allocate the memory of the first node.
free( first);

first last

10 20 30 50

Prof. Shrikant Pujar, Dept. of CS&E 14


DATA STRUCTURES AND APPLICATIONS
C Function to delete an item from front end of the list.

NODE delete_front ( NODE last)


{
NODE first;
if ( last = = NULL)
{
printf(“List is empty”);
}
first = last ->link; //obtain node to be deleted
last ->link = first->link; // store new first node in link of last
printf(“item deleted =%d”, first->info);
free(first); // delete old node
return last; // return address of last node
}

3.4.2.3 Delete a node from rear end:


Step 1: if the list is empty, it is not possible to delete a node from list.
if ( first = = NULL)
{
printf(“List is empty”);
}
Step 2: if list is existing, obtain the address of the first node.
prev = last -> link;
first last

10 20 30 50

Step 3: find the address of the last but one node.


While(prev -> link ! = last)
{ prev = prev -> link;
}
first prev last

10 20 30 50

Step 4: Make the last but one node as the last node and De-allocate the memory of the last node.
prev -> link = last ->link;
free( last);
first last

10 20 30 50

Prof. Shrikant Pujar, Dept. of CS&E 15


DATA STRUCTURES AND APPLICATIONS
C Function to delete an item from rear end of the list.

NODE delete_rear ( NODE last)


{
NODE prev;
if ( last = = NULL)
{
printf(“List is empty”);
}
prev = last ->link; //obtain the address of prev node
while(prev -> link ! = last)
{
prev = prev -> link;
}
printf(“item deleted =%d”, last->info);
free(last); // delete old last node
return prev; // return address of new last node
}

3.4.3 Doubly linked list: A doubly linked list is a linear collection of nodes where each node is
divided into three parts:
Info – contains information to be stored.
llink – contains the address of the left or previous node. llink info rlink
rlink - contains the address of the right or next node.

Example:
first

\0 10 20 30 \0

3.4.3.1 Insert node at front


Step 1: create a node using getnode() function, copy the item 40 into info field and assign
NULL to llink & rlink.
temp = getnode (); temp
temp -> info = item;
\0 40 \0
temp -> llink = temp -> rlink = NULL;

Step 2: insert into existing link.


temp-> rlink = first;
first -> llink = temp;
temp first

\0 40 10 20 30 \0

Prof. Shrikant Pujar, Dept. of CS&E 16


DATA STRUCTURES AND APPLICATIONS
Step 3: temp is inserted and then temp becomes the first node.
return temp;
first

\0 40 10 20 30 \0

C Function to insert an item at the front end of the list.


NODE insert_front (int item, NODE first )
{
NODE temp;
temp = getnode(); //obtain the new temp node
temp -> info = item; //insert item to temp info
temp -> llink = temp-> rlink =NULL;
if(first == NULL)
return temp; //insert node for first time
temp ->rlink = first; //insert at front of existing list
first -> llink = temp;
return temp; //return the address of new first node
}

3.4.3.2 Insert node at the rear


Step 1: create a node using getnode() function, copy the item 40 into info field and assign
NULL to llink & rlink.
temp = getnode (); temp
temp -> info = item;
\0 40 \0
temp -> llink = temp -> rlink = NULL;

Step 2: To insert the node at rear end, first find the address of the last node.
cur = first;
while(cur -> rlink ! = NULL)
{
cur = cur -> rlink;
}
first cur temp

\0 10 20 30 \0 \0 40 \0
Step 3: Insert node at the end.
cur -> rlink = temp;
temp -> llink = cur;
first cur temp

\0 10 20 30 40 \0

Prof. Shrikant Pujar, Dept. of CS&E 17


DATA STRUCTURES AND APPLICATIONS
Step 4: return address of the first node.

return first;
first

\0 10 20 30 40 \0

C Function to insert an item at the rear end of the list.


NODE insert_rear (int item, NODE first )
{
NODE temp,cur;
temp = getnode(); //obtain the new temp node
temp -> info = item; //insert item to temp info
temp -> llink = temp-> rlink =NULL;
if(first == NULL)
return temp; //insert node for first time
cur = first;
while(cur -> rlink ! = NULL) //find address of last node
{
cur = cur -> rlink;
}
cur -> rlink = temp; //insert node at the end
temp -> llink = cur;
return first; //return the address of first node
}

3.4.2.3 Delete a node from the front end:


Step 1: if the list is empty, it is not possible to delete a node from list.
if ( first = = NULL)
{
printf(“List is empty”);
}

Step 2: if list is existing, obtain the address of the second node.


second = first -> rlink;
first second

\0 10 20 30 \0

Step 3: Make second node as first node.


second->l link =NULL;
first second

\0 10 \0 20 30 \0

Prof. Shrikant Pujar, Dept. of CS&E 18


DATA STRUCTURES AND APPLICATIONS
Step 4: Now remove the first node. De-allocate the memory of the first node.
free( first);
first first

\0 10 \0 20 30 \0

C Function to delete an item from front end of the list.

NODE delete_front ( NODE first)


{
NODE second;
if ( first = = NULL)
{
printf(“List is empty”);
}
second = first -> rlink; //get address of second node
second -> llink =NULL; // make second as first node
printf(“item deleted =%d”, first->info);
free(first); // delete first node
return second;
}

3.4.3.4 Delete a node at the rear


Step 1: if the list is empty, it is not possible to delete a node from list.
if ( first = = NULL)
{
printf(“List is empty”);
}

Step 2:if list is existing then find the address of last node and last but one node.
cur = first, prev = NULL;
while(cur -> rlink ! = NULL)
{ prev = cur;
cur = cur -> rlink;
}
first prev cur

\0 10 20 30 40 \0

Step 3:the last node pointed by cur can be deleted.


free(cur);

first prev cur

\0 10 20 30 40 \0

Prof. Shrikant Pujar, Dept. of CS&E 19


DATA STRUCTURES AND APPLICATIONS
Step 4: The node pointed by prev becomes the last node.
prev -> rlink = NULL;
first

\0 10 20 30 \0

C Function to delete an item from rear end of the list.

NODE delete_rear ( NODE first)


{
NODE cur,prev;
if ( first = = NULL)
{
printf(“List is empty”);
}
cur = first;
prev= NULL;
while( cur -> link != NULL)
{
prev = cur;
cur = cur ->link;
}
printf(“item delted =%d”, cur -> info);
free (cur);
prev -> rlink = NULL;
return first;
}

3.4.4 Header Linked List


“A header node in a linked list is special node whose link field always contains address of the first
node of the list and info field contains the number of nodes in list”.
head
10 30 40 50 \0

 The info field of header node does not contain any information.
 The first node starts from link field of header node i.e the node containing the info field 10 is the
first node.
head

4 10 30 40 50 \0

 The info field of the header node contains the number of nodes in the list.

Prof. Shrikant Pujar, Dept. of CS&E 20


DATA STRUCTURES AND APPLICATIONS
Advantages of Header node:
1. Simplifies Insertion and deletion operations.
2. Avoid the usage of various cases such as “if only one node is present what to do”.
3. Designing of program will be very simple.
4. Circular lists with header node are frequently used instead of ordinary linked list.

3.4.4.1 Inserting a node at front:


Step 1: create a node using getnode() function. temp
temp = getnode ();

Step 2: copy the item 50 into info field of temp. temp


temp -> info = item; 50

Step 3: Obtain the address of the first node which is stored in the head link field of head.
first = head -> link;
head first
10 20 30 40 \0

Step 4: temp node is inserted between the head and first node.
head -> link = temp;
temp temp -> link = first;
50

head first
10 20 30 40 \0

Step 5: return the address of header node.


return head;
head

50 10 20 30 40 \0

C Function to insert an item at the front end of the list.


NODE insert_front (int item, NODE head )
{
NODE temp;
temp = getnode(); //obtain the new temp node
temp -> info = item; //insert item to temp info
first = head -> link;
head -> link = temp; // insert new node at front
temp -> link = first;
return head; // return the header node
}

Prof. Shrikant Pujar, Dept. of CS&E 21


DATA STRUCTURES AND APPLICATIONS
3.5 POLYNOMIALS
Polynomial Representation:
representation of Polynomials in a linked list.
Example: a = 3X14 + 2X8 + 1 and b=8X14 – 3X10 + 10X6

2. Adding polynomials

Prof. Shrikant Pujar, Dept. of CS&E 22


DATA STRUCTURES AND APPLICATIONS
C Function for addition of two polynomials.

3.6 Sparse Matrix Representation


 The sparse matrix can be more efficiently represented using linked list.
 Header node is represented by 3 fields.

Next
Down right

 Items/elements in matrix are represented by 5 fields.

row col val


Down right

Prof. Shrikant Pujar, Dept. of CS&E 23


DATA STRUCTURES AND APPLICATIONS
Representation of sparse matrix using linked list:

Prof. Shrikant Pujar, Dept. of CS&E 24

You might also like