Recursive Data Structures ,Linked List-Data Structures and Representation-Lecture Slides, Slides of Data Structures and Algorithms

Prof. Chandrashekar Baag delivered this lecture at Amrita Vishwa Vidyapeetham. This lecture is part of lecture series on Data Structures and Representation course. It includes: Recursive, Data, Structures, Dynamic, Linked, Lists, Array, Manipulations, Stack, Element, Nodes

Typology: Slides

2011/2012

Uploaded on 07/30/2012

dhanvantari
dhanvantari 🇮🇳

2

(2)

45 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 307 Fundamentals of
Computer Science 1
Recursive Data Structures
Linked Lists are dynamic data structures
They grow and shrink one element at a time, normally without some
of the inefficiencies of arrays
Big O of Array Manipulations
Access the kth element
Add or delete an element in the middle of the array while maintaining
relative order
adding element at the end of array? space avail? no space avail?
add element at beginning of an array
If accesses are all at beginning or end of list, a linked
structure offers improvements
Linked Lists could be used as the underlying storage
container for higher level ADTs (stack, queue)
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Recursive Data Structures ,Linked List-Data Structures and Representation-Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

CS 307 Fundamentals of Computer Science

Recursive Data Structures

 Linked Lists are dynamic data structures

  • They grow and shrink one element at a time, normally without some

of the inefficiencies of arrays

 Big O of Array Manipulations

  • Access the kth element
  • Add or delete an element in the middle of the array while maintaining

relative order

  • adding element at the end of array? space avail? no space avail?
  • add element at beginning of an array

 If accesses are all at beginning or end of list, a linked

structure offers improvements

 Linked Lists could be used as the underlying storage

container for higher level ADTs (stack, queue)

CS 307 Fundamentals of Computer Science

Nodes and Lists

A different way of implementing a list

Each element of a Linked List is a separate

Node object.

Each Node tracks a single piece of data plus

a reference (pointer) to the next

Create a new Node every time we add

something to the List

Remove nodes when item removed from list

and allow garbage collector to reclaim that

memory

CS 307 Fundamentals of Computer Science

One Implementation of a Linked List

The Nodes shown on the previous slide are

singly linked

  • a node refers only to the next node in the

structure

  • it is also possible to have doubly linked nodes.
  • The node has a reference to the next node in the

structure and the previous node in the structure

as well

How is the end of the list indicated

  • myNext = null for last node
  • a separate dummy node class / object

CS 307 Fundamentals of Computer Science

A Simple List Interface

public interface IList

void add(Object item);

void add(Object item, int pos);

Object set(Object item, int pos);

void add(List other);

Object get(int pos);

Object remove(int pos);

int size();

CS 307 Fundamentals of Computer Science

Add Element - List Empty (Before)

myHead myTail iMySize

null null 0

Object

item

CS 307 Fundamentals of Computer Science

Add Element - List Empty (After)

myHead myTail iMySize

1

Object

Node

myData myNext

null

CS 307 Fundamentals of Computer Science

Add Element - List Not Empty (After)

2

Object

Node

myData myNext

myHead myTail iMySize

Object

Node

myData myNext

null

CS 307 Fundamentals of Computer Science

Code for default add

public void add(Object item)

CS 307 Fundamentals of Computer Science

Code for get

public Object get(int pos)

CS 307 Fundamentals of Computer Science

Code for remove

public Object remove(int pos)

CS 307 Fundamentals of Computer Science

Remove Back Method

public Object removeBack()

Big O?

CS 307 Fundamentals of Computer Science

Other Possible Features of

Linked Lists

Doubly Linked

Circular

Dummy Nodes for first and last node in list

public class DLNode { private Object myData; private DLNode myNext; private DLNode myPrevious;

CS 307 Fundamentals of Computer Science

Code for arbitrary add -

Doubly Linked List

public void remove(Object item, int pos)

CS 307 Fundamentals of Computer Science

Code for remove

Doubly Linked List

public Object remove(int pos)