1 CSE1301 Computer Programming: Lecture 27 List Manipulation.

Slides:



Advertisements
Similar presentations
2. Getting Started Heejin Park College of Information and Communications Hanyang University.
Advertisements

Using Matrices in Real Life
Advanced Piloting Cruise Plot.
1 Vorlesung Informatik 2 Algorithmen und Datenstrukturen (Parallel Algorithms) Robin Pomplun.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Chapter 1 The Study of Body Function Image PowerPoint
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
Chapter 3: Linked List Tutor: Angie Hui
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
Exit a Customer Chapter 8. Exit a Customer 8-2 Objectives Perform exit summary process consisting of the following steps: Review service records Close.
Custom Services and Training Provider Details Chapter 4.
My Alphabet Book abcdefghijklm nopqrstuvwxyz.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Year 6 mental test 10 second questions
Solve Multi-step Equations
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
Chapter 7: Arrays In this chapter, you will learn about
Turing Machines.
Chapter 10: Applications of Arrays and the class vector
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
Chapter 24 Lists, Stacks, and Queues
1 Arrays … The Sequel Applications and Extensions Chapter 10.
Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
ADTs unsorted List and Sorted List
Python Mini-Course University of Oklahoma Department of Psychology
Data Structures Using C++
LIST PROCESSING.
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1 Joe Meehean. Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 … Item N.
ABC Technology Project
Hash Tables.
1 Symbol Tables Chapter Sedgewick. 2 Symbol Tables Searching Searching is a fundamental element of many computational tasks looking up a name.
1 Undirected Breadth First Search F A BCG DE H 2 F A BCG DE H Queue: A get Undiscovered Fringe Finished Active 0 distance from A visit(A)
VOORBLAD.
15. Oktober Oktober Oktober 2012.
Review Pseudo Code Basic elements of Pseudo code
1 Breadth First Search s s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
The List ADT Textbook Sections
BIOLOGY AUGUST 2013 OPENING ASSIGNMENTS. AUGUST 7, 2013  Question goes here!
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
© 2012 National Heart Foundation of Australia. Slide 2.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
1 of 31 Images from Africa. 2 of 31 My little Haitian friend Antoine (1985)
Addition 1’s to 20.
25 seconds left…...
Slippery Slope
H to shape fully developed personality to shape fully developed personality for successful application in life for successful.
Januar MDMDFSSMDMDFSSS
Week 1.
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Intracellular Compartments and Transport
PSSA Preparation.
Essential Cell Biology
Foundations of Data Structures Practical Session #7 AVL Trees 2.
Immunobiology: The Immune System in Health & Disease Sixth Edition
Presentation transcript:

1 CSE1301 Computer Programming: Lecture 27 List Manipulation

2 Recall List –an array of the same type of elements Linear Search –Advantage: list can be unsorted –Disadvantage: O(N) Binary Search –Advantage: O(log N) –Disadvantage: list must be sorted

3 Topics How can we maintain a list? –unsorted –sorted Operations on a list: –initialize the list –add an element –delete an element –find an element –print the list Add, Find and Delete: sorted or unsorted ?

4 List: Example #define MAX_COUNT 6 /* Max number of items */ #define MAX_LEN20 /* Max length of a name */ struct nameListRec { int count; /*Number of items currently in the list*/ char names[MAX_COUNT][MAX_LEN]; }; typedef struct nameListRec nameList; nameList students; students.names ??? students.count: ???

5 List: Initialize nameList newList ( ) { nameList students; students.count = 0; return students; } ??? students.count: 0 students.names

6 Unsorted List: Add strcpy(students.names [ ???? ], Dave); students.count students.count: 0 ??? students.names ??? students.count++; Dave

7 ???Cary Unsorted List: Add (cont) students.count++; strcpy(students.names [ ???? ], Cary); students.count next available position strcpy(students.names [ ???? ], Dave); students.count Dave??? students.count: 1 students.names

8 Unsorted List: Add (cont) DaveCary??? students.count: 2 students.names strcpy(students.names [ ???? ], Dave); students.count students.count++; strcpy(students.names [ ???? ], Cary); students.count students.count++;

9 Unsorted List: Add (cont) nameList addUnsorted ( nameList students, char *newname ) { strcpy(students.names[student.count], newname); students.count++; return students; }... nameList cse1301;... cse1301 = newList(); cse1301 = addUnsorted(cse1301, Dave); cse1301 = addUnsorted(cse1301, Cary);...

10 Dave Sorted List: Add Case 1: List is empty –same as unsorted ??? students.count: 1 students.names strcpy(students.names [ students.count ], Dave); students.count++; Example: add Dave

11 Sorted List: Add (cont) Case 2: List is not empty –find correct position Dave??? students.count: 1 students.names Example: add Abe

12 Sorted List: Add (cont) Case 2: List is not empty –find correct position –make room by moving all items to the right Dave??? students.count: 1 students.names Example: add Abe

13 Sorted List: Add (cont) Case 2: List is not empty –find correct position –make room by moving all items to the right –put item in that position AbeDave??? students.count: 1 students.names Example: add Abe

14 Sorted List: Add (cont) Case 2: List is not empty –find correct position –make room by moving all to the right –put item in that position –update count AbeDave??? students.count: 2 students.names Example: add Abe

15 Sorted List: Add (cont) Case 2: List is not empty AbeCaryDaveEd??? students.count: 4 students.names Example: add Arnie

16 Sorted List: Add (cont) Case 2: List is not empty AbeCaryDaveEd??? students.count: 4 students.names Example: add Arnie

17 Sorted List: Add (cont) Case 2: List is not empty AbeArnieCaryDaveEd??? students.count: 5 students.names alphabetical ordering is maintained Example: add Arnie

18 Sorted List: Add (cont) Case 2: List is not empty AbeArnieCaryDaveEd??? students.count: 5 students.names What if the array is already full?

19 Algorithm addElement count addElement(array,last,val) { if list is full { print message and exit } else if list is empty { add val in first position of array increment last } else { while ( end of array not reached ) { if ( val < current element ) { shuffle to the right the array elements starting at current element break loop } insert value in the empty spot of array update last } return position of added element }

20 Program addElement int addElement( int *arr, int last, int val, int size ) { int count, i; if (last == size-1) {/* list is full */ printf(List is full\n); return -1; } else if (last == -1) {/* list is empty */ arr[0] = val; last++; return 0; } else { for(count=0; count<=last; count++) {/* add before first larger value */ if ( val < arr[count] ) {/* shuffle along starting at the end */ for (i=last; i >= count; i--) { arr[i+1]=arr[i]; } break; } arr[count] = val; last++; return count; }

21 addElement -- Exercise Modify the C program so that it works on the struct nameList –Version 1: parameters: new item, count and array of strings return value: whether the list is full –Version 2: parameters: new item and a nameList structure return value: updated struct use a function called isListFull to determine if the list is full

22 List: Add an Element Unsorted list –append after the last element Sorted list –find correct position –make room for the new element Special cases: –list is empty –list is full –item to be inserted is already in the list

23 List: Print (Version 1) void printList ( int count, char nomen[][MAX_LEN] ) { inti; for (i=0; i < count; i++) { printf(%d %s\n, i, nomen[i]); } printList ( students.count, students.names ); Array of strings

24 List: Print (Version 2) void printList ( nameList class ) { inti; for (i=0; i < class.count; i++) { printf(%d %s\n, i, class.names[i] ); } printList ( students );

25 List: Find an Element Sorted –Use binary search Unsorted –Use linear search Used for deleting an element

26 List: Delete an Element Same for both sorted and unsorted lists Algorithm: –find the position of the item to be deleted linear or binary search –if found move all items one position to the left decrement count Special cases: –list is empty –item not found

27 List: Delete an Element AbeArnieCaryDaveEd??? students.count: 5 students.names Example: delete Cary - find the position of the element

28 List: Delete an Element AbeArnieDaveEd??? students.count: 5 students.names Example: delete Cary - find the position of the element - remove the element

29 Dave List: Delete an Element AbeArnieDaveEd ??? students.count: 5 students.names Example: delete Cary - find the position of the element - remove the element - shuffle the items after the deleted item to the left We dont need to actually clear this. Just update count

30 List: Delete an Element AbeArnieDaveEd ??? students.count: 4 students.names Example: delete Cary - find the position of the element - remove the element - shuffle the items after the deleted item to the left - decrement count

31 Summary Operations on lists –initialize, add element, delete element, find element, print Add, Find and Delete: sorted or unsorted? Review: array of strings and structs as parameters