Escolar Documentos
Profissional Documentos
Cultura Documentos
Chapter
Data Structures:
Structures: Linked
Linked Lists
Lists
Data
CSC 113
King Saud University
College of Computer and Information Sciences
Department of Computer Science
Dr. S. HAMMAMI
1. Objectives
After you have read and studied this chapter, you should be able to:
Understandthe
theconcept
conceptof
ofaadynamic
dynamicdata
datastructure.
structure.
Understand
Beable
ableto
tocreate
createand
anduse
usedynamic
dynamicdata
datastructures
structuressuch
suchas
aslinked
linked
Be
lists.
lists.
Understandthe
thestack
stackand
andqueue
queueADTs.
ADTs.
Understand
Variousimportant
importantapplications
applicationsof
oflinked
linkeddata
datastructures.
structures.
Various
Knowhow
howto
touse
useinheritance
inheritanceto
todefine
define extensible
extensibledata
data
Know
structures.
structures.
Createreusable
reusabledata
datastructures
structureswith
withclasses,
classes,inheritance
inheritanceand
and
Create
composition.
composition.
Dr. Salah Hammami
KSU-CCIS-CS
Self-referential class
Contains an instance variable that refers to another
object of the same class type
KSU-CCIS-CS
A link to another
Node object.
T
Node<T>
T
Node<T>
Node (in o: T)
setData(in o: T)
getData(): T
setNext(in link: Node<T>)
getNext(): Node<T>
1
1
next
data
Node<T>
Node (in o: T)
setData(in o: T)
getData(): T
setNext(in link:
Node<T>)
getNext():
Node<T>
Dr. Salah Hammami
KSU-CCIS-CS
- name: String
- id: int
- grade: double
Node<T>
T
Node<T>
+ student()
+ set()
Node (in o: T)
setData(in o: T)
getData(): T
setNext(in link: Node<T>)
getNext(): Node<T>
+ get()
Ali
Jamel
Kamal
KSU-CCIS-CS
KSU-CCIS-CS
StudentS1
S1==new
newStudent(Ali,
Student(Ali,42700000,
42700000,3.5)
3.5)
Student
StudentS2
S2==new
newStudent(Ahmed,
Student(Ahmed,4271000,
4271000,3.9)
3.9)
Student
Node <Student>
<Student> pp == new
new Node
Node <Student>(S1);
<Student>(S1);
Node
Node <Student>
<Student> qq == new
new Node(S2);
Node(S2);
Node
allocate storage
storage for
for two
two objects
objects of
of type
typeNode
Node referenced
referenced by
by pp and
and qq. .
allocate
storesthe
theobject
objectS1
S1, ,and
andthe
thenode
nodereferenced
referenced
Thenode
nodereferenced
referencedby
byppstores
The
storesthe
theobject
objectS2
S2. .The
Thenext
nextfields
fieldsof
ofboth
bothnodes
nodesare
arenull
null. .
byqqstores
by
S1
S2
KSU-CCIS-CS
storesthe
theaddress
addressof
ofnode
nodeqqininthe
thelink
linkfield
fieldof
ofnode
nodepp, ,thereby
thereby
stores
nodeqq, ,and
andforming
formingaalinked
linkedlist
listwith
with22nodes.
nodes.
connectingnode
nodepptotonode
connecting
nextfield
fieldof
ofthe
thesecond
secondlist
listnode
nodeindicates
indicatesthe
the
Thediagonal
diagonalline
lineininthe
thenext
The
valuenull
null. .
value
Linked list with two nodes
S1
S2
KSU-CCIS-CS
KSU-CCIS-CS
List<T>
T
name: String
List ()
1
data
1
next
List(n: String)
insertAtFront(o: T)
Node<T>
insertAtBack(o: T)
Node (in o: T)
setData(in o: T)
getData(): T
setNext(in link:
Node<T>)
getNext():
Node<T>
first
removeFromFront():T
removeFromBack():T
isEmpty(): Boolean
size(): int
insertAfter(int, T)
insertBefore(int, T)
remove(int):T
KSU-CCIS-CS
public List()
{ name= ;
first=null;
}
}
Dr. Salah Hammami
KSU-CCIS-CS
KSU-CCIS-CS
KSU-CCIS-CS
public void
void insertAtFront(T
insertAtFront(T obj)
obj) {{
public
Node NN == new
new Node(obj);
Node(obj);
Node
N.setNext(first);
N.setNext(first);
first == N;
N;
first
// insertAtFront()
insertAtFront()
}} //
KSU-CCIS-CS
KSU-CCIS-CS
KSU-CCIS-CS
Solution 1
first == new
new Node(obj);
Node(obj);
first
else {{
else
Node current
current == first;
first;
Node
while (current.getNext()
(current.getNext() !=
!= null)
null)
while
current == current.getNext();
current.getNext();
current
current.setNext(new Node(obj));
Node(obj));
current.setNext(new
// Start
Start at
at head
head of
of list
list
//
// Find
Find the
the end
end of
of the
the list
list
//
// Insert
Insert the
the newObj
newObj
//
}}
// insertAtRear
insertAtRear
}} //
public void
void insertAtBack(T
insertAtBack(T obj)
obj) {{
public
Node NN =new
=new Node(obj);
Node(obj);
Node
Solution 2
if (isEmpty())
(isEmpty())
if
first == N;
N;
first
else {{
else
Node current
current == first;
first;
Node
while (current.getNext()
(current.getNext() !=
!= null)
null)
while
current == current.getNext();
current.getNext();
current
current.setNext(N));
current.setNext(N));
// Start
Start at
at head
head of
of list
list
//
// Find
Find the
the end
end of
of the
the list
list
//
// Insert
Insert the
the newObj
newObj
//
}}
// insertAtRear
insertAtRear
}} //
KSU-CCIS-CS
MethodremoveFromFronts
removeFromFrontssteps
steps
Method
Throwan
anEmptyListException
EmptyListExceptionififthe
thelist
listisisempty
empty
Throw
AssignfirstNode.data
firstNode.datatotoreference
referenceremovedItem
removedItem
Assign
firstNodeand
andlastNode
lastNoderefer
refertotothe
thesame
sameobject,
object,set
setfirstNode
firstNodeand
and
IfIffirstNode
lastNodetotonull
null
lastNode
thelist
listhas
hasmore
morethan
thanone
onenode,
node,assign
assignthe
thevalue
valueof
offirstNode.nextNode
firstNode.nextNodetoto
IfIfthe
firstNode
firstNode
Returnthe
theremovedItem
removedItemreference
reference
Return
KSU-CCIS-CS
KSU-CCIS-CS
Code of removeFromFront
public TT removeFromFrontt()
removeFromFrontt() {{
public
if (isEmpty())
(isEmpty())
if
return null;
null;
return
Node NN == first;
first; //
// TT dd == first.getdata();
first.getdata();
Node
first == first.getNext();
first.getNext();
first
return N.getData();
N.getData(); //
// return
return d;
d;
return
}}
KSU-CCIS-CS
KSU-CCIS-CS
KSU-CCIS-CS
KSU-CCIS-CS
name: String
1
next
List ()
data
List(n: String)
Node<T>
Node (in o: T)
setData(in o: T)
getData(): T
setNext(in link:
Node<T>)
getNext():
Node<T>
displayAll()
1
first
countSimilar(T): int
search(T): int
remove(int): T
remove(T):T
Intersection(List<T>): List<T>
KSU-CCIS-CS
<Interface>
Node current
current == first;
first;
Node
while (current!=
(current!= null)
null)
while
AAAA
display()
{{
current.getData().display()
display();;
current.getData().
current=current.getNext();
current=current.getNext();
}}
}}
}}
This method must be implemented by each substitute class of the generic type.
We add an Interface to our package containing this method display and each substitutions class of the
generic type must implement the Interface.
KSU-CCIS-CS
<Interface>
AAAA
display()
if (current.getData().
(current.getData().compare(t))
compare(t))
if
x++;
x++;
compare(Object): Boolean
current=current.getNext();
current=current.getNext();
}}
return x;
x;
return
}}
This method must be implemented by each substitute class of the generic type.
We add an Interface to our package containing this method compare and each substitutions class of the
generic type must implement the Interface.
KSU-CCIS-CS
<Interface>
AAAA
display()
x++;
x++;
if (current.getData().
(current.getData().compare(t))
compare(t))
if
compare(Object): Boolean
return x;
x;
return
current=current.getNext();
current=current.getNext();
}}
return -1;
-1;
return
}}
This method must be implemented by each substitute class of the generic type.
We add an Interface to our package containing this method compare and each substitutions class of the
generic type must implement the Interface.
Dr. Salah Hammami
KSU-CCIS-CS
KSU-CCIS-CS
Code of remove(T):T
public int
int remove(T
remove(T t)
t) {{
public
int xx == search(t);
search(t);
int
return remove(x);
remove(x);
return
}}
KSU-CCIS-CS