Data Structures Algorithms and Discrete Mathematics I Lecture 10 150209 CARRANO Chapt 9 Agenda HW3 Questions Linked Lists Midterm Prep Review from last time Built a PushPop Stack using a linked list ID: 472169
Download Presentation The PPT/PDF document "CSS 342" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
CSS 342
Data
Structures, Algorithms, and Discrete Mathematics I
Lecture 10. 150209.
CARRANO
Chapt
. 9Slide2
Agenda
HW3 Questions
Linked Lists
Midterm PrepSlide3
Review from last timeā¦
Built a Push/Pop Stack using a linked list
Determined semantics of a stack
Determined that overloading of = required, also copy constructor, and destructorSlide4
Copy Constructor
Can be implemented with call to default constructor and then assignment
MyObj
o1 = o2
Pass by Value
Return by ValueSlide5
Array v. Linked List for Stack
Arrays easy to use, but have fixed size
Increasing size of dynamically allocated array can waste storage, time
Array based implementation good for small bag
Linked chains do not have fixed sizeSlide6
Sorted s
ingly linked listSlide7
Sorted list using linked list
impl
Singly linked list remains the same structure as the stack but need to implement
Insert
RemoveSlide8
insert
(
int
x
)
Node Insertion
Find node to insert after: current,
Node *
insNode
= new Node
;
insNode
->
val
= x;insNode->next = current->next;current->next = insNode;
header
header
val
next
val
next
NULL
NULL
val
next
current
new
Value
next
insNode
X
Three cases for finding node to insert after
1) Empty List
2) First in line
3) All others
Some recommend a dummy front node to collapse 2 and 3Slide9
bool
IntSortedList
::Insert(
int
val
)
{
Node
*
insNode
=
new
Node; insNode
->value = val;
if
(head == NULL
) { head
=
insNode; return
true; }
if (
val < head->value) {
insNode->next = head;
head = insNode;
return true;
}Node
*
pNode = head;while
((pNode->next != NULL
) && ((
pNode->next)->value < val))
{pNode =
pNode
->next;}insNode->next =
pNode->next;pNode->next =
insNode;return true;}InsertSlide10
Dangling References: common causes
A pointer which is initialized but not set to NULL
Delete or free is called and pointer is not set to NULL
Aliasing of pointers which are not updated in tandemSlide11
Computer Scientist of the week
Ada Lovelace
Aka, Augustus Ada Byron, aka Augustus Ada Lovelace, aka the Countess
of Lovelace
Daughter of the famous Poet, Lord Byron
Partnered with Charles Babbage on Analytical Engine
Wrote first computer program!
Algorithm to
be executed
by a machine
Computer Bernoulli numbers
Also contributed to mathematics and phrenologySlide12
remove(
int
)
Stop the current pointer at the node previous to a deleted
node
First node is special case or use a dummy first node
Memorize the node to
remove
Node
*
dNode
= current->next;
Remove this node
current-
>next = current->next->next;Deallocating the node delete
deletedNode;
20
45
76
header
current
Node to be deletedSlide13
RemoveSlide14
List can also be implemented with an array
Easy implementation
Fixed list size
Necessity to shift data down to an extended space upon an insertion and to shift data up upon a
deletion
list[0]
list[1]
list[3]
list[2]
list[4]
list[n]
list[n-1]Slide15
Circular Linked Lists
Linearly linked
lists
:
Circular linked lists
:
Traverse in circular linked lists:
Node
*first =
head;
Node
*cur =
head->
next;
while ( cur != first ) {
display(cur->item); cur = cur->next;};
45
51
NULL
head
45
51
head
dummy
dummySlide16
Doubly Linked Lists
NULL
45
73
51
NULL
head
45
73
51
NULL
head
Single Linked Lists
To be deleted
Doubly Linked Lists
cur->
prev
->next = cur->next
But pointer operations become more complicated.Slide17
Inserting a Node
before the current pointer
45
73
20
newPtr
->next = cur;
//
(a)
newPrt
->
prev
= cur->
prev
; // (b)
cur->
prev
=
newPtr
;
//
(c)
newPtr
->
prev
->next =
newPtr
; // (d)
cur
(a)
(b)
(c)
(d)