Implementing an Unsorted List as a Linked Structure CS 308 – Data Structures.

Slides:



Advertisements
Similar presentations
Sorted Lists CS Data Structures Sections 4.1, 4.2 & 4.3.
Advertisements

Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.
Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the.
C++ Plus Data Structures ADTs Unsorted List and Sorted List
What is a Queue? A queue is a FIFO “first in, first out” structure.
Chapter 5 ADTs Stack and Queue. 2 Goals Describe a stack and its operations at a logical level Demonstrate the effect of stack operations using a particular.
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
Unsorted Lists CS 308 – Data Structures. What is a list? A list is a homogeneous collection of elements. Linear relationship between elements: (1) Each.
1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.
Chapter 4 ADT Sorted List.
Sorted Lists CS Data Structures. Sorted List Specification (partial) InsertItem (ItemType item) Function: Adds item to list Preconditions: (1) List.
Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4.
Recursion CS 308 – Data Structures. What is recursion? smaller version Sometimes, the best way to solve a problem is by solving a smaller version of the.
Heaps CS 308 – Data Structures.
1 expanded by J. Goetz Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 Nell Dale Chapter 3 ADTs Unsorted List and Sorted List Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Implementing a Stack as a Linked Structure CS 308 – Data Structures.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Doubly Linked Lists CS 308 – Data Structures. Node data info: the user's data next, back: the address of the next and previous node in the list.back.next.info.
Dynamic Structures & Arrays.
1 Fall Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List.
Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
Chapter 4 ADT Sorted List.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Binary Search Trees. What is a binary tree? Property 1: each node can have up to two successor nodes.
Chapter 3 ADT Unsorted List. Lecture 7 List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element.
Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations.
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.
Recursion CS 3358 – Data Structures. Big Picture Our objective is to write a recursive program and convince ourselves it is correct with the minimum amount.
Recursion CS 302 – Data Structures Chapter 7. What is recursion? smaller A technique that solves problem by solving smaller versions of the same problem!
3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
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.
Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
1 Data Structures and Algorithms Stacks and Queues.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 4 ADT Sorted List. Sorted Type Class Interface Diagram SortedType class IsFull GetLength ResetList DeleteItem PutItem MakeEmpty GetItem Private.
What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a.
CS 302 – Data Structures Sections 3.1, 3.2, 3.4 & 3.5
1 Nell Dale Lecture 3 ADTs Unsorted List and Sorted List Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by Reneta.
CSI 1340 Introduction to Computer Science II Chapter 3 ADTs Unsorted List and Sorted List.
1 Chapter 4 ADT Sorted List. 2 Sorted Type Class Interface Diagram SortedType class IsFull LengthIs ResetList DeleteItem InsertItem MakeEmpty RetrieveItem.
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
Chapter 5 (Part 1) ADT Stack 1 Fall Stacks TOP OF THE STACK.
1 Nell Dale Chapter 8 Binary Search Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
C++ Plus Data Structures ADTs Unsorted List and Sorted List
TA: Nouf Al-Harbi Data Structures LAB 3 TA: Nouf Al-Harbi
C++ Plus Data Structures
Chapter 16-2 Linked Structures
C++ Plus Data Structures
Unsorted Lists CS3240, L. Grewe.
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
Chapter 9 Priority Queues and Sets
CSI 1340 Introduction to Computer Science II
Yan Shi CS/SE 2630 Lecture Notes
Data Structures and Algorithms Memory allocation and Dynamic Array
Recursion.
TA: Nouf Al-Harbi Data Structures LAB 2 TA: Nouf Al-Harbi
Presentation transcript:

Implementing an Unsorted List as a Linked Structure CS 308 – Data Structures

Implementing an Unsorted List as a Linked Structure Allocate memory for each new element dynamically Link the list elements together Use two pointers, currentPos and listData, to mark the current position in the list and the beginning of the list Use an integer variable, length, to store the current length of the list.

Implementing an Unsorted List as a Linked Structure (cont.)

Unsorted List Class Specification template struct NodeType; template class UnsortedType { public: UnsortedType(); ~UnsortedType(); void MakeEmpty(); bool IsFull() const; int LengthIs() const; void RetrieveItem(ItemType&, bool&); void InsertItem(ItemType); void DeleteItem(ItemType); void ResetList(); bool IsLastItem() const; void GetNextItem(ItemType&); private: int length; NodeType * listData; NodeType * currentPos; };

Function RetrieveItem

Function RetrieveItem (cont.) template void UnsortedType ::RetrieveItem (ItemType& item, bool& found) { NodeType * location; location = listData; found = false; while( (location != NULL) && !found) { if(item == location->info) { found = true; item = location->info; } else location = location->next; }

Function InsertItem Just insert the item at the beginning of the list

Function InsertItem (cont.) template void UnsortedType ::InsertItem (ItemType newItem) { NodeType * location; location = new NodeType ; location->info = newItem; location->next = listData; listData = location; length++; }

Function DeleteItem Find the item first In order to delete it, we must change the pointer in the previous node!!

Function DeleteItem (cont.) Solution: compare one item ahead ((location- >next)->info)) !! Deleting the first node is a special case...

Important:this implementation will work without problems ONLY if the item to be deleted IS in the list ! (precondition)

Function DeleteItem (cont.) template DeleteItem void UnsortedType ::DeleteItem(ItemType item) { NodeType * location = listData; NodeType * tempLocation; if(item == listData->info) { tempLocation = location; listData = listData->next; // delete first node } else { while(!(item == (location->next)->info)) location = location->next; // delete node at location->next tempLocation=location->next; location->next = (location->next)->next; } delete tempLocation; length--; }

Other UnsortedList functions template UnsortedType ::UnsortedType() { length = 0; listData = NULL; } template void UnsortedType ::MakeEmpty() { NodeType * tempPtr; while(listData != NULL) { tempPtr = listData; listData = listData->next; delete tempPtr; } length=0; }

Other UnsortedList functions (cont.) template UnsortedType ::~UnsortedType() { MakeEmpty(); } template bool UnsortedType ::IsFull() const { NodeType * ptr; ptr = new NodeType ; if(ptr == NULL) return true; else { delete ptr; return false; }

template int UnsortedType ::LengthIs() const { return length; } template int UnsortedType ::ResetList() { currentPos = listData; } template void UnsortedType ::GetNextItem(ItemType& item) { item = currentPos->info; currentPos = currentPos->next; } template bool UnsortedType ::IsLastItem() const { return(currentPos == NULL); } Other UnsortedList functions (cont.)

Write a client function that merges two instances of the Unsorted List ADT using the following specification. MergeLists(UnsortedType list1, UnsortedType list2, UnsortedType& result) Function: Merges two unsorted lists into a third unsorted list (no duplicates). Preconditions: list1 and list2 have been initialized. Postconditions: result is an unsorted list that contains all of the items from list1 and list2.

{ ItemType item; bool found; list1.Reset(); list2.Reset(); result.MakeEmpty(); while ( !list1.IsLastItem() ); { list1.GetNextItem(item); if ( !result.IsFull() ) result.InsertItem(item); } while ( !list2.IsLastItem() ); { list2.GetNextItem(item); list1.RetrieveItem(item, found); if ( !found ) if ( !result.IsFull() ) result.InsertItem(item); }

Comparing unsorted list implementations Big-O Comparison of Unsorted List Operations OperationArray Implementation Linked Implementation Class constructorO(1) DestructorO(1)O(N) MakeEmptyO(1)O(N) IsFullO(1) LengthIsO(1) ResetListO(1) GetNextItemO(1) RetrieveNextItemO(N) InsertItemO(1) DeleteItemO(N)

Exercises 9, 10, 11,