Linked lists

1,099 views
975 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,099
On SlideShare
0
From Embeds
0
Number of Embeds
29
Actions
Shares
0
Downloads
54
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Linked lists

  1. 1. Linked List1 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  2. 2. Why Linked List • Arrays are fixed-length • To expand them, you create a new, longer array, and copy the contents of the old array into the new array • Also problem is there to insert or delete an element from an array2 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  3. 3. Continue……. • Solution: Attach a pointer to each item in the array, which points to the next item –This is a linked list –An data item plus its pointer is called a node3 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  4. 4. Definition of linked list A linked list, or one-way list, is a linear collection of data elements,called nodes, where the linear order is given by means of pointer.4 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  5. 5. Node representation in a Linked List struct node Link { data_type info; info struct node *link; }; struct node *ptr;5 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  6. 6. Diagram Of Linked Liststart info info info NULL6 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  7. 7. Dynamic Memory Allocation • How can we allocate memory at run-time instead of compile time? • ANSI C provides standard functions that helps to allocate memory at run time. • <alloc.h> is the required header file which must be on your include path to use the standard functions for dynamic memory allocation.7 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  8. 8. #include <alloc.h > This header file has the following routines used for dynamic memory allocation •malloc() •calloc() •realloc() •free()8 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  9. 9. malloc() • malloc() allocates a block of Size bytes from the memory heap. • malloc() does not assign any value to the allocated memory space so by default it contains garbage value • It allows a programmer to allocate memory explicitly as its needed, and in the exact amounts needed. void *malloc (unsigned long Size);9 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  10. 10. Static Allocation: { int a = 10; a 10 int *p; 500 p = &a; =>500 printf(“%d”, p); p 500 printf(“%d”, *p); =>10 2000 printf(“%d”, &a); =>500 printf(“%d” &p); =>2000 } Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela10
  11. 11. Dynamic Allocation with malloc: { p 10 int *p; p = (int *) malloc (sizeof (int)); scanf(“%d”, p); printf(“%d”, *p); Heap printf(“%d” &p); }11 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  12. 12. calloc() • Allocates a memory block for a given number and size of items • calloc() assign zero value to the allocated memory space so by default it contains zerovoid *calloc (unsigned short NoOfItems,unsigned short SizeOfItems);12 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  13. 13. Dynamic Allocation with calloc: { int n,i,*p, sum=0; printf(“Enter the size of array:”); scanf(“%d”,n); p = (int *) calloc (n,sizeof (int)); for(i=0; i<n; i++) scanf(“%d”, (p+i));13 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  14. 14. Continuing…for(i=0; i<n; i++) { sum + = *(p+i); printf(“%d Element of array is :”,i,*(p+i) ); }printf(“Sum of elements in the array: %d” , sum);}14 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  15. 15. realloc() • Reallocates allocated memory. • realloc() attempts to shrink or expand the previously allocated block to NewSize bytes. void *realloc (void *Ptr, unsigned long NewSize);15 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  16. 16. Dynamic Allocation with realloc: { char *ptr; int n; printf("nEnter the size of the string"); scanf("%d",&n); ptr=(char *) calloc(n,sizeof(char)); printf("n Enter the string"); scanf("%s",ptr);16 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  17. 17. Continuing… printf("n Enter the size of new string"); scanf("%d",&n); ptr=(char *)realloc(ptr,n); printf("n Enter the string"); scanf("%s",ptr); printf("n%s",ptr); }17 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  18. 18. Assignments: dynamic memory allocation 1. Merging of two arrays 2. Find the followings a) Sum of elements in an array b) Max and min elements in an array c) Reverse the elements in an array18 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  19. 19. free()• Frees an allocated block.• free deallocates a memory block allocated by a previous call by malloc or calloc. free(p);This pointer may be any type of pointer variable.19 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  20. 20. Creation of LIFO Linked Liststart Roll Name Roll Name Roll Name Null20 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  21. 21. Program: #include<alloc.h> #include<stdio.h> #include<conio.h> // Node declaration typedef struct node { int roll_no; char name[50]; struct node *next; }student;21 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  22. 22. Continuing… int main() { student *start,*ptr; char ch; start = (student *) malloc (sizeof(student)); ptr=start; printf("n Enter roll no of the student:::"); scanf("%d",&start->roll_no);22 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  23. 23. continuing… printf("n Enter name of the student:::"); scanf("%s",start->name); printf("n Enter your choice U want to continue or not:::"); scanf("%c",&ch);23 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  24. 24. continuing…while(ch==Y||ch==y) {ptr->next=(student *) malloc (sizeof(student *)); ptr=ptr->next; printf("n Enter roll no of the student:::"); scanf("%d",&ptr->roll_no); printf("n Enter name of the student:::"); scanf("%s",ptr->name); printf("n Enter your choice. U want to continue or not"); scanf("%c",&ch); }24 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  25. 25. continuing… ptr->next=NULL; printf("n Informations of the students are:"); ptr=start; while(ptr!=NULL) { printf("n Roll no of the student=%d and name =%s",ptr->roll_no, ptr->name); ptr=ptr->next; } }25 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  26. 26. Types of Linked List • Single Linked List • Circular Linked List • Doubly Linked List26 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  27. 27. Operations on Linked List• Traversing a List• Insertion of a node into a List• Deletion of a node from a List• Copy a linked list to make a duplicate• Merging two linked list into a larger list• Searching for an element in a list27 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  28. 28. Insertion of a node into a List • Insert at front (as a first element) • Insert at end (as a last element) • Insert at any position.28 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  29. 29. Insert at front start data data data NULL new data29 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  30. 30. Procedure InsertFirst() 1.New = Getnode() 2. If(New = NULL) then a. Print “insufficient memory: No insertion” b. Exit 3. Else a. New.info=data b. New.link=start c. start = New 4. Endif 5. Stop30 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  31. 31. { Procedure Getnode() struct node { info; struct node *link; }; struct node *ptr = (struct node *) malloc(sizeof(struct node)); return (ptr); }31 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  32. 32. Insert at end start data data data NULL new data NULL32 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  33. 33. Procedure InsertEnd() 1.New = Getnode(Node) 2. If(New = NULL) then a. Print “Memory underflow: No insertion” b. Exit 3. Else a. ptr = start33 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  34. 34. b. While(ptr.link != NULL) do i. ptr =ptr.link c. End While d. ptr.link=New e. New.info = data f. New.link = NULL 4. Endif 5. Stop34 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  35. 35. Insert at any position start data data data NULL new data35 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  36. 36. Procedure InsertIntermediate() 1.New = Getnode(Node) 2. If(New = NULL) then a. Print “Memory underflow: No insertion” b. Exit 3. Else a. ptr = start b. While(ptr.data != key) and (ptr.link != NULL)do i. ptr =ptr.link c. End While36 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  37. 37. d. if(ptr.link=NULL) then i. Print ”Key is not available in the list” ii. Exit f. Else i. New.info = data ii. New.link = ptr.link iii. ptr.link = New g. Endif 4. Endif 5. Stop37 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  38. 38. Deletion of a node from a List • Delete the first element • Delete the last element • Delete any intermediate element.38 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  39. 39. Delete the first element start data data data NULL39 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  40. 40. Procedure DeleteFirst() 1. ptr = start 2. If(ptr.link = NULL) then a. Print “The List is empty: No deletion possible” b. Exit 3. Else a. ptr=ptr.link b. start.link=NULL c. start = ptr 4. Endif 5. Stop40 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  41. 41. Delete the end elementstart info info NULL info NULL41 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  42. 42. Procedure DeleteEnd() 1. ptr = start 2. If(ptr.link = NULL) then a. Print “The List is empty: No deletion possible” b. Exit42 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  43. 43. 3. Else a. While(ptr.link != NULL) do i. ptr1 = ptr ii. ptr =ptr.link b. End While c. ptr1.link=NULL 4. Endif 5. Stop43 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  44. 44. Delete any intermediate elementstart info info info NULL44 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  45. 45. Procedure DeleteIntermediate() 1. ptr = start 2. While(ptr.data != key) and (ptr.link != NULL)do i. ptr1=ptr ii. ptr =ptr.link 3. End While45 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  46. 46. 4. if(ptr.link=Key) then ptr1.link = ptr.link 5. Else Print “Node don’t exist : Deletion is unsuccessful” 6. Endif 5. Stop46 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  47. 47. Assignments on Linked List1. Traversing a List2. Insertion of a node into a List3. Deletion of a node from a List4. Copy a linked list to make a duplicate5. Merging two linked list into a larger list6. Searching for an element in a list47 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela
  48. 48. Applications of Linked Lists 1. Polynomial Representation and operation on Polynomials Ex: 10 X 6 +20 X 3 + 55 2. Sparse Matrix Representation 0 0 11 22 0 0 0 0 66 Helpful to save space when large sparse matrices are stored 3. Queue, Stack implementation48 Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela

×