Sunteți pe pagina 1din 7

BCA 2

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.

Types of Linked Lists Singly Linked List:


The first type of Linked list is singly linked list. A singly linked list is a type of linked list, whose nodes have two parts: o Information o Pointer to next node Here information can be anything; it can contain any data, int, char, float, structure etc The following fig. Displays a single node of a singly linked list Info Link (next)

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

Singly linked list

Christ College Rajkot

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

Doubly Linked List:


The second type of Linked list is doubly linked list. A doubly linked list is a type of linked list, whose nodes have three parts: o Information o Pointer to next node o Pointer to previous node Here information can be anything; it can contain any data, int, char, float, structure etc The following fig. Displays a single node of a doubly linked list Link (prev) Info Link (next)

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

300 300 200 3 NULL

1
Info

200

100

prev

next

prev

Info

next

prev

Info

next

Doubly linked list

Christ College Rajkot

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

Circular Singly Linked List:


The third type of Linked list is circular singly linked list. The following fig. Displays a single node of a circular singly linked list Info start Link (next)

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

Circular Singly linked list

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

Christ College Rajkot

BCA 2

Linked List

D. S.

Circular Doubly Linked List:


The fourth type of Linked list is circular doubly linked list. The following fig. Displays a single node of a circular doubly linked list Link (prev) start Info Link (next)

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

300 300 200 3 100

1
Info

200

100

prev

next

prev

Info

next

prev

Info

next

Circular Doubly linked list

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

Header Linked List:


The fifth type of Linked list is header-linked list. This type of linked list is different from others because it contains an extra node that resides at head of the list and contains some important information. For ex. if we want to calculate total no. of nodes in a linked list, then weve to traverse all the nodes and calculate them. So, if we can store no. of nodes in header node then we can easily display that no. without traversing all nodes. Similarly, there are other cases where we can store some important information about linked list in header node and avoid some un-necessary operations. Following fig. Displays a header-linked list.

Christ College Rajkot

BCA 2

Linked List

D. S.

Add:

100

3
Info

200 200 next 1 300 2 300 400 3 400 NULL

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

Ordered Linked List:


The sixth and last type of Linked list is ordered linked list. The ordered linked is similar to other linked lists except it maintains order of nodes by their information. For example if weve a linked list with numbers then all the nodes are arranged in ascending or descending order depending upon program logic, that is whenever a new record is inserted to the linked list it goes to its appropriate place instead of going to last position. Following figs. Illustrates an ordered linked list: o Let us assume that weve a linked list with two numbers as follows:

Add: 1

100 200 5

200 NULL

Info

next

Info

next

Christ College Rajkot

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 Ordered linked list

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:

Linked lists vs. arrays


Linked lists have several advantages over arrays. Elements can be inserted into linked lists indefinitely, while an array will eventually either fill up or need to be resized, which is sometimes not possible. Similarly, an array from which many elements are removed may become wastefully empty or need to be made smaller. On the other hand, arrays allow random access, while linked lists allow only sequential access to elements. Singly linked lists, in fact, can only be traversed in one direction. This makes linked lists unsuitable for some applications. Another disadvantage of linked lists is the extra storage needed for references, which often makes them impractical for lists of small data items such as characters.

Christ College Rajkot

BCA 2

Linked List

D. S.

Doubly-linked vs. singly-linked


Double-linked lists require more space per node, and their elementary operations are more expensive; but they are often easier to manipulate because they allow sequential access to the list in both directions. In particular, one can insert or delete a node in a constant number of operations given only that node's address. (Compared with singly linked lists, which require the previous node's address in order to correctly insert or delete.) Some algorithms require access in both directions.

Circularly-linked vs. linearly-linked


Circular linked lists are most useful for describing naturally circular structures, and have the advantage of regular structure and being able to traverse the list starting at any point. They also allow quick access to the first and last records through a single pointer (the address of the last element). Their main disadvantage is the complexity of iteration, which has subtle special cases.

Linked list vs. Header linked list


Simple linked list contains information and link(s), while header linked list contains an extra node called header at the top of linked list. Header node in header-linked list contains important information about list, that makes traversal and some basic operations to be performed quickly compare to simple linked list. Simple linked list doesnt have any specific node to store header information, and some operations take more time and are expensive compare to header-linked list.

Christ College Rajkot

S-ar putea să vă placă și