# 1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free.

## Presentation on theme: "1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free."— Presentation transcript:

1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: This required 2 classes. 1 st class covered linked- lists extempo, 2 nd class discussed malloc() and free(). For Fall05, use this for lecture-1, and develop a 2 nd lecture for covering malloc/free B Smith: This required 2 classes. 1 st class covered linked- lists extempo, 2 nd class discussed malloc() and free(). For Fall05, use this for lecture-1, and develop a 2 nd lecture for covering malloc/free

2

3

4 SWC Team

5 Administrivia Mindstorms Lab 10 (due Friday: 04/15/05) Ex 10.2, pbm 4 Ex 10.3, pbm 2

6 Overview Linked Lists Lists vs Arrays List Insertion List Deletion

7 Recall Arrays An array is a grouped list of two or more variables of the same type Array elements are contiguous. Subscripts can be any integer expression Each array element is a separate variable grades[0] grades[1] grades[2] grades[3] 88917186 grades: 88 91 71 86

8 Linked-List linked-list of grades: 88917186 88917186 array of grades: The data could be held in a struct containing an integer and a pointer The data here is held in an array of integers What is the data type of the pointer? The pointer is defined such that it points to another node. The node it points to is the same type of struct in which it resides.

9 Linked-List Definition Definition: A linked list is a set of items where each item is a part of a node that also contains a link to a node. It is a sequence of nodes, each containing data field(s) (the Item ) one (or more) links to the next node; a reference N EMO node link item

10 End of a Linked List The link in the final node can be represented a number of ways As a null link that points to no node A reference to a dummy node that contains no item A reference back to the first node, making the list a circular list. N EMO N EMO N EMO B Smith: 4/8/05 Fri: Stopped here and worked at the board to define the nodes. B Smith: 4/8/05 Fri: Stopped here and worked at the board to define the nodes.

11 Linked List Code struct node { int item; struct node* next; }; struct node { int item; struct node* next; }; struct node { int item; link next; }; typedef struct node* link; struct node { int item; link next; }; typedef struct node* link; B Smith: Stopped here on 11/2/05. Rate: 3. Used a physical paper link and chairs to simulate linked list. B Smith: Stopped here on 11/2/05. Rate: 3. Used a physical paper link and chairs to simulate linked list. Alternatively, the node could be represented as shown below

12 Telephone List What data structure would you use to organize the following list? What challenges do you see if we want to add Hagar, June to the list if organized as an array of structures? Acme, Sam (555) 898-2392 Dolan, Edith (555) 682-3104 Lanfrank, John (555) 718-4581 Mening, Stephanie (555) 382-7070 Zemann, Harold (555) 219-9912

13 Simple Linked List Example-Telephone List First member is the individuals name Second member is the telephone number Last member is the address of the next item struct TeleType { char name[30]; char phoneNum[15]; struct TeleType *nextaddr; };}; TeleType name[] phoneNum[] nextaddr* Acme, Sam (555)898-2392 t1 &t2

14 Linking The Data Using Addresses Acme, Sam (555)898-2392 t1 Dolan, Edith (555)682-3104 t2 Lanfrank, Jo (555)718-4581 t3 NULL B Smith: This example was used to show them how to walk through a list with a while loop. Elaborate in Sp05 on walking; more diagrams, pix. B Smith: This example was used to show them how to walk through a list with a while loop. Elaborate in Sp05 on walking; more diagrams, pix.

16 Record Insertion Linked lists are especially useful when a record needs to be inserted How would you grow a predefined array? To insert the record for June Hagar into the list, what steps must be taken?

17 List Item Insertion Acme, Sam (555)898-2392 t1 Dolan, Edith (555)682-3104 Lanfrank, Jo (555)718-4581 NULL The next field must be changed to point to Hagar Hagar, June (555)682-3104 This node must point to Lanfrank. B Smith: Stopped here on 11/1/04 B Smith: Stopped here on 11/1/04

18 NULL - End of List The NULL pointer is frequently used as a sentinel to mark the end-of-string NULL. both serve similar purposes

19 List Item Deletion What might be the steps for removing a record from a linked list?

20 Remove Dolan, Edith Acme, Sam (555)898-2392 t1 Dolan, Edith (555)682-3104 Lanfrank, Jo (555)718-4581 NULL The next field must point to Lanfrank t1.next = t1.next->next; t1.next = t2.next;

21 Summary Linked lists help us manage constantly changing lists of data help us with insertion and deletion of existing records is a basic data structure where each item contains information on how to get to the next item is a set of items where each item is part of a node that also contains a link to a node

Similar presentations