# Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.

## Presentation on theme: "Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures."— Presentation transcript:

Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures

Prof. Amr Goneid, AUC2 Elementary Data Structures Linked Lists Stacks Queues Helpful Links: Link to programs in the text book: http://www.cise.ufl.edu/~raj/PList.html Link to Data Structures and Algorithms: http://cpp.datastructures.net/presentations/

Prof. Amr Goneid, AUC3 1. The Linked List head NULL cursor First Last prev Current datanext

Prof. Amr Goneid, AUC4 http://www.cosc.canterbury.ac.nz/people/ mukundan/dsal/LinkListAppl.htmlDemo

Prof. Amr Goneid, AUC5 Variations on Linked Lists The Circular List: Notice that tail->next == head head cursor tail

Prof. Amr Goneid, AUC6 Variations on Linked Lists The Doubly Linked List To advance: cursor = cursor->next; To back : cursor = cursor->back; nextback cursor

Prof. Amr Goneid, AUC7 Variations on Linked Lists The Circular Doubly Linked List The 2-D List:

Prof. Amr Goneid, AUC8 2. Stacks  A simple data container consisting of a linear list of elements  Access is by position (order of insertion)  All insertions and deletions are done at one end, called top  Last In First Out (LIFO) structure  Two basic operations: push: add to top pop: remove from top

Prof. Amr Goneid, AUC9 Example top ++toptop push pop top top--

Prof. Amr Goneid, AUC10 http://www.cosc.canterbury.ac.nz/people/ mukundan/dsal/StackAppl.htmlDemo

Prof. Amr Goneid, AUC11 Run-time stack used in function calls Page-visited history in a Web browser Undo sequence in a text editor Removal of recursion Conversion of Infix to Postfix notation Evaluation of Postfix expressions Reversal of sequences Checking for balanced symbols Some Stack Applications

Prof. Amr Goneid, AUC12 Stack Class Operations construct: construct an empty stack stackIsEmpty  bool : return True if stack is empty stackIsFull  bool : return True if stack is full push(el) : add element (el) at the top pop(el): retrieve and remove the top element stackTop(el): retrieve top element without removing it

Prof. Amr Goneid, AUC13 The stack may be implemented as a dynamic array. The capacity (MaxSize) will be input as a parameter to the constructor The stack ADT may be implemented as a template class to allow for different element types. A Stack Class Definition

Prof. Amr Goneid, AUC14 // File: Stackt.h // Stack template class definition. // Dynamic array implementation #ifndef STACKT_H #define STACKT_H template class Stackt { public: Stackt (int nelements = 128);// Constructor Stackt (const Stackt &);// Copy Constructor ~Stackt ();// Destructor A Stack Class Definition

Prof. Amr Goneid, AUC15 // Member Functions void push(Type );// Push void pop(Type &);// Pop void stackTop(Type &) const;// retrieve top bool stackIsEmpty() const;// Test for Empty stack bool stackIsFull() const;// Test for Full stack private: Type *stack;// pointer to dynamic array int top, MaxSize; }; #endif // STACKT_H #include "Stackt.cpp" A Stack Class Definition

Prof. Amr Goneid, AUC16 A stack can also be implemented as a linked structure. Requires more space than array implementations, but more flexible in size. Easy to implement because operations are at the top (in this case the head node) Linked Stacks

Prof. Amr Goneid, AUC17 Node Specification // The linked structure for a node can be // specified as a Class in the private part of // the main stack class. class node// Hidden from user { public: Type e;// stack element node *next;// pointer to next node }; // end of class node declaration typedef node * NodePointer; NodePointer top;// pointer to top

Prof. Amr Goneid, AUC18 Push Operation top First pnew Last New 2 3 push(v): NodePointer pnew = new node ; pnew->e = v; pnew->next = top; top = pnew; 1

Prof. Amr Goneid, AUC19 Pop Operation 2 3 cursor top pop(v): v = top->e; cursor = top; top = top->next; delete cursor; 1

Prof. Amr Goneid, AUC20 // File: StackL.h // Linked List Stack class definition #ifndef STACKL_H #define STACKL_H template class StackL { public: StackL();// Constructor ~StackL();// Destructor void push(Type );// Push void pop(Type &);// Pop Linked Stack Class

Prof. Amr Goneid, AUC21 void stackTop(Type &) const;// retrieve top bool stackIsEmpty() const;// Test for Empty stack private: // Node Class class node { public: Type e;// stack element node *next;// pointer to next node }; // end of class node declaration Linked Stack Class

Prof. Amr Goneid, AUC22 typedef node * NodePointer; NodePointer top;// pointer to top }; #endif // STACKL_H #include "StackL.cpp" Linked Stack Class

Prof. Amr Goneid, AUC23 All operations are O(1) Analysis of Stack Operations

Prof. Amr Goneid, AUC24 The CSCI 321 course web site contains full definitions and implementations of : Stackt template class: stack class with dynamic array implementation StackL template class: stack class with linked implementation Stack Template Classes

Prof. Amr Goneid, AUC25 3. Queues  A simple data container consisting of a linear list of elements  Access is by position (order of insertion)  Insertions at one end (rear), deletions at another end (front)  First In First Out (FIFO) structure  Two basic operations: enqueue: add to rear dequeue: remove from front

Prof. Amr Goneid, AUC26 An Illustration

Prof. Amr Goneid, AUC27 Enqueue and Dequeue o When last array element is reached, we move back to start o The queue is viewed as a circular array o To enqueue: rear = (rear + 1) % size o To dequeue: front = (front + 1) % size o Both rear and front advance clockwise

Prof. Amr Goneid, AUC28 http://www.cosc.canterbury.ac.nz/people/ mukundan/dsal/QueueAppl.htmlDemo

Prof. Amr Goneid, AUC29 Simulation of waiting lines Simulation of serviceable events Job scheduling Input/Output Buffering Multiprogramming Some Queue Applications

Prof. Amr Goneid, AUC30 Queue Class Operations construct: construct an empty queue queueIsEmpty  bool : return True if queue is empty queueIsFull  bool : return True if queue is full enqueue(el) : add element (el) at the rear dequeue(el): retrieve and remove the front element queueFront(el): retrieve front without removing it queueLength  int : return the current queue length

Prof. Amr Goneid, AUC31 The queue may be implemented as a dynamic array. The capacity (MaxSize) will be input as a parameter to the constructor. The queue ADT may be implemented as a template class to allow for different element types. A Queue Class Definition

Prof. Amr Goneid, AUC32 // File: Queuet.h // Queue template class definition // Dynamic array implementation #ifndef QUEUET_H #define QUEUET_H template class Queuet { public: Queuet (int nelements = 128);// Constructor Queuet (const Queuet &);// Copy Constructor ~Queuet ();// Destructor A Queue Class Definition

Prof. Amr Goneid, AUC33 // Member Functions void enqueue(Type );// Add to rear void dequeue(Type &);// Remove from front void queueFront(Type &) const;// Retrieve front bool queueIsEmpty() const;// Test for Empty queue bool queueIsFull() const;// Test for Full queue int queueLength() const;// Queue Length private: Type *queue;// pointer to dynamic array int front, rear, count, MaxSize; }; #endif // QUEUET_H #include "Queuet.cpp" A Queue Class Definition

Prof. Amr Goneid, AUC34 A Queue can be implemented as a linked structure. Requires more space than array implementations, but more flexible in size. Two pointers are needed: front for dequeue and rear for enqueue Linked Queues

Prof. Amr Goneid, AUC35 Node Specification // The linked structure for a node can be // specified as a Class in the private part of // the main queue class. class node// Hidden from user { public: Type e;// stack element node *next;// pointer to next node }; // end of class node declaration typedef node * NodePointer; NodePointer front, rear;// pointers

Prof. Amr Goneid, AUC36 Enqueue Operation pnew New enqueue(v): NodePointer pnew = new node; pnew->e = v; pnew->next = NULL; rear->next = pnew; rear = pnew; rear 1 2 3 front

Prof. Amr Goneid, AUC37 Dequeue Operation 2 3 cursor front dequeue(v): v = front->e; cursor = front; front = front->next; delete cursor; 1 rear

Prof. Amr Goneid, AUC38 // File: QueueL.h // Linked List Queue class definition #ifndef QUEUEL_H #define QUEUEL_H template class QueueL { public: QueueL(); // Constructor ~QueueL(); // Destructor void enqueue(Type ); // Add to rear Linked Queue Class

Prof. Amr Goneid, AUC39 void dequeue(Type &); // Remove from front void queueFront(Type &) const;// retrieve front bool queueIsEmpty() const;// Test for Empty queue int queueLength() const;// Queue Length private: // Node Class class node { public: Type e;// queue element node *next;// pointer to next node }; // end of class node declaration Linked Queue Class

Prof. Amr Goneid, AUC40 typedef node * NodePointer; NodePointer front, rear;// Pointers int count;// length }; #endif // QUEUEL_H #include "QueueL.cpp" Linked Queue Class

Prof. Amr Goneid, AUC41 All operations are O(1) Analysis of Queue Operations

Prof. Amr Goneid, AUC42 The CSCI 321 course web site contains full definitions and implementations of : Queuet template class: queue class with dynamic array implementation QueueL template class: queue class with linked implementation Queue Template Classes

Download ppt "Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures."

Similar presentations