1 CSE1301 Computer Programming: Lecture 30 Linked Lists (Part 2)

Slides:



Advertisements
Similar presentations
Linked List Alternate approach to maintaining an array of elements Rather than allocating one large group of elements, allocate elements as needed Q: how.
Advertisements

Sorted Lists CS Data Structures Sections 4.1, 4.2 & 4.3.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
1 CSE1301 Computer Programming: Lecture 27 List Manipulation.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
CSCI2100B Linked List Jeffrey
Data Structure Lecture-5
1. List Static List: no adding or deleting Dynamic List: can add or delete items from the list Both static and dynamic lists: linear search, update item.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review Learn about linked lists
DATA STRUCTURE & ALGORITHMS
More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture.
1 CSE1301 Computer Programming: Lecture 28 List Sorting.
1 CSE1301 Computer Programming Lecture 32: List Sorting.
CSE1301 Computer Programming: Lecture 33 Linked Lists.
CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.
hashing1 Hashing It’s not just for breakfast anymore!
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Chapter 3: Arrays, Linked Lists, and Recursion
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A5 – Basic Data Structures – Continued (Lists)
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
HASHING Section 12.7 (P ). HASHING - have already seen binary and linear search and discussed when they might be useful (based on complexity)
Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 03.
Search algorithms for vectors Jordi Cortadella Department of Computer Science.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Chapter 12 B+ Trees CS 157B Spring 2003 By: Miriam Sy.
Hashing Hashing is another method for sorting and searching data.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
“Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Linked List by Chapter 5 Linked List by
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
CS2006- Data Structures I Chapter 5 Linked Lists III.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Searching Topics Sequential Search Binary Search.
Data Structures Arrays and Lists Part 2 More List Operations.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
114 3/30/98 CSE 143 Collection ADTs [Chapter 4] /30/98 Collection ADTs  Many standard ADTs are for collections  Data structures that manage groups.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
Lecture 6 of Computer Science II
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists (cont) [Chapter 4; Chapter 6, pp ]
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Linked Lists Chapter 6 Section 6.4 – 6.6
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Programming Abstractions
Stack and Queue APURBO DATTA.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
LinkedIntList(int n) Write a constructor for LinkedIntList that accepts an int n parameter and makes a list of the number from 0 to n new LinkedIntList(3)
Last Class We Covered Data representation Binary numbers ASCII values
Programming Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
LinkedIntList(int n) Write a constructor for LinkedIntList that accepts an int n parameter and makes a list of the number from 0 to n new LinkedIntList(3)
FIFO Queues CSE 2320 – Algorithms and Data Structures Alexandra Stefan
Linked List Intro CSCE 121.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

1 CSE1301 Computer Programming: Lecture 30 Linked Lists (Part 2)

2 Topics Sorted linked list The “free” list Adding to a linked list Deleting from a linked list

3 struct ListElementRec { int item; int next; }; typedef struct ListElementRec ListElement; ListElement list[maxListLength]; int first; Recall: Linked List END first 1

4 Delete an Item: Typical Case first END Example: Delete item 8

first END Delete an Item: Typical Case (cont) Example: Delete item 8

first END Delete an Item: Typical Case (cont) Example: Delete item 8

first END Delete an Item: Typical Case (cont) Example: Delete item 8

8 Delete the Last Item first END Example: Delete item 9

first END 9 Delete the Last Item (cont) Delete last item: Delete item 9 Example: Delete item 9

first END Delete the Last Item (cont) Example: Delete item 9

first END Delete the First Item Example: Delete item 2

first END Delete the First Item (cont) Example: Delete item 2

first 5 34 END Delete the First Item (cont) Example: Delete item 2

14 Filling In the Gaps Problem: How do we find a free slot if we want to insert a new item into the list?

15 Filling In the Gaps: Solution 1 Mark free slots with special value, and perform linear search first 1 53 END FREE const int END = -1; const int FREE = -2;

16 Filling In the Gaps: Solution 1 (cont) Mark free slots with special value, and perform linear search first 1 53 END FREE Example: Insert 5 into the linked list

17 Filling In the Gaps: Solution 1 (cont) Mark free slots with special value, and perform linear search Example: Insert 5 into the linked list first 1 53 END FREE 5

18 Filling In the Gaps: Solution 1 (cont) Mark free slots with special value, and perform linear search Example: Insert 5 into the linked list first FREE 5

19 Filling In the Gaps: Solution 1 (cont) Mark free slots with special value, and perform linear search Example: Insert 5 into the linked list first FREE END 5

20 Filling In the Gaps: Solution 1 (cont) Mark free slots with special value, and perform linear search Example: Insert 5 into the linked list first FREE END 5 Problem: Linear search for free slots

21 Filling In the Gaps: Solution 2 Close the gaps after a deletion, and always append at the end Example: Delete 4 from the linked list END 5 first 0 count 5

22 Filling In the Gaps: Solution 2 (cont) Close the gaps after a deletion, and always append at the end END 5 first 0 Example: Delete 4 from the linked list 5 count

23 Filling In the Gaps: Solution 2 (cont) Close the gaps after a deletion, and always append at the end END 5 first 0 Example: Delete 4 from the linked list 5 count

24 Filling In the Gaps: Solution 2 (cont) Close the gaps after a deletion, and always append at the end END 5 first 0 Example: Delete 4 from the linked list 5 count

25 Filling In the Gaps: Solution 2 (cont) Close the gaps after a deletion, and always append at the end first END 5 5 Example: Delete 4 from the linked list count

26 Filling In the Gaps: Solution 2 (cont) Close the gaps after a deletion, and always append at the end first END 5 5 Example: Delete 4 from the linked list count

27 Filling In the Gaps: Solution 2 (cont) Close the gaps after a deletion, and always append at the end first END 5 Example: Delete 4 from the linked list 4 count

28 Filling In the Gaps: Solution 2 (cont) Close the gaps after a deletion, and always append at the end first END 5 4 Example: Delete 4 from the linked list count Problem: Moving items and adjusting links

29 Filling In the Gaps: Solution 3 Link the free slots together to form a “free linked list” first 1 53 END 04 free 2

30 Filling In the Gaps: Solution 3 Link the free slots together to form a “free linked list” first 1 53 END 04 free 2 To linked list of items

31 Filling In the Gaps: Solution 3 (cont) Link the free slots together to form a “free linked list” first 1 53 END 04 free 2 To linked list of free elements

32 Initialization Begin with empty item list and a full free list first END free 0

33 Adding an Item -- Example 1 Add item with value = 5 (first item) Step 1: Ensure free != END first END free 0

34 Adding an Item -- Example 1 (cont) first END free 0 5 Step 2: Put value = 5 in the first free slot list[free].item = value;

35 Adding an Item -- Example 1 (cont) first 0 254END31 free 0 5 Step 3: Remember position of new item first=free;

36 Adding an Item -- Example 1 (cont) first 0 254END31 free 1 5 Step 4: Update the free linked list to next free slot free = list[free].next;

37 Adding an Item -- Example 1 (cont) first 0 254END3 free 1 5 Step 5: Put END marker in new element list[first].next = END;

38 Adding an Item -- Example first 0 254END3 free 1 5 Add item with value = 9 Step 1: Ensure free != END

39 Adding an Item -- Example 2 (cont) first 0 254END3 free 1 5 Step 2: Put value = 9 in the first free slot list[free].item = value; 9

40 Adding an Item -- Example 2 (cont) first 0 254END3 free 1 5 Step 3: Remember the position of the new item curr = free; 9 1 curr

41 Adding an Item -- Example 2 (cont) first 0 254END3 free 2 5 Step 4: Update the free linked list to next free slot free = list[free].next; 9 1 curr

42 Adding an Item -- Example 2 (cont) first 0 254END3 free 2 5 Step 5: Find the position of the preceding item prev = findPrevious(first,value,list); 9 1 curr prev 0

43 Adding an Item -- Example 2 (cont) first 0 END 54 3 free 2 5 Step 6: Link current item to the next item in the list list[curr].next = list[prev].next; 9 1 curr prev 0

44 Adding an Item -- Example 2 (cont) first 0 END54 31 free 2 5 Step 7: Insert current item after the previous item list[prev].next = curr; 9 1 curr prev 0

45 Adding an Item -- Code const int END = -1; int addItem (int first, int free, int value, ListElement *list) { int curr, prev; if (free != END) { list[free].item = value; curr = free; free = list[free].next; prev = findPrevious(first, value, list); /* assumption: the list is never empty */ list[curr].next = list[prev].next; list[prev].next = curr; return free; } else { printf(“Not enough space\n”); exit(1); }

46 Exercise 1 Given the list below, follow the algorithm for adding the following items in order: –Add 6: In the “middle” of list –Add 3: Becomes new first item in list Add an item to an empty list first 0 54END free 2 END

47 Deleting an Item -- Example Delete item with value = first 1 53 END 04 free 2

48 Deleting an Item -- Example (cont) Step 1: Find position of the item to be deleted curr = findIndex(first,value,list); first 1 53 END 04 free 2 curr 5

49 Deleting an Item -- Example (cont) Step 2: Ensure the item is in the list curr != END first 1 53 END 04 free 2 curr 5

50 Deleting an Item -- Example (cont) Step 3: Find the position of the previous item prev = findPrevious(first,value,list); first 1 53 END 04 free 2 curr 5 prev 1

51 Deleting an Item -- Example (cont) Step 4: Bypass the current item in the list list[prev].next = list[curr].next; first END 04 free 2 curr 5 prev 1

52 Deleting an Item -- Example (cont) Step 5: Link current position to first free slot list[curr].next = free; first END 04 free 2 curr 5 prev 1

53 Deleting an Item -- Example (cont) Step 6: Make current position the first free slot free = curr; first END 04 free 5 curr 5 prev 1

54 Deleting an Item -- Code const int END = -1; int deleteItem (int first, int free, int value, ListElement *list) { int curr, prev; curr = findIndex(first, value, list); /* assumption: the list is never empty */ if (curr == END) { printf("%d is NOT in the list\n", value); return free; } prev = findPrevious(first, value, list); /* assumption: the last item is never deleted */ list[prev].next = list[curr].next; list[curr].next = free; free = curr; return free; }

55 Exercise 2 Consider the following cases when deleting: –the first item in the linked list –the last item in the linked list –an item that is not in the linked list Modify the deleteItem function to allow – for the deletion of the first item – for the list to become empty Write the algorithm and the C code for –findIndex() : finding the index of the item with a given value –findPrev() : finding the index of the item previous to a given value

56 Main points Linked list –has an item list and a free list –makes it easier to add and delete elements –searching for an element is slow (linear)

57 Reading Knuth, Fundamental Algorithms, Section –pages (1st edition) –pages (3rd edition) Horowitz and Sahni, Fundamentals of Data Structures, Chapter 4, pages