11 views

Uploaded by Sandeep Torvi

You are on page 1of 50

Purpose

Primitive DT:

programmer

ADT:

progra ammer

Interface (API)

data

Cpt S 223. School of EECS, WSU 3

Abstract in that implementation of operations not specified Abstract in ADT definition E.g., List Insert, delete, search, sort

C++ class perfect for ADTs Can change ADT implementation details without breaking code using ADT

List of size N: A0, A1, , AN-1 Each element Ak has a unique position k in the list Elements can be arbitrarily complex Operations

Cpt S 223. School of EECS, WSU 5

Stack ADT

Stack is a list where insert and remove take place only at the top Operations

Push ( se ) e e e o top o us (insert) element on op of stack Pop (remove) element from top of stack p Top: return element at top of stack

Cpt S 223. School of EECS, WSU 6

Queue ADT

Queue is a list where insert takes place at the back, but remove takes place at the front Operations

Enqueue (insert) element at the back of the queue Dequeue (remove and return) element from the front of the queue FIFO (First In First Out)

5 7 2 6 3 2 8 back

Cpt S 223. School of EECS, WSU 7

Enque here

A0 A1 A2 A3

Operations

insert(X,k) O(N) remove(k) O(N) find(X) O(N) findKth(k) O(1) ( ) ( ) printList() O(N)

8

Linked Lists

Elements not stored in contiguous memory Nodes in list consist of data element and next pointer

node

data

next

pointer

Cpt S 223. School of EECS, WSU

null

9

Linked Lists

Operations

Remove(A) O(1)

Cpt S 223. School of EECS, WSU 10

Linked Lists

Operations

11

Doubly-Linked List

Singly-linked list

insert(X,A) insert(X A) and remove(X) require pointer to node just before X Also keep pointer to previous node

prev

next

12

Doubly-Linked List

Insert(X,A)

newA = new Node(A); newA->prev = X->prev; newA->next = X; X->prev->next = newA; X->prev = newA; X->prev->next = X->next; X->next->prev = X->prev;

Remove(X) ( )

Cpt S 223. School of EECS, WSU 13

Sentinel Nodes

Dummy head and tail nodes to avoid special cases at ends of list Doubly-linked Doubly linked list with sentinel nodes

14

List, stack, queue, Generally called containers www.sgi.com/tech/stl/ www sgi com/tech/stl/ www.cppreference.com/cppstl.html

Cpt S 223. School of EECS, WSU 15

WWW resources

vector<Object>

list<Object>

Doubly-linked list with sentinel nodes findKth O(N) insert and remove O(1)

Cpt S 223. School of EECS, WSU 16

Container Methods

Return number of elements in container Remove all elements from container Return true is container has no elements, otherwise returns false

Cpt S 223. School of EECS, WSU 17

void clear()

bool empty()

Add x to end of list Remove object at end of list Return object at end of list Return object at front of list

Cpt S 223. School of EECS, WSU 18

void pop_back ()

List-only Methods

void pop_front ()

19

Vector-only Methods

Return object at index idx in vector with no bounds-checking Return object at index idx in vector with boundschecking Return internal capacity of vector Set new capacity for vector (avoid expansion)

Cpt S 223. School of EECS, WSU

20

Iterators

iterator begin ()

Return appropriate iterator representing first item in container Return appropriate iterator representing end marker in container Position after last item in container

Cpt S 223. School of EECS, WSU 21

iterator end ()

Iterator Methods

Advance iterator itr to next location Return reference to object stored at iterator itrs location Return true if itr1 and itr2 refer to same location; otherwise return false Return true if itr1 and itr2 refer to different locations; otherwise return false

Cpt S 223. School of EECS, WSU 22

*itr

itr1 == itr2

itr1 != itr2 it 1 ! it 2

Example: printList

template <typename Container> void printList (const Container & lst) { for (typename Container::const_iterator itr = lst.begin(); itr != lst.end(); ++itr) { cout << *itr << endl; } }

23

Constant Iterators

iterator begin () const_iterator begin () const g iterator end () co st_ te ato end const iterator e d () co st const Appropriate version above returned based on whether container is const If const_iterator used, then *itr pp cannot appear on left-hand side of assignment (e.g., *itr=0 )

Cpt S 223. School of EECS, WSU 24

Better printList

25

Add x into list, prior to position given by iterator pos Return iterator representing position of inserted item O(1) for lists, O(N) for vectors Remove object whose position is given by iterator pos Return iterator representing position of item f ll f following pos This operation invalidates pos O(1) for lists, O(N) for vectors Remove all items beginning at position start, up to, but not including end

26

Implementation of Vector

constructor copy constructor destructor operator=

27

Implementation of Vector

Automatic resize

28

Implementation of Vector

Iterators (implemented using simple pointers)

Iterator methods

29

Implementation of List

30

Implementation of List

31

Implementation of List

Empty list

32

Implementation of List

33

Implementation of List

34

Implementation of List

35

Implementation of List

36

Implementation of List

37

Stack ADT

Stack is a list where insert and remove take place only at the top Operations

Push ( se ) e e e o top o us (insert) element on op of stack Pop (remove) element from top of stack p Top: return element at top of stack

Cpt S 223. School of EECS, WSU 38

Stack Implementation

Linked List template <typename Object> class stack { public: stack () {} void push (Object & x) { ? s.push_front (x); } void pop () { ? s.pop_front (); } Object & top () { ? s.front (); } private: list<Object> s; } Vector template <typename Object> class stack { public: stack () {} void push (Object & x) { s.push_back (x); } ? void pop () ? { s.pop_back (); } Object & top () ? { s.back (); } private: vector<Object> s; }

39

Stack Implementation

Linked List template <typename Object> class stack { public: stack () {} void push (Object & x) { ? s.push_front (x); } void pop () { ? s.pop_front (); } Object & top () { ? s.front (); } private: list<Object> s; } Vector template <typename Object> class stack { public: stack () {} void push (Object & x) { s.push_back (x); } void pop () { s.pop_back (); } Object & top () { s.back (); } private: vector<Object> s; }

40

Stack Implementation

Linked List template <typename Object> class stack { public: stack () {} void push (Object & x) { s.push_front (x); } void pop () { s.pop_front (); } Object & top () { s.front (); } private: list<Object> s; } Vector template <typename Object> class stack { public: stack () {} void push (Object & x) { s.push_back (x); } void pop () { s.pop_back (); } Object & top () { s.back (); } private: vector<Object> s; }

41

Methods

#include <stack> stack<int> s; for (int i = 0; i < 5; i++ ) { s.push(i); } while (!s.empty()) { cout << s.top() << endl; s.pop(); }

42

Stack Applications

stack<char> s; ; while not end of file { read character c if c = ( then s.push(c) if c = ) then if s.empty() then error else s.pop() } if (! s.empty()) s empty()) then error else okay

43

Stack Applications

Postfix expressions

Class PostFixCalculator { Means ((1 * 2) + 3) + (4 * 5) public: HP calculators ... void Multiply () Unambiguous (no need for { paranthesis) int i1 = s.top(); p() Infix needs paranthesis or s.pop(); else implicit precedence int i2 = s.top(); specification to avoid ambiguity s.pop(); s.push s push (i1 * i2); E g E.g., try a+(b*c) and (a+b)*c } private: stack<int> s; Postfix evaluation uses stack }

12*3+45*+

44

Stack Applications

Function calls Programming languages use stacks to keep g g g g p track of function calls When a function call occurs

Push CPU registers and program counter on to stack (activation record or stack frame) Upon return, restore registers and program d counter from top stack frame and pop

45

Queue ADT

Queue is a list where insert takes place at the back, but remove takes place at the front Operations

Enqueue (insert) element at the back of the queue Dequeue (remove and return) element from the front of the queue FIFO (First In First Out)

5 7 2 6 3 2 8 back

Cpt S 223. School of EECS, WSU 46

Enque here

Queue Implementation

Linked List Li k d Li t template <typename Object> class queue { public: How would the runtime queue () {} change if vector is used void enqueue (Object & x) in implementation? { q.push_back (x); } Object & dequeue () { Object & x = q front (); q.front q.pop_front (); return x; } g Running time ? private: list<Object> q; } Cpt S 223. School of EECS, WSU 47

Methods

#include <queue> queue<int> q; for (int i = 0; i < 5; i++ ) { q.push(i); } while (!q.empty()) { cout << q.front() << endl; q.pop(); }

Push (at back) Pop (from front) Back, Back front Empty, size

48

Queue Applications

49

Summary

C++ Standard Template Library (STL) Numerous applications Building blocks for more complex data structures

Cpt S 223. School of EECS, WSU 50

- Logistic Cockpit.docxUploaded byMani Kandan
- CSS 221 Lecture 1Uploaded byFaizan Khan
- bfsUploaded bymanishbhardwaj8131
- Chap_9Lesson12EmsysNewVxWorksQueue.pdfUploaded byVijay M
- AbstractDataTypes.pdfUploaded byp_simi20039558
- A2Uploaded byPrateek Sharma
- C Lab 2012Uploaded bysudeepc2011
- Csharp ProgrammingUploaded bypriyanka_avaru
- Stack QueueUploaded bymanishbhardwaj8131
- DequeUploaded byuranidioticstupid
- C++Queue17sep.pptxUploaded byVinod Srivastava
- DSUploaded byAravind Seetharaman
- D35 Data Structure2012Uploaded byPonmalar Sivaraj
- Linked List GraphicsUploaded byAjay Kumar
- teramotorserviceUploaded byapi-384495602
- CSE3214_03_QueueingFundamentalsUploaded byBretuoba Nana Yaw Yeboah
- sem2Uploaded bybrainretain123
- 6,13Uploaded byapi-3728229
- SAC Tutorial 6Uploaded byvelkus2013
- C++ 2067Uploaded bySusant Dotel
- C PROGRAM FILE.docxUploaded byAfraz Khan
- APC 03 04 ObjectOrientationUploaded byNguyen Dinh Hung
- AssemblyUploaded bysap2734
- chapter_04.pdfUploaded byJohn Yang
- topic2-lec4Uploaded bySanthosh Mamidala
- iterUploaded byWorse To Worst Satittamajitra
- Linux601-700Uploaded bysuresh
- Introduction to C-SharpUploaded bySơn Trố
- 4-pqUploaded byΓλυκύς Ιησούς
- Lab 10 SolutionUploaded byJyz1

- OOP_C++_CECIUploaded byAbiyot Aderie
- Tutorial.cattellUploaded byTes Meko
- 051101 Spring-transcations Jho EnUploaded byapi-3748544
- C++ MultithreadingUploaded bysank_prasad
- Basic PythonUploaded byHumberto
- System Ver i Log Interview QuestionsUploaded byKesava Ram
- C++_STLUploaded bySartaj Singh
- OOP ExamplesUploaded byAdam Butterworth
- C++ solutionsUploaded byRodney Lucas
- 4 Plus 1Uploaded byIzon Thomaz Mielke
- Interview Questions for FreshersUploaded byswapnilsk25
- uml-unit3.pdfUploaded bybhargavi
- MC0066 CompleteUploaded byMoumita Mondal
- C# and .NET by ExampleUploaded byNaveena Sivamani
- 15740 c Program on Sys Call of LinuxUploaded byRishab Agrawal
- Airline Ticket ManagementUploaded bytkgupta
- Http Www.tutorialscity.com 2017-01-30 _uUploaded byJigar Shah
- Vb .nET CourseUploaded bymanishakhaneja28
- Inner ClassTest1Uploaded byMedlemineMedsaleh
- Did You Know SAP Has One of the WorldUploaded bySoumyaRanjanPattanaik
- UNIT IUploaded byapjkalam1
- k12 Software DevplusanimationUploaded bybronxkie
- c# InterviewsUploaded byrelekarsushant
- SRSUploaded byHeenaAggarwal
- java-testUploaded byV
- Synon BasicUploaded bySagar Palnati
- C7 .pdfUploaded byVicente J Sandoval G
- Mca Syllabus2018 2019Uploaded byekamkohli
- crash_20170830_125041Uploaded bySteven
- Viva QuestionUploaded bySamit Dey