Presentation is loading. Please wait.

Presentation is loading. Please wait.

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


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:

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

2 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/

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

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

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

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

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

8 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

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

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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

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

20 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

21 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

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

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

24 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

25 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

26 Prof. Amr Goneid, AUC26 An Illustration

27 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

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

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

30 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

31 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

32 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

33 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

34 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

35 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

36 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

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

38 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

39 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

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

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

42 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


Ads by Google