1 / 19

Lists

Lists. Introduction to Lists Linear Lists Adding and Deleting in linear Lists Linked Lists Pointers in Linked Lists Inserting into a Linked List Deleting from a Linked List. Introduction to Lists. An organization’s membership list may grow and shrink in size.

Download Presentation

Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lists • Introduction to Lists • Linear Lists • Adding and Deleting in linear Lists • Linked Lists • Pointers in Linked Lists • Inserting into a Linked List • Deleting from a Linked List

  2. Introduction to Lists • An organization’s membership list may grow and shrink in size. • Your phone book may also grow and shrink in size as time passes • We need a mechanism to store dynamic lists in the memory

  3. linear Lists • linear Lists are stored in consecutive memory locations as shown below: Adapted for academic use from "Computer Science: An Overview" by J. Brookshear

  4. Adding and Deleting in linear Lists • Let us consider a phone book. It can be implemented with an array containing names and phone numbers Fred 423-3158 David 473-4169 Alice 473-7792 Bob 423-1673 Carol 483-3943 linear List of Phone Numbers

  5. Adding and Deleting in linear Lists • Deleting an entry is a two-step operation Fred 423-3158 David 473-4169 Alice 473-7792 Carol 483-3943 Fred 423-3158 David 473-4169 Alice 473-7792 Carol 483-3943 Deleting an entry from the linear List of Phone Numbers

  6. Adding and Deleting in linear Lists • Adding a new entry can take place towards the end of the list Fred 423-3158 David 473-4169 Alice 473-7792 Carol 483-3943 Fred 423-3158 David 473-4169 Alice 473-7792 Carol 483-3943 Joe 423-7225 Adding an entry to the linear List of Phone Numbers

  7. Linked Lists • If the linear list becomes large, deleting an entry in the middle of the list becomes very slow • It is because of the fact that we have to fill the gaps left after deleting en entry • If we wish to maintain the list as sorted, we have to sort it after each addition, causing additional processing overheads

  8. Linked Lists • This problem can be solved if we implement the list as a linked list • Linked lists have entries connected with pointers • Deleting an entry can be implemented by re-arranging pointers • So we leave the entries where they are and just re-align the pointers

  9. Pointers in Linked Lists • Pointers are used in C++ and other languages for pointing to other variables • A pointer is declared as a variables that can hold the address of another variable • When we declare a variable, a memory location is reserved for it by the system • For example • int my_money; • my_money=200;

  10. Pointers • Now assume that memory location 0XFF8C is reserved by the system for the variable my_money • Location 0XFF8C contains the value 200 • Next, we declare a pointer variable my_key • int *my_key; • It means that my_key will hold the address of an integer variable

  11. Pointers • Next, we initialize pointer my_key to point to the variable my_money • my_key = &my_money; 0XFF8C 200 0XFF8C my_key my_moneyy

  12. Pointers • Conceptually, my_key points to my_money 0XFF8C 200 0XFF8C my_key my_moneyy

  13. Pointers • Now, there are two ways to access my_money • We can refer to it directly • We can refer to it through the pointer

  14. Pointers • Think about other pointers My mailing address My Home

  15. Pointers • Web links are also pointers UCLA Server Computer http://www.ucla.edu

  16. Inserting into a Linked List Header NEXT Bob 242-7111 NEXT Fred 423-3158 New Entry

  17. Inserting into a Linked List NEXT NEXT Bob 242-7111 Fred 423-3158 Header

  18. Deleting from a Linked List NEXT NEXT NEXT Alice 242-7111 Bob 423-3178 Fred 423-3158 Header

  19. Deleting from a Linked List NEXT Bob 423-3178 NEXT NEXT Alice 242-7111 Fred 423-3158 Header

More Related