Title: Concept of lists,
1- Concept of lists,
- as an abstract data structure
- and
- Implementations of lists, with arrays and others
2pointer is only an implementation issue
- Indices (relative addresses), indexing variable,
array elements - (absolute) addresses, pointer, and elements
-
-
3A simple list example (not linked list!)
- Concept of a list, e.g. a list of integers
- A list is a linear sequence of objects
- Print out info
- Empty test
- Search
- Insertion
- Deletion
-
- implemented
- by a static array (over-sized if necessary)
- int list1000 int size
- by a dynamic array
- int listsize int size
- by a linked list and more
-
-
4Using a (static) array
const int DIM1000 int main() int ADIM int
n cin gtgt n
int main() int A1000 int n cin gtgt
n initialize(A, n, 0) print(A,
n) addEnd(A,n,5) // or A addEnd(A,n,5)
print(A, n) addHead(A,n,5) // or A
addHead(A,n,5) print(A, n) deleteFirst(A,n)
// or A deleteFirst(A,n) print(A,
n) selectionSort(A, n) print(A, n)
Static array!
How to use a list?
5Initialize
- void initialize(int list, int size, int value)
- for(int i0 iltsize i)
- listi value
6Print out a list
- void print(int list, int size)
- cout ltlt " "
- for(int i0 iltsize i)
- cout ltlt listi ltlt " "
- cout ltlt "" ltlt endl
7Delete the first element
// for deleting the first element of the
array void deleteFirst(int list, int
size) for(int i0 iltsize-1 i) listi
listi1 size--
deleteFirst(A,n)
- int deleteFirst(int list, int size)
-
- for(int i0 iltsize-1 i)
- listi listi1
-
-
- size--
- return list
-
B deleteFirst(A,n)
Pointer type!
8 int deleteFirst(int head, int
size) size-- return head
- int deleteFirst(int list, int size)
-
- for(int i0 iltsize-1 i)
- listi listi1
-
-
- size--
- return list
-
int deleteFirst(int list, int
size) for(int i0 iltsize-1 i) (listi)
(listi1) size-- return list
int deleteFirst(int head, int
size) for(int i0 iltsize-1 i) (headi)
(headi1) size-- return head
9Adding Elements
// for adding a new element to end of array void
addEnd(int list, int size, int value) if
(size lt DIM) listsize value size
addEnd(A,n,v)
If not full!
- int addEnd(int list, int size, int value)
-
- if ((sizegt0) (size lt DIM))
- listsize value
- size
-
- return list
B addEnd(A,n,v)
10Add at the beginning
// for adding a new element at the beginning of
the array void addHead(int list, int size, int
value) if(size lt DIM) for(int isize-1
igt0 i--) listi1 listi list0
value size else cout ltlt out of array
memory!!! ltlt endl
addHead(A,n,v)
- int addHead(int list, int size, int value)
-
- if(size lt DIM)
- for(int isize-1 igt0 i--)
- listi1 listi
- list0 value
- size
- return list
-
- else
B addHead(A,n,v)
11Using a dynamic array
int main() cout ltlt "Enter list size " int
n cin gtgt n int A new intn
12How to use a list?
int main() cout ltlt "Enter list size " int
n cin gtgt n int A new intn initialize(A,
n, 0) print(A, n) A addEnd(A,n,5) print(A,
n) A addHead(A,n,5) print(A, n) A
deleteFirst(A,n) print(A, n) selectionSort(A,
n) print(A, n) delete A
int A1000 int n cin gtgt n
13Initialize and Print-out the same as before
- void initialize(int list, int size, int value)
- for(int i0 iltsize i)
- listi value
void print(int list, int size) cout ltlt "
" for(int i0 iltsize i) cout ltlt
listi ltlt " " cout ltlt "" ltlt endl
14Delete the first element
- // for deleting the first element of the array
- int deleteFirst(int list, int size)
- int newList
- if (sizegt1)
- newList new intsize-1 // make new array
-
- // copy and delete old array
- for(int i0 iltsize-1 i)
- newListi listi1
- delete list
-
- size--
- return newList
-
- else
-
If not empty!!!, cannot delete an empty list.
15Remark
Instead of A deleteFirst(A,n) we can also
just do deleteFirst(A,n) if we define as a
void type function
void deleteFirst(int A, int size) A
newList
For a static array, A does not change, but the
content of A changed. For a dynamic array, A
changed!
16Adding Elements
- // for adding a new element to end of array
- int addEnd(int list, int size, int value)
- int newList
- newList new int size1 // make new array
-
- if(size) // copy and delete old array
- for(int i0 iltsize i)
- newListi listi
- delete list
-
- newListsize value
- size
- return newList
Not empty list
17Add at the beginning
- // for adding a new element at the beginning of
the array - int addHead(int list, int size, int value)
- int newList
- newList new int size1 // make new array
-
- if(size) // copy and delete old array
- for(int i0 iltsize i)
- newListi1 listi
- delete list
-
- newList0 value
- size
- return newList
18Search and delete
- // for adding a new element to end of array
- int delete(int list, int size, int value)
-
- Search the element in the array and get the
position - Shift all elements after it towards this position
-
19 20Motivation
- A List is a useful structure to hold a
collection of data. - Currently, we use arrays for lists
- Examples
- List of ten students marks
- int studentMarks10
- List of temperatures for the last two weeks
- double temperature14
21Motivation
- list using static array
- int myArray1000
- int n
- We have to decide (to oversize) in advance the
size of the array (list) - list using dynamic array
- int myArray
- int n
- cin gtgt n
- myArray new intn
- We allocate an array (list) of any specified size
while the - program is running
- linked-list (dynamic size)
- size ??
- The list is dynamic. It can grow and shrink to
any size.
22How to use a linked list?
int main() initialize(A,0) print(A) A
addEnd(A,5) print(A) A addHead(A,5)
print(A) A deleteFirst(A)
print(A) selectionSort(A) print(A)
int main() initialize(A, n, 0) print(A,
n) A addEnd(A,n,5) print(A, n) A
addHead(A,n,5) print(A, n) A
deleteFirst(A,n) print(A, n) selectionSort(A,
n) print(A, n)
n is removed, so we dont need to care about
it!
23Array naturally represents a (ordered) list, the
link is implicit, consecutive and contiguous!
Now the link is explicit, any places!
Data
75
85
20
45
Link
Link
Data
45
85
20
75
24Linked List Structure
- Definition
- struct Node
- int data
- Node next
-
- typedef Node NodePtr
- Create a Node
- NodePtr p
- p new Node
- Delete a Node
- delete p
- Definition
- struct Node
- int data
- Node next
-
- Create a Node
- Node p
- p new Node
- Delete a Node
- delete p
25- Access fields in a node
- (p).data //access the data field
- (p).next //access the pointer field
- Or it can be accessed this way
- p-gtdata //access the data field
- p-gtnext //access the pointer field
26Representing and accessing linked lists
- We define a pointer
- NodePtr head (also Node head)
- that points to the first node of the linked
list. When the linked list is empty then head is
NULL.
27Passing a Linked List to a Function
It is roughly the same as for an array!!!
l deleteHead(l) node deleteHead(node list)
- When passing a linked list to a function it
should suffice to pass the value of head. Using
the value of head the function can access the
entire list. - Problem If a function changes the beginning of a
list by inserting or deleting a node, then head
will no longer point to the beginning of the
list. - Solution When passing head always pass it by
reference - or using a function to return
a new pointer value
28Manipulation (implementation) of a Unsorted
Linked List
29Start the first node from scratch
head NULL
Head
- NodePtr newPtr
- newPtr new Node
- newPtr-gtdata 20
- newPtr-gtnext NULL
- head newPtr
-
30Inserting a Node at the Beginning
- newPtr new Node
- newPtr-gtdata 13
- newPtr-gtnext Head
- head newPtr
-
20
Head
13
newPtr
31Keep going
32Adding an element to the head
- void addHead(NodePtr head, int newdata)
-
- NodePtr newPtr new Node
- newPtr-gtdata newdata
- newPtr-gtnext Head
- head newPtr
33It can also be written (more functionally) as
NodePtr addHead(NodePtr head, int
newdata) NodePtr newPtr new
Node newPtr-gtdata newdata newPtr-gtnext
Head return newPtr
Compare it with addHead with a dynamic array
implementation
34Deleting the Head Node
- NodePtr p
- p head
- head head-gtnext
- delete p
-
(to delete)
head
50
40
13
20
p
35- void deleteHead(NodePtr head)
-
- if(head ! NULL)
- NodePtr p head
- head head-gtnext
- delete p
-
Or as a function
NodePtr deleteHead(NodePtr head) if(head !
NULL) NodePtr p head head
head-gtnext delete p return
head
36A (unsorted) list with linked data representation
struct Node int data Node next
? Data representation
? Operations
void addHead(Node head, int newdata) Node
newPtr new Node newPtr-gtdata
newdata newPtr-gtnext Head head newPtr
- void deleteHead(Node head)
- if(head ! NULL)
- Node p head
- head head-gtnext
- delete p
-
void ()
37Re-write
struct Node int data Node next
void addHead(Node head, int newdata) Node
newPtr new Node newPtr-gtdata
newdata newPtr-gtnext Head head newPtr
typedef List Node List addhead(List int
data) List deleteHead(List)
- void deleteHead(Node head)
- if(head ! NULL)
- Node p head
- head head-gtnext
- delete p
-
void ()
38Displaying a Linked List
- p head print out (p)
- p p-gtnext print out (p)
head
20
45
p
head
20
45
p
39A linked list is displayed by walking through its
nodes one by one, and displaying their data
fields (similar to an array!).
- void displayList(NodePtr head)
- NodePtr p
- p head
- while(p ! NULL)
- cout ltlt p-gtdata ltlt endl
- p p-gtnext
-
p ? p-gtnext
For an array
void displayArray(int data, int size) int
i i0 while ( iltsize ) cout ltlt datai
ltlt endl i
void displayArray(int data, int size)
int p pdata while ( (p-data)ltsize )
cout ltlt p ltlt endl p
i ? p
40Searching for a value in a linked list (look at
array searching first!)
41Remember searching algorithm in an array
- void main()
- const int size8
- int datasize 10, 7, 9, 1, 17, 30, 5, 6
- int value
- cout ltlt "Enter search element "
- cin gtgt value
- int n0
- int position-1
- bool foundfalse
- while ( (nltsize) (!found) )
- if(datan value)
- foundtrue
- positionn
- n
-
- if(position-1) cout ltlt "Not found!!\n"
- else cout ltlt "Found at " ltlt position ltlt endl
It is essentially the same!
42Searching for a value
- NodePtr searchNode(NodePtr head, int item)
- NodePtr p head
- NodePtr position NULL
- bool foundfalse
- while((p ! NULL) (!found))
- if(p-gtdata item)
- found true position p
- p p-gtnext
-
- return position
int searchArray(int data, int size, int
value) int n0 int position-1 bool
foundfalse while ( (nltsize) (!found) )
if(datan value) foundtrue
positionn n return
position
n?p
If we use a pointer to an array, it will be even
closer!
43More operation adding to the end
- Original linked list of integers
- Add to the end (insert at the end)
60
50
40
13
20
Last element
The key is how to locate the last element or node
of the list!
44Add to the end
- void addEnd(NodePtr head, int newdata)
- NodePtr newPtr new Node
- newPtr-gtdata newdata
- newPtr-gtnext NULL
- NodePtr last head
- if(last ! NULL) // general non-empty list
case - while(last-gtnext ! NULL)
- lastlast-gtnext
- last-gtnext newPtr
-
- else // deal with the case of empty list
- head newPtr
Link new object to last-gtnext
Link a new object to empty list
45Add to the end as a function
- NodePtr addEnd(NodePtr head, int newdata)
- NodePtr newPtr new Node
- newPtr-gtdata newdata
- newPtr-gtnext NULL
- NodePtr last head
- if(last ! NULL) // general non-empty list
case - while(last-gtnext ! NULL)
- lastlast-gtnext
- last-gtnext newPtr
-
- else // deal with the case of empty list
- head newPtr
- return head
46Manipulation (implementation) of a Sorted Linked
List
47Inserting a value in a sorted list
How to do it in a sorted array? a static array
and a dynamic array
- 1. Find the position
- 2. Free up the place by moving the others
- 3. Insert the new value
48Inserting a Node
- 1. (a) Create a new node using
- NodePtr newPtr new node
- (b) Fill in the data field correctly.
- 2. Find prev and cur such that
- the new node should be inserted between
prev and cur. - 3. Connect the new node to the list by using
- (a) newPtr-gtnext cur
- (b) prev-gtnext newPtr
49Finding prev and cur
- Suppose that we want to insert or delete a node
with data value newValue. Then the following code
successfully finds prev and cur such that - prev-gtdata lt newValue lt cur-gtdata
50Its a kind of search algo,
prev NULL cur head foundfalse while(
(cur!NULL) (!found) ) if (newValue gt
cur-gtdata) prevcur curcur-gtnext
else found true
Prev is necessary as we cant go back!
51A useful programming trick boolean found can
be avoided.
Finally, it is equivalent to
prev NULL cur head while( (cur!NULL)
(newValuegtcur-gtdata) ) prevcur curcur-gt
next
Logical AND () is short-circuited, sequential,
i.e. if the first part is false, the second part
will not be executed.
52- //insert item into linked list according to
ascending order - void insertNode(NodePtr head, int item)
- NodePtr newp, cur, pre
- newp new Node
- newp-gtdata item
- pre NULL
- cur head
- while( (cur ! NULL) (itemgtcur-gtdata))
- pre cur
- cur cur-gtnext
-
- if(pre NULL) //insert to head of linked list
- newp-gtnext head
- head newp
- else
- pre-gtnext newp
- new-gtnext cur
If the position happens to be the head
General case
53Deleting a Node
- To delete a node from the list
- 1. Locate the node to be deleted
- (a) cur points to the node.
- (b) prev points to its predecessor
- 2. Disconnect node from list using
- prev-gtnext cur-gtnext
- 3. Return deleted node to system
- delete cur
(to delete)
Head
...
20
45
75
85
cur
prev
54Delete an element in a sorted linked list
- void deleteNode(NodePtr head, int item)
- NodePtr prevNULL, cur head
- while( (cur!NULL) (item gt cur-gtdata))
- prev cur
- cur cur-gtnext
-
- if ( cur!NULL cur-gtdataitem)
-
- if(curHead)
- Head Head-gtnext
- else
- prev-gtnext cur-gtnext
- delete cur
-
Get the location
If its in, negation of not in
We can delete only if the element is present! If
(curNULL cur-gtdata!item) Item is not in the
list!
If the element is at the head
General case
55Other variants of linked lists
56Motivation there are always many special cases
- the empty case
- the first element case
- the last element case
- the middle element case
-
tedious implementation
57A note on Dummy Head node
- A well-known implementation trick or method
add one more node at the beginning, which does
not store any data, to ease operations. - always present, even when the linked list is
empty - Insertion and deletion algorithms
- initialize prev to reference the dummy head
node, rather than NULL
head
empty list!
head
40
13
20
Dummy head node
list of (40,13,20)
58Circular Linked Lists
- A Circular Linked List is a special type of
Linked List - It support the traversing from the end of the
list to the beginning of the list by making the
last node points back to the head of the list
Rear
59Doubly Linked Lists
- In a Doubly Linked-List each item points to both
its predecessor and successor - prev points to the predecessor
- next points to the successor
60Doubly Linked List Definition
- struct Node
- int data
- Node next
- Node prev
-
- typedef Node NodePtr
61Doubly Linked Lists with Dummy Head Node
- To simplify insertion and deletion by avoiding
special cases of deletion and insertion at front
and rear, a dummy head node is added at the head
of the list - The last node also points to the dummy head node
as its successor
62Idea of dummy object
dummy object is also called a sentinel, it
allows the simplification of special cases, but
confuses the emptyness NULL!
- Instead of pointing to NULL, point to the
dummy!!! - Skip over the dummy for the real list
63Empty list
Head-gtnext head compared with headNULL
64- Concept of lists,
- as an abstract data structure
- Implementations of lists, with arrays and others
- The same list operators, different efficiencies