Lists 4 Introduction to Lists 4 Linear Lists 4 Adding and Deleting in linear Lists 4 Linked Lists 4 Pointers in Linked Lists 4 Inserting into a Linked List 4 Deleting from a Linked List
Introduction to Lists 4 An organization’s membership list may grow and shrink in size. 4 Your phone book may also grow and shrink in size as time passes 4 We need a mechanism to store dynamic lists in the memory
Adapted for academic use from "Computer Science: An Overview" by J. Brookshear linear Lists 4 linear Lists are stored in consecutive memory locations as shown below:
Adding and Deleting in linear Lists 4 Let us consider a phone book. It can be implemented with an array containing names and phone numbers Fred David Alice Bob Carol linear List of Phone Numbers
Adding and Deleting in linear Lists 4 Deleting an entry is a two-step operation Fred David Alice Carol Deleting an entry from the linear List of Phone Numbers Fred David Alice Carol
Adding and Deleting in linear Lists 4 Adding a new entry can take place towards the end of the list Fred David Alice Carol Fred David Alice Carol Joe Adding an entry to the linear List of Phone Numbers
Linked Lists 4 If the linear list becomes large, deleting an entry in the middle of the list becomes very slow 4 It is because of the fact that we have to fill the gaps left after deleting en entry 4 If we wish to maintain the list as sorted, we have to sort it after each addition, causing additional processing overheads
Linked Lists 4 This problem can be solved if we implement the list as a linked list 4 Linked lists have entries connected with pointers 4 Deleting an entry can be implemented by re-arranging pointers 4 So we leave the entries where they are and just re-align the pointers
Pointers in Linked Lists 4 Pointers are used in C++ and other languages for pointing to other variables 4 A pointer is declared as a variables that can hold the address of another variable 4 When we declare a variable, a memory location is reserved for it by the system 4 For example 4 int my_money; 4 my_money=200;
Pointers 4 Now assume that memory location 0XFF8C is reserved by the system for the variable my_money 4 Location 0XFF8C contains the value Next, we declare a pointer variable my_key 4 int *my_key; 4 It means that my_key will hold the address of an integer variable
Pointers 4 Next, we initialize pointer my_key to point to the variable my_money 4 my_key = &my_money; 0XFF8C200 0XFF8C my_keymy_money y
Pointers 4 Conceptually, my_key points to my_money 0XFF8C200 0XFF8C my_keymy_money y
Pointers 4 Now, there are two ways to access my_money 4 We can refer to it directly 4 We can refer to it through the pointer
Pointers 4 Think about other pointers My mailing address My Home
Pointers 4 Web links are also pointers UCLA Server Computer
Inserting into a Linked List Header Fred NE XT Bob New Entry NE XT
Inserting into a Linked List Header Fred NE XT Bob NE XT
Deleting from a Linked List Header Bob NE XT Alice NE XT Fred NE XT
Deleting from a Linked List Header Bob NE XT Alice NE XT Fred NE XT