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.

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? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
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 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
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.
1 Chapter 4 Stack and Queue ADT. 2 Stacks of Coins and Bills.
Implementing an Unsorted List as a Linked Structure 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.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
Implementing a Stack as a Linked Structure CS 308 – Data Structures.
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.
Chapter 3 ADT Unsorted List. Lecture 7 List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element.
What is a Stack? n Logical (or ADT) level: A stack is an ordered group of homogeneous items in which the removal and addition of items can take place only.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
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.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
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.
Data Structures: Stacks Queues 1. 2 Stack ADT: What is a Stack? a stack is a varying-length, collection of homogeneous elements Insertion and Deletion.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
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.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
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.
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 C++ Plus Data Structures Abstract Data Types Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by.
C++ Plus Data Structures ADTs Unsorted List and Sorted List
C++ Plus Data Structures
Chapter 5 ADTs Stack and Queue.
Chapter 16-2 Linked Structures
C++ Plus Data Structures
Unsorted Lists CS3240, L. Grewe.
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.
Chapter 16 Linked Structures
C++ Plus Data Structures
CSI 1340 Introduction to Computer Science II
Yan Shi CS/SE 2630 Lecture Notes
Data Structures and Algorithms Memory allocation and Dynamic Array
TA: Nouf Al-Harbi Data Structures LAB 2 TA: Nouf Al-Harbi
Presentation transcript:

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 addition of stack items can take place only at the top of the stack. A stack is a LIFO “last in, first out” structure.

Stack ADT Operations IsEmpty -- Determines whether the stack is currently empty. IsFull -- Determines whether the stack is currently full. Push (ItemType newItem) -- Adds newItem to the top of the stack. Pop -- Removes the item at the top of the stack. Top – Returns a copy of top items 3

ADT Stack Operations Transformers Push Pop Observers IsEmpty IsFull Top change state observe state 4

5 Another Stack Implementation One advantage of an ADT is that the kind of implementation used can be changed. The dynamic array implementation of the stack has a weakness -- the maximum size of the stack is passed to the constructor as parameter. Instead we can dynamically allocate the space for each stack element as it is pushed onto the stack.

6 ItemType is char class StackType StackType Top Pop Push IsFull IsEmpty Private data: topPtr ~StackType ‘C’ ‘V’

7 class StackType StackType Top Pop Push IsFull IsEmpty Private data: topPtr ~StackType ItemType is float

8 ItemType is StrType class StackType StackType Top Pop Push IsFull IsEmpty Private data: topPtr ~StackType cat dog

9 Tracing Client Code letter ‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

10 Tracing Client Code letter ‘V’ Private data: topPtr NULL charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

11 Tracing Client Code letter Private data: topPtr ‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

12 Tracing Client Code letter Private data: topPtr ‘C’ ‘V’ ‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

13 Tracing Client Code letter Private data: topPtr ‘S’ ‘C’ ‘V’ ‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

14 Tracing Client Code letter Private data: topPtr ‘S’ ‘C’ ‘V’ ‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

15 Tracing Client Code letter Private data: topPtr ‘C’ ‘V’ ‘S’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

16 Tracing Client Code letter Private data: topPtr ‘C’ ‘V’ ‘S’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

17 Tracing Client Code letter Private data: topPtr ‘V’ ‘C’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

18 Tracing Client Code letter Private data: topPtr ‘V’ ‘C’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

19 Tracing Client Code letter Private data: topPtr ‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

20 Tracing Client Code letter Private data: topPtr ‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

21 Tracing Client Code letter Private data: topPtr ‘K’ ‘V’ charletter = ‘V’; StackType myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); If (!myStack.IsEmpty() ) { letter = myStack.Top( ); myStack.Pop(); } myStack.Push(‘K’);

22 // DYNAMICALLY LINKED IMPLEMENTATION OF STACK Struct NodeType; //Forward declaration class StackType { public: //Identical to previous implementation private: NodeType* topPtr; };. Struct NodeType { ItemType info; NodeType* next; }; 22

Using operator new If memory is available in an area called the free store (or heap), operator new allocates the requested object, and returns a pointer to the memory allocated. The dynamically allocated object exists until the delete operator destroys it. 23

24 A Single Node.info.next ‘D ’ The user’s data Pointer to the next node in the stack

25 Node terminology

26 Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’ newItem

27 Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’ newItem location

28 Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’ newItem location

29 Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’ newItem location ‘B’

30 Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’ newItem location ‘B’

31 Adding newItem to the stack newItem = ‘B’; NodeType* location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; topPtr ‘X’ ‘C’ ‘L’ ‘B’ newItem location ‘B’

32 void StackType::Push ( ItemType newItem ) // Adds newItem to the top of the stack. { if (IsFull()) throw FullStack(); NodeType* location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; } 32 Implementing Push

The object currently pointed to by the pointer is deallocated, and the pointer is considered unassigned. The memory is returned to the free store. Using operator delete 33

34 Deleting item from the stack NodeType* tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; topPtr ‘B’ ‘X’ ‘C’ ‘L’ tempPtr item

35 Deleting item from the stack NodeType* tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; topPtr item ‘B’ ‘X’ ‘C’ ‘L’ tempPtr ‘B’

36 Deleting item from the stack NodeType* tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; topPtr item ‘B’ ‘X’ ‘C’ ‘L’ tempPtr ‘B’

37 Deleting item from the stack NodeType* tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; topPtr item ‘B’ ‘X’ ‘C’ ‘L’ tempPtr ‘B’

38 Deleting item from the stack NodeType * tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; topPtr item ‘X’ ‘C’ ‘L’ tempPtr ‘B’

39 void StackType::Pop()// Remove top item from Stack. { if (IsEmpty()) throw EmptyStack(); else { NodeType* tempPtr; tempPtr = topPtr; topPtr = topPtr ->next; delete tempPtr; } ItemType StackType::Top() // Returns a copy of the top item in the stack. { if (IsEmpty()) throw EmptyStack(); else return topPtr->info; } 39 Implementing Pop / Top

40 Implementing IsFull bool StackType::IsFull() const // Returns true if there is no room for another // ItemType on the free store; false otherwise { NodeType* location; try { location = new NodeType; delete location; return false; } catch(std::bad_alloc exception) { return true; }

41 Why is a destructor needed? When a local stack variable goes out of scope, the memory space for data member topPtr is deallocated. But the nodes that topPtr points to are not automatically deallocated. A class destructor is used to deallocate the dynamic memory pointed to by the data member.

42 Implementing the Destructor stackType::~StackType() // Post: stack is empty; // All items have been deallocated. { NodeType* tempPtr; while (topPtr != NULL) { tempPtr = topPtr; topPtr = topPtr-> next; delete tempPtr; }

43 What is a Queue? Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the rear), and elements are removed from the other end (the front). A queue is a FIFO “first in, first out” structure.

Queue ADT Operations MakeEmpty -- Sets queue to an empty state. IsEmpty -- Determines whether the queue is currently empty. IsFull -- Determines whether the queue is currently full. Enqueue (ItemType newItem) -- Adds newItem to the rear of the queue. Dequeue (ItemType& item) -- Removes the item at the front of the queue and returns it in item. 44

ADT Queue Operations Transformers MakeEmpty Enqueue Dequeue Observers IsEmpty IsFull change state observe state 45

46 class QueType QueType ~QueType Enqueue Dequeue. Private Data: qFront qRear ‘C’‘Z’ ‘T’

// DYNAMICALLY LINKED IMPLEMENTATION OF QUEUE #include "ItemType.h" // for ItemType template class QueType { public: QueType( ); // CONSTRUCTOR ~QueType( ) ;// DESTRUCTOR bool IsEmpty( ) const; bool IsFull( ) const; void Enqueue( ItemType item ); void Dequeue( ItemType& item ); void MakeEmpty( ); private: NodeType * qFront; NodeType * qRear; }; 47

// DYNAMICALLY LINKED IMPLEMENTATION OF QUEUE continued // member function definitions for class QueType template QueType ::QueType( ) // CONSTRUCTOR { qFront = NULL; qRear = NULL; } template bool QueType ::IsEmpty( ) const { return ( qFront == NULL ) } 48

template void QueType ::Enqueue( ItemType newItem ) // Adds newItem to the rear of the queue. // Pre: Queue has been initialized. // Queue is not full. // Post: newItem is at rear of queue. { NodeType * ptr; ptr = new NodeType ; ptr->info = newItem; ptr->next = NULL; if ( qRear == NULL ) qFront = ptr; else qRear->next = ptr; qRear = ptr; } 49

template void QueType ::Dequeue( ItemType& item ) // Removes element from from front of queue // and returns it in item. // Pre: Queue has been initialized. // Queue is not empty. // Post: Front element has been removed from queue. // item is a copy of removed element. { NodeType * tempPtr; tempPtr = qFront; item = qFront->info; qFront = qFornt->next; if ( qFront == NULL ) qRear = NULL; delete tempPtr; } 50

51 What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. That is, each list element (except the first) has a unique predecessor, and each element (except the last) has a unique successor.

ADT Unsorted List Operations Transformers MakeEmpty InsertItem DeleteItem Observers IsFull LengthIs RetrieveItem Iterators ResetList GetNextItem change state observe state process all 52

#include “ItemType.h” // unsorted.h... template class UnsortedType { public : // LINKED LIST IMPLEMENTATION UnsortedType ( ) ; ~UnsortedType ( ) ; void MakeEmpty ( ) ; bool IsFull ( ) const ; int LengthIs ( ) const ; void RetrieveItem ( ItemType& item, bool& found ) ; void InsertItem ( ItemType item ) ; void DeleteItem ( ItemType item ) ; void ResetList ( ); void GetNextItem ( ItemType& item ) ; private : NodeType * listData; int length; NodeType * currentPos; } ; 53

54 class UnsortedType MakeEmpty ~UnsortedType DeleteItem. InsertItem UnsortedType RetrieveItem GetNextItem ‘X’ ‘C’ ‘L’ Private data: length 3 listData currentPos ?

// LINKED LIST IMPLEMENTATION ( unsorted.cpp ) #include “itemtype.h” template UnsortedType ::UnsortedType ( ) // constructor // Pre: None. // Post: List is empty. { length = 0 ; listData = NULL; } template int UnsortedType ::LengthIs ( ) const // Post: Function value = number of items in the list. { return length; } 55

template void UnsortedType ::RetrieveItem( ItemType& item, bool& found ) // Pre: Key member of item is initialized. // Post: If found, item’s key matches an element’s key in the list //and a copy of that element has been stored in item; otherwise, // item is unchanged. { bool moreToSearch ; NodeType * location ; location = listData ; found = false ; moreToSearch = ( location != NULL ) ; while ( moreToSearch && !found ) { if ( item == location->info ) // match here { found = true ; item = location->info ; } else // advance pointer { location = location->next ; moreToSearch = ( location != NULL ) ; } } } 56

template void UnsortedType ::InsertItem ( ItemType item ) // Pre: list is not full and item is not in list. // Post: item is in the list; length has been incremented. { NodeType * location ; // obtain and fill a node location = new NodeType ; location->info = item ; location->next = listData ; listData = location ; length++ ; } 57

58 Inserting ‘B’ into an Unsorted List ‘X’ ‘C’ ‘L’ Private data: length 3 listData currentPos ?

59 location = new NodeType ; ‘X’ ‘C’ ‘L’ Private data: length 3 listData currentPos ? item location ‘B’

60 location->info = item ; ‘X’ ‘C’ ‘L’ Private data: length 3 listData currentPos ? item location ‘B’

61 location->next = listData ; ‘X’ ‘C’ ‘L’ Private data: length 3 listData currentPos ? item location ‘B’

62 listData = location ; ‘X’ ‘C’ ‘L’ Private data: length 3 listData currentPos ? item location ‘B’

63 length++ ; ‘X’ ‘C’ ‘L’ Private data: length 4 listData currentPos ? item location ‘B’

64 class SortedType MakeEmpty ~SortedType DeleteItem. InsertItem SortedType RetrieveItem GetNextItem ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos ?

65 InsertItem algorithm for Sorted Linked List Find proper position for the new element in the sorted list using two pointers predLoc and location, where predLoc trails behind location. Obtain a node for insertion and place item in it. Insert the node by adjusting pointers. Increment length.

66 Implementing SortedType member function InsertItem // LINKED LIST IMPLEMENTATION (sorted.cpp) #include “ItemType.h” template void SortedType :: InsertItem ( ItemType item ) // Pre: List has been initialized. List is not full. // item is not in list. // List is sorted by key member. // Post: item is in the list. List is still sorted. {. }

67 The Inchworm Effect

68 Inserting ‘S’ into a Sorted List ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos ? predLoc location moreToSearch

69 Finding proper position for ‘S’ ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos ? predLoc location NULL moreToSearch true

70 Finding proper position for ‘S’ ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos ? predLoc location moreToSearch true

71 Finding Proper Position for ‘S’ ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos ? predLoc location moreToSearch false

72 Inserting ‘S’ into Proper Position ‘C’ ‘L’ ‘X’ Private data: length 4 listData currentPos predLoc location moreToSearch false ‘S’