Download - Linked lists
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela1
Linked List
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela2
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 array
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela3
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 node
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela4
Definition of linked listA 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.
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela5
Node representation in a Linked List
infoLinkstruct node
{
data_type info;
struct node *link;
};
struct node *ptr;
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela6
Diagram Of Linked List
infostart info info NULL
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela7
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.
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela7
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela8
#include <alloc.h >This header file has the following routines used for dynamic memory allocation
•malloc()•calloc()•realloc()•free()
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela8
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela9
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 it's needed, and in the exact amounts needed.void *malloc (unsigned long Size);
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela9
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela10
Static Allocation:{ int a = 10;int *p;p = &a;printf(“%d”, p); printf(“%d”, *p); printf(“%d”, &a); printf(“%d” &p); }
10a
500
500p
2000
=>500
=>10
=>500
=>2000
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela11
Dynamic Allocation with malloc:
10p
Heap
{int *p;p = (int *) malloc (sizeof (int));scanf(“%d”, p); printf(“%d”, *p); printf(“%d” &p);}
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela12
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 zero
void *calloc (unsigned short NoOfItems, unsigned short SizeOfItems);
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela12
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela13
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));
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela14
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);}
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela15
realloc()• Reallocates allocated memory.
• realloc() attempts to shrink or expand the previously allocated block to NewSize bytes.
void *realloc (void *Ptr, unsigned longNewSize);
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela15
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela16
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);
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela17
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);}
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela18
Assignments: dynamic memory allocation
1. Merging of two arrays 2. Find the followings
a) Sum of elements in an arrayb) Max and min elements in an arrayc) Reverse the elements in an array
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela19
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.
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela19
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela20
Creation of LIFO Linked List
Rollstart NameRoll Name Roll Name Roll Name Null
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela21
Program:#include<alloc.h>#include<stdio.h>#include<conio.h>// Node declarationtypedef struct node{int roll_no;char name[50];struct node *next;}student;
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela22
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);
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela23
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);
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela24
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); }
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela25
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;} }
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela26
Types of Linked List• Single Linked List• Circular Linked List• Doubly Linked List
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela27
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 list
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela28
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.
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela29
Insert at front
datastart data data NULL
datanew
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela30
Procedure InsertFirst()1.New = Getnode() 2. If(New = NULL) thena. Print “insufficient memory: No insertion”b. Exit
3. Elsea. New.info=datab. New.link=startc. start = New
4. Endif5. Stop
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela31
Procedure Getnode(){
struct node
{
info;
struct node *link;
};
struct node *ptr = (struct node *) malloc(sizeof(structnode));
return (ptr); }
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela32
Insert at end
data data data NULL
datanew NULL
start
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela33
Procedure InsertEnd()1.New = Getnode(Node)2. If(New = NULL) thena. Print “Memory underflow: No insertion”b. Exit
3. Elsea. ptr = start
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela34
b. While(ptr.link != NULL) doi. ptr =ptr.link
c. End Whiled. ptr.link=Newe. New.info = dataf. New.link = NULL
4. Endif5. Stop
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela35
Insert at any position
data data datastart NULL
datanew
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela36
ProcedureInsertIntermediate()1.New = Getnode(Node) 2. If(New = NULL) thena. Print “Memory underflow: No insertion”b. Exit
3. Elsea. ptr = startb. While(ptr.data != key) and (ptr.link != NULL)do
i. ptr =ptr.linkc. End While
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela37
d. if(ptr.link=NULL) theni. Print ”Key is not available in the list”ii. Exit
f. Elsei. New.info = dataii. New.link = ptr.linkiii. ptr.link = New
g. Endif4. Endif5. Stop
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela38
Deletion of a node from a List
• Delete the first element• Delete the last element• Delete any intermediate
element.
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela39
Delete the first element
datastart data data NULL
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela40
ProcedureDeleteFirst()1. ptr = start 2. If(ptr.link = NULL) thena. Print “The List is empty: No deletion possible”b. Exit
3. Elsea. ptr=ptr.linkb. start.link=NULLc. start = ptr
4. Endif5. Stop
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela41
Delete the end element
info info info NULLNULLstart
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela42
ProcedureDeleteEnd()1. ptr = start 2. If(ptr.link = NULL) thena. Print “The List is empty: No deletion
possible”b. Exit
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela43
3. Elsea. While(ptr.link != NULL) do
i. ptr1 = ptrii. ptr =ptr.link
b. End Whilec. ptr1.link=NULL4. Endif5. Stop
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela44
Delete any intermediate element
infostart info info NULL
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela45
ProcedureDeleteIntermediate()1. ptr = start 2. While(ptr.data != key) and (ptr.link !=
NULL)doi. ptr1=ptrii. ptr =ptr.link
3. End While
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela46
4. if(ptr.link=Key) thenptr1.link = ptr.link
5. ElsePrint “Node don’t exist : Deletion is unsuccessful”
6. Endif5. Stop
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela47
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 list
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela48
Applications of Linked Lists
1. Polynomial Representation and operation on PolynomialsEx: 10 X 6 +20 X 3 + 55
2. Sparse Matrix Representation0 0 1122 0 00 0 66
Helpful to save space when large sparse matrices are stored
3. Queue, Stack implementation