Linked Lists Comp 1402/1002 Using Defined Types Data Structures - - PDF document

linked lists
SMART_READER_LITE
LIVE PREVIEW

Linked Lists Comp 1402/1002 Using Defined Types Data Structures - - PDF document

Linked Lists Comp 1402/1002 Using Defined Types Data Structures are key to Computer Science JAVA libraries: Vector, ArrayList, Hashtable C Arrays 1 Arrays Properties Elements are consecutive in memory


slide-1
SLIDE 1

1

Linked Lists

Comp 1402/1002

Using Defined Types

Data Structures are key to Computer Science

  • JAVA libraries:

– Vector, – ArrayList, – Hashtable…

  • C

– Arrays

slide-2
SLIDE 2

2

Arrays

  • Properties

– Elements are consecutive in memory – Easy to determine location in memory – Access a single element quickly – But CANNOT change size

  • declare new space
  • Copy all elements (at times)

– May not correspond to amount of data

Alternative

  • Properties

– Easily grows to an arbitrary size – Correspond to amount of data – Less rigid

  • Possible alternative are lists

– Singly linked list – Doubly linked list – Trees (binary trees, 2-3 trees, finger trees, B-trees…)

slide-3
SLIDE 3

3

Lists

  • Lists can be viewed as chains

– Each piece of information is a link – Links are independent of each other – Links are somehow connected to each other

  • Properties

– Elements are in arbitrary location – Must view all predecessors to find one – Grows to arbitrary size – Massive re-orderings possible quickly

Processing a List

Lists can be viewed as chains Begin at the beginning of the list (chain)

  • first element is the first link
  • second element is the second link
  • third element is the third link

Could be arbitrary length

slide-4
SLIDE 4

4

Singly Linked Lists Single Link per Element

slide-5
SLIDE 5

5

What is in a NODE ?

Each element (NODE) in a list contains:

  • Data
  • a pointer to another NODE

How will we define the NODE type ?

Global Declarations !

typedef int KEY_TYPE; /*applic dependent*/ typedef struct { KEY_TYPE key; … /* other data */ } DATA; typedef struct nodeTag { DATA data; struct nodeTag *link; /* Link is ptr to node */ } NODE;

slide-6
SLIDE 6

6

List Orderings

Three main types of lists:

Time dependent

  • 1. First in First out - FIFO (queue)
  • 2. Last in First out - LIFO (stack)

Key dependent

  • 3. Sorted list

Maintaining a List

  • Access the list

– Keep a pointer to the first NODE (for now)

NODE * pList; /* pList is a pointr to first node */

  • r

NODE *head; /* head is a pointer to first node */

  • All functions modifying a list

– Must have access to the list (Take pList as a parameter) – e.g.:

NODE *insertNode(NODE *pList...);

slide-7
SLIDE 7

7

Insert a Node

  • 1. Allocate memory
  • 2. Locate predecessor (A pointer , pPre)
  • 3. Point new node to its successor
  • 4. Point predecessor to new node

Meanings of pPre

slide-8
SLIDE 8

8

Insert into Empty List Insert Node at Beginning

slide-9
SLIDE 9

9

Insert Node in Middle Insert Node at End

slide-10
SLIDE 10

10

Deleting a Node

Still requires the predecessor’s location!

  • Remove the node
  • Free the memory space !

Delete First Node

slide-11
SLIDE 11

11

Delete General Case Searching Through a List

Search should return:

  • If a match exists

– A pointer to the node containing the key

  • If there is no match

– NULL – A pointer to the largest element that is smaller or equal to the key (for a sorted list)

slide-12
SLIDE 12

12

Traverse Linked Lists

printList, averageList, …

searchList

int searchList (NODE *pList, NODE **pPre, NODE **pCur, KEY_TYPE target) { . . . }

slide-13
SLIDE 13

13

Inserting a Node

NODE * insertNode(NODE *pList, NODE *pPre, DATA data) { . . . }

Deleting a Node

NODE * deleteNode(NODE *pList, NODE *pPre) { . . . }