linked lists
play

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


  1. Linked Lists Comp 1402/1002 Using Defined Types Data Structures are key to Computer Science • JAVA libraries: – Vector, – ArrayList, – Hashtable… • C – Arrays 1

  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…) 2

  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 3

  4. Singly Linked Lists Single Link per Element 4

  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; 5

  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 */ or 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...); 6

  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 7

  8. Insert into Empty List Insert Node at Beginning 8

  9. Insert Node in Middle Insert Node at End 9

  10. Deleting a Node Still requires the predecessor’s location! • Remove the node • Free the memory space ! Delete First Node 10

  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) 11

  12. Traverse Linked Lists printList, averageList, … searchList int searchList (NODE *pList, NODE **pPre, NODE **pCur, KEY_TYPE target) { . . . } 12

  13. Inserting a Node NODE * insertNode(NODE *pList, NODE *pPre, DATA data) { . . . } Deleting a Node NODE * deleteNode(NODE *pList, NODE *pPre) { . . . } 13

Recommend


More recommend