data structures 18

37
 Data Structures By Sailakshmi.P

Upload: muhammadabid007

Post on 05-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 1/39

Page 2: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 2/39

Data Structure:

A Data Structure can be defined as a way of organizing and storing data in a computer so that it

can be used effectively.

Types of Data Structures:*Linear Data Structure

*Non-Linear Data Structure

Page 3: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 3/39

Linear Data Structure:

*Stack*Queue

*Linked List:

*1.Single linked list

*2.Double linked list

Non-Linear Data Structure:

*Trees

*Graphs

Page 4: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 4/39

Stack:

Stack is a homogeneous collection of items of any typearranged linearly with access only one end called top.The

data can be added or removed only one end.stack follows

Last In First Out(LIFO).

pop push

Page 5: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 5/39

Implementation Of Stack:

* Stack implemented as an array

*Stack implemented with pointers

Array Implementation:

To implement stack by using one dimensional arrayImplemented this way data is stored in the array.

2 30 top

1 20

0 10If the stack is empty the value becomes -1

Page 6: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 6/39

Stack is implemented as arrays are useful if fixedamount of data is to be used. If the amount of data is not fixed then array is poor choice forimplementing stack.

Implementing With Pointers:To solve this problem stack can be implementingwith pointers as a form of linked list.

30 20 10

Linked list

Page 7: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 7/39

Operations on Stack:

1.Push:

This operations adds or pushes another item on to thestack.

if it performs the number of items on the stack is less thann.

2.Pop:

This operation removes on item from the stack.

If it performs the number items on the stack must begrater than 0.

Page 8: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 8/39

3.Top:

This operation returns the value of item at the top of the stack.

It does not remove that item.

4.Is Empty:

This operation returns true if the stack is empty and

false if it is not.

5.IsNull:

This operations returns true if the stack is full andfalse if it is not.

Page 9: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 9/39

Representation Of Stack:

Page 10: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 10/39

Stack Over Flow:

Stack over flow occurs when the stack is fulla push operation is performed.

Stack Under Flow:

This occurs when the stack is empty and a

pop operation is performed.

Page 11: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 11/39

Queue:A queue is an ordered collection of data structure such

that the data is inserted at one end and deleted at

another end.

A queue is defined as a data structure which holds acollection of items to be processed on First In First Out.

Delete insert

Frontend rearend

Page 12: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 12/39

Queue Representation:

1.array:It represents the current location for taking

information (Front), the other representing

the current location for placing new

information(Rear).*Each time information is added to the queue

we increment rear.

*Each time information is taken from the

queue we increment front.

Page 13: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 13/39

EX:

delete  insert 

S B A M

front rear

Array implementation of queue do have draw backs.

The max queue size has to be set at compile time

rather than at run time.space can be wasted if we do

not use full capacity of the queue.

Page 14: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 14/39

Operations On Queue:*Adding new item to the queue.

*Removing item of the queue.

The operation of adding new item on thequeue occurs only at one end of the queuecalled rear(the back).

The operations of removing items of thequeue occurs at the other end of the queue.

The number of items in the queue is calledqueue size.

If the number of items in the queue is zero

an attempt at a remove operation produce aqueue underflow.

Page 15: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 15/39

Circular Queue:

It follows FIFO principle.

*In circular queue the last node is connected

back to the first node to make a circle.

*Elements are added at the rear end and the

elements are deleted at the front end of the

queue.

*Both the front and rear points to the

beginning of the array.

*It is also called “Ring Buffer”. 

Page 16: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 16/39

Circular queue creation:

*Using single linked list

*Using double linked list

*Using arrays.

Single linked list:

In circular linked list instead of storing NULL

value in the last node of the single linked

list,store the address of first node forms a

circular linked list.

Using this it is possible to directly traverse

to the first node after reaching the last

node.

Page 17: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 17/39

Representation:

root

data data data

(circular single linked list)

Page 18: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 18/39

Using Double linked list:

In double linked list the right side pointer points to

the next node address.

Representation:

L

R D D

Page 19: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 19/39

Using Array:

In arrays the range of subscript is 0 to n-1.where n isthe max.size.

To make array as circular array by making subscript 0

as the next address of the subscript n-1 using

formulaSubscript=(subscript+1)%max.size.

Representation:

Page 20: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 20/39

Advantages of circular queue:

*In array representation of circular queuememory is used effectively to store

elements.

*In the linked representation of circular

queue ,the extra burden of maintainingnull pointer is avoided as the last element

points to the first element of the list.

*There is no need to maintaining head node

to indicate the first node of the operation

Page 21: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 21/39

Dequeue:(double ended queue)

A queue is a linear queue in which insertion and

deletion are made either end of the queue.

Representation:

front rear

ins ins

del del

Page 22: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 22/39

Operations on dequeue:

*Insertion from front end

*Deletion from front end*Insertion from rear end

*Deletion from rear end

It is flexible datastructure when compared to stacks and

queues.

Types of dequeues:

*Input restricted dequeue

*Output restricted dequeue

In input restricted dequeue allows insertions at only one

end,where as an out put restricted dequeue permits

deletion from only one end.

Page 23: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 23/39

Linked List:List refers linear collection of data items.items areinserted and deleted dynamically.

The main operations on lists are insertion of new

element,deletion of particular element and accessingparticular element.

Lists and arrays are same but list is a dynamic datastructure and array is static data structures because itssize is fixed.

A

1 2 3 4 5 6 7

Page 24: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 24/39

The linked list is a very different type of collection

from an array.using such lists we can store collection of 

information limited only by the total amount of memory that the operating system will allow to use.

There is no need to specify our needs in advance.

Linked allocation having draw backs:

*No direct access to a particular element.

*Additional memory required for pointers.

Page 25: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 25/39

Types of linked lists:

*Single linked list

*Double linked list.

Single linked list:

A single linked list is simply a linked list.it is linear

collection of data items.each item in the list is called

node.each node of the list has two fields.

*Information It contains item being stored in the list

Page 26: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 26/39

*Next address(pointer):

Contains the address to the next item in the list.Last node in the list contains NULL points to indicate that

it is end of the list.

Representation:

Head

node node node

inf ptr inf ptr inf null

Page 27: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 27/39

* Linked list also contains a pointer variable,called

HEAD,which points to the first node of the list.the

HEAD variable is not exactly part of the list but it isused as a handle for the list.

* Items are added to a list,memory for node

dynamically allocated.

* Thus the number of items that may be added to a list

only the amount memory available.

* A list said to be empty list or null list when there are

no nodes in it.

In this case the null pointer is assigned to the variable

HEAD.

Page 28: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 28/39

Operations on lists:

*Traversing the list

*Insertion

*Deletion

Traversing list:

Each element in a single linked list contains a pointerto the next element they can be traversed in one

direction from head to tail.

Inserting element into list:

When list insertion is performed it is critical that allptrs adjusted properly.

Page 29: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 29/39

Inserting at the beginning of the list:

A node n is inserted in to the list at the beginning.

*Allocate space for new node.*Copy the item to be inserted in to it.

*Make the new node’s next ptr points to the current headof the list

*Make the head of the list points to the newly allocatednode.

head

N

(new node)

Page 30: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 30/39

After insertion:

Head

N

Page 31: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 31/39

Insertion at the end of the list:

Suppose we want to insert an element at the end of thelist.

If the list is empty, then the new node becomes first nodeitself.other wise,the new node pointer should be NULL.

head

(before insert last node)Head

(After insert last node)

N

Page 32: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 32/39

Insertion after a given node:

Suppose we want to insert an element(N) in a list after aspecified node A.

Head P

A

head

N

Page 33: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 33/39

Deletion from a list:

The deletion of list element in a single linked list having

that the list element immediately before the deletedelement is made point to the list element immediatelyfollowing the deleted element.

head P

head

ptr(prev) ptr(p)

Page 34: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 34/39

Double linked list:

In single linked list we can traverse in only one

direction.in double linked list have pointer both to thenext element and to the previous element in the list.

Representation:

Head

lp inf rp lp inf rp lp inf rp tail

Main advantage of double linked list is searching both the

directions.

Page 35: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 35/39

Insertion:

*The list is empty: the new node becomes single node of the

list,so that the left and right parts of the new node should benull.

head

tail

*The new node to be inserted before the first node the newnode ptrs be adjusted.

Head P

tail

Page 36: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 36/39

(After insertion):

P

Head tail

N

*New node to be inserted at the middle of the list

head P tail

Head P tail

N

Page 37: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 37/39

Deletion:

*The list is empty it returns underflow.

*There is one node make the head and tail have addressesnull.

*The deleted node is first node head points to right node of 

the deleted node.left ptr of new first node is set to null.

*The deleted node is the last node tail points to left node of the deleted node and right ptr node is set to null.

Head P tail

(before deletion)

Page 38: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 38/39

(after deletion):

Head P tail

Page 39: Data Structures 18

7/31/2019 Data Structures 18

http://slidepdf.com/reader/full/data-structures-18 39/39