48
Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela 1 Linked List

Linked lists

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Linked lists

Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela1

Linked List

Page 2: Linked lists

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

Page 3: Linked lists

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

Page 4: Linked lists

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.

Page 5: Linked lists

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;

Page 6: Linked lists

Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela6

Diagram Of Linked List

infostart info info NULL

Page 7: Linked lists

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

Page 8: Linked lists

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

Page 9: Linked lists

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

Page 10: Linked lists

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

Page 11: Linked lists

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);}

Page 12: Linked lists

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

Page 13: Linked lists

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));

Page 14: Linked lists

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);}

Page 15: Linked lists

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

Page 16: Linked lists

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);

Page 17: Linked lists

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);}

Page 18: Linked lists

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

Page 19: Linked lists

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

Page 20: Linked lists

Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela20

Creation of LIFO Linked List

Rollstart NameRoll Name Roll Name Roll Name Null

Page 21: Linked lists

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;

Page 22: Linked lists

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);

Page 23: Linked lists

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);

Page 24: Linked lists

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); }

Page 25: Linked lists

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;} }

Page 26: Linked lists

Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela26

Types of Linked List• Single Linked List• Circular Linked List• Doubly Linked List

Page 27: Linked lists

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

Page 28: Linked lists

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.

Page 29: Linked lists

Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela29

Insert at front

datastart data data NULL

datanew

Page 30: Linked lists

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

Page 31: Linked lists

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); }

Page 32: Linked lists

Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela32

Insert at end

data data data NULL

datanew NULL

start

Page 33: Linked lists

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

Page 34: Linked lists

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

Page 35: Linked lists

Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela35

Insert at any position

data data datastart NULL

datanew

Page 36: Linked lists

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

Page 37: Linked lists

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

Page 38: Linked lists

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.

Page 39: Linked lists

Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela39

Delete the first element

datastart data data NULL

Page 40: Linked lists

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

Page 41: Linked lists

Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela41

Delete the end element

info info info NULLNULLstart

Page 42: Linked lists

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

Page 43: Linked lists

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

Page 44: Linked lists

Prof. B. Majhi, HOD Dept, CSE NIT, Rourkela44

Delete any intermediate element

infostart info info NULL

Page 45: Linked lists

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

Page 46: Linked lists

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

Page 47: Linked lists

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

Page 48: Linked lists

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