190 likes | 281 Views
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.
E N D
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. • Your phone book may also grow and shrink in size as time passes • We need a mechanism to store dynamic lists in the memory
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
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
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
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
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
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
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;
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
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
Pointers • Conceptually, my_key points to my_money 0XFF8C 200 0XFF8C my_key my_moneyy
Pointers • Now, there are two ways to access my_money • We can refer to it directly • We can refer to it through the pointer
Pointers • Think about other pointers My mailing address My Home
Pointers • Web links are also pointers UCLA Server Computer http://www.ucla.edu
Inserting into a Linked List Header NEXT Bob 242-7111 NEXT Fred 423-3158 New Entry
Inserting into a Linked List NEXT NEXT Bob 242-7111 Fred 423-3158 Header
Deleting from a Linked List NEXT NEXT NEXT Alice 242-7111 Bob 423-3178 Fred 423-3158 Header
Deleting from a Linked List NEXT Bob 423-3178 NEXT NEXT Alice 242-7111 Fred 423-3158 Header