LinkedLists - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

LinkedLists

Description:

Linked lists allow for random insertion and deletion ... Stop for a Pause. For the few of you that all that made sense to, go ahead and use it. ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 33
Provided by: davidmcp
Category:

less

Transcript and Presenter's Notes

Title: LinkedLists


1
Linked-Lists!!
  • Good Golly!!

2
Linked Lists Basics
  • Arrays have so many drawbacks, I cant even list
    them all here, but here are some
  • Wasted space
  • Wasted time
  • Clunky for insertion or deletion in the middle
  • etc.

3
Linked-List Basics
  • Need a better solution
  • Linked-List
  • Idea
  • Information is stored in nodes
  • Each node points to the node directly following
    it
  • To find, insert or delete you simply manipulate
    nodes

4
Trade-offs
  • Linked lists allow for random insertion and
    deletion
  • Search, however, could take searching the entire
    list
  • If an array is ordered, you could search less
    than half and find what you are seeking
  • Linked data structures in general allow greater
    flexibility

5
Current
Head
Tail
Linked List Object
6
Specifics
  • A linked list contains a head node, a tail node
    and a current node.
  • These are used to keep track of the data
    structure
  • A linked-list class would make these private
    data.
  • A linked list should have several different types
    of insertion and deletion.

7
Insertion/Deletion
  • You can have an insert before the head node, an
    insert after the tail node, an insert before the
    current node and an insert after the current
    node.
  • You can think up many more, but should do at
    least insert before and after the current node.
  • You should do a remove current node.

8
Find
  • Find simply walks down the list of nodes,
    starting at the head node looking for a
    particular piece of data
  • When it finds it, it stops and the current node
    pointer should be pointing to that node

9
More Specifics
  • The head, tail and current node pointers are
    dynamically allocated pointers to a node class.
  • I heard the magic word, dynamic.
  • That means the linked list class needs to
    implement a copy constructor, assignment operator
    and a destructor in addition to any other methods.

10
Start of a Linked List class
  • class LinkedList
  • public
  • //stuff
  • private
  • Node Head
  • Node Tail
  • Node Current

11
Question
  • What is Node?
  • Node is a class
  • Node holds or points to the data.
  • Nodes also point to at least the next node in the
    chain.

12
Heres a nodelets take a look inside!!
Pointer to the next node in the chain
Space for the data or a pointer to the data
Usually called Next
13
Specifics
  • Nodes should have at least two private pieces of
    data
  • A place to store the data
  • A pointer to the next node in the chain
  • The should support various setting and getting
    methods of both the next pointer and the data

14
Node Class start
  • class Node
  • public
  • //stuff
  • private
  • Node Next
  • Record R

15
Head
Current
Record to be inserted
Tail
16
Head
Current
New Node
Tail
17
Head
Current
New Node
NULL
Tail
18
Head
Current
Record to be deleted
Tail
19
Head
Current
Record before Current
Tail
20
Head
Current
Record before Current
Tail
21
Issues
  • Insertion
  • Insert after current, after tail, and before head
    is more or less straight forward
  • You create a new node
  • Have the new node point to what you used to point
    to
  • Have the node you are sitting in point to the
    node
  • In the case of the insert before head, redirect
    the head pointer to point to the new head
  • In the case of insert after tail, redirect tail
    to point to new node
  • In any case of current, need to check to see if
    the current is the head or tail and take
    appropriate actions

22
More Insertion Issues
  • Insert before current raises a problem
  • How do you get to the node before you?
  • Solution, create a temporary node pointer and
    walk down from head and look ahead to see if the
    next node is the current node.
  • When you see the current node ahead of you, stop
    and insert after the node the temporary pointer
    is pointing to.
  • Again taking into account if the temporary node
    is the head or the tail

23
Deletion Issues
  • Same problem as insert before current
  • How do you find the node that is in front of you
    to be able to hook up the chain again?
  • Use same solution.
  • May want to write a private helper function that
    finds the node before the current node.
  • You can call it whenever you need.
  • It could return a pointer to you.

24
Alternative
  • Create a doubly linked-list.
  • Each node not only points to the next node in the
    chain, but also the node before it in the chain.
  • This simplifies the problem of how do you get the
    node in front of you.
  • It complicates insertion and deletion because now
    you have more pointers to keep track of.

25
Node Class
  • class Node
  • public
  • Node()
  • Node( Record R )
  • Node( const Node RHS)
  • conts Node operator( const Node RHS)
  • Node()

26
More Node
  • Node getNext()
  • void setNext( Node N )
  • Record getData()
  • void setData( Record R )

27
More Node
  • private
  • Node Next
  • Record recordPtr

28
Node Implementation
  • NodeNode()
  • Next 0
  • recordPtr 0
  • NodeNode( Record R )
  • Next 0
  • recordPtr R

29
Destructor
  • NodeNode()
  • delete recordPtr
  • delete Next

30
assignment Operator
  • const Node Nodeopetator( const Node RHS )
  • if ( this ! RHS )
  • delete Next
  • delete recordPtr
  • Next 0
  • recordPtr new (nothrow) Record(RHS.recordPtr)
  • return this

31
getNext/setNext
  • Node NodegetNext()
  • return temp
  • void NodesetNext( Node N )
  • Next N

32
getData/setData
  • Record NodegetData()
  • Record R recordPtr
  • return R
  • void NodesetData( Record R )
  • recordPtr R

33
Some more implemenation
  • class LinkedList
  • public
  • LinkedList()
  • LinkedList( const LinkedList )
  • LinkedList operator( const LinkedList )
  • LinkedList()
  • bool insertAfterCurrent( Record )
  • bool insertBeforeCurrent( Record)

34
More Implementation
  • void gotoHead()
  • void gotoTail()
  • bool find( Record ) const
  • bool deleteCurrent( )
  • void print( ostream ) const
  • void clear( )

35
More Implementation
  • private
  • Node findNodeBeforeCurr( )
  • Node Head
  • Node Tail
  • Node Current

36
Constructor
  • LinkedListLinkedList()
  • Head Tail Current 0

37
Destructor
  • LinkedListLinkedList()
  • Node temp Head
  • Current Head
  • while ( Current ! 0 )
  • Current Current-gtgetNext()
  • delete Temp
  • Temp Current
  • Head Tail Current 0

38
insertAfterCurrent
  • bool LinkedListinsertAfterCurrent( Record R )
  • bool success true
  • Node newNode new (nothrow) Node( R )
  • if ( !newNode )
  • success false
  • else if ( Head 0 )
  • Head Tail Current newNode
  • else
  • newNode-gtsetNext( Current-gtgetNext() )
  • Current-gtsetNext( newNode)
  • return success

39
deleteCurrent
  • bool LinkedListdeleteCurrent()
  • bool success true
  • if ( Current ! Head Current ! Tail )
  • Node prior findNodeBeforeCurrent()
  • prior-gtsetNext( Current-gtgetNext() )
  • delete Current
  • Current prior
  • return success

40
findNodeBeforeCurrent
  • Node LinkedListfindNodeBeforeCurrent()
  • Node temp Head
  • if ( Head Current )
  • temp 0
  • else
  • while ( Temp-gtgetNext() ! Current )
  • Temp Temp-gtgetNext()
  • return temp

41
copy constructor
  • LinkedListLinkedList( const LinkedList LL )
  • Head new Node( new Record( LL.Head-gtgetData()
    ) )
  • Current Tail Head 0
  • Node temp LL.Head
  • while ( temp ! 0 )
  • insertAfterCurrent( LL.Current-gtgetData() )
  • temp temp-gtgetNext()
  • //Is this all?

42
Stop for a Pause
  • For the few of you that all that made sense to,
    go ahead and use it. You will have a better data
    structure.
  • For the other 97, lets take a look at a
    slightly easier implementation

43
Alternative Node Class
  • class Node
  • public
  • Node()
  • Node( Record R )
  • bool setData( Record R)
  • bool getData( Record R)

44
Continuing
  • void setNext( Node N )
  • Node getNext()
  • private
  • Node Next
  • Record Data
  • //much easier

45
Constructors
  • NodeNode()
  • Next 0
  • Data Record()
  • NodeNode( Record R )
  • Next 0
  • Data R

46
Reporters
  • bool NodegetData(Record R)
  • R Data
  • return true
  • Node NodegetNext()
  • return Next

47
Mutators
  • bool NodesetData( Record R )
  • Data R
  • return true
  • void NodesetNext( Node N )
  • Next N

48
Alternative Linked List
  • class LL
  • public
  • LL()
  • LL(Record R)
  • LL(const LL RHS)
  • const LL operator(const LL RHS)

49
More
  • LL()
  • bool insertAfterCurrent(Record R)
  • bool insertBeforeCurrent(Record R)
  • bool deleteCurrent()
  • bool getData(Record R)

50
Still More
  • bool advance()
  • bool gotoHead()
  • bool gotoTail()
  • void print(ostream out)
  • bool clear()

51
A little More
  • private
  • Node findNodeBeforeCurrent()
  • Node Head
  • Node Tail
  • Node Current

52
  • bool LLinsertAfterCurrent( Record R )
  • bool success true
  • Node newNode new (nothrow) Node( R )
  • if ( !newNode )
  • success false
  • else if ( Head 0 )
  • Head Tail Current newNode
  • else
  • newNode-gtsetNext( Current-gtgetNext() )
  • Current-gtsetNext( newNode)
  • return success

53
Copy Constructor
  • LLLL( const LL RHS )
  • Head Tail Current 0
  • Node temp RHS.Head
  • while ( temp ! 0 )
  • insertAfterCurrent( temp-gtgetData() )
  • temp temp-gtgetNext()
  • //Is that all?

54
Review for Test 2
  • The test is during the next class meeting, in
    this classroom, during your normal class time!!
  • The test will cover chapter 7, specifically
    section 7.6 on new and delete
  • Chapter 8, specifically, copy constructors,
    assignment operators, and destructors
  • All other information covered in the notes that
    was not explicitly covered from the book

55
More Review
  • Examples of stuff from notes
  • argc and argv
  • Linked Lists and Nodes
  • at least the basic logic, but not code
  • stacks and queues
  • dynamic memory inside of a class
Write a Comment
User Comments (0)
About PowerShow.com