Documente Academic
Documente Profesional
Documente Cultură
Linked List
D. S.
Linked List:
A linked list is one of the fundamental data structures, and can be used to implement other data structures. It consists of a sequence of nodes, containing information and one or two references ("links") pointing to the next and/or previous nodes. The main benefit of a linked list over a traditional array is that the order of the linked items may be different from the order that the data items are stored in memory or on disk, allowing the list of items to be traversed in a different order. A linked list is a self-referential data type because it contains a pointer or link to another datum of the same type. Linked lists permit insertion and removal of nodes at any point, which is not true in array, stack or queue. Several different types of linked list exist: singly linked lists, doubly linked lists, and circularly linked lists.
Generally, each node represents a structure, containing variables for information and a structure pointer of itself. We can illustrate structure of a simple node as follows: struct link { int no; struct link *next; }; In above example *next is a pointer of type structure link and thus we can say that it points to itself. The above structure contains a pointer that points to it and it is known as selfreferential structure. The *next can be used to point next node of the list. We should assign NULL to next of last node. Following figure illustrates a singly linked list. // the information part // pointer of structure itself
Add:
100
200 2 300 3
300 400 4
400 NULL
1
Info
200
next
Info
next
Info
next
Info
next
BCA 2
Linked List
D. S.
In above figure weve a linked list with four nodes, these nodes has 1,2,3 and 4 in information part and 200,300,400 and NULL in next part. As from address details it is clear that link part of each node points to next node, and since, there isnt any node after last node, it points to NULL. So, we can say that while traversing when we reach to NULL as next of any node then that node is the last node of the linked list. Here, each node contains a single link to their next node, and thats why such type of list is known as singly linked list. Operations: 1. 2. 3. 4. 5. 6. 7. Create Traverse (Display) Insert (First, Last, Before, After) Edit Delete (First, Last, Specific) Sort Search
Generally, each node represents a structure, containing variables for information and structure pointers of itself. We can illustrate structure of a simple node as follows: struct link { int no; struct link *next, *prev; }; The *next can be used to point next node and *prev can be used to point to the previous node of the current node. We should assign NULL to next of last node and NULL to prev of first node. Following figure illustrates a doubly linked list. // the information part // pointers of structure itself
Add: NULL
100
200
1
Info
200
100
prev
next
prev
Info
next
prev
Info
next
BCA 2
Linked List
D. S.
In above figure weve a linked list with three nodes, these nodes has 1,2 and 3 in information part and 200,300, NULL in next, further they have NULL, 100 and 200 in prev part respectively. As from address details it is clear that next part of each node points to next node, and prev part points to previous node and since, there isnt any node after last node and before first node, prev of first and next of last node points to NULL. Here, each node contains two links for their next and previous node, and thats why such type of list is known as doubly linked list.
Operations: 1. 2. 3. 4. 5. 6. 7. Create Traverse (Display) Insert (First, Last, Before, After) Edit Delete (First, Last, Specific) Sort Search
The circular singly liked list is a modified version of singly linked list and works in same fashion except it has no end, compare to singly linked list. The *next can be used to point next node of the list. We should assign START to next of last node. Following figure illustrates a circular singly linked list.
Add:
100
200 2 300 3
300 400 4
400 100
1
Info
200
next
Info
next
Info
next
Info
next
As from above fig. We can see that above list doesnt have any end, and it works in circular fashion, thats why it is known as circular singly linked list. Operations: 1. 2. 3. 4. 5. 6. Create Traverse (Display) Insert (First / Last, Before, After) Edit Delete (First / Last, Specific) Search
BCA 2
Linked List
D. S.
The circular doubly liked list is a modified version of doubly linked list and works in same fashion except it has no end, compare to doubly linked list. We should assign START to next of last node and address of last node to prev of first node. Following figure illustrates a circular doubly linked list.
Add: 300
100
200
1
Info
200
100
prev
next
prev
Info
next
prev
Info
next
As from above fig. We can see that above list doesnt have any end, and it works in circular fashion, thats why it is known as circular doubly linked list.
Operations: 1. 2. 3. 4. 5. 6. Create Traverse (Display) Insert (First / Last, Before, After) Edit Delete (First / Last, Specific) Search
BCA 2
Linked List
D. S.
Add:
100
3
Info
Header Node
Info Start
next
Info
next
Info
next
Header linked list As from above fig. We can see that the header-linked list is same as other types of linked list, except it contains a header node, which has count of total nodes. Also, we can observe that start is at address 200 even though our first node is at 100. Header node is not considered as an ordinary node in linked list and thats why weve started our linked list from second node instead of header. Basically, there are two types of header-linked lists o Grounded Header Linked List o Circular Header Linked List
Note: Header circular linked list is a combination of header-linked list and circular linked list and has same mechanism as header-linked list.
Operations: 1. 2. 3. 4. 5. 6. 7. Create Traverse (Display) Insert (First / Last, Before, After) Edit Delete (First / Last, Specific) Sort Search
Add: 1
100 200 5
200 NULL
Info
next
Info
next
BCA 2
Linked List
D. S.
o Now suppose we append a node with number 3 then as per simple logic it should go to last position. o But in order linked list as it lies between first and last node it should be placed in between first and last nodes. Add: 1 100 500 3 500 200 5 200 NULL
Info
next
Info
next
Similarly, next appended node will be placed at proper position instead of last position, of course if it will be larger than all other nodes then it will be placed at last position. We can apply different logics of sorting for different data-types like char or strings Operations: 1. 2. 3. 4. 5. 6. Create Traverse (Display) Insert Edit Delete Search
Applications of Linked List: Polynomial manipulation Linked Dictionary Multiple precision arithmatic
Differences:
BCA 2
Linked List
D. S.