14
MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS (MEFGI) FACULTY OF COMPUTER APPLICATIONS TOPIC : LINKED LIST SUB : FOP (610001) (MCA – I) 1 Prepared By : Ashwin R. Dobariya Introduction of Linked list concept Arrays are probably the most common data structures used to store large numbers of homogeneous data elements. It is easy to use and access the elements of any array using appropriate index of elements. However, arrays have some disadvantages also and they are as follows. (1)Fixed size : The size of an array is fixed. With a little extra effort, by dynamically allocating an array in the heap, specifying the size of the array can be deferred until the array is created at runtime, but after that it remains fixed. Arrays can be dynamically resized with the function realloc(), but that requires some programming effort. (2) Wastage of space : When user stores numbers of elements less that the size of an array then it leads to wastage of space. (3)Sequential storage : An array allocates memory for all its elements in sequence as one block of memory. For arrays, continues space is required. If the program ever needs to process larger number of elements, the code will crash. (4)Possibility of overflow : If the program ever needs to process more that the size, there is a possibility of overflow and the code breaks. (5)Difficulty in insertion and deletion : Inserting new elements at the front cannot be efficiently done because existing elements need to be shifted to make space. Similar is the case for deletion. Therefore operations require a lot of movement of data, thereby leading to an inefficient and timeconsuming algorithm. In case of deletion, the space of the deleted element cannot be freed. An appropriate solution of these problems is the linked list, which, at some cost in memory space, permits lists to be constructed and modified easily.

Linked list materials - WordPress.com · A linked list is an ordered collection of elements, where each element has at least one pointer for pointing to the next element of the list

Embed Size (px)

Citation preview

Page 1: Linked list materials - WordPress.com · A linked list is an ordered collection of elements, where each element has at least one pointer for pointing to the next element of the list

MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS (MEFGI) FACULTY OF COMPUTER APPLICATIONS

TOPIC : LINKED LIST SUB : FOP (610001) (MCA – I)

1 Prepared By : Ashwin R. Dobariya

Introduction of Linked list concept Arrays are probably the most common data structures used to store large numbers of homogeneous data elements. It is easy to use and access the elements of any array using appropriate index of elements. However, arrays have some disadvantages also and they are as follows.

(1) Fixed size : The size of an array is fixed. With a little extra effort, by dynamically allocating an array in the heap, specifying the size of the array can be deferred until the array is created at runtime, but after that it remains fixed. Arrays can be dynamically resized with the function realloc(), but that requires some programming effort.

(2) Wastage of space : When user stores numbers of elements less that the size of an array then it leads to wastage of space.

(3) Sequential storage : An array allocates memory for all its elements in sequence as one block of memory. For arrays, continues space is required. If the program ever needs to process larger number of elements, the code will crash.

(4) Possibility of overflow : If the program ever needs to process more that the size, there is a possibility of overflow and the code breaks.

(5) Difficulty in insertion and deletion : Inserting new elements at the front cannot be efficiently done because existing elements need to be shifted to make space. Similar is the case for deletion. Therefore operations require a lot of movement of data, thereby leading to an inefficient and time‐consuming algorithm. In case of deletion, the space of the deleted element cannot be freed. An appropriate solution of these problems is the linked list, which, at some cost in memory space, permits lists to be constructed and modified easily.

Page 2: Linked list materials - WordPress.com · A linked list is an ordered collection of elements, where each element has at least one pointer for pointing to the next element of the list

MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS (MEFGI) FACULTY OF COMPUTER APPLICATIONS

TOPIC : LINKED LIST SUB : FOP (610001) (MCA – I)

2 Prepared By : Ashwin R. Dobariya

A linked list is an ordered collection of elements, where each element has at least one pointer for pointing to the next element of the list and at least one value. Such as element is known as node of a linked list. There are many types of linked list such as

• Singly linked list

• Circular linked list

• Doubly linked list

• Doubly circular linked list The simplest kind of linked list is a singly linked list, which has one link per node. The link is nothing but a pointer. This link points to the next node in the list, or to a NULL value or empty list if it is the final node. Linked lists have their strengths and weaknesses, but they happen to be strong where arrays are weak. A singly linked list is simply a sequence of dynamically allocated objects, each of which refers to its successor In the list. It allocates space for each element separately in its own block of memory called a linked list element or node. Each node contains two fields : (1) data field (2) Pointer field A data field is used to store whatever data type the list holds. A pointer field is used to hold the address of next node in the list. The next field is used to link one node to the next node. The beginning of the linked list is stored in a pointer termed as head which points to the first node in the list. The first node contains a pointer to the second node and so on. The last node in the list has its next filed set to NULL to mark the end of the list. No matter how many nodes get added to the list, head will always be the first node in the linked list. The empty list : Initially the pointer head is initialized as NULL indicating the empty list, i.e. the list with no node.

Page 3: Linked list materials - WordPress.com · A linked list is an ordered collection of elements, where each element has at least one pointer for pointing to the next element of the list

MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS (MEFGI) FACULTY OF COMPUTER APPLICATIONS

TOPIC : LINKED LIST SUB : FOP (610001) (MCA – I)

3 Prepared By : Ashwin R. Dobariya

A node of the singly linked list can be represented in c using the struct as follows :

struct node { int data; struct node *next; };

The first field is an integer named data and the second is a pointer to the next node. Such structures that contain a pointer that points to the same structure type are called self referential structures. Therefore, a linked list is an ordered collection of structures connected by logical links that are stored as a part of data in the structure itself. The link is in the form of a pointer to another structure of the same type. The data field may be any complex data type if required. Applications of Linked list :

• In computer programming, linked lists are extensively used in Data Base Management System, Process Management, Operating Systems, Text editors etc. An important applications of the linked list is to represent polynomials and their manipulations.

• Representing polynomials using linked lists is advantageous because linked lists can accommodate a number of polynomials of growing sizes so that their combined size does not exceed the total memory available. The general for of a polynomial of degree n is P(x) = a0 + a1x + a2x2 + a3x3 + …. + anxn Disadvantages of Linked Lists : In spite of the several advantages of using linked list, there are obviously some shortcomings too.

Page 4: Linked list materials - WordPress.com · A linked list is an ordered collection of elements, where each element has at least one pointer for pointing to the next element of the list

MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS (MEFGI) FACULTY OF COMPUTER APPLICATIONS

TOPIC : LINKED LIST SUB : FOP (610001) (MCA – I)

4 Prepared By : Ashwin R. Dobariya

One is pointer management. Pointer, if not dealt carefully, may lead to serious errors in execution. Linked lists also consume extra space than the space for actual data as the links among than nodes are maintained through the pointers. A major drawback of linked list is that they are not suited for random access. To access a single node in linked storage, it is necessary to traverse a long path to reach the desire node, which takes a lot of time and space (because of using pointers) The following main primitive operations can be performed on singly linked list :

1. Insert a node in the list a. After a particular node b. After nth node c. Before a particular node

2. Search a particular node 3. Remove a particular node 4. Sort the nodes 5. Destroy (Delete all nodes)

Internal structure of linked list is shown as under: A single node contains two fields such as

2000 2 5000 5 6000 9 8000 12 NULL

head

2000 5000 6000 8000

First node Last node

2 5000

Data field

Pointer field

Page 5: Linked list materials - WordPress.com · A linked list is an ordered collection of elements, where each element has at least one pointer for pointing to the next element of the list

MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS (MEFGI) FACULTY OF COMPUTER APPLICATIONS

TOPIC : LINKED LIST SUB : FOP (610001) (MCA – I)

5 Prepared By : Ashwin R. Dobariya

Algorithm of insert a new node & Deletion an existing node.

For insertion: –A new node is created holding the new item. –The next pointer of the new record is set to link it to the item which is to follow it in the list. –The next pointer of the item which is to precede it must be modified to point to the new item.

Page 6: Linked list materials - WordPress.com · A linked list is an ordered collection of elements, where each element has at least one pointer for pointing to the next element of the list

MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS (MEFGI) FACULTY OF COMPUTER APPLICATIONS

TOPIC : LINKED LIST SUB : FOP (610001) (MCA – I)

6 Prepared By : Ashwin R. Dobariya

For deletion : The next pointer of the item immediately preceding the one to be deleted is altered, and made to point to the item following the deleted item.

Page 7: Linked list materials - WordPress.com · A linked list is an ordered collection of elements, where each element has at least one pointer for pointing to the next element of the list

MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS (MEFGI) FACULTY OF COMPUTER APPLICATIONS

TOPIC : LINKED LIST SUB : FOP (610001) (MCA – I)

7 Prepared By : Ashwin R. Dobariya

Following menu driven program illustrates all the operations of singly linked list such as

1.Create List

2.Add new node at beginning of the list

3.Add new node after a particular node

4.Add new node before a particular node

5.Display all nodes on screen

6.Count all the nodes of the linked list

7. Display all the nodes in Reverse order.

8.Search a particular node from the linked list.

//***********************************************************************************//

/* Program of single nexted list with all operations*/ # include <stdio.h> # include <malloc.h> void create_list(int d); //User define functions prototype declaration void addatbeg(int d); void addafter(int d,int pos); void del(int d); void display(); void count(); void rev(); void search(int d); void addbefore(int d,int pos); struct node { int data; struct node *next; }*head;

Page 8: Linked list materials - WordPress.com · A linked list is an ordered collection of elements, where each element has at least one pointer for pointing to the next element of the list

MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS (MEFGI) FACULTY OF COMPUTER APPLICATIONS

TOPIC : LINKED LIST SUB : FOP (610001) (MCA – I)

8 Prepared By : Ashwin R. Dobariya

void main() { int choice,n,m,position,i; head=NULL; clrscr(); while(1) { printf("\n\n*******************\n\n"); printf("\n1.Create List\n"); printf("\n2.Add at begining\n"); printf("\n3.Add after \n"); printf("\n4.Delete\n"); printf("\n5.Display\n"); printf("\n6.Count\n"); printf("\n7.Reverse\n"); printf("\n8.Search\n"); printf("\n9.Add before\n"); printf("\n10. Exit \n"); printf("\n*******************\n\n"); printf("Enter your choice : "); scanf("%d",&choice); printf("\n‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐\n\n"); switch(choice) { case 1: printf("How many nodes you want : "); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nEnter the data in node : "); scanf("%d",&m); create_list(m); } break; case 2: printf("Enter the element : "); scanf("%d",&m); addatbeg(m);

Page 9: Linked list materials - WordPress.com · A linked list is an ordered collection of elements, where each element has at least one pointer for pointing to the next element of the list

MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS (MEFGI) FACULTY OF COMPUTER APPLICATIONS

TOPIC : LINKED LIST SUB : FOP (610001) (MCA – I)

9 Prepared By : Ashwin R. Dobariya

break; case 3: printf("Enter the element : "); scanf("%d",&m); printf("Enter the position after which this element is inserted : "); scanf("%d",&position); addafter(m,position); break; case 4: if(head==NULL) { printf("List is empty\n"); continue; } printf("Enter the element for deletion : "); scanf("%d",&m); del(m); break; case 5: display(); break; case 6: count(); break; case 7: rev(); break; case 8: printf("Enter the element to be searched : "); scanf("%d",&m); search(m); break; case 9: printf("Enter the element : "); scanf("%d",&m); printf("Enter the position after which this element is inserted : "); scanf("%d",&position);

Page 10: Linked list materials - WordPress.com · A linked list is an ordered collection of elements, where each element has at least one pointer for pointing to the next element of the list

MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS (MEFGI) FACULTY OF COMPUTER APPLICATIONS

TOPIC : LINKED LIST SUB : FOP (610001) (MCA – I)

10 Prepared By : Ashwin R. Dobariya

if(position <=1) addatbeg(m); else addbefore(m,position); break; case 10: exit(); default: printf("Wrong choice\n"); } /*End of switch */ } /*End of while */ } /*End of main()*/ void create_list(int d) { struct node *q,*tmp; tmp= malloc(sizeof(struct node)); tmp‐>data=d; tmp‐>next=NULL; if(head==NULL) /*If list is empty */ head=tmp; else { /*Element inserted at the end */ q=head; while(q‐>next!=NULL) q=q‐>next; q‐>next=tmp; } } /*End of create_list()*/

Page 11: Linked list materials - WordPress.com · A linked list is an ordered collection of elements, where each element has at least one pointer for pointing to the next element of the list

MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS (MEFGI) FACULTY OF COMPUTER APPLICATIONS

TOPIC : LINKED LIST SUB : FOP (610001) (MCA – I)

11 Prepared By : Ashwin R. Dobariya

void addatbeg(int d) { struct node *tmp; tmp=malloc(sizeof(struct node)); tmp‐>data=d; tmp‐>next=head; head=tmp; } /*End of addatbeg()*/ void addafter(int d,int pos) { struct node *tmp,*q; int i; q=head; for(i=0;i<pos‐1;i++) { q=q‐>next; if(q==NULL) { printf("There are less than %d elements",pos); return; } } /*End of for*/ tmp=malloc(sizeof(struct node) ); tmp‐>next=q‐>next; tmp‐>data=d; q‐>next=tmp; } /*End of addafter()*/ void del(int d) { struct node *tmp,*q; if(head‐>data == d) { tmp=head; head=head‐>next; /*First element deleted*/ free(tmp); return;

Page 12: Linked list materials - WordPress.com · A linked list is an ordered collection of elements, where each element has at least one pointer for pointing to the next element of the list

MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS (MEFGI) FACULTY OF COMPUTER APPLICATIONS

TOPIC : LINKED LIST SUB : FOP (610001) (MCA – I)

12 Prepared By : Ashwin R. Dobariya

} q=head; while(q‐>next‐>next != NULL) { if(q‐>next‐>data==d) /*Element deleted in between*/ { tmp=q‐>next; q‐>next=tmp‐>next; free(tmp); return; } q=q‐>next; } /*End of while */ if(q‐>next‐>data==d) /*Last element deleted*/ { tmp=q‐>next; free(tmp); q‐>next=NULL; return; } printf("Element %d not found\n",d); } /*End of del()*/ void display() { struct node *q; if(head == NULL) { printf("\nList is empty\n"); return; } q=head; printf("\nList is : \t "); while(q!=NULL) { printf("%d ", q‐>data); q=q‐>next; } printf("\n");

Page 13: Linked list materials - WordPress.com · A linked list is an ordered collection of elements, where each element has at least one pointer for pointing to the next element of the list

MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS (MEFGI) FACULTY OF COMPUTER APPLICATIONS

TOPIC : LINKED LIST SUB : FOP (610001) (MCA – I)

13 Prepared By : Ashwin R. Dobariya

} /*End of display() */ void count() { struct node *q=head; int cnt=0; while(q!=NULL) { q=q‐>next; cnt++; } printf("\nNumber of elements are %d : ",cnt); } /*End of count() */ void rev() { struct node *prev,*cur,*pNext; if(head == NULL) return; prev = NULL; cur = head; do { pNext = cur‐>next; cur‐>next = prev; prev = cur; cur = pNext; }while(cur != NULL); head = prev; display(); } /*End of rev()*/

Page 14: Linked list materials - WordPress.com · A linked list is an ordered collection of elements, where each element has at least one pointer for pointing to the next element of the list

MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS (MEFGI) FACULTY OF COMPUTER APPLICATIONS

TOPIC : LINKED LIST SUB : FOP (610001) (MCA – I)

14 Prepared By : Ashwin R. Dobariya

void search(int d) { struct node *ptr = head; int pos = 1; while(ptr!=NULL) { if(ptr‐>data==d) { printf("\n Item %d found at position %d\n",d,pos); return; } ptr = ptr‐>next; pos++; } if(ptr == NULL) printf("\nItem %d not found in list \n",d); } /*End of search()*/ void addbefore(int d,int pos) { struct node *tmp,*q,*p; int i; q=head; for(i=0;i<pos‐1;i++) { p=q; q=q‐>next; if(q==NULL) { printf("There are less than %d elements",pos); return; } } /*End of for*/ tmp=malloc(sizeof(struct node) ); tmp‐>data=d; tmp‐>next = q; p‐>next = tmp; } /*End of addbefore()*/