Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


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 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 2

3 3

4 4 SWC Team

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

6 6 Overview Linked Lists Lists vs Arrays List Insertion List Deletion

7 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] grades:

8 8 Linked-List linked-list of grades: 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 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 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 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 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) Dolan, Edith (555) Lanfrank, John (555) Mening, Stephanie (555) Zemann, Harold (555)

13 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) t1 &t2

14 14 Linking The Data Using Addresses Acme, Sam (555) t1 Dolan, Edith (555) t2 Lanfrank, Jo (555) 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.

15 15 List Implementation #include struct TeleType { char name[30]; char phoneNum[15]; struct TeleType *nextaddr; }; int main() { struct TeleType t1 = {"Acme, Sam","(555) "}; /* an instance */ struct TeleType t2 = {"Dolan, Edith","(555) "}; struct TeleType t3 = {"Lanfrank, John","(555) "}; struct TeleType *first; /* create a pointer to a structure */ first = &t1; /* store t1's address in first */ t1.nextaddr = &t2; /* store t2's address in t1.nextaddr */ t2.nextaddr = &t3; /* store t3's address in t2.nextaddr */ t3.nextaddr = NULL; /* store the NULL address in t3.nextaddr */ printf("\n%s \n%s \n%s",first->name,t1.nextaddr->name,t2.nextaddr->name); return 0; }

16 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 17 List Item Insertion Acme, Sam (555) t1 Dolan, Edith (555) Lanfrank, Jo (555) NULL The next field must be changed to point to Hagar Hagar, June (555) This node must point to Lanfrank. B Smith: Stopped here on 11/1/04 B Smith: Stopped here on 11/1/04

18 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 19 List Item Deletion What might be the steps for removing a record from a linked list?

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

21 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


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

Similar presentations


Ads by Google