Module 3-1
Module 3-1
Module-3
Linked List
3.1 Introduction
Linked list is a collection of zero or more nodes ,where each node has some information. Given the
address of the first node, any node in the list can be obtained.
Evey node consis of two parts one is the information part and the other is the address of the next node.
The pointer of the last node contains a special value called NULL
CSE@HKBKCE 1
Module-3
3. Insertion and deletion is efficient
To create a linked list of integers the node can be defined as follows using a self referential
structure.
Sruct Node
{
int info;
struct Node * link;
};
Typedef struct Node NODE;
After the node is created we have to create a new empty list as follows
node * first=NULL;
• The pointer first stores the address of the first node in the list. With this information we will
be able to access the location of all the other nodes in the list.
• To obtain a node we use the statement
• First=(node*) malloc(sizeof(node)
• To place the information 5 ,we can use the statement Firs->info= 5;
• As there are no other nodes in the list the link part can be made NULL as follow
• First->link=NULL
CSE@HKBKCE 2
Module-3
The node created is shown is represented as follows
First 5
Instead of using the malloc function the following getnode() function can be used to get a new
node
NODE * getNode(void)
{
/* provide a node for use */
NODE * new;
if (avail)
{
new = avail;
avail = avail→link;
return new;
}
else
{
new=( NODE *)malloc(sizeof(NODE));
return new;
}
}
Instead of the free function the following retnode function can be used
CSE@HKBKCE 3
Module-3
reinsert the space into the free storage list. This is done when a list is implemented by linear arrays. But
this method may be too time consuming for the operating system of the computer. So an alternate method
is devised.
The operating system of a computer may periodically collect all the deleted space on to the free storage
list this technique si called garbage collection
}
}
}
}
Delete the nodes from a linked list pointed by first whose information part is specified is item
prev=NULL;
cur=first;
while (cur!=NULL)
{
If (cur->info==item)
{
prev->link=cur->link;
free(cur);
return(first);
}
else
{
prev=cur;
cur=cur->link;
}
}
Printf(“node with item not found”)
return(first);
}
Delete the NODE present at location loc, the NODE that precedes is present at location locp. If
there is only one NODE then locp=NULL
if (first==NULL)
printf(“list is empty);
else
{
cur=first
while(cur!=NULL)
{
Printf(“%d \t”, cur->info);
cur=cur->link;
}
}
if (first==NULL)
{
printf(“list is empty);
return(0)
}
cur=first
while(cur!=NULL)
{
count++
cur=cur->link;
CSE@HKBKCE 8
Module-3
}
return(count)
}
CSE@HKBKCE 10
Module-3
struct node
{
int info;
struct node *link;
};
typedef struct node NODE;
NODE * front,*rear;
front=NULL;
rear=NULL;
temp->info=item;
temp->link=NULL;
if (front==NULL)
{
rear=temp;
front=temp;
}
else
{
rear->link=temp;
rear=temp;
}
}
Function deletes the NODE in the front and returns the item
int del_front(NODE * front)
{
NODE cur;
int itemdel;
if(front==NULL)
{
printf("Queue underflow\n");
return front;
}
cur=front;
itemdel=cur->info;
CSE@HKBKCE 11
Module-3
front=front->link;
free(cur);
return(itemdel);
}
Notice that the direction of links for both the stack and the queue facilitate easy insertion and deletion of
NODEs. We can easily add or delete a NODE from the top of the stack. we can easily add a NODE to
the rear of the queue and delete a NODE at the front.
stack * top[MAX_STACKS]
We assume that the initial condition for the stacks is: top[i] = NULL, 0≤i < MAX_STACKS
and the boundary condition is: top[i] = NULL if the ith stack is empty
Function push creates a new NODE, temp, and inserts the NODE in front of the ith stack.
CSE@HKBKCE 12
Module-3
void push(int i, int item)
{/* add item to the ith stack */
stack * temp;
temp=(stack*)malloc(sizeof(stack))
temp→data = item;
temp→link = top[i];
top[i] = temp;
}
Function pop returns the top element and changes top to point to the address contained in its link
field.
int pop(int i)
{/* remove top element from the ith stack */
int itemdel;
Stack * temp;
if (top[i]==NULL)
return stackEmpty();
temp = top[i];
itemdel = temp→data;
top[i] = top[i]→link;
free(temp);
return item;
}
We assume that the initial condition for the queues is: front[i] = NULL, ,rear[i]=NULL0 ≤ i <
MAX_QUEUES
and the boundary condition is: front[i] = NULL iff the ith queue is empty
}
}
Function deleteq deletes the item in the front of the ith queue
int deleteq(int i)
{/* delete an element from queue i */
Queue * temp;
int itemdel
if (front[i]==NULL)
return queueEmpty();
temp = front[i];
itemdel = temp→data;
front[i]= front[i]→link;
free(temp);
return itemdel;
}
last
By keeping a pointer at the last instead of the front we can now insert easily at the front and end of the
list
CSE@HKBKCE 14
Module-3
void insertFront(NODE *last, NODE * New)
{/* insert an item at the front of the circular list whose last NODE is last */
if (last==NULL)
{
/* list is empty, change last to point to new entry */
last = new;
last→link = last;
}
else
{
/* list is not empty, add new entry at front */
new→link = last→link;
last→link = new;
}
}
if (last==NULL)
{
/* list is empty, change last to point to new entry */
last = new;
last→link = last;
}
else
{
/* list is not empty, add new entry at front */
New→link = last→link;
last→link = New;
last=New;
}
}
return count;
}
Note: Unless stated it is assumed that the linked list is circular header list.
Each NODE now has two link fields, one linking in the forward direction and the other linking in the
backward direction.
A NODE in a doubly linked list has at least three fields, a left link field (llink), a data field (data),
CSE@HKBKCE 16
Module-3
and a right link field (rlink). It can be represented as follows
A doubly linked list may or may not be circular. The data field of the header NODE usually contains no
information.
struct NODE
{
strunct NODE * llink;
int data;
struct NODE * rlink;
};
CSE@HKBKCE 17
Module-3
typedef struct NODE DNODE;
The function dinsert() inserts a newNODE into a doubly linked list after a NODE pointed by ptr
The function ddelete() deletes a NODE from a doubly linked list pointed by head
Space Efficiency: We have the overhead of storing two pointers for each element.
3.11. Polynomials
Polynomial Representation
We should be able to represent any number of different polynomials as long as memory is available. In
general, A polynomial is represented as :
A(x)= am-1xm-1 +……….a0x0
where the ai are nonzero coefficients and the ei are nonnegative integer exponents such that
CSE@HKBKCE 18
Module-3
We represent each term as a NODE containing coefficient and exponent fields, as well as a pointer to
the next term. Assuming that the coefficients are integers, the type declarations are:
struct polyNode {
int coef;
int expon;
struct polyNode * link;
};
Typedef struct polyNode POLY;
Consider the polynomials a = 3x14 + 2x8 + 1x+2 and b = 8x12- 3x10 + 10x5 +3 It can be represented as
follows
a 3 14 2 8 1 1 2 0
b 8 12 3 10 10 5 3 0
To add two polynomials, we examine their terms starting at the NODEs pointed to by a and b.
1. If a→expon = b→expon, we add the two coefficients a→coef + b→coef and create a new term
for the result c. a = a→link; b = b→link;
2. If a→expon < b→expon, then we create a duplicate term of b, attach this term to the result,called
c, and b = b→link;
3. If a→expon > b→expon, then we create a duplicate term of a, attach this term to the result,
called c, and a = a→link;
POLY *Pointer padd(POLY * a, POLY * b) /* return a polynomial which is the sum of a and b */
{
POLY * c,*tempa, *tempb,*lastc;
int sum;
c= (POLY*)malloc(sizeof(POLY))
c->link=NULL
tempa=a
tempb=b
lastc=c;
while (tempa!=NULL && tempb!=NULL)
CSE@HKBKCE 19
Module-3
{
switch (COMPARE(tempa→expon, tempb→expon))
{
case -1: lastc=attach(tempb→coef, tempb→expon,lastc);
tempb = tempb→link;
break;
return(c);
}
CSE@HKBKCE 20
Module-3
If m > 0 and n > 0, the while loop is entered. Each iteration of the loop requires O(1) time.At each
iteration, either a or b moves to the next term or both move to the next term. Since the iteration
terminates when either a or b reaches the end of the list, therefore, the number of iterations is bounded
by m + n - 1.
The time for the remaining two loops is bounded by O(n + m). The first loop can iterate m times and
the second can iterate n times. So, the asymptotic computing time of this algorithm is O(n +m).
CSE@HKBKCE 21
Module-3
• Each header NODE is in three lists: a list of rows, a list of columns, and a list of header NODEs. The
list of header NODEs also has a header NODE that has the same structure as an entry NODE.
• The row and col value of the header NODE consist of the dimension of the matrix
Example:
Consider the sparse matrix shown below.
Since there are two different types of NODEs a union is used to create the appropriate data structure. The
necessary C declarations are as follows:
CSE@HKBKCE 22
Module-3
#define MAX_SIZE 50 /*size of largest matrix*/
CSE@HKBKCE 23