Professional Documents
Culture Documents
Abstract
Data Types
Doubly
Single
Linked
Linked List
List
LISTS
Circular
Polynomial
Linked
ADT
List
2
INTRODUCTION
Each node consists of two fields : data field and pointer field.
elem node
An abstract data type (ADT) is a
set of operations.
Objects such as lists, sets, and graphs, along with their operations, can
be viewed as abstract data types, just as integers, reals, and Booleans
are data types.
5
LIST ADT :
•First() : Returns the address of the first node in the linked list so the
other nodes to the right of it can be accessed.
•Last() : Returns the address of the last node in the linked list so the
other nodes to the right of it can be accessed.
A B C D 8
Singly Linked List (contd..)
(NULL)
Head B C D
• The very first node of the linked list is called Header node where no
data is stored, but holds the address of the very first data element.
• The elements can be traversed from left to right. Hence, it is also
called as one-way list.
9
Singly Linked List Operations
11
a) Insertion of a Node at the Front
ALGORITHM
1. Get a memory block of type NODE and store the starting address
of NODE in a pointer variable NEW.
i.e. NEW=GETNODE(NODE)
2. a) NEW.LINK = HEADER.LINK
b) NEW.DATA = X
c) HEADER.LINK = NEW
3. stop
12
b) Insertion of a Node at the End
ALGORITHM
1. Get a memory block of type NODE and store the starting
address
of this NODE in a pointer variable NEW.
i.e. NEW=GETNODE(NODE)
2. If NEW = NULL then print “Memory is Insufficient”
exit
Else
a) ptr = HEADER
b) ptr = ptr.LINK
13
3) When prt.LINK becomes NULL, ptr is the last node of the singly
linked list.
i. ptr.LINK = new
ii. new.data = X
iii. Make the LINK field of the new node to be NULL
i.e., make this new node as the last node.
NULL=new.LINK
14
c) Insertion of a Node at any position
ALGORITHM
1. Get a memory block of type NODE and store the address of it in a
pointer variable NEW.
i.e. NEW=GETNODE(NODE)
2. If NEW = NULL then print “Memory is Insufficient”
exit
Else
a) ptr = HEADER
b) Get the data item of the Node key after which the new node has
to be placed.
c) Now traverse the singly linked list from the HEADER node
i.e., ptr = ptr.LINK
15
3. If ( ptr.LINK = NULL)
print “ key is not available in the list”
exit.
Else
a) Make the LINK field of the new node to point the node
pointed out by ptr.
i.e., NEW.LINK = ptr.LINK
b) Place the data X in the DATA field of the NEW node.
i.e., NEW.DATA = X
c) Now make the LINK field of the ptr node to point the
NEW node. ptr.LINK=NEW
4. EXIT
16
2. Traversing through Singly Linked list
• Visiting each node in the singly linked list starting from the first
node to the last node.
ALGORITHM
1. Get the first node address from the pointer field of the HEADER
node. i.e., ptr = HEADER.LINK
2. Now traverse the singly linked list as long as ptr is not NULL.
i.e. while (ptr is not NULL) do steps (a) and (b).
a) Print the data in the ptr node.
i.e. print ptr.DATA.
b) Go to the next node of ptr.
i.e. ptr = ptr.LINK.
3. Stop. 17
3. Deletion of a Node
18
a) Deletion of a Node at the Front
ALGORITHM
1. Get the address of the first node from the link field of the HEADER node.
i.e. ptr = HEADER.LINK
2. If ( ptr is NULL )
print “ The singly linked list is empty”
EXIT.
3. Otherwise do the following:
a) Get the address of the node which is pointed out by the first node.
i.e., ptr = ptr.LINK
b) Now the HEADER node should be made to point to the second node.
i.e. , HEADER.LINK = ptr.LINK.
c) Deallocate or free the space allocated for the ptr node.
i.e., return (ptr) to the free store.
4. stop
19
b) Deletion of a Node at the End
ALGORITHM
1. Start from the HEADER node. i.e., ptr = HEADER.
2. If the LINK field of HEADER node is NULL
print “ the list is empty; no deletion”.
3. Otherwise do the following :
a) Traverse the singly linked list from the first node to the last
node
to find the last but one node.
i.e., while (ptr.LINK is not NULL)
i. ptr1 = ptr.
ii. Ptr = ptr.LINK
b) ptr1.LINK = NULL
c) ptr is the last node of the singly linked list. 20
4. Stop.
c) Deletion of a Node at any position
ALGORITHM
22
Copy A Linked List To Make A Duplicate
• The entire singly linked list is copied into another by allocating
new memory
space.
ALGORITHM:
a) Allocate memory space for a node and store the starting address of it in NEW
i.e., NEW = GETNODE(NODE)
b) Move the DATA field of ptr node to the NEW node
i.e., NEW.DATA = ptr.DATA
c) Make the LINK field of ptr1 node to point to the NEW node
i.e., ptr1.LINK = NEW
d) Make the LINK field of the NEW node to be NULL
i.e., NEW.LINK = NULL
e) Make the NEW node to be the current node of the duplicate linked list.
i.e., ptr1 = NEW
f) Go to the next node of the original linked list
i.e., ptr = ptr.LINK 24
5. Stop.
Merging of two singly linked list into one list
• Two singly linked lists are combined to form a
singly linked list.
ALGORITHM:
1. Start from the HEADER node of the first singly linked list
i.e., ptr = HEADER1
2. Traverse the first singly linked list until the last node of this is
reached.
a) Go to the next node.
i.e., ptr = ptr.LINK
3. Once the last node is reached (ptr), make the LINK field of this last node to point
to the HEADER2 node’s LINK content
i.e., ptr.LINK = HEADER2.LINK
4. Now return the HEADER2 node to the free store.
5. Now HEADER1 is the HEADER node of the merged singly linked list.
i.e., HEADER = HEADER1. 25
6. Stop.
Search for an element in a singly linked list
• The nodes in a singly linked list are traversed to find a node
with the data X
ALGORITHM:
1. a) Start from the HEADER node. i.e., ptr = HEADER
b) Go to the first node in the singly linked list. i.e., ptr = ptr.LINK
2. Assume loc = NULL. Get the data to be searched – X
3. Now traverse the singly linked list from the first node to the last node.
a) If the DATA field of ptr node is X.
i. Loc = ptr
ii. Return loc
iii. Go to step 5
otherwise
i. Go to the next node of the singly linked list
4. If there is no match. Print “ Search is not successful”
5. Stop.
26
Doubly linked list
• Also called as two-way lists or two-way chains where it is
possible to traverse either from left to right or from right to left.
• Each node can have any number of data fields and two link fields
which are used to point to the previous node and next node.
• There are two NULL: at the first and last nodes in the list.
• Advantages:
• Given a node, it is easy to visit its predecessor.
• Convenient to traverse lists backwards
A B C
27
Head
• It is convenient to traverse lists backwards. Add an extra field
to the data structure, containing a pointer to the previous cell.
The cost of this is an extra link, adds to the space requirement
and also doubles the cost of insertions and deletions because
there are more pointers to fix.
• On the other hand, it simplifies deletion, because you no
longer have to refer to a key by using a pointer to the previous
cell; this information is now at hand.
Operations in a Doubly linked list
Insertion
Deletion
29
Function to check whether the list is empty or Function to check whether the element is in the last position
not int is_last (List L, position P)
int is_empty(list L) {
{ return(PtrRlink==NULL);
return(HeaderRlink==NULL); }
error(“List is empty”);
}
10 20 40 55 70
REAR 32
Circular linked lists (contd..)
• Circular linked lists are usually sorted.
Applications:
• Circular linked lists are useful for playing video and sound files
in “looping” mode.
• They are also a stepping stone to implementing graphs.
Advantages:
• Any node can be accessible from any other node by chaining
10 20 40 55 70
• Doubly Linked Circular List : The last node points to the first node
and there are two links between the
nodes of the linked list.
A B C
Head 34
Operations on a Circular linked list
Insertion
Traversing
Deletion
Copying
Merging
Searching
35
Function to Insert an element Function to Delete an element
The position of the previous element is obtained by
void insert(Element X,List L,position Ptr) calling the find Llink() function.
{ void delete(Element X, List L)
position Ptr, new; {
New =malloc(sizeof(struct node)); Position Ptr, toDel;
if(New=NULL) Ptr=findLlink(X,L);
Error(“Out of space”); toDel=PtrRlink;
NewElement=x; PtrRlink=toDelRlink;
If(ptrRlink=F) free(toDel);
NewRlink=PtrRlink; }
PtrRlink=New;
}
LINKED STACK
• In the linked list implementation, we would:
• Maintain the stack as a linked list.
• A pointer variable top points to the start of the list.
• The first element of the linked list is considered as the
stack top.
STACK: LINKED LIST STRUCTURE
PUSH OPERATION
top
STACK: LINKED LIST STRUCTURE
POP OPERATION
top
DECLARATION STACK CREATION
STRUCT LIFO
VOID CREATE (STACK **TOP)
{ {
INT VALUE; *TOP = NULL;
STRUCT LIFO *NEXT;
}; /* TOP POINTS TO NULL,
TYPEDEF STRUCT LIFO INDICATING EMPTY
STACK; STACK */
}
STACK *TOP;
PUSHING AN ELEMENT POPPING AN ELEMENT FROM THE
INTO THE STACK STACK
INT POP (STACK *TOP)
VOID PUSH (STACK *TOP, INT ELEMENT) {
{ INT T;
STACK *NEW; STACK *P;
NEW = MALLOC(SIZEOF(STACK)); IF (*TOP == NULL)
IF (TOP == MAX) {
{ PRINTF (“\N STACK IS EMPTY”);
PRINTF (“\N STACK IS FULL”);
EXIT(-1);
}
EXIT(-1); ELSE
} {
NEW->VALUE = ELEMENT; T = (*TOP)->VALUE;
NEW->NEXT = *TOP; P = *TOP;
*TOP = NEW; *TOP = (*TOP)->NEXT;
} FREE (P);
RETURN T;
}
}
LINKED QUEUE
• Create a linked list to which items would be added to one
end and deleted from the other end.
• Two pointers will be maintained:
• One pointing to the beginning of the list (point from
where elements will be deleted).
• Another pointing to the end of the list (point where new
elements will be inserted).
Rear
front rear
QUEUE: LINKED LIST STRUCTURE
DEQUEUE
front rear
DECLARATION QUEUE CREATION
• Implementation of stacks and queues
• Implementation of graphs : adjacency list representation of graphs is most
popular which is uses linked list to store adjacent vertices.
• Dynamic memory allocation : we use linked list of free blocks.
• Maintaining directory of names
• Performing arithmetic operations on long integers
• Representing sparse matrices
• Manipulation of polynomials by storing constants in the node of linked list
• APPLICATIONS OF LINKED LIST IN REAL WORLD-
• Image viewer – previous and next images are linked, hence can be
accessed by next and previous button.
• Previous and next page in web browser – we can access previous and
next url searched in web browser by pressing back and next button
since, they are linked as linked list.
• Music player – songs in music player are linked to previous and next
song. You can play songs either from starting or ending of the list.
• APPLICATIONS OF CIRCULAR LINKED LISTS:
N
• AN ADT FOR SINGLE-VARIABLE POLYNOMIALS f ( x) ai x i
i 0
• ARRAY IMPLEMENTATION
THE POLYNOMIAL ADT…
• Acceptable if most of the coefficients aj are nonzero, undesirable if this is not the case
• E.G. Multiply
P1 ( x) 10 x1000 5 x14 1
P2 ( x) 3x1990 2 x1492 11 x 5
• Most of the time is spent multiplying zeros and stepping through nonexistent
parts of the input polynomials
• Implementation using a singly linked list
• Each term is contained in one cell, and the cells are sorted in decreasing order of
exponents
Void mult_polynomial( POLYNOMIAL poly1, POLYNOMIAL poly2, POLYNOMIAL
Poly_prod )
{
Unsigned int i, j;
Zero_polynomial( poly_prod );
Poly_prodhigh_power = poly1high_power + poly2high_power;
If( poly_prodhigh_power > MAX_DEGREE )
Error("exceeded array size");
Else
For( i=0; i<=poly1high_power; i++ )
For( j=0; j<=poly2high_power; j++ )
Poly_prodcoeff_array[i+j] += poly1coeff_array[i] * poly2coeff_array[j];
Poly_prodpower[i+j] = poly1power[i] + poly2power[j];
}
DYNAMIC MEMORY MANAGEMENT
• Any processing requires memory to be allocated for their storage. New strings may be Input or
created, old strings discarded, and strings in general may expand or contract during this
Processing. This requires some means of allocating storage in segments of variable size, and
"Recycling" unused space for new data.
There are four design criteria for a memory allocator.
a) Efficiency of representation
b) speed of allocation
c) speed of "recycling“
d) utilization of memory.
THE BOUNDARY TAG REPRESENTATION
• Even though there is a large contiguous chunk of free space, the memory
manager perceives it as two smaller segments and so may falsely conclude that it
has insufficient free space to satisfy a large request.
• The task of identifying and Merging adjacent free segments should be done when
a segment is released.
• IDENTIFICATION: The simplest identification method would be to have the
neighbors' "used/free" flags kept in these locations. This means that each
segment needs "used/ free" flags at both ends. There are advantages to Storing a
length value rather than a pointer.
• COALESCENCE:To merge two or three adjacent blocks, it is necessary to change
all of their bookkeeping information. It is straightforward to find the beginning
of the following segment. To find the beginning of the preceding segment, either
a pointer to the beginning of the segment, or an offset
BOUNDARY TAG METHOD
ALLOCATING MEMORY
• Free list- a data structure to keep track of the “holes” between the allocated memory.
4-65
EXAMPLES OF GENERALIZED LISTS
• A = ( ): the null, or empty, list; its length is zero.
• B = (a, (b, c)): a list of length two; its first element is the
atom a, and its second element is the linear list (b, c).
• C = (b, b, ( )): a list of length three whose first two
elements are the list b, and the third element is the null list.
• D = (a, d): is a recursive list of length two; d corresponds
to the infinite list d = (a, (a, (a, …))).
• Lists may be shared by other lists.
• Lists may be recursive.
4-66