Unit-2 - Linked List

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

LINKED LISTS

Linked lists and arrays are similar since they both store collections of data. Array is the
most common data structure used to store collections of elements. Arrays are convenient
to declare and provide the easy syntax to access any element by its index number. Once
the array is set up, access to any element is convenient and fast. The disadvantages of
arrays are:

The size of the array is fixed. Most often this size is specified at compile time. This
makes the programmers to allocate arrays, which seems "large enough" than
required.

Inserting new elements at the front is potentially expensive because existing


elements need to be shifted over to make room.

Deleting an element from an array is not possible.

Linked lists have their own strengths and weaknesses, but they happen to be strong
where arrays are weak. Generally array's allocates the memory for all its elements in one
block whereas linked lists use an entirely different strategy. Linked lists allocate memory
for each element separately and only when necessary.

Here is a quick review of the terminology and rules of pointers. The linked list code
will depend on the following functions:

malloc() is a system function which allocates a block of memory in the "heap" and
returns a pointer to the new block. The prototype of malloc() and other heap functions
are in stdlib.h. malloc() returns NULL if it cannot fulfill the request. It is defined by:

void *malloc (number_of_bytes)

Since a void * is returned the C standard states that this pointer can be converted to
any type. For example,
char *cp;
cp = (char *) malloc (100);

Attempts to get 100 bytes and assigns the starting address to cp. We can also use the
sizeof() function to specify the number of bytes. For example,

int *ip;
ip = (int *) malloc (100*sizeof(int));
free() is the opposite of malloc(), which de-allocates memory. The argument to free() is
a pointer to a block of memory in the heap a pointer which was obtained by a malloc()
function. The syntax is:

free (ptr);

The advantage of free() is simply memory management when we no longer need a


block.

3.1. Linked List Concepts:

A linked list is a non-sequential collection of data items. It is a dynamic data structure.


For every data item in a linked list, there is an associated pointer that would give the
memory location of the next data item in the linked list.

The data items in the linked list are not in consecutive memory locations. They may be
anywhere, but the accessing of these data items is easier as each data item contains the
address of the next data item.

Advantages of linked lists:

Linked lists have many advantages. Some of the very important advantages are:

1. Linked lists are dynamic data structures. i.e., they can grow or shrink during
the execution of a program.
2. Linked lists have efficient memory utilization. Here, memory is not pre-
allocated. Memory is allocated whenever it is required and it is de-allocated
(removed) when it is no longer needed.
3. Insertion and Deletions are easier and efficient. Linked lists provide flexibility
in inserting a data item at a specified position and deletion of the data item
from the given position.
4. Many complex applications can be easily carried out with linked lists.

Disadvantages of linked lists:

1. It consumes more space because every node requires a additional pointer to


store address of the next node.
2. Searching a particular element in list is difficult and also time consuming.

3.2. Types of Linked Lists:

Basically we can put linked lists into the following four items:

1. Single Linked List.


2. Double Linked List.
3. Circular Linked List.

A single linked list is one in which all nodes are linked together in some sequential
manner. Hence, it is also called as linear linked list.
A double linked list is one in which all nodes are linked together by multiple links which
helps in accessing both the successor node (next node) and predecessor node (previous
node) from any arbitrary node within the list. Therefore each node in a double linked list
has two link fields (pointers) to point to the left node (previous) and the right node (next).
This helps to traverse in forward direction and backward direction.

A circular linked list is one, which has no beginning and no end. A single linked list can
be made a circular linked list by simply storing address of the very first node in the link
field of the last node.

A circular double linked list is one, which has both the successor pointer and predecessor
pointer in the circular manner.

Comparison between array and linked list:

ARRAY LINKED LIST

Size of an array is fixed Size of a list is not fixed

Memory is allocated from stack Memory is allocated from heap

It is necessary to specify the number of It is not necessary to specify the


elements during declaration (i.e., during number of elements during declaration
compile time). (i.e., memory is allocated during run
time).
It occupies less memory than a linked It occupies more memory.
list for the same number of elements.
Inserting new elements at the front is Inserting a new element at any position
potentially expensive because existing can be carried out easily.
elements need to be shifted over to
make room.
Deleting an element from an array is Deleting an element is possible.
not possible.

Trade offs between linked lists and arrays:

FEATURE ARRAYS LINKED LISTS

Sequential access efficient efficient

Random access efficient inefficient

Resigning inefficient efficient

Element rearranging inefficient efficient

Overhead per elements none 1 or 2 links


Applications of linked list:

1. Linked lists are used to represent and manipulate polynomial. Polynomials are
expression containing terms with non zero coefficient and exponents. For
example:

P(x) = a0 Xn + a1 Xn-1 n-1 X + an

2. Represent very large numbers and operations of the large number such
as addition, multiplication and division.

3. Linked lists are to implement stack, queue, trees and graphs.

4. Implement the symbol table in compiler construction

3.3. Single Linked List:

A linked list allocates space for each element separately in its own block of memory called
a "node". The list gets an overall structure by using pointers to connect all its nodes
together like the links in a chain. Each node contains two fields; a "data" field to store
whatever element, and a "next" field which is a pointer used to link to the next node.
Each node is allocated in the heap using malloc(), so the node memory continues to exist
until it is explicitly de-allocated using free(). The front of the list is a pointer to
.

A single linked list is shown in figure 3.2.1.

HEAP

10 200 20 300 30 400 40 X


100 200 400
The start
pointer holds
Stores the next
the address the data. node address. The next field of the
of the first last node is NULL.
node of the
list.

Figure 3.2.1. Single Linked List

The beginning of the linked list is stored in a "start" pointer which points to the first
node. The first node contains a pointer to the second node. The second node contains a
pointer to the third node, ... and so on. The last node in the list has its next field set to
NULL to mark the end of the list. Code can access any node in the list by starting at the
start and following the next pointers.

The start pointer is an ordinary local pointer variable, so it is drawn separately on the
left top to show that it is in the stack. The list nodes are drawn on the right to show that
they are allocated in the heap.
Implementation of Single Linked List:

Before writing the code to build the above list, we need to create a start node, used to
create and access other nodes in the linked list. The following structure definition willdo
(see figure 3.2.2):

Creating a structure with one data item and a next pointer, which will be pointing
to next node of the list. This is called as self-referential structure.

Initialise the start pointer to be NULL.

struct slinklist
{
int data; node: data next
struct slinklist* next;
};

typedef struct slinklist node; start


Empty list: NULL
node *start = NULL;

Figure 3.2.2. Structure definition, single link node and empty list

The basic operations in a single linked list are:

Creation.
Insertion.
Deletion.
Traversing.

Creating a node for Single Linked List:

Creating a singly linked list starts with creating a node. Sufficient memory has to be
allocated for creating a node. The information is stored in the memory, allocated by using
the malloc() function. The function getnode(), is used for creating a node, after allocating
memory for the structure of type node, the information for the item (i.e., data) has to be
read from the user, set next field to NULL and finally returns theaddress of the node.
Figure 3.2.3 illustrates the creation of a node for single linked list.

node* getnode()
{ newnode
node* newnode;
newnode = (node *) malloc(sizeof(node)); 10 X
printf("\n Enter data: "); 100
scanf("%d", &newnode -> data);
newnode -> next = NULL;
return newnode;
}

Figure 3.2.3. new node with a value of 10


The following steps are to be followed to create

Get the new node using getnode().


newnode = getnode();

If the list is empty, assign new node as start.


start = newnode;

If the list is not empty, follow the steps given below:

The next field of the new node is made to point the first node (i.e.
start node) in the list by assigning the address of the first node.

The start pointer is made to point the new node by assigning the
address of the new node.

Figure 3.2.4 shows 4 items in a single linked list stored at different locations in
memory.

start
100

10 200 20 300 30 400 40 X


100 200 300 400

Figure 3.2.4. Single Linked List with 4 nodes

vo id createlist(int n)
{
int i;
node * new node;
node *temp;
for(i = 0; i < n ; i+ +)
{
new node = getnode();
if(start = = NULL)
{
start = new node;
}
else
{
temp = start;
while(temp - > next != NULL)
temp = temp - > next;
temp - > next = new node;
}
}
}
Insertion of a Node:

One of the most primitive operations that can be done in a singly linked list is the insertion
of a node. Memory is to be allocated for the new node (in a similar way that isdone while
creating a list) before reading the data. The new node will contain empty data field and
empty next field. The data field of the new node is then stored with the information read
from the user. The next field of the new node is assigned to NULL. The new node can then
be inserted at three different places namely:

Inserting a node at the beginning.


Inserting a node at the end.
Inserting a node at intermediate position.

Inserting a node at the beginning:

The following steps are to be followed to insert a new node at the beginning of the list:

Get the new node using getnode().


newnode = getnode();

If the list is empty then start = newnode.

If the list is not empty, follow the steps given below:


newnode -> next = start;
start = newnode;

Figure 3.2.5 shows inserting a node into the single linked list at the beginning.

start
500

10 200 20 300 30 400 40 X


100 200 300 400

100

Figure 3.2.5. Inserting a node at the beginning

The function insert_at_beg(), is used for inserting a node at the beginning

void insert_at_beg()
{
node *newnode;
newnode = getnode();
if(start == NULL)
{
start = newnode;
}
else
{
newnode -> next = start;
start = newnode;
}
}
Inserting a node at the end:

The following steps are followed to insert a new node at the end of the list:

Get the new node using getnode()


newnode = getnode();

If the list is empty then start = newnode.

If the list is not empty follow the steps given below:


temp = start;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = newnode;

Figure 3.2.6 shows inserting a node into the single linked list at the end.

start
100

10 200 20 300 30 400 40 500

50

Figure 3.2.6. Inserting a node at the end.

The function insert_at_end(), is used for inserting a node at the end.

void insert_at_end()
{
node *newnode, *temp;
newnode = getnode();
if(start == NULL)
{
start = newnode;
}
else
{
temp = start;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = newnode;
}
}

Inserting a node at intermediate position:

The following steps are followed, to insert a new node in an intermediate position in the
list:

Get the new node using getnode().


newnode = getnode();
Ensure that the specified position is in between first node and last node. If
not, specified position is invalid. This is done by countnode() function.

Store the starting address (which is in start pointer) in temp and prev pointers.
Then traverse the temp pointer upto the specified position followed by prev
pointer.

After reaching the specified position, follow the steps given below:
prev -> next = newnode;
newnode -> next = temp;

Let the intermediate position be 3.

Figure 3.2.7 shows inserting a node into the single linked list at a specified intermediate
position other than beginning and end.

start temp
100

10 200 20 500 30 400 40

50 300
500 new node

Figure 3.2.7. Inserting a node at an intermediate position.

The function insert_at_mid(), is used for inserting a node in the intermediate position.

void insert_at_mid()
{
node *newnode, *temp, *prev;
int pos, nodectr, ctr = 1;
newnode = getnode();
printf("\n Enter the position: ");
scanf("%d", &pos);
nodectr = countnode(start);
if(pos > 1 && pos < nodectr)
{
temp = prev = start;
while(ctr < pos)
{
prev = temp;
temp = temp -> next;
ctr++;
}
prev -> next = newnode;
newnode -> next = temp;
}
else
{
printf("position %d is not a middle position", pos);
}
}
Deletion of a node:

Another primitive operation that can be done in a singly linked list is the deletion of a
node. Memory is to be released for the node to be deleted. A node can be deleted from
the list from three different places namely.

Deleting a node at the beginning.


Deleting a node at the end.
Deleting a node at intermediate position.

Deleting a node at the beginning:

The following steps are followed, to delete a node at the beginning of the list:

If the list is not empty, follow the steps given below:


temp = start;
start = start -> next;
free(temp);

Figure 3.2.8 shows deleting a node at the beginning of a single linked list.

start
200

X
200 300 400
temp

Figure 3.2.8. Deleting a node at the beginning.

The function delete_at_beg(), is used for deleting the first node in the list.

void delete_at_beg()
{
node *temp;
if(start == NULL)
{
printf("\n No nodes are exist..");
return ;
}
else
{
temp = start;
start = temp -> next;
free(temp);
printf("\n Node deleted ");
}
}
Deleting a node at the end:

The following steps are followed to delete a node at the end of the list:

If the list is not empty, follow the steps given below:

temp = prev = start;


while(temp -> next != NULL)
{
prev = temp;
temp = temp -> next;
}
prev -> next = NULL;
free(temp);

Figure 3.2.9 shows deleting a node at the end of a single linked list.

start
100

10 200 20 300 30 X 40
100 200 300

Figure 3.2.9. Deleting a node at the end.

The function delete_at_last(), is used for deleting the last node in the list.

void delete_at_last()
{
node *temp, *prev;
if(start == NULL)
{
printf("\n Empty List..");
return ;
}
else
{
temp = start;
prev = start;
while(temp -> next != NULL)
{
prev = temp;
temp = temp -> next;
}
prev -> next = NULL;
free(temp);
printf("\n Node deleted ");
}
}
Deleting a node at Intermediate position:

The following steps are followed, to delete a node from an intermediate position in the
list (List must contain more than two node).

If the list is not empty, follow the steps given below.


if(pos > 1 && pos < nodectr)
{
temp = prev = start;
ctr = 1;
while(ctr < pos)
{
prev = temp;
temp = temp -> next;
ctr++;
}
prev -> next = temp -> next;
free(temp);
printf("\n node deleted..");
}

Figure 3.2.10 shows deleting a node at a specified intermediate position other than
beginning and end from a single linked list.

Start
100

10 20 30 40

Figure 3.2.10. Deleting a node at an intermediate position.

The function delete_at_mid(), is used for deleting the intermediate node in the list.

void delete_at_mid()
{
int ctr = 1, pos, nodectr;
node *temp, *prev;
if(start == NULL)
{
printf("\n Empty List..");
return ;
}
else
{
printf("\n Enter position of node to delete: ");
scanf("%d", &pos);
nodectr = countnode(start);
if(pos > nodectr)
{
printf("\nThis node doesnot exist");
}
if(pos > 1 && pos < nodectr)
{
temp = prev = start;
while(ctr < pos)
{
prev = temp;
temp = temp -> next;
ctr ++;
}
prev -> next = temp -> next;
free(temp);
printf("\n Node deleted..");
}
else
{
printf("\n Invalid position..");
getch();
}

}
}

Traversal and displaying a list (Left to Right):

To display the information, you have to traverse (move) a linked list, node by node from
the first node, until the end of the list is reached. Traversing a list involves the following
steps:

Assign the address of start pointer to a temp pointer.


Display the information from the data field of each node.

The function traverse() is used for traversing and displaying the information stored in
the list from left to right.

void traverse()

node *temp;
temp = start;
printf("\n The contents of List (Left to Right):
\n"); if(start == NULL )
printf("\n Empty List");
else

while (temp != NULL)

printf("%d ->", temp -> data);


temp = temp -> next;

printf("X");

Alternatively there is another way to traverse and display the information. That is in
reverse order. The function rev_traverse(), is used for traversing and displaying the
information stored in the list from right to left.
void rev_traverse(no de *st)
{
if(st = = NULL)
{
return;
}
else
{
rev_traverse(st - > next);
printf("%d - >", st - > data);
}
}

Counting the Number of Nodes:

The following code will count the number of nodes exist in the list using recursion.

int countnode(node *st)


{
if(st = = NULL)
return 0;
else
return(1 + countnode(st - > next));
}

3.4. Using a header node:

A header node is a special dummy node found at the front of the list. The use of header
node is an alternative to remove the first node in a list. For example, the picture below
shows how the list with data 10, 20 and 30 would be represented using a linked list
without and with a header node:

100

10 200 20 300 30 X
100 200 300

Single Linke d List without a header node

sta rt
400

100 10 200 20 300 30 X


400 100 200 300

Single Linked List with header node

Note that if your linked lists do include a header node, there is no need for the special
case code given above for the remove operation; node n can never be the first node in
the list, so there is no need to check for that case. Similarly, having a header node can
simplify the code that adds a node before a given node n.
Note that if you do decide to use a header node, you must remember to initialize an
empty list to contain one (dummy) node, you must remember not to include the header
node in the count of "real" nodes in the list.

It is also useful when information other than that found in each node of the list is needed.
For example, imagine an application in which the number of items in a list is often
calculated. In a standard linked list, the list function to count the number of nodes has
to traverse the entire list every time. However, if the current length is maintained in a
header node, that information can be obtained very quickly.

3.5. Array based linked lists:

Another alternative is to allocate the nodes in blocks. In fact, if you know the maximum
size of a list a head of time, you can pre-allocate the nodes in a single array. The result
is a hybrid structure an array based linked list. Figure 3.5.1 shows an example of null
terminated single linked list where all the nodes are allocated contiguously in an array.

start a
100 b

200 300 X
c
100 200

Conceptual structure d

Implementation

Figure 3.5.1. An array based linked list

3.6. Double Linked List:

A double linked list is a two-way list in which all nodes will have two links. This helps in
accessing both successor node and predecessor node from the given node position. It
provides bi-directional traversing. Each node contains three fields:

Left link.
Data.
Right link.

The left link points to the predecessor node and the right link points to the successor
node. The data field stores the required data.

Many applications require searching forward and backward thru nodes of a list.
For example searching for a name in a telephone directory would need forward
and backward scanning thru a region of the whole list.

The basic operations in a double linked list are:

Creation.
Insertion.
Deletion.
Traversing.
A double linked list is shown in figure 3.3.1.

Stores the previous


node address.

start
X 200 100 20 300 200 30 X
100 200
The start
pointer holds
the address Stores the next
of the first last node is NULL.
node of the
list.

Figure 3.3.1. Double Linked List

The beginning of the double linked list is stored in a "start" pointer which points to the
set to NULL.

The following code gives the structure definition:

struct dlinklist
node: left data right
struct dlinklist *left;
int data;
struct dlinklist *right;

}; start

typedef struct dlinklist node; Empty list: NULL


node *start = NULL;
Figure 3.4.1. Structure definition, double link node and empty list

Creating a node for Double Linked List:

Creating a double linked list starts with creating a node. Sufficient memory has to be
allocated for creating a node. The information is stored in the memory, allocated by using
the malloc() function. The function getnode(), is used for creating a node, after allocating
memory for the structure of type node, the information for the item (i.e., data) has to be
read from the user and set left field to NULL and right field also set to NULL (see figure
3.2.2).

node* getnode()
{
node* newnode;
newnode = (node *) malloc(sizeof(node));
printf("\n Enter data: "); X 10 X
scanf("%d", &newnode -> data);
newnode -> left = NULL;
newnode -> right = NULL;
return newnode;
}
The following

Get the new node using getnode().

newnode =getnode();

If the list is empty then start = newnode.

If the list is not empty, follow the steps given below:

The left field of the new node is made to point the previous node.

The previous nodes right field must be assigned with address of the
new node.

void createlist(int n)
{
int i;
node * new node;
node *tem p;
for(i = 0; i < n; i+ +)
{
new node = getnode();
if(start = = NULL)
{
start = new node;
}
else
{
temp = start;
while(temp - > right)
temp = temp - > right;
tem p - > right = new no de;
new node - > left = temp;
}
}
}

Figure 3.4.3 shows 3 items in a double linked list stored at different locations.

start
100

X 10 200 100 20 300 200 30 X


200

Figure 3.4.3. Double Linked List with 3 nodes


Inserting a node at the beginning:

The following steps are to be followed to insert a new node at the beginning of the list:

Get the new node using getnode().

newnode=getnode();

If the list is empty then start = newnode.

If the list is not empty, follow the steps given below:

newnode -> right = start;


start -> left = newnode;
start = newnode;

The function dbl_insert_beg(), is used for inserting a node at the beginning. Figure
3.4.4 shows inserting a node into the double linked list at the beginning.

start
400

400 10 200 100 20 300 200 30 X


200

X 40 100

400

Figure 3.4.4. Inserting a node at the beginning

Inserting a node at the end:

The following steps are followed to insert a new node at the end of the list:

Get the new node using getnode()

newnode=getnode();

If the list is empty then start = newnode.

If the list is not empty follow the steps given below:

temp = start;
while(temp -> right != NULL)
temp = temp -> right;
temp -> right = newnode;
newnode -> left = temp;

The function dbl_insert_end(), is used for inserting a node at the end. Figure 3.4.5
shows inserting a node into the double linked list at the end.
start

100

X 10 200 100 20 300 200 30 400

200

300 40 X
400

Figure 3.4.5. Inserting a node at the end

Inserting a node at an intermediate position:

The following steps are followed, to insert a new node in an intermediate position in the
list:

Get the new node using getnode().

newnode=getnode();

Ensure that the specified position is in between first node and last node. If
not, specified position is invalid. This is done by countnode() function.

Store the starting address (which is in start pointer) in temp and prev pointers.
Then traverse the temp pointer upto the specified position followed by prev
pointer.

After reaching the specified position, follow the steps given below:

newnode -> left = temp;


newnode -> right = temp -> right;
temp -> right -> left = newnode;
temp -> right = newnode;

The function dbl_insert_mid(), is used for inserting a node in the intermediate position.
Figure 3.4.6 shows inserting a node into the double linked list at a specified intermediate
position other than beginning and end.

Start
100 40 200
100

400

X 10 400 400 20 300


200
100

200 30
300
Deleting a node at the beginning:

The following steps are followed, to delete a node at the beginning of the list:

If the list is not empty, follow the steps given below:

temp = start;
start = start -> right;
start -> left = NULL;
free(temp);

The function dbl_delete_beg(), is used for deleting the first node in the list. Figure
3.4.6 shows deleting a node at the beginning of a double linked list.

start
200

X 10 200 X 20 300 200 30 X


200

Figure 3.4.6. Deleting a node at beginning

Deleting a node at the end:

The following steps are followed to delete a node at the end of the list:

If the list is not empty, follow the steps given below:

temp = start;
while(temp -> right != NULL)
{
temp = temp -> right;
}
temp -> left -> right = NULL;
free(temp);

The function dbl_delete_last(), is used for deleting the last node in the list. Figure 3.4.7
shows deleting a node at the end of a double linked list.

start
100

X 10 200 100 20 X

200

Figure 3.4.7. Deleting a node at the end


Deleting a node at Intermediate position:

The following steps are followed, to delete a node from an intermediate position in the
list (List must contain more than two nodes).

If the list is not empty, follow the steps given below:

Get the position of the node to delete.

Ensure that the specified position is in between first node and last
node. If not, specified position is invalid.

Then perform the following steps:


if(pos > 1 && pos < nodectr)
{
temp = start;
i = 1;
while(i < pos)
{
temp = temp -> right;
i++;
}
temp -> right -> left = temp -> left;
temp -> left -> right = temp -> right;
free(temp);
printf("\n node deleted..");
}

The function delete_at_mid(), is used for deleting the intermediate node in the list. Figure
3.4.8 shows deleting a node at a specified intermediate position other than beginning
and end from a double linked list.

Start
100

10 300 100 20 300 100 30

100 200

Figure 3.4.8 Deleting a node at an intermediate position

Traversal and displaying a list (Left to Right):

To display the information, you have to traverse the list, node by node from the first
node, until the end of the list is reached. The function traverse_left_right() is used for
traversing and displaying the information stored in the list from left to right.

The following steps are followed, to traverse a list from left to right:

If the list is not empty, follow the steps given below:


temp = start;
while(temp != NULL)
{
print temp -> data;
temp = temp -> right;
}

Traversal and displaying a list (Right to Left):

To display the information from right to left, you have to traverse the list, node by node
from the first node, until the end of the list is reached. The function traverse_right_left()
is used for traversing and displaying the information stored in the list from right to left.
The following steps are followed, to traverse a list from right to left:

If the list is not empty, follow the steps given below:


temp = start;
while(temp -> right != NULL)
temp = temp -> right;
while(temp != NULL)
{
print temp -> data;
temp = temp -> left;
}

Counting the Number of Nodes:

The following code will count the number of nodes exist in the list (using recursion).

int countnode(node *start)


{
if(start = = NULL)
return 0;
else
return(1 + countnode(start - >right ));
}

3.7. Circular Single Linked List:

It is just a single linked list in which the link field of the last node points back to the
address of the first node. A circular linked list has no beginning and no end. It is necessary
to establish a special pointer called start pointer always pointing to the first node of the
list. Circular linked lists are frequently used instead of ordinary linked list because many
operations are much easier to implement. In circular linked list no null pointers are used,
hence all pointers contain valid address.

A circular single linked list is shown in figure 3.6.1.


start
100

10 200 20 300 30 400 40 100

200 300 400

Figure 3.6.1. Circular Single Linked List


The basic operations in a circular single linked list are:

Creation.
Insertion.
Deletion.
Traversing.

The following steps

Get the new node using getnode().

newnode = getnode();

If the list is empty, assign new node as start.

start = newnode;

If the list is not empty, follow the steps given below:

temp = start;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = newnode;

newnode -> next = start;

Inserting a node at the beginning:

The following steps are to be followed to insert a new node at the beginning of the
circular list:

Get the new node using getnode().

newnode = getnode();

If the list is empty, assign new node as start.

start = newnode;
newnode -> next = start;

If the list is not empty, follow the steps given below:

last = start;
while(last -> next != start)
last = last -> next;
newnode -> next = start;
start = newnode;
last -> next = start;
The function cll_insert_beg(), is used for inserting a node at the beginning. Figure 3.6.2
shows inserting a node into the circular single linked list at the beginning.

start
500

10 200 20 300 30 400 40 500


100

5 100

500

Figure 3.6.2. Inserting a node at the beginning

Inserting a node at the end:

The following steps are followed to insert a new node at the end of the list:

Get the new node using getnode().

newnode = getnode();

If the list is empty, assign new node as start.

start = newnode;
newnode -> next = start;

If the list is not empty follow the steps given below:

temp = start;
while(temp -> next != start)
temp = temp -> next;
temp -> next = newnode;
newnode -> next = start;

The function cll_insert_end(), is used for inserting a node at the end.

Figure 3.6.3 shows inserting a node into the circular single linked list at the end.

start
100

10 200 20 300 30 400 40 500

50 100

Figure 3.6.3 Inserting a node at the end.


Deleting a node at the beginning:

The following steps are followed, to delete a node at the beginning of the list:

If the list is not empty, follow the steps given below:

last = temp = start;


while(last -> next != start)
last = last -> next;
start = start -> next;
last -> next = start;

After deleting the node, if the list is empty then start = NULL.

The function cll_delete_beg(), is used for deleting the first node in the list. Figure 3.6.4
shows deleting a node at the beginning of a circular single linked list.

start
200

10 20 30
40 200
200 300 400
temp

Figure 3.6.4. Deleting a node at beginning.

Deleting a node at the end:

The following steps are followed to delete a node at the end of the list:

If the list is not empty, follow the steps given below:

temp = start;
prev = start;
while(temp -> next != start)
{
prev = temp;
temp = temp -> next;
}
prev -> next = start;

After deleting the node, if the list is empty then start = NULL.

The function cll_delete_last(), is used for deleting the last node in the list.
Figure 3.6.5 shows deleting a node at the end of a circular single linked list.

start
100

10 200 20 300 30 100 40 100

100 200 300

Figure 3.6.5. Deleting a node at the end.

Traversing a circular single linked list from left to right:

The following steps are followed, to traverse a list from left to right:

If

If the list is not empty, follow the steps given below:

temp = start;
do
{
printf("%d ", temp -> data);
temp = temp -> next;
} while(temp != start);

Traversal Application of Lists: Polynomial Representation and Addition.

Polynomial Manipulation

Polynomial manipulations are one of the most important applications of linked lists.
Polynomials are an important part of mathematics not inherently supported as a data
type by most languages. A polynomial is a collection of different terms, each comprising
coefficients, and exponents. It can be represented using a linked list. This representation
makes polynomial manipulation efficient.

While representing a polynomial using a linked list, each polynomial term represents a
node in the linked list. To get better efficiency in processing, we assume that the term
of every polynomial is stored within the linked list in the order of decreasing exponents.
Also, no two terms have the same exponent, and no term has a zero coefficient and
without coefficients. The coefficient takes a value of 1.

Each node of a linked list representing polynomial constitute three parts:

o The first part contains the value of the coefficient of the term.
o The second part contains the value of the exponent.
o The third part, LINK points to the next term (next node).
The structure of a node of a linked list that represents a polynomial is shown
below:

Consider a polynomial P(x) = 7x2 + 15x3 - 2 x2 + 9. Here 7, 15, -2, and 9 are the
coefficients, and 4,3,2,0 are the exponents of the terms in the polynomial. On
representing this polynomial using a linked list, we have

Observe that the number of nodes equals the number of terms in the polynomial. So we
have 4 nodes. Moreover, the terms are stored to decrease exponents in the linked list.
Such representation of polynomial using linked lists makes the operations like
subtraction, addition, multiplication, etc., on polynomial very easy.

Addition of Polynomials:

To add two polynomials, we traverse the list P and Q. We take corresponding terms of
the list P and Q and compare their exponents. If the two exponents are equal, the
coefficients are added to create a new coefficient. If the new coefficient is equal to 0,
then the term is dropped, and if it is not zero, it is inserted at the end of the new linked
list containing the resulting polynomial. If one of the exponents is larger than the other,
the corresponding term is immediately placed into the new linked list, and the term with
the smaller exponent is held to be compared with the next term from the other list. If
one list ends before the other, the rest of the terms of the longer list is inserted at the
end of the new linked list containing the resulting polynomial.

Let us consider an example an example to show how the addition of two polynomials is
performed,

P(x) = 3x4 + 2x3 - 4 x2 + 7

Q (x) = 5x3 + 4 x2 - 5

These polynomials are represented using a linked list in order of decreasing exponents
as follows:
To generate a new linked list for the resulting polynomials that is formed on the addition
of given polynomials P(x) and Q(x), we perform the following steps,

1. Traverse the two lists P and Q and examine all the nodes.
2. We compare the exponents of the corresponding terms of two polynomials. The first
term of polynomials P and Q contain exponents 4 and 3, respectively. Since the exponent
of the first term of the polynomial P is greater than the other polynomial Q, the term
having a larger exponent is inserted into the new list. The new list initially looks as shown
below:

3. We then compare the exponent of the next term of the list P with the exponents of the
present term of list Q. Since the two exponents are equal, so their coefficients are added
and appended to the new list as follows:

4. Then we move to the next term of P and Q lists and compare their exponents. Since
exponents of both these terms are equal and after addition of their coefficients, we get
0, so the term is dropped, and no node is appended to the new list after this,

5. Moving to the next term of the two lists, P and Q, we find that the corresponding terms
have the same exponents equal to 0. We add their coefficients and append them to the
new list for the resulting polynomial as shown below:

You might also like