Linked List

40  Download (0)

Full text

(1)

Linked List

(Dynamic Memory Allocation)

(2)

2

(3)

Dynamic Memory Allocation

Dynamic Memory Allocation refers to managing system memory at runtime.

Permits us to allocate additional memory space or to release unwanted space at run time .

Optimizing the use of storage space.

Can be defined as the process of allocating memory at run time.

Achieved using dynamic storage management

3

(4)

Memory Allocation Functions

malloc() allocates request size of bytes and returns a pointer to the first byte of the allocated space.

calloc() allocates space for an array of elements initializes them to zero and then returns a pointer to the memory.

free() frees previously allocated space.

realloc() modifies the size of previously allocated space.

4

(5)

malloc() Function

It allocates a block of memory of specified size and returns a pointer to it.

malloc() allocates the user a specified number of bytes but does not initialize. Once allocated, the program accesses this block of memory via a pointer that malloc() returns

Syntax: ptr = (data_type *) malloc(byte_size);

Ex: ptr=(int *)malloc(100*sizeof(int));

This means that a memory space equivalent to 100 times the size of int bytes is reserved and the address of the first byte of the memory allocated is assigned to the

5

(6)

C - typedef

The C programming language provides a keyword called typedef, which you can use to give a type a new name.

typedef is a keyword used in C language to assign alternative names to existing datatypes.

Following is the general syntax for using typedef,

typedef <existing_name> <alias_name>

Ex: typedef char BYTE;

After this type definition, the identifier BYTE can be used as an abbreviation for the type char

For example : BYTE b1, b2;

You can use typedef to give a name to your user defined data types as well.

6

(7)

General Syntax :- typedef struct

{

type member1;

type member2;

type member3;

} type_name;

Here type_name represents the stucture definition associated with it.

Now this type_name can be used to declare a variable of this stucture type.

type_name t1, t2;

(8)

Calloc (): Allocate multiple blocks of memory

malloc() allocates single block of storage space, whereas calloc() allocates multiple blocks of storage each of the same size and then sets all bytes to zero

General Form:

ptr=(data_type *)calloc( n, element_size);

Allocates contiguous space for n blocks, each of size element_size bytes

all bytes are initialized to zero and a pointer to the first byte of the allocated region is returned

8

(9)

Linked List

(10)

Introduction

To store Records we need List.

An array is an example of List (set of elements).

In an array elements are sequentially organized.

We use index to sequentially access & manipulate these elements.

.

10

(11)

int number[10];

We need to specify the size of the array in the beginning itself.

Contiguous memory cells are required.

This list may grow as more information is added.

Hence more space need to be allocated which is not possible with arrays.

Why arrays are not preferred ?

(12)

Linked Lists

To overcome the disadvantages of arrays (sequential).

Linked list is another way of representing a list (set of elements).

In Linked List space for new record is allocated dynamically.

Each Element is stored as a Structure unit that also contains a link (pointer to next element) to the structure containing the next item.

12

(13)

Linked list is a linear data structure that contains sequence of elements such that each element links to its next element in the sequence. Each element in a linked list is called as "Node".

A list is a sequence of data, and linked list is a sequence of

data linked with each other.

(14)

14

(15)

Each structure/element of the list is called as a node.

A node consists of two parts- part 1: contains the item.

part 2: contains the address to the next item (a pointer).

The link is in the form of a pointer to another structure.

The order of access is given by the links from one item to the next (index in array).

Item1 Structure 1

Item2

Structure 2

Item3

Structure 3

next

Linked List

(16)

Hence a Linked List is a collection of structures ordered not by their physical placement in memory (like an array) but by logical links that are stored as part of the data in the structure itself.

Such structure is represented as follows:

struct linkedlist {

int item;

struct linkedlist

*next;

}

node

item

next

Linked List

16

(17)

Example

struct linked_list // create structure {

int number;

struct linked_list *next;

};

struct linked_list node1, node2; // declare the nodes:

node1 node2

node1.numbe rnode1.next

node2.numbe r

node2.next

(18)

Creating a Linked List

First we need to create the node structure.

Then we have to get the memory allocated for it dynamically.

struct linked_list {

int number;

struct linked_list *next;

};

typedef struct linked_list node;

node *head;

head=(node *)malloc(sizeof(node));

18

(19)

Creating Linked List

0

head node

number next

10

head node1

number next

20 0

node2

number next

To assign values:

head->next=(node *)malloc(sizeof(node));

head->next->number=20;

head->next->next=NULL;

for second node To assign values:

head->number=10;

head->next=NULL;

for first node

(20)

Types of Linked Lists

Linear singly linked list

Two-way or doubly linked list

Circular linear linked list

Circular doubly linked list

20

(21)

Illustration of Various Linked Lists

A B C 0

Single Linked list

A B C

Circular list

A B C 0

0

A B C

Two way linked list

(22)

Advantages and Disadvantages of linked lists

Advantages:

It is a dynamic data structure, can grow or shrink in size during the execution of a program unlike arrays.

Does not waste memory space as we need not specify the number of nodes to be used in the list, we can create any number we want.

Provides flexibility in arranging items efficiently.

Insertion, deletion can be performed easily.

Disadvantages:

Access to an arbitrary item is cumbersome and time consuming.

Uses more storage than an array for the same number of items.

22

(23)

Application of Linked List

Stack Implementation:

*Insertion and deletion from the beginning in a linked list.

Queues implementation:

*Insertion from the beginning and deletion from the end in a linked list.

(24)

Linked listOperations

We can perform the following operations on Linked List.

1. Creating a list

2. Traversing the list

3. Counting the items in a list

4. Search for an item for editing or printing

5. Inserting new item

6. Deleting an item

7. Concatenating two lists.

24

(25)

Insertion and deletion in Linear

Linked List

(26)

Insertion:

In Beginning In End

In Middle

26

(27)

Assume following template for linked list operation:

typedef struct linked_list {

int number;

struct linked_list *next;

};

linked_list node;

node *head, *ptr;

head=(node *)malloc(sizeof(node));

(28)

Inserting an object in the beginning of list

Original Linked List

Head

10

20 45 75 85

20 45 75 85

10

Head

Insert 10

28

(29)

Inserting a node in the beginning of list

1.If ptr=NULL then print overflow // free memory exhausted exit ;

else

Set ptr= address of new node // create a new node and assign its address to ptr Endif

2. Set ptr->number=item;

3. If head=NULL then // list is empty

Set head=ptr; // make new node as start node Else

Set ptr->next = head;

4. head=ptr;

(30)

Original linked list of integers:

After insertion

50 40 13 20

50 40 13 20 60

Last element

Inserting an element at the end

Head

Head

30

(31)

Inserting an element at the end

1. If ptr=NULL then // free memory exhausted Print OVERFLOW

Exit;

else

ptr= (node *) malloc (sizeof(node));

// create a new node and assign its address to ptr Endif

2. Set ptr->number=item // assign value to new node 3. Set ptr->next=NULL;

4. If head=NULL then // list is empty

Set head=ptr; // make new node as start node Else // search for last node

Set temp=head;

5. Do

temp=temp->next;

while (temp->next!=NULL); // searching last node

31

(32)

Inserting an element in the middle

20 45 75 85

20 45 75 85

60

old value

Original Linked List

Insertion

32

(33)

Inserting an element Somewhere in the middle

1. If ptr=NULL then // free memory exhausted Print OVERFLOW

Exit;

else

ptr= (node *) malloc (sizeof(node));

// create a new node and assign its address to ptr Endif

2. Set ptr->number=item;

3. If head=NULL then // list is empty

Set head=ptr; // make new node as start node ptr->next=NULL;

endif

4. Node *temp; // create a pointer Set temp = head, I=0, loc;

5. Repeat step 6 & 7 until I<loc 6. Set temp=temp->next;

7. set I=I+1;

8. Set ptr->next=temp->next; // connect new node to the next node 9. temp->next=ptr; // connect new node to the previous node

33

(34)

Deletion:

at the Beginning at the Middle

at the End

34

(35)

(to delete) Head

50 40 13 20

(to delete) Head

50 40 13 20

Original Linked List

Delete at beginning

After deletion

Head

40 13 20

Deleting an element at the Beginning

(36)

Deleting an element at the Beginning

1.If head=NULL then print underflow or print list is empty exit ;

endif

2. Set ptr= head; // Assign address of 1st node to some pointer variable 3. Set head=head->next // Assign the address of 2nd node to head node 4. print element deleted

5. free(ptr); // release the memory space

36

(37)

Head

5 0

4 0

1 3

...

Deleting an element at the End

(to delete) Head

50 40 13 20

Original Linked List

Delete the last element

(38)

...

Deleting an element at the End

1. If head=NULL then

Print UNDERFLOW // list is empty Exit;

ENDIF

2. If head->next =NULL then // if only ONE node is there in List Set ptr=head;

Set head = NULL

print element deleted is=ptr->number;

free(ptr);

endif

5. Set ptr=head;

6. Repeat steps 7 and 8 until ptr->next!=NULL;

7. Set loc=ptr; // loc pointer is holding address of previous node and ptr is pointing to current node

8. Set ptr=ptr->next;

9. Set loc->next=NULL;

10 free(ptr);

38

(39)

20 45 75 85 Original Linked List

After delete

Deleting an element at the middle

Head

Delete 45

20 45 75 85

Head

20 75 85

(40)

Double Linked list DDL:

Kindly follow the given structure for DDL

struct linked_list {

int number;

struct linked_list *next, *prev;

};

typedef struct linked_list node;

node *head, *ptr;

head=(node *)malloc(sizeof(node));

40

Figure

Updating...

References

Related subjects :