array implementation and linked list as datat structure

33
ARRAY IMPLEMENTATION AND LINKED LIST AS A DATA STRUCTURE PRESENTED BY: AKSHAY WADALKAR

Upload: tushar-aneyrao

Post on 17-Jun-2015

11.258 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Array implementation and linked list as datat structure

ARRAY IMPLEMENTATION AND LINKED LIST AS A

DATA STRUCTUREPRESENTED BY:

AKSHAY WADALKAR

Page 2: Array implementation and linked list as datat structure

An array is a collection of elements of similar datatype.

Contiguous memory allocation takes place.

An array is a DS in which we can access every element directly using position variable .

It is rather an organizational concept.

Array elements can be accessed individually.

Syntax: datatype nameofarray [dimension];

ARRAY

Page 3: Array implementation and linked list as datat structure

Two types of array-1. Single dimensional

single for loop.

2. Multidimensional

nesting of for loop.

Types of array

Page 4: Array implementation and linked list as datat structure

Array can be of integer ,character and string.

Integer and character array can be implemented by same logic

Implementation of string array is quiet different from the two.

We can study the array implementation using integer array.

Single dimensional array

Page 5: Array implementation and linked list as datat structure

7143258058

169

23

a[0] i=0

a[1] i=1

a[2] i=2

a[3] i=3

a[4] i=4

a[5] i=5

a[6] i=6

a[7] i=7

a[8] i=8

a[9] i=9

int a[10]={7,1,32,58,0,5,8,16,9,23};

Integer array “a”.

It is of dimension 10 (from 0 to 9).

Take positing variable i.

Its storage will be continuous 20 bytes(2 bytes each).

Creation of integer array

Page 6: Array implementation and linked list as datat structure

ALGORITHM FOR INSERTION(1 diamension)

1. DECLARATIONN SIZE

2. OPERATIONRepeat for i= 0 to (size-1)arr[i]= numend repeat

3. OUTPUTRETURN(arr[i])

Page 7: Array implementation and linked list as datat structure

DECLARATIONi rowsj coloumn OPERATION

◦ Repeat for i=0 to (rows-1) Repeat for j=0 to (coloumn-1)

Array[i][j]=num End repeat

◦ End repeat OUTPUT

Return(Array[i][j])

ALGORITHM FOR INSERTION(2 diamension)

Page 8: Array implementation and linked list as datat structure

No need to declare large number of variables individually.

Variables are not scattered in memory , they are stored in contiguous memory.

Ease the handling of large no of variables of same datatype.

Advantages

Page 9: Array implementation and linked list as datat structure

Rigid structure.

Can be hard to add/remove elements.

Cannot be dynamically resized in most languages.

Memory loss.

LIMITATION

Page 10: Array implementation and linked list as datat structure

LINKED LISTAS A DATA STRUCTURE

Page 11: Array implementation and linked list as datat structure

Each element (node) inside a linked list is linked to the previous node and successor (next) node.

This allows for more efficient insertion and deletion of nodes.

WHAT IS A LINKED LIST?

5 3 14 2

continued

Page 12: Array implementation and linked list as datat structure

Each item has a data part (one or more data members), and a link that points to the next item.

One natural way to implement the link is as a pointer; that is, the link is the address of the next item in the list.

It makes good sense to view each item as an object, that is, as an instance of a class.

We call that class: Node

The last item does not point to anything. We set its link member to NULL. This is denoted graphically by a self-loop

Definition Details

Page 13: Array implementation and linked list as datat structure

Insert a new item◦ At the head of the list, or◦ At the tail of the list, or◦ Inside the list, in some designated position

Search for an item in the list◦ The item can be specified by position, or by

some value Delete an item from the list

◦ Search for and locate the item, then remove the item, and finally adjust the surrounding pointers

Operations on Linked Lists

Page 14: Array implementation and linked list as datat structure

Suppose you want to find the item whose data value is A

You have to search sequentially starting from the head item rightward until the first item whose data member is equal to A is found.

At each item searched, a comparison between the data member and A is performed.

Searching for an Item

Page 15: Array implementation and linked list as datat structure

•Since nodes in a linked list have no names, we use two pointers, pre (for previous) and cur (for current).

•At the beginning of the search, the pre pointer is null and the cur pointer points to the first node.

•The search algorithm moves the two pointers together towards the end of the list.

LOGIC FOR SEARCHING A LINKED LIST

Page 16: Array implementation and linked list as datat structure

ALGORITHM Declaration

◦ Current 0 Searching

◦ for (current = first; current != NULL; current = current->next)

◦ if (searchItem == current(data)) ◦ return (current);◦ Break

Output◦ return (NULL);

Page 17: Array implementation and linked list as datat structure

Example

Page 18: Array implementation and linked list as datat structure

Insertion of an Element at the Head :

B efore the insertion :

hea d

nex t nex t nex t

ele m ent ele m ent ele m ent

R om e S eattle To ronto

Page 19: Array implementation and linked list as datat structure

H a v e a n e w n o d e :

h e a d

n e x t

e l e m e n t

n e x t n e x tn e x t

e l e m e n t e l e m e n t e l e m e n t

B a l t i m o r e R o m e S e a t t l e T o r o n t o

Node x = new Node();x.setElement(new String(“Baltimore”));The following statement is not correct:x.element = new String(“Baltimore”));

Page 20: Array implementation and linked list as datat structure

A f t e r t h e i n s e r t i o n :

h e a d

n e x t

e l e m e n t

n e x t n e x tn e x t

e l e m e n t e l e m e n t e l e m e n t

B a l t i m o r e R o m e S e a t t l e T o r o n t o

x.setNext(head);head = x;

Page 21: Array implementation and linked list as datat structure

Deleting an Element at the Head :

B efo re th e d e le tio n :

h ea d

n ex t

e le m en t

n ex t n ex tn ex t

e le m en t e le m en t e le m en t

B alt im ore R om e S ea tt le T o ronto

Page 22: Array implementation and linked list as datat structure

R e m o v e t h e n o d e f r o m t h e l i s t :

h e a d

n e x t

e l e m e n t

n e x t n e x tn e x t

e l e m e n t e l e m e n t e l e m e n t

B a l t i m o r e R o m e S e a t t l e T o r o n t o

head = head.getNext();

Page 23: Array implementation and linked list as datat structure

A f te r th e d e le t io n :

h e a d

n e x t n e x t n e x t

e le m e n t e le m e n t e le m e n t

R o m e S e a tt le T o ro n t o

Page 24: Array implementation and linked list as datat structure

Insertion of an Element at the Tail :

Before the insertion:

head

next next next

element element element

Rome Seattle Toronto

tail

Page 25: Array implementation and linked list as datat structure

H a v e a n e w n o d e :

h e a d

n e x t

e l e m e n t

n e x t n e x t n e x t

e l e m e n t e l e m e n t e l e m e n t

B a l t i m o r eR o m e S e a t t l e T o r o n t o

t a i l

Node x = new Node( );x.setElement(new String(“Baltimore”));x.setNext(null);tail.setNext(x);tail = x;

Page 26: Array implementation and linked list as datat structure

A f t e r t h e i n s e r t i o n :

h e a d

n e x t

e l e m e n t

n e x t n e x t n e x t

e l e m e n t e l e m e n t e l e m e n t

B a l t i m o r eR o m e S e a t t l e T o r o n t o

t a i l

Page 27: Array implementation and linked list as datat structure

Deleting an Element at the Tail :

Deletion of an element at the tail of a singly linked list takes more effort. The difficulty is related with the fact that the last node does not have a link to the previous node which will become the new tail of the list.

Page 28: Array implementation and linked list as datat structure

B e f o r e t h e d e l e t i o n :

h e a d

n e x t

e l e m e n t

n e x t n e x t n e x t

e l e m e n t e l e m e n t e l e m e n t

B a l t i m o r eR o m e S e a t t l e T o r o n t o

t a i l

Page 29: Array implementation and linked list as datat structure

R e m o v e t h e n o d e : H o w c a n w e f i n d t h e n e w t a i l ?

n e x t

e l e m e n t

n e x t n e x t n e x t

e l e m e n t e l e m e n t e l e m e n t

B a l t i m o r eR o m e S e a t t l e T o r o n t o

h e a d t a i l ?

should be removed

Page 30: Array implementation and linked list as datat structure

Singly Linked Lists and Arrays

Singly linked list Array Elements are stored in linear order, accessible with links. Do not have a fixed size. Cannot access the previous element directly. No binary search.

Elements are stored in linear order, accessible with an index. Have a fixed size. Can access the previous element easily. Binary search.

Page 31: Array implementation and linked list as datat structure

CS314 Linked Lists 31

Linked lists are dynamic, they can grow or shrink as necessary

Linked lists are non-contiguous; the logical sequence of items in the structure is decoupled from any physical ordering in memory

Advantages of linked lists

Page 32: Array implementation and linked list as datat structure

•A linked list is a very efficient data structure for sorted list that will go through many insertions and deletions.

•A linked list is a dynamic data structure in which the list can start with no nodes and then grow as new nodes are needed. A node can be easily deleted without moving other nodes, as would be the case with an array.

•For example, a linked list could be used to hold the records of students in a school. Each quarter or semester, new students enroll in the school and some students leave or graduate.

Applications of linked lists

Page 33: Array implementation and linked list as datat structure

THANK YOU…………….!!!