A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Linked Lists Geletaw S..
COMP171 Fall 2005 Lists.
Stacks, Queues, and Linked Lists
Lists Chapter 6 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Lists: An internal look
CHP-5 LinkedList.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
Data Structures Using C++ 2E
Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
Reference: Vinu V Das, Principles of Data Structures using C and C++
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Lists. Container Classes Many applications in Computer Science require the storage of information for collections of entities e.g. a student registration.
Lists Chapter 6 5/14/15 Adapted from instructor slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
More Linking Up with Linked Lists Chapter 11 5/19/2015 Adopted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++,
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.
Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Exam1 Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMorewww.desiamore.com/ifm1.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Lists Chapter.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009
Queues Chapter 4.
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 6 Section 6.4 – 6.6
Data Structure Dr. Mohamed Khafagy.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Stacks and Queues.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Chapter 4 Linked Lists
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Array Lists Chapter 6 Section 6.1 to 6.3
Chapter 16-2 Linked Structures
Arrays and Linked Lists
Chapter 16 Linked Structures
Dynamic allocation (continued)
Data Structures and Algorithms Memory allocation and Dynamic Array
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order to do that, we need to identify: 1. The collection of data items 2. Basic operation that must be performed on them Abstract Data Type (ADT): a collection of data items together with the operations on the data

Abstract Data Type (ADT) The word “abstract” refers to the fact that the data and the basic operations defined on it are being studied independently of how they are implemented We think about what can be done with the data, not how it is done

Chapter 6 Lists Dr. Bernard Chen Associate Professor University of Central Arkansas

Consider Every Day Lists Groceries to be purchased Job to-do list List of assignments for a course Dean's list Can you name some others??

List List is a collection of things It is homogeneous --- the elements are all of the same type It has finite length The elements are arranged sequentially

LIST ADT Collection of data elements A sequence with a finite number of data items, all of the same type Basic operations Construction---create a empty list Empty---check if the list is empty Insert---insert an item Delete---remove a item Traverse---go through the list or part of it

Building a List class It consists of two steps: Design the class Implement the class

Design the list class Designing a class to implement an ADT consist of identifying “functions” members that are need to be carry out It is important that we describe the function members independently of how the class will be implemented Function members must be describe in some manner that does not depend on any particular representation of the objects

Design the list class Should contain at least the following function members Constructor empty() insert() delete() display()

Implementation of the List In this chapter, two approaches to implement List: Array Based Pointer (Link list) Based

Static Array based An array is a common choice for storing list elements Element are sequential Most languages support array Algorithm development is easy An array thus seems to be a natural storage structure for implementing a list

Static Array based Normally sequential orderings of list elements match with array elements Of course, the array must be large enough to hold all of the list elements It means we need to chose carefully

Static Array based Three variables we use to tract array: Array--the array that stores the list Capacity--array’s capacity Size--the number of list elements that are stored in the array Constructor: Since we use static array, we can let the complier handle the memory allocation, and our constructor only need to set size to 0 Empty: only need to check whether size is 0

Static Array based Traverse: Lists are easy traversed using a loop: for index i ranging from 0 to size-1: process array[i]

Static Array based Insert: it’s more complicated For example: if we wish to insert the new value 56 at index 4: Original , 25, 34, 48, 61, 79, 82, 89, 91, 99 Produce--- 23, 25, 34, 48, 56, 61, 79, 82, 89, 91, 99

Static Array based Insert: we need to do Check the capacity Need to move array elements to make room for the new element

Static Array based Algorithm for insert: If size is equal to capacity: display error due to limit space If (pos size): display error due to error position Else: for i in range from size to pos+1: array[i]=array[i-1] //C++ starts index from 0 array[pos]=item size++

Static Array based The efficiency of this insert algorithm obviously depends on the number of array elements that must be shifted to make room for the new item In worst case, the new item must be inserted at the beginning of the list O(n) The best case occurs when the new item is inserted at the end of it O(1) Two important special kinds of lists for which are stacks and queues

Static Array based Delete: also requires shifting array elements For example, to delete the second item in: 23, 25, 34, 48, 56, 61, 79, 82, 89, 91, 99

Static Array based Algorithm for delete: If size is zero: issue a error message If pos =size: issue an error message Otherwise: for index i ranging from pos to size-2: array[i]=array[i+1] size--

Static Array based The computing time of such a function is easily seen to be the same as insert function --- O(n) in the worst case and O(1) for the best

Implementation of the List In this chapter, two approaches to implement List: Array Based Pointer (Link list) Based

Linked List Data part – stores an element of the list Next part – stores link/pointer to next element class node { public: int data; node* next; };

Linked List Linked list nodes contain Data part – stores an element of the list Next part – stores link/pointer to next element (when no next element, null value)

Design the list class Should contain at least the following function members Constructor empty() insert() delete() display()

Construction To construct an empty list, we simply make top a null link to indicate that it does not refer to any node: top = null_value ;

Construction PointerList() { top = NULL; count=0; }

Empty We can then perform the Empty operation--- determining whether a list is empty, simply by checking whether first is null: bool empty() { if(top==NULL) return true; else return false; }

Traverse We begin by initializing an auxiliary variable temp to point to the first node: Initialize a variable temp to point to first node Process data where temp points

Traverse Traverse (ctd) set twmp = temp->next, process ptr->data Continue until ptr == null

Traverse void print() { node *temp; temp = top; while(temp!=NULL) { cout data<<","; temp=temp->next; }

Insertion To insert a new data value into a linked list, we must first obtain a new node and store the value in its data part The second step is to connect this new node to existing list Two cases in this situation: (1) insertion after some element in the list and (2) insertion at the beginning of the list

Insertion To insert 20 after 17 Need address of item before point of insertion predptr points to the node containing 17 Get a new node pointed to by newptr and store 20 in it Set the next pointer of this new node equal to the next pointer in its predecessor, thus making it point to its successor. Reset the next pointer of its predecessor to point to this new node 20 newptr predptr

Insertion Note: insertion also works at end of list pointer member of new node set to null Insertion at the beginning of the list predptr must be set to first pointer member of newptr set to that value (Where first points to) first set to value of newptr In all cases, no shifting of list elements is required !

Insertion (normal case) void insert(int position, int element) { node *preptr; preptr = top; for(int i=0;i<position-1;i++) preptr=preptr->next; newptr->next = preptr->next; preptr->next = newptr; }

void insert(int position, int element) { node *newptr = new node; newptr->data = element; if(top == NULL)//insert the very first element if(position==0) { newptr->next = NULL; top = newptr; } else cout<<"Location Error!!"; else if(position==0)//insert on the first position when some elements existed { newptr->next = top; top = newptr; } else//most cases belongs to this situation (as showed in the class slide) { node *preptr; preptr = top; for(int i=0;i<position-1;i++) preptr=preptr->next; newptr->next = preptr->next; preptr->next = newptr; }

Deletion For deletion, there are also two cases to consider: Deleting an element that has a predecessor Delete the first element in the list

Deletion Delete node containing 22 from list. Suppose ptr points to the node to be deleted predptr points to its predecessor (the 17) Do a bypass operation : Set the next pointer in the predecessor to point to the successor of the node to be deleted Deallocate the node being deleted. predptr ptr To free space

Deletion void remove(int position) { node *preptr; preptr = top; for(int i=0;i<position-1;i++) preptr=preptr->next; node * ptr = preptr->next; preptr->next = ptr->next; delete(ptr); }

Deletion Complete void remove(int position) { if(position==0)//delete the first element { node * ptr = top; top = ptr->next; delete(ptr); } else { node *preptr; preptr = top; for(int i=0;i<position-1;i++) preptr=preptr->next; node * ptr = preptr->next; preptr->next = ptr->next; delete(ptr); }

Linked Lists - Advantages Access any item as long as external link to first item maintained Insert new item without shifting Delete existing item without shifting Can expand/contract as necessary

Linked Lists - Disadvantages No longer have direct access to each element of the list Many sorting algorithms need direct access Binary search needs direct access Access of n th item now less efficient must go through first element, and then second, and then third, etc.