Data Representation Methods

Download Report

Transcript Data Representation Methods

Linear List
Linked Representation
Linked Representation
• list elements are stored, in memory,
in an arbitrary order
• explicit information (called a link)
is used to go from one element to
the next
Memory Layout
Layout of L = (a,b,c,d,e) using an array representation.
a b
c
d
e
The figures show computer memory. An array uses a contiguous
chunk of memory.
A linked representation uses an arbitrary layout.
c
a
e
d
b
Linked Representation
c
a
e
d
firstNode
pointer (or link) in e is NULL
use a variable firstNode to get to the
first element a
b
Normal Way To Draw A Linked List
firstNode
NULL
a
b
c
link or pointer field of node
data field of node
d
e
Chain
firstNode
NULL
a
b
c
d
e
•A chain is a linked list in which each node
represents one element.
• There is a link or pointer from one element to
the next.
• The last node has a NULL pointer.
Node Representation
template <class T>
struct chainNode
{
// data members
T element;
chainNode<T> *next;
next
element
// constructors come here
};
Constructors Of chainNode
?
chainNode() {}
chainNode(const T& element)
{this->element = element;}
?
?
element
chainNode(const T& element, chainNode<T>* next)
{this->element = element;
this->next = next;}
next
element
get(0)
firstNode
NULL
a
b
c
d
e
checkIndex(0);
desiredNode = firstNode; // gets you to first node
return desiredNode->element;
Do a checkIndex first. Then move to desired node. Once you are at the desired node,
you can return the element in that node as in
return desiredNode.element;
When firstNode = null, there is no element whose index is 0.
get(1)
firstNode
NULL
a
b
c
d
e
checkIndex(1);
desiredNode = firstNode->next; // gets you to second node
return desiredNode->element;
get(2)
firstNode
NULL
a
b
c
d
e
checkIndex(2);
desiredNode = firstNode->next->next; // gets you to third node
return desiredNode->element;
get(5)
firstNode
NULL
a
b
c
d
e
checkIndex(5);
// throws exception
desiredNode = firstNode->next->next->next->next->next;
// desiredNode = NULL
return desiredNode->element; // NULL.element
Get(theIndex)
• Check theIndex
• currentNode = firstNode
• For (i=0; i<theIndex;i++)
– currentNode = currentNode->next;
• Return currentNode->element
Erase An Element
firstNode
NULL
a
b
c
d
erase(0)
deleteNode = firstNode;
firstNode = firstNode->next;
delete deleteNode;
e
erase(2)
firstNode
NULL
a
b
cc
d
beforeNode
e
first get to node just before node to be removed
beforeNode = firstNode->next;
erase(2)
firstNode
null
a
b
c
d
e
beforeNode
save pointer to node that will be deleted
deleteNode = beforeNode->next;
erase(2)
firstNode
null
a
b
c
d
e
beforeNode
now change pointer in beforeNode
beforeNode->next = beforeNode->next->next;
delete deleteNode;
Erase(theIndex)
• Check theIndex
• If theIndex == 0
– Delete firstNode
– firstNode = firstNode->next
• Else
–
–
–
–
Find beforeNode
deleteNode = beforeNode->next;
beforeNode->next = beforNode->next->next
Delete deleteNode
• listSize--
insert(0,’f’)
firstNode
NULL
f
a
b
c
d
e
newNode
Step 1: get a node, set its data and link fields
newNode = new chainNode<char>(theElement,
firstNode);
insert(0,’f’)
firstNode
NULL
f
a
b
c
newNode
Step 2: update firstNode
firstNode = newNode;
d
e
One-Step insert(0,’f’)
firstNode
NULL
f
a
b
c
d
e
newNode
firstNode = new chainNode<char>(‘f’, firstNode);
insert(3,’f’)
firstNode
newNode
f
NULL
a
c
b
d
e
beforeNode
• first find node whose index is 2
• next create a node and set its data and link fields
chainNode<char>* newNode = new chainNode<char>( ‘f’,
beforeNode->next);
• finally link beforeNode to newNode
beforeNode->next = newNode;
Two-Step insert(3,’f’)
firstNode
newNode
f
NULL
a
b
c
d
e
beforeNode
beforeNode = firstNode->next->next;
beforeNode->next = new chainNode<char>
(‘f’, beforeNode->next);
Insert(theIndex, theElement)
• Check theIndex (0-listSize)
• If theIndex == 0
– create newNode
– newNode = firstNode
• Else
–
–
–
–
Create newNode
Find the beforeNode
newNode->next = beforeNode->next
beforeNode->next = newNode
• listSize++
Other chain structures
Chain With Header Node
headerNode
NULL
a
b
c
d
e
firstNode
NULL
a
b
c
d
insert/erase code is simplified
e
Empty Chain With Header Node
headerNode
NULL
The Class chainWithHeader
template<class T>
class chainWithHeader : public linearList<T>
{
public:
// other ADT methods defined here
protected:
void checkIndex(int theIndex) const;
chainNode<T>* HeaderNode;
int listSize;
};
Circular List
firstNode
a
b
c
d
e
firstNode
a
b
c
d
Insert(0,’f’)
insert(listSize,’g’)
erase(0)
erase(listSize-1)
e
Doubly Linked List
firstNode
lastNode
NULL
NULL
a
b
c
d
e
Node Representation
template <class T>
struct doublyChainNode
{
// data members
T element;
chainNode<T> *previous;
chainNode<T> *next;
// constructors come here
};
previous
next
element
firstNode
lastNode
NULL
NULL
a
b
c
d
Insert(0,’f’)
insert(listSize,’g’)
erase(0)
erase(listSize-1)
e
Doubly Linked Circular List
firstNode
a
b
c
d
e
headerNode
a
b
c
d
Insert(0,’f’)
insert(listSize,’g’)
erase(0)
erase(listSize-1)
e
Doubly Linked Circular List With Header Node
headerNode
a
b
c
d
e
Empty Doubly Linked Circular List With Header Node
headerNode
The STL Class list/vector




Linked implementation of a linear list.
Doubly linked circular list with header node.
Has many more methods than chain.
Similar names and signatures.
Lab 2
• Check the note and download your lab
material before lab, bring your labtop with
you in class!