Linked%20Lists - PowerPoint PPT Presentation

About This Presentation
Title:

Linked%20Lists

Description:

Data may be stored consecutively in a linked list. Each element of ... (b) Fill in the data ... Uses 'Deep Copy' in the copy-constructor to copy the entire ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 30
Provided by: andrew184
Category:
Tags: 20lists | fillin | linked

less

Transcript and Presenter's Notes

Title: Linked%20Lists


1
  • Programming
  • Linked Lists

2
Linked Lists Basic Idea
  • Data may be stored consecutively in a linked
    list.
  • Each element of the linked list
  • Stores one piece of data
  • Points to the next element or NULL.
  • Linked lists provide natural implementation.
  • A linked list of integers

20
45
75
85
3
Linked Lists Operations
  • Original linked list of integers
  • Insertion by pointers
  • Deletion by pointers

20
45
75
85
old value
20
45
75
85
60
20
45
75
85
deleted item
4
Pointer Based Linked Lists
  • Linked lists store data in increasing order
  • Definitions
  • 1. Defining a node (recursive datatype - use
    itself to define itself!)
  • class node
  • public
  • int data
  • node next
  • // end class
  • typedef node ptrType // ptrType is a pointer to
    a node

20
45
75
85
5
typedef
  • typedef allows you to make new shortcuts to
    existing types
  • typedef int FunnyInt
  • FunnyInt k // same as int k
  • typedef int IntPtr
  • IntPtr p// same as int p
  • p new node
  • p 100

p
100
6
Pointer Based Linked Lists
  • Definitions (contd)
  • 2. To create/delete node
  • P new node
  • delete P
  • 3. The fields in P can be accessed using
  • P-gtdata 100 and P-gtnext NULL
  • Alternatively, we could also write
  • (P).data 100 and (P).next NULL

p
100
7
Accessing Linked Lists
  • We define a pointer
  • ptrType Head
  • that points to the first node of the linked
    list. When the linked list is empty (empty list)
    then Head is NULL.

Head
20
45
75
85
Head
8
Traversing a Linked List
  • A linked list is displayed by visiting its nodes
    one by one, and displaying their data fields.
  • void DisplayList(ptrType Head)
  • ptrType current Head
  • while(current ! NULL)
  • cout ltlt current-gtdata ltlt endl
  • current current-gtnext

9
Traversing a Linked List
  • Alternatively, we can use for loop to traverse a
    linked list
  • void DisplayList(ptrType Head)
  • ptrType cur
  • for (cur Head cur ! NULL cur
    cur-gtnext)
  • cout ltlt cur-gtdata ltlt endl

10
Displaying a Linked List
  • current Head
  • current current-gtnext

Head
20
45
current
Head
20
45
current
11
Deleting a Node
  • To delete a node from the list
  • 1. Locate the node to be deleted
  • (a) cur points to the node to be deleted.
  • (b) prev points to curs 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
12
Deleting the Head Node
  • When the node to be deleted is the first node,
    there is no predecessor node.
  • Use
  • Head Head-gtnext
  • instead of
  • prev-gtnext cur-gtnext

(to delete)
Head
...
20
45
75
85
cur
13
Inserting a Node
  • To insert a new node into the list
  • 1. (a) Create a new node using
  • 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 (the node being
    pointed at by prev) and cur.
  • 3. Connect the new node to the list by using
  • (a) newPtr-gtnext cur
  • (b) prev-gtnext newPtr

14
Inserting a Node
Head
...
20
45
75
15
Inserting a Node at the Beginning
  • When the new node is to be inserted at the
    beginning of the list there is no predecessor
    node. Use
  • newPtr-gtnext Head Head newPtr
  • instead of
  • newPtr-gtnext cur prev-gtnext newPtr

...
20
45
75
Head
cur
13
newPtr
16
Finding prev and cur
  • In previous slides we assumed that prev and cur
    were known. How did we find them?
  • Suppose that we want to insert or delete a node
    with data value newValue. Then the following code
    successfully finds prev and cur.
  • prev NULL
  • cur Head
  • while(cur!NULL newValue gt cur-gtdata)
  • prev cur
  • cur cur-gtnext

17
Finding prev and cur
  • Insertion Example with newValue60

Head
...
20
45
75
prev NULL cur Head while(cur!NULL
newValue gt cur-gtdata) prev cur cur
cur-gtnext
18
Passing a Linked List to a Function
  • 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.

Head
20
45
75
85
19
Pointer Based Linked Lists
  • We now describe how to implement a Linked-List
    ADT (Abstract Data Type) using pointers.
  • Main Points
  • Contains a Size field and a Head pointing to
    the list.
  • Implemented as a Class.
  • Uses Deep Copy in the copy-constructor to copy
    the entire data structure.
  • Destructor deallocates dynamic memory of object.

Head
Size
4
20
45
75
85
20
  • include ltiostreamgt
  • using namespace std
  • class listNode // a node on the list
  • public
  • int data // a data item on the list
  • listNode next // pointer to next node
  • typedef listNode ptrType // pointer to node

21
  • class listClass
  • public
  • // constructors and destructor
  • listClass()
  • listClass(const listClass L)
  • listClass()
  • // list operations
  • bool empty()
  • int length()
  • void add(int newdata)
  • void del(int deldata)
  • void print()
  • private
  • int Size
  • ptrType Head
  • ptrType new_and_check()

22
ptrType listClassnew_and_check() // allocate
a new node returns only upon successful //
memory allocation else, exit ptrType
temp temp new listNode if (temp NULL)
// not successful cerr ltlt "Memory allocation
error!" ltlt endl exit(1) return temp
23
  • listClasslistClass()
  • Size 0
  • Head NULL
  • listClasslistClass()
  • while(Head!NULL)
  • ptrType cur Head
  • Head Head-gtnext
  • delete cur
  • bool listClassempty()
  • if(Size0)
  • return true
  • else
  • return false

24
  • listClasslistClass(const listClass L)
  • Size L.Size
  • if(L.HeadNULL)
  • Head NULL // empty list
  • return
  • Head new_and_check () // copy first node
  • Head-gtdata L.Head-gtdata
  • ptrType cur Head // new list pointer
  • ptrType orig L.Head-gtnext
  • while(orig ! NULL)
  • cur-gtnext new_and_check()
  • cur cur-gtnext
  • cur-gtdata orig-gtdata
  • orig orig-gtnext
  • cur-gtnext NULL

25
  • void listClassadd(int newdata)
  • Size
  • ptrType newPtr new listNode
  • if(newPtrNULL)
  • cerr ltlt "Memory error" ltlt endl
  • exit(1)
  • newPtr-gtdata newdata
  • newPtr-gtnext NULL
  • ptrType prev, cur Head
  • while(cur!NULL newdata gt cur-gtdata)
  • prev cur
  • cur cur-gtnext
  • if(curHead)
  • newPtr-gtnext Head
  • Head newPtr
  • else

26
  • void listClassdel(int deldata)
  • ptrType prev, cur Head
  • while(cur!NULL deldata gt cur-gtdata)
  • prev cur
  • cur cur-gtnext
  • if(curNULL cur-gtdata!deldata)
  • cout ltlt "Delete error " ltlt deldata
  • ltlt " not in list!" ltlt endl
  • return
  • if(curHead)
  • Head Head-gtnext
  • else
  • prev-gtnext cur-gtnext
  • delete cur
  • Size--

27
  • void listClassprint()
  • cout ltlt " "
  • ptrType cur Head
  • while(cur ! NULL)
  • cout ltlt cur-gtdata ltlt " "
  • cur cur-gtnext
  • cout ltlt "" ltlt endl

28
  • void main()
  • listClass L
  • L.add(30)
  • L.print()
  • listClass N(L)
  • N.print()
  • L.add(20)
  • L.print()
  • L.add(40)
  • L.print()
  • L.add(10)
  • L.print()
  • L.del(50)
  • L.print()
  • L.del(40)
  • L.print()
  • L.del(30)
  • L.print()
  • L.del(20)

29
Output
  • 30
  • 30
  • 20 30
  • 20 30 40
  • 10 20 30 40
  • Delete error 50 not in list!
  • 10 20 30 40
  • 10 20 30
  • 10 20
  • 10
Write a Comment
User Comments (0)
About PowerShow.com