Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

2 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

3 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

4 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:

5 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 423-3158 David 473-4169 Alice 473-7792 Bob 423-1673 Carol 483-3943 linear List of Phone Numbers

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

7 Adding and Deleting in linear Lists 4 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

8 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

9 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

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

11 Pointers 4 Now assume that memory location 0XFF8C is reserved by the system for the variable my_money 4 Location 0XFF8C contains the value 200 4 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

12 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

13 Pointers 4 Conceptually, my_key points to my_money 0XFF8C200 0XFF8C my_keymy_money y

14 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

15 Pointers 4 Think about other pointers My mailing address My Home

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

17 Inserting into a Linked List Header Fred 423-3158 NE XT Bob 242-7111 New Entry NE XT

18 Inserting into a Linked List Header Fred 423-3158 NE XT Bob 242-7111 NE XT

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

20 Deleting from a Linked List Header Bob 423-3178 NE XT Alice 242-7111 NE XT Fred 423-3158 NE XT


Download ppt "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."

Similar presentations


Ads by Google