single linked list

18
SINGLE LINKED LIST IMPLEMENTATION -BY B.ABDUL KALAM ASHAD B.ANISH KUMAR MOHAN RAJ D.SAPTHAGIRINATHAN SUDHARSHAN DATA STRUCTURE ASSIGNMENT - I

Upload: sathasivam-r

Post on 15-Jul-2015

177 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: single linked list

SINGLE LINKED LIST

IMPLEMENTATION

-BY

B.ABDUL KALAM ASHAD

B.ANISH KUMAR

MOHAN RAJ

D.SAPTHAGIRINATHAN

SUDHARSHAN

DATA STRUCTURE

ASSIGNMENT - I

Page 2: single linked list

SINGLE LINKED LIST

Linked list is a collection of similar elements.

Each elements points to the next element.

Linked list is a linear list of specially designed

nodes, where each node divided into two parts.

INFO FIELD NEXT FIELD

Page 3: single linked list

Info field: it contains necessary information

about the items of the list to be stored and

processed.

Next field: it contains address of the next

node. This field is used to access the next data

item in other

48 17 142head

Page 4: single linked list

REPRESENTATION OF LINKED LIST

1. Static or sequential or array representation.

The linked list will be maintained or represented by two

parallel arrays of same sizes. One array for the actual data item

is called info field of the node and the other array for the

address called the next field of the nodes.

Info Next

28

8

24

1

2

-1

28 8 24

Page 5: single linked list

2. Dynamic or pointers or linked representation.

The size of the linked list may increase or decrease according

to the requirement of application so dynamic representation of linked

list do not have limitation and use space proportional to the actual

number of elements of the list.

It wants ultimate size of the linked list to declare in advance.

Array implementation of the linked list is very simple and

efficient in random access of the elements.

Features:

Page 6: single linked list

OPERATION ON SINGLE LINKED LIST

Creating linked list:

To create a linked list we have to maintain the list of free

nodes in array implementation.

Initially all the nodes are empty and link to one another in

sequence.

The next field of the last node contains -1 to indicate the end

of the list.

Example:

Free=0;

For(i=0;i<size;i++)

List [i].next=i+1;

list[i].next= -1;

Page 7: single linked list

Traversing a linked list:

This operation is used to visit each one of the list exactly

once in order to access the info stored in nodes.

Algorithm:

1: if start=null

a: print “list is empty”

b: exit

End if.

2: Set ptr = start

3: Repeat steps 4 and 5 until ptr! =null

4: access and apply ptrinfo.

5: set ptr= ptrnext

End repeat

6: Exit

Page 8: single linked list

Insertion of an element into the linked list at various positions:

Insertion of node into a linked list requires a free node in

which the information can be inserted and then the node can be

inserted into the linked list.

Before header or at beginning.

At the end of list.

At the specified position.

Page 9: single linked list

Insertion at beginning

• Follow the previous steps and we get

48 17 142head //

head 93

Step 1 Step 2

Step 3

Page 10: single linked list

Insertion At end

• Follow the previous steps and we get

48 17 142head //

93

Step 1 Step 2

Step 3

48 17 142head //

Page 11: single linked list

To insert a new node at the specified position we have to

search position in the list.

Then we can insert the node after the position. Let the

address of the specified node be kept in ptr.

Adjust the pointer so that next pointer so that next pointer of

the specified node pointed by the ptr.

The next pointer of the specified node will point to the next

pointer of the node pointed by new ptr.

Insertion at the specified position

Page 12: single linked list

Insertion at the specified position

• Follow the previous steps and we get

48 17 142head //

Step 1 Step 2

48 17 142head //

Page 13: single linked list

Deletion an element from the linked list

If the node be deleted, that element should be search all

over the list still the node find. Then it should be deleted.

Deletion at the beginning position of the linked list.

Deletion at the ending position of linked list.

Deletion at a specified position in linked list.

As like as insertion the deletion is also performed in three ways.

Page 14: single linked list

Deletion at beginning

• Follow the steps and we get

48 17 142head //

head 93

Step 1

Step 2

Page 15: single linked list

Deletion At end

• Follow the steps and we get

48 17 142head //

93

Step 1

Step 2

48 17 142head //

Page 16: single linked list

Deletion at the specified position

• Follow the steps and we get

48 17 142head //

Step 1

Step 2

48 17 142head //

Page 17: single linked list

1. If start =NULL

2. Print”over flow”

3. Return

4. End if

5. Set ptr=start

6. Assign value=startinfo

7. Set start=startnext(second node becomes the first node).

8. Release the node pointed by ptr to the memory heap.

9. Exit.

ALGORITHM

Page 18: single linked list