cs261 data structures
play

CS261 Data Structures Linked Lists - Introduction Dynamic Arrays - PowerPoint PPT Presentation

CS261 Data Structures Linked Lists - Introduction Dynamic Arrays Revisited Dynamic array can sometimes be slow When? Why? Linked Lists to the Rescue Dynamic Array Linked List in memory in memory da LL Alan Newell data head a


  1. CS261 Data Structures Linked Lists - Introduction

  2. Dynamic Arrays Revisited • Dynamic array can sometimes be slow – When? – Why?

  3. Linked Lists to the Rescue Dynamic Array Linked List in memory in memory da LL Alan Newell data head a a 1956 Size = 3 b Size = 3 c Capacity = 5 c b What can we now do…and not do …quickly?

  4. Linked Lists - Characteristics • Data elements held in structures called “ links ” • Like a chain: each link is tied to the next link link … data data next next link link • Links are 1 – 1 with elements, allocated and released as necessary

  5. Typical Link Structure (Singly Linked) struct Link { /* Single link. */ TYPE val; /* Data contained by this link. */ struct Link *next; /* Pointer to next link. */ }; next val

  6. Linked List Variations All linked lists consists of links … but there are other design decisions: – Header (special value to point to start) or no header? – Use null as terminator, or special value (sentinel) for end? – Use single or double links? – Pointer to first element only, or pointer to first and last? backSent List frontSent prev prev prev prev prev … Link Link next next next next next

  7. Linked List Stack Implementing a stack interface with a linked list: – Header with head reference only: null if empty – Null terminated – Singly linked – Where should the ‘top’ of the stack be???? • Answer: First element is easy to access List Stack firstLink val: 2 val: 7 val: 4 next: next: next: null

  8. Linked List Stack struct linkedListStack { struct Link *firstLink; /* Initialize routine sets to zero/ NULL . */ }; void linkedListStackInit (structlinkedListStack s) { s->firstLink = 0; } List Stack firstLink

  9. Linked List Stack void pushListStack(struct ListStack *s, TYPE d) { /* You are going to write this: 1. Allocate (malloc) a new link (check that it works!). */ } val:? firstLink next:? List Stack val: 2 val: 4 next: next: null

  10. Linked List Stack void pushListStack(struct ListStack *s, TYPE d) { /* You are going to write this: 1. Allocate (malloc) a new link (check that it works!). 2. Set data fields in the new link. 3. Change head to point to new link. */ } val:20 firstLink next: List Stack val: 2 val: 4 next: next: null

  11. Linked List Stack void pushListStack(struct ListStack *s, TYPE d) { /* You are going to write this: 1. Allocate (malloc) a new link (check that it works!). 2. Set data fields in the new link. 3. Change head to point to new link. */ } List Stack firstLink val:20 val: 2 val: 4 next: next: next: null

  12. Linked List Tips… • Draw the diagram! • Go through the steps visually, labeling each step • Convert each step to C code • Try the boundary cases: – Empty list? – List with several items?

  13. Other Linked List Operations • How do you tell if stack is empty? • How do you return first element (i.e., firstLink )? • How do you remove an element?

  14. Your Turn • Complete Worksheet 17: Linked List Introduction, List Stack

  15. Linked List Stack void popListStack(struct ListStack *s, TYPE d) { struct Link *first; assert(s->firstLink); first = s->firstLink; s->firstLink = first->next; free(first); struct ListStack { } struct Link *firstLink; } struct Link { TYPE val; struct Link *next; List Stack }; firstLink val:20 val: 2 val: 4 next: next: next: null

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend