ds-unit-3
ds-unit-3
Linked List:
A linked list is a sequence of data structures, which are connected together via
links.
Linked List is a sequence of links which contains items. Each link contains a
connection to another link. Linked list is the second most-used data structure after
array. Following are the important terms to understand the concept of Linked List.
Link − Each link of a linked list can store a data called an element.
Next − Each link of a linked list contains a link to the next link called Next.
LinkedList − A Linked List contains the connection link to the first link
called First.
As per the above illustration, following are the important points to be considered.
Linked List contains a link element called first.
Each link carries a data field(s) and a link field called next.
Each link is linked with its next link using its next link.
Last link carries a link as null to mark the end of the list.
Basic Operations
Following are the basic operations supported by a list.
Insertion − Adds an element at the beginning of the list.
Deletion − Deletes an element at the beginning of the list.
Display − Displays the complete list.
Search − Searches an element using the given key.
Delete − Deletes an element using the given key.
Insertion Operation
Adding a new node in linked list is a more than one step activity. We shall learn
this with diagrams here. First, create a node using the same structure and find the
location where it has to be inserted.
This will put the new node in the middle of the two. The new list should look like
this −
Similar steps should be taken if the node is being inserted at the beginning of the
list. While inserting it at the end, the second last node of the list should point to the
new node and the new node will point to NULL.
Deletion Operation
Deletion is also a more than one step process. We shall learn with pictorial
representation. First, locate the target node to be removed, by using searching
algorithms.
The left (previous) node of the target node now should point to the next node of the
target node −
LeftNode.next −> TargetNode.next;
This will remove the link that was pointing to the target node. Now, using the
following code, we will remove what the target node is pointing at.
TargetNode.next −> NULL;
We need to use the deleted node. We can keep that in memory otherwise we can
simply deallocate memory and wipe off the target node completely.
Reverse Operation
This operation is a thorough one. We need to make the last node to be pointed by
the head node and reverse the whole linked list.
First, we traverse to the end of the list. It should be pointing to NULL. Now, we
shall make it point to its previous node −
We have to make sure that the last node is not the last node. So we'll have some
temp node, which looks like the head node pointing to the last node. Now, we shall
make all left side nodes point to their previous nodes one by one.
Except the node (first node) pointed by the head node, all nodes should point to
their predecessor, making them their new successor. The first node will point to
NULL.
We'll make the head node point to the new first node by using the temp node.
As per the above illustration, following are the important points to be considered.
Doubly Linked List contains a link element called first and last.
Each link carries a data field(s) and two link fields called next and prev.
Each link is linked with its next link using its next link.
Each link is linked with its previous link using its previous link.
The last link carries a link as null to mark the end of the list.
Basic Operations
Following are the basic operations supported by a list.
Insertion − Adds an element at the beginning of the list.
Deletion − Deletes an element at the beginning of the list.
Insert Last − Adds an element at the end of the list.
Delete Last − Deletes an element from the end of the list.
Insert After − Adds an element after an item of the list.
Delete − Deletes an element from the list using the key.
Display forward − Displays the complete list in a forward manner.
Display backward − Displays the complete list in a backward manner.
Insertion Operation
Following code demonstrates the insertion operation at the beginning of a doubly
linked list.
Example
//insert link at the first location
void insertFirst(int key, int data) {
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(isEmpty()) {
//make it the last link
last = link;
} else {
//update first prev link
head->prev = link;
}
Deletion Operation
Following code demonstrates the deletion operation at the beginning of a doubly
linked list.
Example
//delete first item
struct node* deleteFirst() {
head = head->next;
if(isEmpty()) {
//make it the last link
last = link;
} else {
//make link a new last link
last->next = link;
Basic Operations
Insertion Operation
Following code demonstrates the insertion operation in a circular linked list based on single
linked list.
Example
insertFirst(data):
Begin
create a new node
node -> data := data
if the list is empty, then
head := node
next of node = head
else
temp := head
while next of temp is not head, do
temp := next of temp
done
next of node := head
next of temp := node
head := node
end if
End
Deletion Operation
Following code demonstrates the deletion operation in a circular linked list based on single
linked list.
deleteFirst():
Begin
if head is null, then
it is Underflow and return
else if next of head = head, then
head := null
deallocate head
else
ptr := head
while next of ptr is not head, do
ptr := next of ptr
next of ptr = next of head
deallocate head
head := next of ptr
end if
End
Following code demonstrates the display list operation in a circular linked list.
display():
Begin
if head is null, then
Nothing to print and return
else
ptr := head
while next of ptr is not head, do
display data of ptr
ptr := next of ptr
display data of ptr
end if
End
Applications of Linked lists:
Application of linked list in computer science—
n+wl4cd*
Insertlon ct the beginnlng
Insertion at end
. We will traverse the lisr until we find the last node.
. Then we insert the new node to the end of the list.
r Note that we have to consider special cases such as list being empty.
I In case of a list being empty, we will return the updated head of the linked list
because in this case, the inserted node is the first as well as the last node of the
linked list.
now
Insertlon at the cnd
Insertion nfter t given node
We are given the reference to a node, and the new node is inserted after the given node.
i
nswNoda
Insertlon after a given node
{
r
Ileleting a ilde in Llnked List
*******
Polynomials using linked list and Arrays.
Polynomials and Sparse Matrix are two important applications of arrays and linked lists. A
polynomial is composed of different terms where each of them holds a coefficient and an
exponent. This tutorial chapter includes the representation of polynomials using linked lists and
arrays.
What is polynomial?
A polynomial p(x) is the expression in variable x which is in the form (ax n + 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:
Representation of Polynomial
There may arise some situation where you need to evaluate many polynomial expressions and
perform basic arithmetic operations like addition and subtraction with those numbers. For this,
you will have to get a way to represent those polynomials. The simple way is to represent a
polynomial with degree 'n' and store the coefficient of n+1 terms of the polynomial in the array.
So every array element will consist of two values:
Coefficient and
Exponent
#include <iostream>
#include <iomanip.h>
using namespace std;
struct poly {
int coeff;
int pow_val;
poly* next;
};
class add {
poly *poly1, *poly2, *poly3;
public:
add() { poly1 = poly2 = poly3 = NULL; }
void addpoly();
void display();
};
void add::addpoly()
{
int i, p;
poly *newl = NULL, *end = NULL;
cout << "Enter highest power for x\n"; cin >> p;
//Read first poly
cout << "\nFirst Polynomial\n"; for (i = p; i >= 0; i--) {
newl = new poly;
newl->pow_val = p;
cout << "Enter Co-efficient for degree" << i << ":: "; cin >> newl->coeff;
newl->next = NULL;
if (poly1 == NULL)
poly1 = newl;
else
end->next = newl;
end = newl;
}
//Addition Logic
poly *p1 = poly1, *p2 = poly2;
end = NULL;
while (p1 != NULL && p2 != NULL) {
if (p1->pow_val == p2->pow_val) {
newl = new poly;
newl->pow_val = p--;
newl->coeff = p1->coeff + p2->coeff;
newl->next = NULL;
if (poly3 == NULL)
poly3 = newl;
else
end->next = newl;
end = newl;
}
p1 = p1->next;
p2 = p2->next;
}
}
void add::display()
{
poly* t = poly3;
cout << "\n\nAnswer after addition is : ";
while (t != NULL) {
cout.setf(ios::showpos);
cout << t->coeff;
cout.unsetf(ios::showpos);
cout << "X" << t->pow_val;
t = t->next;
}
}
int main()
{
add obj;
obj.addpoly();
obj.display();
}
Output:
Representation of polynomial using linked list:
A polynomial can be thought of as an ordered list of non zero terms. Each non zero term is a
two-tuple which holds two pieces of information:
class polyll {
private:
struct polynode {
float coeff;
int exp;
polynode* link;
} * p;
public:
polyll();
void poly_append(float c, int e);
void display_poly();
void poly_add(polyll& l1, polyll& l2);
~polyll();
};
polyll::polyll()
{
p = NULL;
}
void polyll::poly_append(float c, int e)
{
polynode* temp = p;
if (temp == NULL) {
temp = new polynode;
p = temp;
}
else {
while (temp->link != NULL)
temp = temp->link;
temp->link = new polynode;
temp = temp->link;
}
temp->coeff = c;
temp->exp = e;
temp->link = NULL;
}
void polyll::display_poly()
{
polynode* temp = p;
int f = 0;
A sparse matrix can be represented by using TWO representations, those are as follows...
Triplet Representation
epresentation (Array Representation)
Linked Representation
In linked representation, we use a linked list data structure to represent a sparse matrix. In
this linked list, we use two different nodes namely header node and element node. node Header
node consists of three fields and element node consists of five fields as shown in the image...
Consider the above same sparse matrix used in the Triplet representation. This sparse matrix
can be represented using linked representation as shown in the below image...
In the above representation, H0, H1,..., H5 indicates the header nodes which are used to
represent indexes. Remaining nodes are used to represent non non-zero
zero elements in the matrix,
except the very first node which is used to represent abstract information of the sparse matrix
(i.e., It is a matrix of 5 X 6 with 6 non
non-zero elements).