Download Extending the Robot Programming Language

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Array data structure wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Quadtree wikipedia , lookup

Red–black tree wikipedia , lookup

Interval tree wikipedia , lookup

Binary search tree wikipedia , lookup

Linked list wikipedia , lookup

B-tree wikipedia , lookup

Transcript
Lecture 18
Arrays of Linked List
The concept


Suppose we want to combine two data
structures we know: arrays(vectors) and linked
lists
It could be useful in implementing
–
–





Graph (using adjacency lists)
Dictionary etc..
We start with the node class
Then build a vector of nodes where each entry
is the starting point for a list.
Vector V = new Vector(10);
Node first = new Node();
V.add(first);
Array of Linked List

Consider the following data structure

Each entry in the array is an address of a linked list
Node list[10] ; // defines an array of linked lists
To initialize each cell of the array to NULL


–
for (int I=0;I<10;I++)
list[I] = NULL;
Building a Linked List





Suppose we are building a list originating from
V[0]
Every new node that comes will be added to the
beginning of the list.
How do we add an entry to the beginning of the
list?
Node first = new Node(10, first);
The above code does two things
–
–
Point the next field of new node to current first
Rename the new node to first
The Java code

Node first = new Node() ; // this will serve as
the dummy node in the
Creating the first list

Create the first node

list[0] = new node(“Andy”,23);
To create the other nodes in the first list (begins with
‘A’)

–
if (node_begins_with_A)
{
nextnode=create_node(name,age); // calls the
constructor
list[0]=insert(list[0],nextnode); // inserts the new
node to the
// correct position
}
Creating the first list

Create the first node

list[0] = new node(“Andy”,23);
To create the other nodes in the first list (begins with
‘A’)

–
if (node_begins_with_A)
{
nextnode=create_node(name,age); // calls the
constructor
list[0]=insert(list[0],nextnode); // inserts the new
node to the
// correct position
}
Finding the place to insert

(for any node)
Find which list the node belongs to
D

Doe
if (name[0] >= ‘a’ && name[0] <=‘z’)
insert_index = name[0]-97;

if (name[0] >= ‘A’ && name[0] <=‘Z’)
Insert_index = 68-65=3
insert_index = name[0]-65;

if (list[insert_index]==NULL) // list is empty
list[insert_index] = create_node(name,age);
else
{ nextnode=create_node(name,age);
list[insert_index] =
insert(list[insert_index],nextnode);}
Creating the first list

Create the first node

list[0] = new node(“Andy”,23);
To create the other nodes in the first list (begins with
‘A’)

–
if (node_begins_with_A)
{
nextnode=create_node(name,age); // calls the
constructor
list[0]=insert(list[0],nextnode); // inserts the new
node to the
// correct position
}
Finding the place to insert

(for any node)
Find which list the node belongs to
D

Doe
if (name[0] >= ‘a’ && name[0] <=‘z’)
insert_index = name[0]-97;

if (name[0] >= ‘A’ && name[0] <=‘Z’)
Insert_index = 68-65=3
insert_index = name[0]-65;

if (list[insert_index]==NULL) // list is empty
list[insert_index] = create_node(name,age);
else
{ nextnode=create_node(name,age);
list[insert_index] =
insert(list[insert_index],nextnode);}
Traversing the List

Read a name and locate where it belongs
D

if (name[0] >= ‘a’ && name[0] <=‘z’)
Doe
search_index = name[0]-97;

if (name[0] >= ‘A’ && name[0] <=‘Z’)
Insert_index = 68-65=3
search_index = name[0]-65;

print(list[search_index]);

Traversing the List with a trailer (prev)
prev=next=list[search_index];
while (list[search_index]!= NULL)

{ prev=next; next = next->pointer;}
Using a trailer is a useful mechanism for inserting or deleting
nodes
Creating the first list

Create the first node

list[0] = new node(“Andy”,23);
To create the other nodes in the first list (begins with
‘A’)

–
if (node_begins_with_A)
{
nextnode=create_node(name,age); // calls the
constructor
list[0]=insert(list[0],nextnode); // inserts the new
node to the
// correct position
}
Finding the place to insert

(for any node)
Find which list the node belongs to
D

Doe
if (name[0] >= ‘a’ && name[0] <=‘z’)
insert_index = name[0]-97;

if (name[0] >= ‘A’ && name[0] <=‘Z’)
Insert_index = 68-65=3
insert_index = name[0]-65;

if (list[insert_index]==NULL) // list is empty
list[insert_index] = create_node(name,age);
else
{ nextnode=create_node(name,age);
list[insert_index] =
insert(list[insert_index],nextnode);}
Traversing the List

Read a name and locate where it belongs
D

if (name[0] >= ‘a’ && name[0] <=‘z’)
Doe
search_index = name[0]-97;

if (name[0] >= ‘A’ && name[0] <=‘Z’)
Insert_index = 68-65=3
search_index = name[0]-65;

print(list[search_index]);

Traversing the List with a trailer (prev)
prev=next=list[search_index];
while (list[search_index]!= NULL)

{ prev=next; next = next->pointer;}
Using a trailer is a useful mechanism for inserting or deleting
nodes
An Application - Dictionary

Arrange the words in alphabetical order
D

Searching for a word involves
–
–
–


finding its array index (starting point)
Traversing that particular linked list
Faster search capabilities (direct index
access)
Any application where arranging data in
categories make sense
Hashing
Creating the first list

Create the first node

list[0] = new node(“Andy”,23);
To create the other nodes in the first list (begins with
‘A’)

–
if (node_begins_with_A)
{
nextnode=create_node(name,age); // calls the
constructor
list[0]=insert(list[0],nextnode); // inserts the new
node to the
// correct position
}
Finding the place to insert

(for any node)
Find which list the node belongs to
D

Doe
if (name[0] >= ‘a’ && name[0] <=‘z’)
insert_index = name[0]-97;

if (name[0] >= ‘A’ && name[0] <=‘Z’)
Insert_index = 68-65=3
insert_index = name[0]-65;

if (list[insert_index]==NULL) // list is empty
list[insert_index] = create_node(name,age);
else
{ nextnode=create_node(name,age);
list[insert_index] =
insert(list[insert_index],nextnode);}
Traversing the List

Read a name and locate where it belongs
D

if (name[0] >= ‘a’ && name[0] <=‘z’)
Doe
search_index = name[0]-97;

if (name[0] >= ‘A’ && name[0] <=‘Z’)
Insert_index = 68-65=3
search_index = name[0]-65;

print(list[search_index]);

Traversing the List with a trailer (prev)
prev=next=list[search_index];
while (list[search_index]!= NULL)

{ prev=next; next = next->pointer;}
Using a trailer is a useful mechanism for inserting or deleting
nodes
An Application - Dictionary

Arrange the words in alphabetical order
D

Searching for a word involves
–
–
–


finding its array index (starting point)
Traversing that particular linked list
Faster search capabilities (direct index
access)
Any application where arranging data in
categories make sense
Hashing