Professional Documents
Culture Documents
Chapter Four
Linked Lists
If the memory is allocated for the variable during the compilation (i.e. before execution) of a
program, then it is fixed and cannot be changed. For example, an array A[100] is declared with
100 elements, then the allocated memory is fixed and cannot be decreased and increased the
SIZE of the array if required. So we have to adopt an alternative strategy to allocate memory
only when it is required. There is a special data structure called linked list that provides a more
flexible storage system and it does not require the use of arrays.
A linked list is a linear collection of specially designed data elements, called nodes, linked to one
another by means of pointers. Each node is divided into two parts: the first part contains the
information of the elements, and the second part contains the address of the next node in the
linked list. Address part of the node is also called linked or next field.
PTR
PTRData=5 50 NULL 0
Figure1. Nodes
Example
1
Complied By Abraham W.
Data Structure and Algorithms For 2nd Year Computer Science students
Each node is pictured with two parts. The left part of each node contains the data items and the
right part represents the address of the next node; there is an arrow drawn from it to the next
node. The next pointer of the last node contains a special value, called the NULL pointer, which
does not point to any address of the node. That is NULL pointer indicates the end of the linked
list. START pointer will hold the address of the 1st node in the list START =NULL if there is no
list (i.e.; NULL list or empty list).
2
Complied By Abraham W.
Data Structure and Algorithms For 2nd Year Computer Science students
Consider a list A having n nodes and B with m nodes. Then the operation concatenation will
place the 1st node of B in the (n+1)th node in A. After concatenation A will contain (n+m) nodes
4.1 Review on Pointers, Dynamic Memory allocation and De-allocation
Although C++ supports all the functions (i.e., malloc, calloc, realloc and free) used in C, it also
defines two unary operators new and delete that performs the task of allocating and freeing the
memory in a better and easier way.
An object (or variable) can be created by using new, and destroyed by using delete, as and when
required. A data object created inside a block with new, will remain in existence until it is
explicitly destroyed by using delete. Thus, the lifetime of an object is directly under our control
and is unrelated to the block structure of the program.
The new operator can be used to create objects of any type. It takes the following general form:
Pointer_Variable = new data_type;
Here, Pointer_Variable is a pointer of type data_type. The new operator allocates sufficient
memory to hold a data object of type data_type and returns the address of the object. The
data_type may be any valid data type. The Pointer_Variable holds the address of the memory
space allocated. For example:
int *Var1 = new int;
float *Var2 = new float;
Where Var1 is a pointer of type int and Var2 is a pointer of type float.
When a data object is no longer needed, it is destroyed to release the memory space for reuse.
The general form of its use is:
delete Pointer_Variable;
The Pointer_Variable is the pointer that points to a data object created with new.
delete Var1;
delete Var2
4.2 Types of Linked Lists(Singly Linked List and Doubly Linked List)
Basically we can divide the linked list into the following three types in the order in which they
(or node) are arranged.
Singly linked list
Doubly linked list
Circular linked list
3
Complied By Abraham W.
Data Structure and Algorithms For 2nd Year Computer Science students
struct node
4
Complied By Abraham W.
Data Structure and Algorithms For 2nd Year Computer Science students
int age ;
};
Suppose START is the first position in linked list. Let DATA be the element to be inserted in the
new node. POS is the position where the new node is to be inserted. TEMP is a temporary
pointer to hold the node address.
Insert a Node at the beginning
1. Input DATA to be inserted
2. Create a NewNode
3. NewNode → DATA = DATA
4. If (SATRT equal to NULL)
(a) NewNode → Link = NULL
5. Else
(a) NewNode → Link = START
6. START = NewNode
7. Exit
Insert a Node at the end
1. Input DATA to be inserted
2. Create a NewNode
3. NewNode → DATA = DATA
4. NewNode → Next = NULL
8. If (SATRT equal to NULL)
(a) START = NewNode
9. Else
(a) TEMP = START
(b) While (TEMP Next not equal to NULL)
(i) TEMP = TEMP Next
10. TEMPNext = NewNode
11. Exit
Insert a Node at any specified position
1. Input DATA and POS to be inserted
2. Initialize TEMP = START; and j = 0
3. Repeat the step 3 while (k is less than POS)
(a) TEMP = TEMP Next
(b) If (TEMP is equal to NULL)
5
Complied By Abraham W.
Data Structure and Algorithms For 2nd Year Computer Science students
Suppose START is the first position in linked list. Let DATA be the element to be deleted. TEMP, HOLD is a
temporary pointer to hold the node address.
1. Input the DATA to be deleted
2. If ((STARTDATA) is equal to DATA)
(a) TEMP = START
(b) START = STARTNext
(c) Set free the node TEMP, which is deleted
(d) Exit
3. HOLD = START
4. While ((HOLDNextNext) not equal to NULL))
(a) if ((HOLDNEXTDATA) equal to DATA)
(i) TEMP = HOLDNext
(ii) HOLDNext = TEMPNext
(iii) Set free the node TEMP, which is deleted
(iv) Exit
(b) HOLD = HOLDNext
5. if ((HOLDnextDATA) == DATA)
(a) TEMP = HOLDNext
(b) Set free the node TEMP, which is deleted
(c) HOLDNext = NULL
(d) Exit
6. Disply “DATA not found”
7. Exit
6
Complied By Abraham W.
Data Structure and Algorithms For 2nd Year Computer Science students
The program that will implement all the operations of singly linked list coded and compiled in
Borland C++
7
Complied By Abraham W.
Data Structure and Algorithms For 2nd Year Computer Science students
8
Complied By Abraham W.
Data Structure and Algorithms For 2nd Year Computer Science students
9
Complied By Abraham W.
Data Structure and Algorithms For 2nd Year Computer Science students
10
Complied By Abraham W.
Data Structure and Algorithms For 2nd Year Computer Science students
Suppose START is the first position in linked list. Let DATA be the element to be inserted in the new
node. POS is the position where the NewNode is to be inserted. TEMP is a temporary pointer to hold the
node address.
1. Input the DATA and POS
2. Initialize TEMP = START; i = 0
3. Repeat the step 4 if (i less than POS) and (TEMP is not equal to NULL)
4. TEMP = TEMPRPoint; i = i +1
5. If (TEMP not equal to NULL) and (i equal to POS)
(a) Create a New Node
(b) NewNodeDATA = DATA
(c) NewNodeRPoint = TEMPRPoint
(d) NewNodeLPoint = TEMP
(e) (TEMPRPoint)LPoint = NewNode
(f ) TEMPRPoint = New Node
6. Else
(a) Display “Position NOT found”
7. Exit
Algorithm for deleting a node
Suppose START is the address of the first node in the linked list. Let POS is the position of the node to be deleted.
TEMP is the temporary pointer to hold the address of the node. After deletion, DATA will contain the information
on the deleted node.
1. Input the POS
2. Initialize TEMP = START; i = 0
3. Repeat the step 4 if (i less than POS) and (TEMP is not equal to NULL)
4. TEMP = TEMPRPoint; i = i +1
5. If (TEMP not equal to NULL) and (i equal to POS)
(a) Create a New Node
(b) NewNodeDATA = DATA
(c) NewNodeRPoint = TEMPRPoint
(d) NewNodeLPoint = TEMP
(e) (TEMPRPoint)LPoint = NewNode
(f ) TEMPRPoint = New Node
6. Else
(a) Display “Position NOT found”
11
Complied By Abraham W.