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

Module 3 DS

The document discusses linked lists and their implementation. It begins with definitions of linked lists and their basic components like nodes and links. It then covers representation of linked lists in memory, memory allocation, garbage collection, different types of linked lists, and basic operations like traversing, searching, insertion and deletion. Examples of linked list implementations in C programming language are also provided showing how to create nodes, insert and delete nodes from the list.

Uploaded by

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

Module 3 DS

The document discusses linked lists and their implementation. It begins with definitions of linked lists and their basic components like nodes and links. It then covers representation of linked lists in memory, memory allocation, garbage collection, different types of linked lists, and basic operations like traversing, searching, insertion and deletion. Examples of linked list implementations in C programming language are also provided showing how to create nodes, insert and delete nodes from the list.

Uploaded by

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

R L JALAPPA INSTITUTE OF TECHNOLOGY

Dept.. of Computer Science & Engg.

Module 3

Iliyaz Pasha M
Assistant Professor

De
1 partment of Computer Science, RLJIT
Outline
 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.
 Header linked lists.
 Linked Stacks and Queues.
 Applications of Linked lists –
Polynomials.
Sparse matrix representation.
 Programming Examples.
Department of Computer Science, RLJIT 2
Linked List
 A linked list is a data structure which is collection of zero or more
nodes where each node is connected to one or more nodes. Each
node has two fields namely
 info field – This field is used to store the data or information
to be manipulated.
 link or next field – This field contains address of the next
node.
 The pictorial representation of a singly linked list where each
node is connected to the next node is shown below.

 The above list consists Department


of fourof Computer
nodesScience,
with RLJIT
info 3 fields containing
the data items 20, 30, 10 and 60.
Linked List
Types of Linked List:
1. Singly linked lists (SLL)
2. Doubly linked lists (DLL)
3. Circular singly linked lists (CSLL)
4. Circular doubly linked lists (CDLL)

Advantages of Linked Lists:-


 Insertion and deletion operations can be done efficiently.
 No wastage of memory. The memory can be allocated or deal
located as per requirement.

Department of Computer Science, RLJIT 4


Linked List
Memory Allocation:
 The process of reserving the memory space to store the data is
called memory allocation.
 The memory space is allocated for variables in the following
situations:-
1. The BSS ( Block Started by Symbol) Segment:- it is often called
uninitialized data segment. It contains all global variables and
static variables that are initialized to zero or don’t have explicit
initialization in source code.
2. The memory space for all local variables is reserved on the stack
area in memory.
3. The memory space for all dynamically variables is reserved in
the heap area. Once, memory space is used and no longer
required, we can free the memory space using free( ).
Department of Computer Science, RLJIT 5
Linked List
Garbage Collection:
 The memory allocation for the objects (nodes) is done from the
heap area of memory. If some object is created and which is not
been is use from a long time, then such an object is called
garbage.
 The Garbage Collection is a technique in which all such garbage
(objects not used) is collected and recycled.
 The Garbage Collection technique is cleans up the heap so that
memory occupied by the unused objects can be freed and can be
used allocating for the new objects.
 The Garbage Collection Algorithm works in 2 steps:
1. Mark
2. Sweep

Department of Computer Science, RLJIT 6


Linked List
Basic operations on Linked List:
 Create (creates the linked list)
 Insert
 Delete
 Search

Department of Computer Science, RLJIT 7


Representing a node of Linked List:
struct node
{
int info;
struct node *link; /* Self Referential Structures */
};

Observe from the above structure definition that a node has two fields:
info – it contains the information.
link – it is a pointer field and hence it should contain the address.
The link field contains the address of the next node in the list whose type
is same as the link field.
To create the variable of a node, we follow the declaration.
Along with structure declaration of node, create a typedef name for node.
typedef struct node *NODE;
A pointer variable for NODE can be declared as shown below
NODE start;
If the linked list is empty, we store a special value NULL in the start i.e.,
start = NULL //Department
emptyof Computer
linkedScience,
list RLJIT 8
Linked List Operations (One – Way List):
Singly Linked List (SLL):
It is a collection of zero or more nodes, where each node has 2 or more fields,
but only one link field which contains address of the next node. Each node in
the list can be accessed using the link field which contains address of the next
node.
Example: Singly Linked List (SLL) consisting of items 50, 20,5,10 and 80 is
shown below.

Note: The variable first contains address of the first node. The link field of
the last node contains \0 (NULL) indicating it is the last node.
Observe from above figure that:
 This list contains 5 nodes and each node contains of two fields, info and link.
 The info field of each node contains the data. In this list, the items 50, 20, 45, 10
and 80 represent the data.
 The numbers on top of each node i.e, 2004, 1020, 5012, 1008 and 6016 represent
the address of each node in theDepartment
memory. of Computer Science, RLJIT 9
Singly Linked List (SLL):
It is called singly because this list consists of only one link, to point to next
node.
Example:

Note: The last field of the last node is \0 (NULL), means here is no further
nodes.

Initially, when no nodes are created, the node first will contain \0 (NULL).
This indicate the linked list does not exist. Hence called an empty linked list.

For example, an empty list can be written as shown below.


Department of Computer Science, RLJIT 10
The following are the set of operation, that can be performed on
the Linked List (One – Way List):
1. Creation of Node in the list
2. Inserting of Node into the list
3. Deleting a Node from the list
4. Searching or Traversing a list

Department of Computer Science, RLJIT 11


Implementation of SLL:
struct node
{
int info;
struct node *link;
};
typedef struct node* NODE;
NODE first = NULL, temp = NULL, last = NULL;

Create a Node:
void create ( )
{
temp = (NODE) malloc (sizeof(struct node));
if ( temp == NULL )
{
printf(“Insufficient Memory or out of memory\n”);
return ;
}
printf (“Enter the information to be stored\n”);
scanf (“%d”, &temp -> info);
temp ->link = NULL; Department of Computer Science, RLJIT 12
}
Insertion at the beginning (front) :
void insertBeg ( )
{
if ( first == NULL ) // List is empty
{
create ( );
first = temp;
last = first;
}
else
{
create ( );
temp ->link = first;
first = temp;
}
}

Department of Computer Science, RLJIT 13


Insertion at the end (rear) :
void insertEnd ( )
{
if ( first == NULL ) // List is empty
{
create ( );
first = temp;
last = first;
}
else
{
create ( );
last->link = temp;
last = temp;
}
}

Department of Computer Science, RLJIT 14


Display :
void display ( )
{
struct node *temp1;
temp1 = first;
if (temp1 == NULL ) // List is empty
{
printf (“List is empty\n”);
return ;
}
printf(“The contents of Linked List are :\n”);
while ( temp1 != NULL )
{
printf(“%d\n”, temp1->info);
temp1 = temp1->link;
}
}

Department of Computer Science, RLJIT 15


Delete a node at the beginning (front) :
void deleteBeg ( )
{
struct node *temp;
temp = first; // Store address of 1st node in temp
if (temp -> link == NULL ) // Only one node in list
{
printf (“The item deleted = %d\n”, temp->info);
free(temp);
first = last = NULL;
return ;
}
else
{
first = temp->link; // make 2nd node as 1st node
printf (“The item deleted = %d\n”, temp->info);
free(temp);
}
}

Department of Computer Science, RLJIT 16


Delete a node at the end (rear) :
void deleteEnd ( )
{
struct node *temp;
temp = first; // Store address of 1st node in temp
if (temp -> link == NULL ) // Only one node in list
{
printf (“The item deleted = %d\n”, temp->info);
free(temp);
first = last = NULL;
return ;
}
else
{
while (temp->link != last)
{
temp = temp->link;
}
printf (“The item deleted = %d\n”, last->info);
free(last);
temp->link = NULL;
Department of Computer Science, RLJIT 17
last = temp;
Doubly Linked List ( DLL ) :
A doubly linked list is a collection of nodes where each node is divided
into 3 parts of fields:
info – where the information has to be stored.
llink or prev – contains the address of the previous node.
rlink or next – contains the address of the next node.

In doubly linked list, traversing the list is possible in both directions.

Department of Computer Science, RLJIT 18


Implementation of Doubly Linked List ( DLL ) :
struct node
{
int info;
struct node *llink;
struct node *rlink;
};

typedef struct node* NODE;

NODE first = NULL, last = NULL, temp = NULL;

Department of Computer Science, RLJIT 19


Implementation of DLL:
struct node
{
int info;
struct node *next;
struct node *prev;
};
typedef struct node* NODE;
NODE first = NULL, temp = NULL, last = NULL;
Create a Node:
void create ( )
{
temp = (NODE) malloc (sizeof(struct node));
if ( temp == NULL )
{
printf(“Insufficient Memory or out of memory\n”);
return ;
}
printf (“Enter the information to be stored\n”);
scanf (“%d”, &temp -> info);
temp ->next = NULL; Department of Computer Science, RLJIT 20
temp->prev = NULL;
Insertion at the beginning (front) :
void insertBeg ( )
{
if ( first == NULL ) // List is empty
{
create ( );
first = temp;
last = first;
}
else
{
create ( );
temp ->next = first;
first->prev = temp;
first = temp;
}
}

Department of Computer Science, RLJIT 21


Insertion at the end (rear) :
void insertEnd ( )
{
if ( first == NULL ) // List is empty
{
create ( );
first = temp;
last = first;
}
else
{
create ( );
last->next = temp;
temp ->prev = last;
last = temp;
}
}

Department of Computer Science, RLJIT 22


Display :
void display ( )
{
struct node *temp1;
temp1 = first;
if (temp1 == NULL ) // List is empty
{
printf (“List is empty\n”);
return ;
}
printf(“The contents of Linked List are :\n”);
while ( temp1 != NULL )
{
printf(“%d\n”, temp1->info);
temp1 = temp1->next;
}
}

Department of Computer Science, RLJIT 23


Delete a node at the beginning (front) :
void deleteBeg ( )
{
struct node *temp;
temp = first; // Store address of 1st node in temp
if ( first == NULL )
{
printf(“List is empty”);
return;
}
if (temp -> next== NULL ) // Only one node in list
{
printf (“The item deleted = %d\n”, temp->info);
free(temp);
first = last = NULL;
}
else
{
first = first->next; // make 2nd node as 1st node
printf (“The item deleted = %d\n”, temp->info);
free(temp); Department of Computer Science, RLJIT 24
first->prev=NULL;
Delete a node at the end (rear) :
void deleteEnd ( )
{
struct node *temp, *temp2;
temp = first; // Store address of 1st node in temp
if ( first == NULL )
{
printf(“List is empty”);
return;
}
if (temp -> next == NULL ) // Only one node in list
{
printf (“The item deleted = %d\n”, temp->info);
free(temp);
first = last = NULL;
return ;
}
else
{
temp2 = last->prev;
temp2->next = NULL;
printf (“The item deleted = %d\n”, last->info);
free(last);
last = temp2; Department of Computer Science, RLJIT 25
Delete a node at the end (rear) :
void deleteEnd ( )
{
struct node *temp, *temp2;
temp = first; // Store address of 1st node in temp
if ( first == NULL )
{
printf(“List is empty”);
return;
}
if (temp ->next== NULL ) // Only one node in list
{
printf (“The item deleted = %d\n”, temp->info);
free(temp);
first = last = NULL;
return ;
}
else
{
temp2 = last->prev;
temp2->next = NULL;
printf (“The item deleted = %d\n”, last->info);
free(last);
last = temp2; Department of Computer Science, RLJIT 26
Circular Singly Linked List (CSLL):
In Circular Singly Linked List, the last node of the list will contain the
address of the first node in the list.
Example:

Implementation:
struct node
{
int info;
struct node *next;
};

typedef struct node *NODE;


Department of Computer Science, RLJIT 27
NODE last = NULL;
Insertion at the beginning (front) :
NODE insertBeg ( int item, NODE last)
{
NODE temp;
temp = (NODE) malloc (sizeof(struct node));
temp->info = item;
if (last == NULL)
{
last = temp;
}
else
{
temp->next = last->next;
}
last->next = temp;
return last;
}

Department of Computer Science, RLJIT 28


Insertion at the end (rear) :
NODE insertEnd( int item, NODE last)
{
NODE temp;
temp = (NODE) malloc (sizeof(struct node));
temp->info = item;
if (last == NULL)
{
last = temp;
}
else
{
temp->next = last->next;
}
last->next = temp;
return temp;
}

Department of Computer Science, RLJIT 29


Display :
void display (NODE last)
{
NODE temp;
if (last == NULL ) // List is empty
{
printf (“List is empty\n”);
return ;
}
printf(“The contents of Linked List are :\n”);
temp = last->next;
while ( temp != last )
{
printf(“%d\n”, temp->info);
temp = temp->next;
}
printf(“%d\n”,temp->info);
}

Department of Computer Science, RLJIT 30


Delete a node at the beginning (front) :
NODE deleteBeg ( NODE last)
{
NODE first;
if (last == NULL)
{
printf(“ List is empty”);
return;
}
if (last -> next == last ) // Only one node in list
{
printf (“The item deleted = %d\n”, last->info);
free(last);
return NULL;
}
first = last->next;
last->next = first->next;
printf (“The item deleted = %d\n”, first->info);
free(first);
return last;
} Department of Computer Science, RLJIT
31
Delete a node at the end (rear) :
NODE deleteEnd ( NODE last)
{
NODE prev;
if (last == NULL)
{
printf(“ List is empty”);
return;
}
if (last -> next == last ) // Only one node in list
{
printf (“The item deleted = %d\n”, last->info);
free(last);
return NULL;
}
prev = last->next;
while ( prev->next ! = last)
{
prev = prev ->next;
}
prev->next = last->next;
printf (“The item deleted = %d\n”, last->info);
free(last);
Department of Computer Science, RLJIT 32
return prev;
Circular Doubly Linked List (CDLL):
Here traversing is possible in forward, backward and also in circular fashion.
Each node has 3 fields:
info – where the information has to be stored.
llink or prev – contains the address of the previous node.
rlink or next – contains the address of the next node.
The prev field of the 1st node contains the address of the last node whereas
the next field of the last node contains address of the first node.
Example:

Department of Computer Science, RLJIT 33


Implementation:
struct node
{
int info;
struct node *next;
struct node *prev;
};

typedef struct node *NODE;

NODE first;
first = NULL;

Department of Computer Science, RLJIT 34


Insert a Node at the Beginning:
NODE insertBeg ( int item, NODE first)
{
NODE temp, last;
temp = (NODE) malloc (sizeof(NODE));
temp->info = item;
temp->prev = temp->next = temp;
if(first == NULL)
last = first->prev;
temp->next = first;
first->prev = temp;
last->next = temp;
temp->prev = last;
return temp;
}

Department of Computer Science, RLJIT 35


Insert a Node at the rear end:
NODE insertEnd ( int item, NODE first)
{
NODE temp, last;
temp = (NODE) malloc (sizeof(NODE));
temp->info = item;
temp->prev = temp->next = temp;
if(first == NULL)
return temp;
last = first->prev;
temp->next = temp;
temp->prev = last;
first->prev = temp;
temp->next = first;
return first;
}

Department of Computer Science, RLJIT 36


Delete a Node at the Beginning:
NODE deleteBeg(NODE first)
{
NODE second, last;
if(first == NULL)
{
printf(“ List is empty”);
return NULl;
}
if (first->next == first) //Only one node
{
printf(“Item deleted = %d\n”, first->info);
free(first);
return NULL;
}
second = first->next;
last = first->prev;
second->prev = last;
last->next = second;
printf(“Item deleted = %d\n”, first->info);
free(first);
return second; Department of Computer Science, RLJIT 37
}
Delete a Node from the rear end:
NODE deleteEnd (NODE first)
{
NODE last, temp;
if(first == NULL)
{
printf(“ List is empty”);
return NULl;
}
if (first->next == first) //Only one node
{
printf(“Item deleted = %d\n”, first->info);
free(first);
return NULL;
}
last = first->prev;
temp = last->prev;
temp->next = first;
first->prev = temp;
printf(“Item deleted = %d\n”, last->info);
free(last);
return first; Department of Computer Science, RLJIT 38
}
Display Circular Doubly Linked List:
void display (NODE first)
{
NODE cur, last;
if(first == NULL)
{
printf(“ List is empty”);
return NULl;
}
printf(“ Contents are:\n”);
cur = first;
last = first->prev;
while ( cur != last)
{
printf(“%d\n”,cur->info);
cur = cur->next;
}
printf(“%d”,cur->info);
}

Department of Computer Science, RLJIT 39


Circular Singly Linked List with Header Nodes :
What is a header Node?
Definition: A header node is a special node whose next field always contains the
address of the first node of the list.
 Using header node, any node in the list can be accessed.
 The info field of header node usually doesn't contain any information and such a
node doesn’t represent an item in the list.
 Useful information such as number of nodes in the list can be stored in the info
field.
 If the list is empty, then next field of header node contains NULL(\0).
Advantages of header nodes:
1. Simplifies insertion and deletion operations.
2. Design of program will be simple.
Example 1:An empty list using header node is pictorially represented as shown:

Example 2: The Non-empty list using header node.

Department of Computer Science, RLJIT 40


Insert a node at the beginning (Front End):
NODE insertBeg ( int item, NODE head)
{
NODE temp;
temp = (NODE) malloc (sizeof(NODE));
temp->info = item;
temp->next = head->next;
head->next = temp;
return head;
}

Department of Computer Science, RLJIT 41


Insert a node at the end (Rear End):
NODE insertEnd ( int item, NODE head)
{
NODE temp, cur;
temp = (NODE) malloc (sizeof(NODE));
temp->info = item;
cur = head->next;
while(cur->next != head )
{
cur = cur->next;
}
cur->next = temp;
temp->next = head;
return head;
}

Department of Computer Science, RLJIT 42


Delete a node at the beginning (front end):
NODE deleteFront ( NODE head )
{
NODE first, second;
if (head->next == head)
{
printf(“List is empty\n”);
return head;
}
first = head->next; // obtain 1st node of list
second = first->next; // obtain 2nd node of list
head->next = second; // make 2nd node as 1st node
printf(“Item deleted = %d\n”, first->info);
free(first);
return head;
}

Department of Computer Science, RLJIT 43


Delete a node at the end(rear end):
NODE deleteEnd ( NODE head )
{
NODE prev, cur;
if (head->next == head)
{
printf(“List is empty\n”);
return head;
}
cur = head->next; // obtain 1st node of list
prev = head; // previous to 1st node
while(cur->next != head)
{
prev = cur;
cur = cur->next;
}
prev->next = head;
printf(“Item deleted = %d\n”, cur->info);
free(cur);
return head;
} Department of Computer Science, RLJIT 44
Display the contents of CSLL using head node:
void display ( NODE head )
{
NODE temp;
if (head->next == head)
{
printf(“List is empty\n”);
return head;
}
printf(“The Contents are:\n”);
temp = head->next; // obtain 1st node of list
while(temp != head)
{
printf(“%d\n”, temp->info);
temp = temp->next;
}
}

Department of Computer Science, RLJIT 45


Circular Doubly Linked List with Header Nodes :
A Circular Doubly Linked List is a variation of doubly linked with a header
node in which:
 The prev field of header contains the address of the last node in the list.
 The next field of header contains the address of the first node in the list.
 The prev field of last node contains the address of its previous node.
 The next field of the last node contains the address of header node of the
list.

Implementation:
struct node
{
int info;
struct node *prev, *next;
}; Department of Computer Science, RLJIT 46
Insertion node at the Beginning:
NODE insertBeg ( NODE head, int item)
{
NODE temp, first;
temp = (NODE) malloc(sizeof(NODE));
temp->info = item;
first = head->next;
temp->prev = head;
head->next = temp;
temp->next = first;
first->prev = temp;
return head;
}

Department of Computer Science, RLJIT 47


Insertion node at the rear end:
NODE insertEnd ( NODE head, int item)
{
NODE temp, last;
temp = (NODE) malloc (sizeof(NODE));
temp->info = item;
last = head->prev;
last->next = temp;
temp->prev = last;
temp->next = head;
head->prev = temp;
return head;
}

Department of Computer Science, RLJIT 48


Delete a node from the front end:
NODE deleteBeg ( NODE head )
{
NODE first, second;
if(head->next == head)
{
printf(“List is empty”);
return;
}
first = head->next;
second = first->next;
head->next = second;
second->prev = head;
printf(“Item deleted = %d\n”,first->info);
free(first);
return head;
}

Department of Computer Science, RLJIT 49


Delete a node from the rear end:
NODE deleteEnd( NODE head )
{
NODE last, temp;
if(head->next == head)
{
printf(“List is empty”);
return;
}
last = head->prev;
temp = last->prev;
head ->prev = temp;
temp->next = head;
printf(“Item deleted = %d\n”, last->info);
free(last);
return head;
}

Department of Computer Science, RLJIT 50


Display contents of CDLL with header node:
void display( NODE head )
{
NODE temp;
if(head->next == head)
{
printf(“List is empty”);
return;
}
printf(“contents are:\n”);
temp = head->next;
while( temp != head )
{
printf(“%d\n”, temp->info);
temp = temp->next;
}
}

Department of Computer Science, RLJIT 51


Stacks using Linked List:
 We know that stack is a special type of data structure, where elements are
inserted at one end and elements are deleted from the same end. That is, if
an element is inserted at front end, then an element has to be deleted from
front end. Or if an element is inserted at rear end, then an element has to
be deleted from the rear end.

 Thus , a stack can be implemented using the following functions:


insertBeg()
deleteBeg()
display()

Or

insertEnd()
deleteEnd()
display()

Department of Computer Science, RLJIT 52


Queues using Linked List:
 A queue is a special type of data structure, where insertion takes place at
one end and elements are deleted at the other end.

 Thus , a queue can be implemented using the following functions:


insertBeg()
deleteEnd()
display()
Or
insertEnd()
deleteBeg()
display()
For double ended queues using linked lists:
 Implemented using the following functions.
insertBeg()
deleteBeg()
insertEnd()
deleteEnd()
display()
Department of Computer Science, RLJIT 53
Applications of Linked List:
1. Evaluation of polynomials
2. Addition of polynomials
3. Multilinked data structure (Sparse Matrices)
4. Arithmetic operations on long positive numbers
5. In symbol table construction (compiler design)

Department of Computer Science, RLJIT 54


Polynomial Representation and Evaluation:
Polynomial Representation using Linked List
Let us assume we have a polynomial consisting of three variables x, y and z.
struct node
{
int cf, px, py, pz;
struct node *next;
};
Where cf is coefficient
typedef struct node *NODE; px, py and pz are store power of x, y and z
Using the above structure, the polynomial
5x3y2z2 + 3y2 + 4x3z Note: Powers ( 0(zero) indicate No term )
Can be represented using singly linked list as shown:

Department of Computer Science, RLJIT 55


Function to insert at the rear end of the list using CSLL:
NODE insertrear ( int cf, int px, int py, int pz, NODE head)
{
NODE temp, cur;
temp = (NODE) malloc (sizeof(NODE));
temp-> cf = cf;
temp-> px = px;
temp-> py = py;
temp-> pz = pz;
cur = head -> next;
while (cur-> next != head)
{
cur = cur-> next;
}
cur -> next = temp; // insert the node at the end
temp -> next = head;
return head;
}
Department of Computer Science, RLJIT 56
How to read a polynomial consisting of n term:
NODE readpoly ( NODE head )
{
int i, n;
int cf, px, py, pz;
printf(“Enter the number of terms in the polynomial\n”);
scanf(“%d”, &n);
for( i = 1; i <= n; i++)
{
printf(“Enter term : %d\n”, i);
printf(“Enter the value of cf, px, py and pz:\n”);
scanf(“%d%d%d%d”,&cf,&px,&py,&pz);
head = insertrear(cf, px, py, pz, head);
}
return head;
}

Department of Computer Science, RLJIT 57


Display a polynomial :
void display ( NODE head)
{
NODE temp;
if ( head-> next == head)
{
printf(“Polynomial doesn’t exist\n”);
}
temp = head->next;
while (temp != head)
{
printf(“%d”, temp->cf);
if(temp->px != 0)
printf(“x^%d”,temp->px);
if(temp->py != 0)
printf(“y^%d”,temp->py);
if(temp->pz != 0)
printf(“z^%d”,temp->pz);
temp = temp->next;
} Department of Computer Science, RLJIT 58
}
Function to evaluate a polynomial
void evaluate ( NODE head )
{
NODE h1;
int x, y, z;
float result=0.0;

h1=head->next;

printf("\nEnter x, y, z, terms to evaluate:\n");


scanf("%d%d%d", &x, &y, &z);
while(h1 != head)
{
result = result + (h1->cf * pow(x,h1->px) * pow(y,h1->py) *
pow(z,h1->pz));
h1=h1->next;
}
printf("\nPolynomial result is: %f", result);
}
Department of Computer Science, RLJIT 59
Algorithm for evaluating a polynomial:
Step 1: Read the value of x, y and z
Step 2: For each term, substitute x, y and z
Step 3: Add all the terms, the final sum is obtained

Algorithm for add 2 polynomials:


For each term of polynomial 1
Step 1: Access each term of polynomial 1
Step 2: Search for power of above term in polynomial 2
Step 3: If found in polynomial 2
Add Coefficient and add sum to polynomial 3
else
Add the term of polynomial 1 to polynomial 3
end for
Add remaining terms of polynomial 2 to polynomial 3
step 4: Copy reaming term from polynomial 2 to polynomial 3

Department of Computer Science, RLJIT 60


Sparse Matrix Representation using multilinked data structre:
A sparse matrix is a matrix that has very few non-zero elements spread
out thinly.
In other words, a matrix in which most of the elements are zeroes is
called a sparse matrix.

Example:

Department of Computer Science, RLJIT 61


The sparse matrix can be more efficiently represented using linked list.
In the linked representation.
 Each column of a sparse matrix is represented as a circularly linked list with
a header node having three fields: down, right and next fields as shown
below.

 Each row of a sparse matrix is represented as a circularly linked list with a


header node having three fields: down, right and next fields as shown
below.

 Each item is represented as a node having 5 fields as shown below.

Department of Computer Science, RLJIT 62


Now, consider the following 5 X 4 matrix. Note: The first node in the above
list identified by the variable head
contains the size of the matrix
where 5 represents the number of
rows, 4 represent the number of
columns and 6 represent non-zero
elements to be manipulated.

The above matrix can be represented using linked list as shown below.

Department of Computer Science, RLJIT 63


Additional List Operations (SLL).
1. Write a function to length of the list or to count the number of nodes
present.
int length ( NODE first)
{
NODE cur;
int count = 0;
if (first == NULL)
return 0;
while ( cur != NULL)
{
cur=cur->next;
count++;
}
return count;
}

Department of Computer Science, RLJIT 64


2. Function to search for an item in a list
void search ( int key, NODE first)
{
NODE cur;
if (first == NULL)
{
printf(“List is empty”);
return ;
}
cur = first;
while ( cur != NULL)
{
if ( key == cur->info)
break;
cur=cur->next;
}
if ( cur == NULL)
{
printf(“search unsuccessful\n”);
return;
}
printf(“search is successful\n”);
Department of Computer Science, RLJIT 65
}
3. Function to delete a node whose information (info) field is specified.
// search for the node to be deleted
NODE deleteinfo( int key, NODE first) prev = NULL
{ cur = first;
NODE prev,cur; while ( cur != NULL)
if (first == NULL) {
{ if ( key == cur->info)
printf(“List is empty”); break;
return NULL; prev = cur;
} cur=cur->next;
if( key == first->info) //if key is }
present in 1st node if ( cur == NULL)
{ // if end of list, key not found
cur = first; {
first = first->next; printf(“search
free (cur); unsuccessful\n”);
return first; return first;
} }
// search successful, then delete
prev->next = cur->next;
free (cur);
return first;
Department of Computer Science, RLJIT 66
}
4. To concatenate two lists:
NODE concat( NODE first, NODE sec)
{
NODE cur;
if (first == NULL)
return sec;
if (sec == NULL)
return first;

cur = first;
while ( cur->next != NULL)
{
cur=cur->next;
}
cur -> next = sec; // attach 1st node of 2nd list to end of 1st list
return first;
}

Department of Computer Science, RLJIT 67


5. To reverse a list (Invert):
NODE reverse( NODE first)
{
NODE cur, temp;
cur = NULL;
while ( first != NULL)
{
temp = first ->next;
first -> next = cur;
cur = first;
first = temp;
}
return cur;
}

Department of Computer Science, RLJIT 68


6. To create an ordered (sorted) list:
NODE insert( int item, NODE first)
{
NODE temp, prev, cur
temp = (NODE) malloc (sizeof(NODE));
temp->info = item;
temp->next = NULL;
if(first == NULL)
return temp;
if(item <= first->info)
{
temp->next = first;
return temp;
}
prev = NULL;
cur = first;
while( cur != NULL && item >cur->info)
{
prev = cur;
cur = cur->next;
}
prev->next = temp;
temp->next = cur;
return first; Department of Computer Science, RLJIT 69
Questions???
1. Define Linked Lists. Explain in detail, the primitive operations performed on
Singly Linked Lists. List the different types of linked lists.
2. Write the following algorithm for SLL.
a) Inserting ITEM as the 1st node in the list.
b) Deleting the node with the given ITEM of information.
3. Write the function to perform the following on SLL: (usually using integer data
type)
a) Creating an ordered SLL. (or create a list)
b) Count number of elements (Length)
c) Search an element
d) Display all the elements in SLL
e) Deleting a node at rear end of SLL.
f) Insert a node at front end of SLL.
g) Inverting a singly linked list. (Reversing)
h) Concatenating the singly linked list. (Ex: LIST_1 and LIST_2 for LIST_3)
i) Assume the list contains 3 nodes with data 10, 20, 30. Insert a node with
data 40 at the end of the list.
j) Insert a node with data 50 between the nodes having data values 10 and 20.
k) Delete the node whose data is 20.
Department of Computer Science, RLJIT
70
Questions???
4. How can an ordinary queue be represented using a singly linked list? Write C
function for linked implementation of ordinary queue insertion & deletion.
5. Explain the following with suitable example: Circular LL and Doubly LL
6. What is Doubly linked list? Write C functions for the following operations on
Doubly Linked List (DLL).
a) Concatenation of 2 DLL.
b) Search the DLL for the given key element.
c) Insert a node at beginning
d) Deleting a node at rear end
e) delete front
f) Inserting a node at specified position.
(OR)
Illustrate with example the following operations on DLL:
1. Inserting a node at the beginning
2. Inserting at the intermediate position
3. Deletion of a node with given value
4. Search a key element
7. What are the advantages of DLL over singly linked list? Explain with an
example. Department of Computer Science, RLJIT 71
Questions???
8. List out the differences between SLL and DLL.
9. Write C program to implement linked stacks
10. Write a note on header linked list. Explain the widely used header lists with
diagrams.
11. Write an algorithm to add 2 polynomials using circular singly linked list
(SLL). And also P(x, y ,z) = 6x2y2z-4yz5+3x2yz-2xyz3
12. Write node structure for linked representation of polynomial. Write function
to add two polynomials represented using linked list.
13. Define sparse matrix. For the given sparse matrix give the linked list
representation.

Department of Computer Science, RLJIT 72

You might also like