Review on linked lists - PowerPoint PPT Presentation

About This Presentation
Title:

Review on linked lists

Description:

Using the value of head the function can access the entire list. ... Solution: When passing head always pass it by reference (not good! ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 41
Provided by: csU54
Category:
Tags: good | head | linked | lists | review

less

Transcript and Presenter's Notes

Title: Review on linked lists


1
Review on linked lists
2
Motivation
  • 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

3
Motivation
  • 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.

4
Array naturally represents a (ordered) list,
the link is implicit, consecutive and
contiguous! Now the link is explicit, any places!
Data
75
85
20
45
0
1
array
2
Link
Link
Data
45
linked list
85
20
75
5
Linked Lists Basic Idea
  • A linked list is an ordered collection of data
  • Each element of the linked list has
  • Some data
  • A link to the next element
  • The link is used to chain the data
  • Example A linked list of integers

6
Linked Lists Basic Ideas
  • The list can grow and shrink

75
7
Linked Lists Operations
  • Original linked list of integers
  • Insertion (in the middle)
  • Deletion (in the middle)

20
45
75
85
old value
20
45
75
85
60
20
45
75
85
deleted item
8
Definition of linked list type
  • struct Node
  • int data
  • Node next
  • We can also
  • typedef Node NodePtr

9
Linked List Structure
  • Node Data Link
  • Definition
  • struct Node
  • int data //contains useful information
  • Node next //points to next element or NULL
  • Create a Node
  • Node p
  • p new Node //points to newly allocated memory
  • Delete a Node
  • delete p

10
  • 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

11
Representing and accessing linked lists
  • We define a pointer
  • Node head
  • that points to the first node of the linked
    list. When the linked list is empty then head is
    NULL.

12
Passing a Linked List to a Function
It is roughly the same as for an array!!!
  • 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 (not good!)
  • or using a function to return
    a new pointer value

13
Implementation of an (Unsorted) Linked List
14
Start the first node from scratch
head NULL
Head
  • Node newPtr
  • newPtr new Node
  • newPtr-gtdata 20
  • newPtr-gtnext NULL
  • head newPtr

15
Inserting a Node at the Beginning
  • newPtr new Node
  • newPtr-gtdata 13
  • newPtr-gtnext Head
  • head newPtr

20
Head
13
newPtr
16
Keep going

17
Adding an element to the head
NodePtr
  • void addHead(Node head, int newdata)
  • Node newPtr new Node
  • newPtr-gtdata newdata
  • newPtr-gtnext Head
  • head newPtr

Call by reference, scaring!!!
18
Also written (more functionally, better!) as
Node addHead(Node head, int newdata) Node
newPtr new Node newPtr-gtdata
newdata newPtr-gtnext Head return newPtr
Compare it with addHead with a dynamic array
implementation
19
Deleting the Head Node
  • Node p
  • p head
  • head head-gtnext
  • delete p

(to delete)
head
50
40
13
20
p
20
  • void deleteHead(Node head)
  • if(head ! NULL)
  • NodePtr p head
  • head head-gtnext
  • delete p

As a function
Node deleteHead(Node head) if(head !
NULL) NodePtr p head head
head-gtnext delete p return
head
21
Displaying a Linked List
  • p head
  • p p-gtnext

head
20
45
p
head
20
45
p
22
A linked list is displayed by walking through its
nodes one by one, and displaying their data
fields (similar to an array!).
  • void displayList(Node head)
  • NodePtr p
  • p head
  • while(p ! NULL)
  • cout ltlt p-gtdata ltlt endl
  • p p-gtnext

For an array
void displayArray(int data, int size) int
n0 while ( nltsize ) cout ltlt datai ltlt
endl n
23
Searching for a node (look at array searching
first!)
  • //return the pointer of the node that has
    dataitem
  • //return NULL if item does not exist
  • Node searchNode(Node head, int item)
  • NodePtr p head
  • NodePtr result NULL
  • bool foundfalse
  • while((p ! NULL) (!found))
  • if(p-gtdata item)
  • found true
  • result p
  • p p-gtnext
  • return result

24
Remember array searching algorithm
  • 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!
25
Variations of linked lists
  • Unsorted linked lists
  • Sorted linked lists
  • Circular linked lists
  • Doubly linked lists

26
Further considerations for the unsorted lists
  • Physical copy of list for operators like
    delection and addHead
  • delete should be understood as a decomposition
    into a sub-list

27
Node deleteHead(Node head) // physically
copy head into a new one, newhead // so to keep
the original list intact! Node newhead
if(newhead ! NULL) Node p newhead
newhead newhead-gtnext delete
p return newhead
28
More 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!
29
Add 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
30
Add 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

31
Implementation of a Sorted Linked List
32
Inserting 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

33
Finding 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

34
Its 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!
35
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.
36
  • //insert item into linked list according to
    ascending order
  • Node insertNode(Node 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
37
  • // not recommended void type function
  • 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

38
Deleting 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
39
Delete an element in a sorted linked list
  • Node deleteNode(Node 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
  • return head

Get the location
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
40
// in a void function, not recommended
  • 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
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
Write a Comment
User Comments (0)
About PowerShow.com