CSCE 210 Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Linear Lists – Array Representation
Advertisements

Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R4. Disjoint Sets.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Introduction to Stacks & Queues.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Data Structures Using C++ 2E
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R1. ADTs as Classes.
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.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R2. Binary Search Trees.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 6. Dictionaries(3): Binary Search Trees.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
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.
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2a. Simple Containers: The Stack.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
Chapter 16: Linked Lists.
CSCE 210 Data Structures and Algorithms
C++ Programming:. Program Design Including
CSCE 210 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
CS505 Data Structures and Algorithms
Chapter 4 The easy stuff.
CSCE 210 Data Structures and Algorithms
Template Classes and Functions
Linked Lists Chapter 6 Section 6.4 – 6.6
Big-O notation Linked lists
CSCI 210 Data Structures and Algorithms
Chapter 4 Linked Lists.
CSCE 210 Data Structures and Algorithms
Chapter 4 Linked Lists
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Array Lists Chapter 6 Section 6.1 to 6.3
Chapter 16-2 Linked Structures
Chapter 4 Linked Lists.
Chapter 18: Linked Lists.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 4 Linked Lists.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 16 Linked Structures
Pointers & Dynamic Data Structures
Array-Based Lists & Pointers
(1 - 2) Introduction to C Data Structures & Abstract Data Types
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Data Structures & Programming
Presentation transcript:

CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R3. Dictionaries(1): Key Tables and Lists Prof. Amr Goneid, AUC

Dictionaries(1):Key Tables and Lists The Key Table ADT Key Table The Key Table Class Definition Key Table Class implementation Example Application The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation Prof. Amr Goneid, AUC

1. The Key Table Key Table Abstraction: Key Table: a container of data in the form of a linear configuration of elements in which we can insert and delete nodes in any order. It also supports search by content (key), and can represent a dictionary ADT. Element: a container for one key and associated data item Current Element: special element in the table, indicated by a pointer to the current position. EmptyKey: a special key value to indicate that the table slot is empty. Initially all table positions are empty. Prof. Amr Goneid, AUC

Ordered Key Table Class We will construct a class “Ktable” whose objects are key tables. They will be implemented as dynamic arrays. The data members will be the elements and a pointer to these elements. An element contains a key field and a data field. Search is by content (key) The table will be ordered on the key field. Prof. Amr Goneid, AUC

(a) ADT Key Table Key Table Data Members Elements. Each element has: Data or information field of type dataType. A key of type keyType Others: T, a pointer to a dynamic array of elements; P, a pointer to the current element; MaxSize, The maximum size (Capacity) of the table csize, The current size of table (no. of filled slots) Empty, A special key value to indicate an empty slot Prof. Amr Goneid, AUC

Element Specification // The element structure can be specified as a Class // in the private part of the main Ktable class. class element // Hidden from user { public: keyType key; // key dataType data; // Data }; // end of class element declaration Prof. Amr Goneid, AUC

Key Table Operations construct: Create table emptyTable (E): Initialize table and fill with empty symbol tableIsEmpty  bool : return True if table is empty tableIsFull  bool : return True if table is full occupancy  int : return the current no. of elements in the table Prof. Amr Goneid, AUC

Key Table Operations updateData (d) : to update the data portion of the current element to contain d; assume the current position is nonempty. retrieveData (d): to return the data (d) in the current element; assume the current position is nonempty. deleteCurrent : delete the current element. Assume the current position is nonempty initially. Prof. Amr Goneid, AUC

Key Table Operations search (k)  bool : Search the table for the slot with key part that matches (k). If found, set p to the slot and return True, else return false orderInsert (k,d) : insert an element in a position that maintains an ascending order of the key portion. traverse: traverse table to print key and info fields. Prof. Amr Goneid, AUC

(b) Key Table Class Definition // File: Ktable.h // Definition of Ktable Template Class #ifndef KTABLE_H #define KTABLE_H // Specification of the class template <class keyType, class dataType> class Ktable { public: Prof. Amr Goneid, AUC

Key Table Class Definition // Member Functions Ktable(int nelements = 128); // Constructor ~Ktable(); // Destructor // Functions Prototype Definitions void emptyTable(const keyType & ); bool tableIsEmpty() const; bool tableIsFull() const; Prof. Amr Goneid, AUC

Key Table Class Definition int occupancy() const; void updateData(const dataType & ); void retrieveData(dataType &) const; void deleteCurrent(); bool search(const keyType & ); bool orderInsert(const keyType &, const dataType & ); void traverse() const; Prof. Amr Goneid, AUC

Key Table Class Definition private: // Element Class class element { public: keyType key; // key dataType data; // Data }; // end of class element declaration Prof. Amr Goneid, AUC

Key Table Class Definition element *T; // Pointer to Storage Array int p; // Pointer to current element // Maximum and Current Sizes int MaxSize, csize; keyType Empty; // Empty symbol }; // end of Ktable Class definition #endif // KTABLE_H #include "Ktable.cpp" Prof. Amr Goneid, AUC

(c) Implementation Files Full implementation of the Key Table class is found at: http://www.cse.aucegypt.edu/~csci210/codes.zip Prof. Amr Goneid, AUC

Key Table Class Implementation Note: Since the keys are kept in ascending order, it is possible to use the binary search algorithm in the search function. The implementation of this option is left as an exercise. Prof. Amr Goneid, AUC

(d) Example Application An ordered list of characters and their frequencies in a string: Given a string, build a List of characters and their count in the string. The list is ordered alphabetically on the characters. Prof. Amr Goneid, AUC

Ordered List // File: KTabletest.cpp // Applies Ktable Class #include <iostream> #include <string> using namespace std; #include "Ktable.h“ int main() { Ktable<char , int> ctable; string s; char c; int i, count; bool keyfound; Prof. Amr Goneid, AUC

Ordered List // Read a string cout << "Enter a string:" << endl; getline(cin,s); cout << s << endl; // display it ctable.emptyTable(char(126)); for (i = 0; i < s.length(); i++) // for every character { c = toupper(s.at(i)); // Search for character in the table keyfound = ctable.search (c); Prof. Amr Goneid, AUC

Ordered List if (keyfound) // if found { ctable.retrieveData(count); // get data count++; // increment count ctable.updateData(count); // store back } // Not found, a new node is inserted else ctable.orderInsert(c,1); Prof. Amr Goneid, AUC

Ordered List // print characters and their frequencies ctable.traverse(); cout << ctable,occupancy() << endl; // current table size // empty table ctable.emptyTable(char(126)); // the size now should be zero cout << ctable.occupancy() << endl; return 0; } Prof. Amr Goneid, AUC

Sample Output Enter a string: The Rain in Spain 3 A 2 E 1 H 1 I 3 N 3 10 Press any key to continue Prof. Amr Goneid, AUC

2. The Linked List Linked List Abstraction: Linked List: a container of data in the form of a linear configuration of nodes in which we can insert and delete nodes in any order. Each Node is linked to its successor in the list. If it also supports search by contents, it can represent a dictionary ADT. Node: a container for one data item and has a direct relationship with at most two other nodes, its predecessor (if any) and its successor (if any). Head node or first node is the only node without a predecessor. Current node: special node in the list, indicated by the current position. Previous Node: the predecessor of the current node Prof. Amr Goneid, AUC

Ordered Linked List Class We will construct a class “List” whose objects are linked lists. They will be implemented as dynamic lists. The data members will be the nodes and the pointers to these nodes. A node contains a key field and a data field. Search is by content (key) The list will be ordered on the key field. Prof. Amr Goneid, AUC

(a) ADT Linked List Linked List Data Members Nodes. Each node has: Data or information field of type dataType. A key of type keyType Link field (next) , a pointer to next node Pointers: head, a pointer to the first node; cursor, a pointer to the current node; prev, a pointer to the previous node. Prof. Amr Goneid, AUC

Data Members key data next Current Last NULL head First prev cursor Prof. Amr Goneid, AUC

Node Specification // The linked structure for a node can be // specified as a Class in the private part of // the main linked list class. class node // Hidden from user { public: keyType key; // key dataType data; // Data node *next; // pointer to next node }; // end of class node declaration typedef node * NodePointer; // Pointers NodePointer head, cursor, prev; Prof. Amr Goneid, AUC

Linked List Operations Notation Meaning head the head pointer cursor pointer to current node prev pointer to predecessor node pnew pointer to a new node d item with the same type as the data portion of a node k item with type as the key portion of the node b boolean value L Length of list Prof. Amr Goneid, AUC

Linked List Class Operations construct & initialize list to empty listIsEmpty  b : return True if list is empty curIsEmpty  b : return True if current position is empty toFirst : to make the current node the first node; if list is empty, the current position is still empty atFirst  b : to return True if the current node is the first node or if the list and the current position are both empty. Prof. Amr Goneid, AUC

Linked List Class Operations advance : to advance to next node. Assume the current position is nonempty initially. toEnd : to make the current node the tail node; if list is empty, the current position is still empty atEnd  b : to return True if the current node is the tail node or if the list and the current position are both empty. listSize  L : to return the size of the list updateData (d) : to update the data portion of the current node to contain d; assume the current position is nonempty. Prof. Amr Goneid, AUC

Linked List Class Operations retrieveData  d: to return the data in the current node; assume the current position is nonempty. insertFirst (k,d) : insert a node with key (k) and data (d) at the head of the list; the new node becomes the current node. insertAfter (k,d) : insert a node after the current node without changing the current position; assume the current position is nonempty in a non-empty list. insertBefore (k,d) : insert a node before the current node ; current position becomes the new node Prof. Amr Goneid, AUC

Linked List Class Operations insertEnd(k,d): insert a node at the end of the list, current position becomes the new node. deleteNode : delete the current node and set the current position to the next node; if the current node is the last node initially, the current position becomes empty; assume the current position is nonempty initially. deleteFirst: delete the first node and set the current position to the next node; if it was initially the only node, the current position becomes empty; Prof. Amr Goneid, AUC

Linked List Class Operations deleteEnd: delete the last node and set the current position to empty. makeListEmpty : delete whole list search (k)  b : search the list for the node with key part that matches (k). If found, set cursor to the node and return True, else return false and the current position becomes empty. Prof. Amr Goneid, AUC

Linked List Class Operations orderInsert (k,d) : insert a node in a position that maintains an ascending order of the key portion of the nodes. traverse: traverse list to print key and info fields. The Linked List will be implemented as a template class to allow different types for the key and data fields. Prof. Amr Goneid, AUC

(b) Linked List Class Definition // File: List.h // Definition of Simple Linked List Template Class #ifndef LIST_H #define LIST_H // Specification of the class template <class keyType, class dataType> class List { public: Prof. Amr Goneid, AUC

List Class Header File // Member Functions // Create an empty List // Class Destructor ~List(); // Functions Prototype Definitions bool listIsEmpty() const; bool curIsEmpty() const; void toFirst(); bool atFirst() const; void advance(); Prof. Amr Goneid, AUC

List Class Header File void toEnd(); bool atEnd() const; int listSize() const; void updateData (const dataType & ); void retrieveData (dataType &) const; void insertFirst (const keyType &, const dataType & ); void insertAfter (const keyType &, const dataType & ); void insertBefore (const keyType &, const dataType & ); void insertEnd (const keyType &, const dataType & ); void deleteNode(); void deleteFirst(); Prof. Amr Goneid, AUC

List Class Header File void deleteEnd(); void makeListEmpty(); bool search (const keyType & ); void orderInsert(const keyType &, const dataType & ); void traverse(); Prof. Amr Goneid, AUC

List Class Header File private: key data next // Node Class class node { public: keyType key; // key dataType data; // Data node *next; // pointer to next node }; // end of class node declaration key data next Prof. Amr Goneid, AUC

List Class Header File typedef node * NodePointer; // Pointers NodePointer head, cursor, prev; }; // End of class List declaration #endif // LIST_H #include "List.cpp" Prof. Amr Goneid, AUC

(c) Implementation Files Full implementation of the Linked List class is found at: http://www.cse.aucegypt.edu/~csci210/codes.zip Prof. Amr Goneid, AUC

(d) Example Application An ordered list of characters and their frequencies in a string: Given a string, build a List of characters and their count in the string. The list is ordered alphabetically on the characters. Prof. Amr Goneid, AUC

Ordered List // File: ListAppl.cpp // Applies List Class: Ordered linked list #include <iostream> #include <string> using namespace std; #include "List.h" int main() { List<char, int> clist; string s; char c; int i, count; bool keyfound; Prof. Amr Goneid, AUC

Ordered List // Read a string cout << "Enter a string:" << endl; getline(cin,s); cout << s << endl; // display it for (i = 0; i < s.length(); i++) // for every character { c = toupper(s.at(i)); // convert to upper case // Search for character in the list keyfound = clist.searchForKey(c); Prof. Amr Goneid, AUC

Ordered List if (keyfound) // if found { clist.retrieveData(count); // get data in node count++; // increment count clist.storeData(count); // store back } // Not found, a new node is inserted else clist.orderInsert(c,1); Prof. Amr Goneid, AUC

Ordered List // print characters and their frequencies clist.traverse(); cout << clist.listSize() << endl; // current list size //clist.makeListEmpty(); // empty list, or clist.~List(); // free memory // the size now should be zero cout << clist.listSize() << endl; return 0; } Prof. Amr Goneid, AUC

Sample Output Enter a string: The Rain in Spain 3 A 2 E 1 H 1 I 3 N 3 10 Press any key to continue Prof. Amr Goneid, AUC

Learn on your own about: Array-based implementation of Linked Lists Prof. Amr Goneid, AUC