Sei sulla pagina 1di 9

The main reason for developing linked list is to remove some of the problems faced in the array which are as following 1.) The size of array is fixed due to which either a lot of memory is wasted if we have very few elements in the array or undesirable results are produced if number of elements in the array exceeds the limits of the defined size. 2.) The insertions and deletions of elements is complex and inefficient process as large number of elements need to be shifted to make room for new element or to delete an existing element. So to overcome these limitations, another data structure known as linked list is used Linked list is probably the second most fundamental data structure used after arrays. Linked lists have their own strengths and weaknesses but they happen to be strong where arrays are weak. It can replace array as the basis for implementing other data structures such as stacks and queue. Linked link is frequently used in those situations where we have an unpredictable number of data elements and when we want to insert and delete a large number of elements quickly.

Definition
Linked list is a linear data structure consisting of elements called nodes where each node is composed of two parts : information part and a link part also called the next pointer part. 1. The information part contains user supplied data and 2. The link part which is pointer that points to next node in the linked list. The link part of the last node is set to null that marks the end of the list.

Node

In the figure each node in the linked list is represented by a rectangular box consisting of two parts. The first part contains the information part which contains application data and the second part called the link or the next pointer contains the address of the next pointer contains the address of the next node in the list. The link to the next node is represented by drawing an arrow starting from the link part to the node it is pointing to. The information part of the node may not contain a single data item but may also contain an entire record of data items

1. Insertion and deletion of an element can be performed quickly in linked list as you simple need to rearrange certain pointers once the insertion/removal point is known. 2. Linked list is dynamic data structure as storage space is allocated dynamically on other hand array is a static data structure in which number of element are fixed. 3. In linked list , the time required to access any node depends on the location of that node whereas in arrays an element takes a constant time irrespective of its position. 4. Unlike arrays the successive node in the linked list need not occupy adjacent memory locations. 5. Linked list can be easy joined and splitted as compare to arrays because it simply requires . 6. Linked list require additional space for storing links that connects nodes .

A number of operations can be performed on a liked list these include Traversing : visiting each element in the list Searching : finding the location of a desired element from the list Insertion : to insert a new element in the list Deletion : to remove an existing element from the list Sorting : to arrange the elements of a list in a particular order

TRAVERSING
Traversing involves processing each node of the linked list exactly once from the beginning to the end following the chain of references. A linked list stored in memory with pointer HEAD pointing to the first node and first node and NULL indicating the end of the list. While traversing we need to process each node and for this we use a pointer variable that keeps track of the current node being processed. In order to process the next node we need to update this pointer variable so that it point to the next node.

list->info

list->next

## while(list->next!=null) { printf(%d,list->info); list=list->next; }

SEARCHING
searching is one of the simplest operation of the linked list. It is the process of finding the location of the node containing the desired item in the linked list. Search is considered successful if the node containing the item is found and unsuccessful otherwise. To search a given item from the linked list, we start from the beginning of the list and compare the given item with the information part of each node. The searching continues until either the item is found or the end of the list is encountered.
while(list->next!=NULL) { if(list->info==item) { return(list); } else { list=list->next; } } return(NULL); 3

INSERTION
Insertion is among the most frequent operation performed on the linked list. Insertion can be performed on a sorted or unsorted linked list. Insertions into an unsorted linked list is generally performed either at the beginning or at the end or in between after or before an item. In sorted linked list, new data is inserted in such a way that the ordering of the list maintained.

Insertion at beginning
new1->next=list; list=new1;

## Insertion before a node

new1->next=prv->next; prv->next=new1;

## insertion after a node

new1->next=loc->next; loc->next=new1; } break;

Insertion at end
while(list->next==NULL) { list=list->next; } new1->next=list; list=new1; }

DELETION
Deletion is another frequent operation performed on the linked list. The delete operation logically removes a node from the linked list by changing various link pointers. Once the node is deleted, the space taken by the node in the linked list must be reclaimed and added to the free storage list. In order to logically delete a node, it is important to first locate the target node (node to be deleted). However to delete a node, we not only require a pointer pointing to the target node but also require a pointer pointing to its predecessor. This is because once we delete the target node there is no way to reconnect its predecessor to the successor due to forward chaining property of the singly linked list. When we find the target and its predecessor, we simply need to change link part of the predecessor to point to the target nodes successor.

CIRCULAR LISTIn this list each node (except the first one) has a unique predecessor and each node (except the last one) has a unique successor. The main drawback of singly linked list is that from the given node, we can access all the nodes that follow it but not the one preceding it. To overcome this problem, we make slight modifications in the singly linked list by replacing the null pointer in the last node of the list with the address of its first node. Such a list is called a singly linked circular list or simply circular linked list.