Sei sulla pagina 1di 9

Linked list

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

Linked List V/S Array


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 .

Operations on linked list


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.

TYPE OF LINKED LIST:HEADER LINKED LISTA Header linked list is a linked list that always contains a special node at the front of the list. This special node is called a header node. It does not contain actual data item included in the list. But usually contain some useful information about the entire linked list such as total number of nodes in the list (excluding header node), pointer to the last node in the list or to the last node accessed. The header node in the list is never deleted and always points to the first actual node in the list.

Header

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.

DOUBLY LINKED LISTThis type of lists allowed us to move through the list in the forward direction. Although these lists offer some advantages but still remain limited for certain type of applications. Suppose we want to delete a particular node from a linked list whose location LOC is given. Deleting a node in such a case is not efficient because our links point in only one direction and we have no direct way to locate the target nodes predecessor except by traversing the list from the beginning. Another task that is difficult to perform on a singly linked list is traversing the list in the reverse direction. Also suppose we are somewhere in the middle of a long linked list and need to find a node two position to the left of the current node. In such a case we would have to retraverse the list from the beginning to locate the node. To overcome these problems, the linked list is redefined so that each node in the list not only has a pointer to its successor but also its predecessor. Such type of linked list is called a doubly linked list (or two way chain). Since each node of doubly linked list contains two pointers instead of the usual one so we can easily traverse the list in either direction, easily delete a node and easily insert a node anywhere in the list (before a node, after a node, front of the list and end of the list).

MULTI LINKED LISTA multi-linked list is a collection of nodes where each node can have any number of links to other nodes. Using this representation, we can organize collection of elements (records) in different ways. The doubly linked list where each node has only two link parts which are exactly inverse of each other (i.e. one points to the predecessor and other to its successor) is one of the form of multi-linked list.

// Linked List Program #include<stdio.h> #include<conio.h> struct linked { int info; struct linked *next; }; typedef struct linked node; void create(node *list); void traverse(node *list); node *insert(node *list); node *del(node *list); node *search(node *list); void main() { node *loc,*head; int ch; head=(node *)malloc(sizeof(node)); create(head); do { printf("\n enter choice 1 traverse 2 search 3 insert 4 delete 5 exit"); scanf("%d",&ch); switch(ch) { case 1: traverse(head); break; case 2: loc=search(head); if(loc==NULL) { printf("item not found"); } else { printf("item found at location %d",loc); } break; case 3: head=insert(head); break; case 4:

head=del(head); break; } } while(ch!=5); getch(); } void create(node *list) { printf("enter new element -999 for end"); scanf("%d",&list->info); if(list->info==-999) list->next=NULL; else { list->next=(node *)malloc(sizeof(node)); create(list->next); } } void traverse(node *list) { if(list->next!=NULL) { printf("%d\t",list->info); traverse(list->next); } else { printf("%d",list->info); } } node *insert(node *list) { int ch,element; node *new1,*loc,*prv; int item; printf("enter the item for insert"); scanf("%d",&item); new1=(node *)malloc(sizeof(node)); new1->info=item; new1->next=NULL; printf("enter position for insertion 1.start 2.before 3. after 4. end"); scanf("%d",&ch); switch(ch) 8

{ case 1: new1->next=list; list=new1; break; case 2: printf("enter the item before new node"); scanf("%d",&item); loc=list; while(loc->next!=NULL && loc->info!=item) { prv=loc; loc=loc->next; } if(loc->info==item) { new1->next=prv->next; prv->next=new1; } else printf("item not found"); break; case 3: loc=search(list); if(loc==NULL) { printf("enter item not found"); } else { new1->next=loc->next; loc->next=new1; } break; case 4: while(list->next==NULL) { list=list->next; } new1->next=list; list=new1; } return(list); }

node * del(node *list) { int item; node *prv,*loc; printf("enter the item to which you want to delete"); scanf("%d",&item); loc=list; while(loc->next!=NULL && loc->info!=item) { prv=loc; loc=loc->next; } if(loc->info==item) { if(loc==list) { list=loc->next; free(loc); } else prv->next=loc->next; free(loc); } else { printf("item not found"); } return(list); } node * search(node *list) { int item; printf("enter the item for search"); scanf("%d",&item); while(list->next!=NULL) { if(list->info==item) return(list); else list=list->next; } return(NULL); } 9

Potrebbero piacerti anche