Lists, Stacks, and Queues

Size: px
Start display at page:

Download "Lists, Stacks, and Queues"

Transcription

1 Data Structure Chapter 4 Lists, Stacks, and Queues Dr. Patrick Chan School of Computer Science and Engineering South China University of Technology Outline Lists (Ch 4.1) Introduction (4.1) Array-based List (4.1.1) Linked List (4.1.2) Comparison (4.1.3) Doubly Linked List (4.1.5) Stacks (Ch 4.3) Array-based Stack (Ch 4.3.1) Linked Stack (Ch 4.3.2) Queues (Ch 4.4) Array-based Queue (Ch 4.4.1) Linked Queue (Ch 4.4.2) Dictionary Example (Ch 4.2) 2

2 Lists: Introduction In real life, many data are finite and in order For example: Bank accounts Student records Application Example Simple Grading System Store the score for each student A List can be used Student I II III IV V Score Lists: Introduction A list of size n a 0 a 1 a 2 a n-1 Head element 3 rd element Tail Each element has a data type (atomic or structure) Empty list contains no elements The length is the number of elements stored The beginning of the list is called the The end of the list is called the 4

3 Lists: Introduction Sorted lists Elements positioned in ascending/descending order of value Unsorted lists No necessary relationship between element values and positions 5 Lists: Introduction Example: Simple Grading System 99 Student I II III IV V Score Head Tail 99 6

4 Lists: Introduction Functions: Get Value Insert / Modify / Delete Search 7 Lists: ADT class List { public: // reinitialize the list virtual void clear() = 0; // insert an item into the list before the current item virtual bool insert(const Elem&) = 0; // insert an item into the list at the end of the list virtual bool append(const Elem&) = 0; // remove the item at the current virtual bool remove(elem&) = 0; //set current to the beginning virtual void setstart() = 0; // set current to the last virtual void setend() = 0; 8

5 Lists: ADT // set current to the previous item virtual void prev() = 0; // set current to the next item virtual void next() = 0; // the size of the list before the current virtual int leftlength() const = 0; // the size of the list after and including the current virtual int rightlength() const = 0; // set current to the position indicated by the argument virtual bool setpos(int pos) = 0; // return the value of the current item in the argument virtual bool getvalue(elem&) const = 0; // print out the items in the list virtual void print() const = 0; ; 9 Lists Lists can be implemented by different methods (Physical Form) Array-Based List Linked List (pointer)

6 Lists: Array-Based List // Array-based list class AList : public List <Elem> { private: int ; // Maximum size of list int ; // Actual elem count int ; // Position of Elem* listarray; // Array holding list public: AList(int size=defaultlistsize) { = size; = = 0; listarray = new Elem[]; ~AList() { delete [] listarray; Alist grade(6); 11 Lists: Array-Based List Insert Function // Insert at front of right partition bool AList<Elem>::insert(const Elem& item) { if ( == ) return false; for(int i=; i>; i--) // Shift Elems up to make room listarray[i] = listarray[i-1]; listarray[] = item; ++; // Increment list size grade.ins ert( ); 12

7 Lists: Array-Based List Insert Function // Insert at front of right partition bool AList<Elem>::insert(const Elem& item) { if ( == ) return false; for(int i=; i>; i--) // Shift Elems up to make room listarray[i] = listarray[i-1]; listarray[] = item; ++; // Increment list size grade.ins ert( ); grade.ins ert( ); 13 Lists: Array-Based List Insert Function // Insert at front of right partition bool AList<Elem>::insert(const Elem& item) { if ( == ) return false; for(int i=; i>; i--) // Shift Elems up to make room listarray[i] = listarray[i-1]; listarray[] = item; ++; // Increment list size grade.ins ert( ); grade.ins ert( ); grade.ins ert( ); 14

8 Lists: Array-Based List Set Position Function bool AList<Elem>::setPos(int pos) { if ((pos >= 0) && (pos <= )) = pos; return (pos >= 0) && (pos <= ); grade.set Pos(2); 15 Lists: Array-Based List Get Value Function int AList<Elem>::righ tlen gth() const { return - ; int AList<Elem>::left Leng th() const { return ; bool ALis t<elem>::getvalue(elem& it) const { if (rightlength() == 0) return false; else { it = listarray[]; mark = grade.getvalue(mark); 16

9 Lists: Array-Based List Append Function bool AList<Elem>::append(const Elem& item) { if ( == ) return false; listarray[++] = item; grade.app end( ); 17 Lists: Array-Based List Append Function AList<Elem>::append(const Elem& item) { if ( == ) return false; listarray[++] = item; grade.app end( ); grade.app end( ); 18

10 Lists: Array-Based List Remove Function // Remove and return first Elem in right partition bool AList<Elem>::remove(Elem& it) { if (rightlength() == 0) return false; it = listarray[]; // Copy Elem for(int i=; i<-1; i++) listarray[i] = listarray[i+1]; // Shift them down --; // Decrement size grade.rem ove( mark); mark = 19 Lists: Array-Based List Other Functions void AList<Elem>::setStart() { = 0; void AList<Elem>:: setend() { = ; void AList<Elem>:: prev() { if (!= 0) --; void AList<Elem>:: next() { if ( <= -1) ++; grade.setstart(); 20

11 Lists: Array-Based List Other Functions void AList<Elem>::setStart() { = 0; void AList<Elem>:: setend() { = ; void AList<Elem>:: prev() { if (!= 0) --; void AList<Elem>:: next() { if ( <= -1) ++; grade.setstart(); grade.setend(); 21 Lists: Array-Based List Other Functions void AList<Elem>::setStart() { = 0; void AList<Elem>:: setend() { = ; void AList<Elem>:: prev() { if (!= 0) --; void AList<Elem>:: next() { if ( <= -1) ++; grade.setstart(); grade.setend(); grade.prev(); 22

12 Lists: Array-Based List Other Functions void AList<Elem>::setStart() { = 0; void AList<Elem>:: setend() { = ; void AList<Elem>:: prev() { if (!= 0) --; void AList<Elem>:: next() { if ( <= -1) ++; grade.setstart(); grade.setend(); grade.prev(); grade.next(); 23 Lists: Array-Based List Print Function void AList<Elem>:: print() const { int temp = 0; cout << < ; while (temp < ) cout << listarray[temp++] << ; cout << ; while (temp < ) cout << listarray[temp++] << ; cout << >\n ; grade.print(); < > 24

13 Lists: Array-Based List Print Function void AList<Elem>:: print() const { int temp = 0; cout << < ; while (temp < ) cout << listarray[temp++] << ; cout << ; while (temp < ) cout << listarray[temp++] << ; cout << >\n ; grade.print(); < > 25 Small Exercise AList height(4); height.append(163); height.append(1); height.setpos(1); height.insert(180); height.prev(); height.remove(temp);

14 Small Exercise We would like to implement to a sorted list (ascending order) by modifying the current list data type Q1: List all functions the list data type has. Which functions should be modified or deleted in sorted list data type? clear insert append remove setstart setend Prev Next setpos leftlength rightlenght getvalue print 27 Small Exercise Q2: Rewrite the insert function for sorted list data type. // Array-based list class AList : public List<Elem> { priva te: int ; // Maximum size of list int ; // Actual elem count int ; // Position of Elem* l ista rray; // Array holding list publi c: ALi st(i nt size=defaultlistsize) { = size; = = 0; listarray = new Elem[]; ~AList() { delete [] listarray; bool ALis t<elem>::insert( cons t Elem& item) {. 28

15 Small Exercise bool AList<Elem>::insert(const Elem& item) { if ( == ) return false; suitpos = 0; while ((item>listarray[suitpos])&&(suitpos < )) suitpos++; for(int i=; i>; suitpos; i--) i--) // Shift Elems up to make room listarray[i] = listarray[i-1]; listarray[] suitpos] = = item; item; ++; // Increment list size slist.insert(34); 29 Lists: Array-Based List Pros and Cons Advantages: No over if all array positions are full Over: Amount of space necessary to maintain the data structure Disadvantages: Array must be allocated in advance Too many: Waste Memory Linked List can solve this problem 30

16 Lists: Linked List A node in a linked list is defined as: // Linked list node class Link { public: Elem element; // Value for this node Link *next; // Pointer to next node Link(const Elem& elemval, Link* nextval=){ element = elemval; next = nextval; Link(Link* nextval=){ next = nextval; ; Link temp; Link temp(); element next 31 Lists: Linked List // Linked list implementation class LList : public List<Elem> { priva te: Link<Elem>* ; //Point to list er Link<Elem>* ; //Pointer to last Elem Link<Elem>* ; //Last element on left int lef tcnt; //Size of left int rightcnt; //Size of right void in it() { //Initia lization routine = = = new Link<Elem>; leftcnt = rightcnt = 0; void removeall() { //Remove link nodes to free store while(hea d!= ) { = ; = ->next; delete ; LList Grade; Grade.init(); leftcnt = 0 rightcnt = 0 32

17 Lists: Linked List Link(const Elem& elemval, Link* nextval=){ element = elemval; next = nextval; Insert Function Link<Elem>* temp = new Link<Elem>(item, ->next); // Insert at front of right partition ->next = temp; bool LList<Elem>::insert(const Elem& item) { ->next = new Link<Elem>(item, ->next); if ( == ) = ->next; rightcnt++; grade.ins ert( ); leftcnt = 0 rightcnt = Lists: Linked List Insert Function // Insert at front of right partition bool LList<Elem>::insert(const Elem& item) { ->next = new Link<Elem>(item, ->next); if ( == ) = ->next; rightcnt++; grade.ins ert( ); grade.ins ert( ); Link<Elem>* temp = new Link<Elem>(item, ->next); ->next = temp; leftcnt = 0 rightcnt = 12 34

18 Lists: Linked List Insert Function // Insert at front of right partition bool LList<Elem>::insert(const Elem& item) { ->next = new Link<Elem>(item, ->next); if ( == ) = ->next; rightcnt++; grade.ins ert( ); grade.ins ert( ); grade.ins ert( ); leftcnt = 0 rightcnt = Lists: Linked List Set Position Function // Set the size of left partition to pos bool LList<Elem>::setPos(int pos) { if ((pos < 0) (pos > rightcnt+leftcnt)) return false; = ; for(int i=0; i<pos; i++) = ->next; rightcnt = rightcnt+leftcnt-pos; leftcnt = pos; grade.set Pos(3); leftcnt = 03 rightcnt = 30 36

19 Lists: Linked List Set Position Function // Set the size of left partition to pos bool LList<Elem>::setPos(int pos) { if ((pos < 0) (pos > rightcnt+leftcnt)) return false; = ; for(int i=0; i<pos; i++) = ->next; rightcnt = rightcnt+leftcnt-pos; leftcnt = pos; grade.set Pos(3); grade.set Pos(2); leftcnt = 32 rightcnt = Lists: Linked List Get Value Function int LList<Elem>:: rightlength() const { return rightcnt; int LList<Elem>:: leftlength() const { return leftcnt; bool LList<Elem>:: getvalue(elem& it) const { if(rightlength() == 0) return false; it = ->next->element; grade.getvalue(mark); mark = leftcnt = 2 rightcnt = 1 38

20 Lists: Linked List Append Function // Append Elem to end of the list bool LList<Elem>::append(const Elem& item) { = ->next = new Link<Elem>(item, ); rightcnt++; grade.app end( ); leftcnt = 2 rightcnt = Lists: Linked List Append Function // Append Elem to end of the list bool LList<Elem>::append(const Elem& item) { = ->next = new Link<Elem>(item, ); rightcnt++; grade.app end( ); grade.app end( ); leftcnt = 2 rightcnt = 23 40

21 Lists: Linked List Remove Function // Remove and return first Elem in right partition bool LList<Elem>::remove(Elem& it) { if (->next == ) return false; it = ->next->element; // Remember val Link<Elem>* ltemp = ->next; // Remember link node ->next = ltemp->next; // Remove if ( == ltemp) // Reset mark = = ; grade.rem ove( mark); delete ltemp; // Reclaim space rightcnt--; leftcnt = 2 rightcnt = Lists: Linked List Set Start and Set End Functions void LList<Elem>:: setstart() { = ; rightcnt += leftcnt; leftcnt = 0; grade.setstart(); void LList<Elem>:: setend() { = ; leftcnt += rightcnt ; rightcnt = 0; leftcnt = 20 rightcnt = 24 42

22 Lists: Linked List Set Start and Set End Functions void LList<Elem>:: setstart() { = ; rightcnt += leftcnt; leftcnt = 0; void LList<Elem>:: setend() { = ; leftcnt += rightcnt ; rightcnt = 0; grade.setstart(); grade.setend(); leftcnt = 04 rightcnt = Lists: Linked List Previous Functions void LList<Elem>:: prev() { Link<Elem>* temp = ; if ( == ) return; // No prev Elem while (temp->next!=) temp=temp->next; = temp; leftcnt--; grade.prev(); rightcnt++; leftcnt = 43 rightcnt = 01 44

23 Lists: Linked List Next Functions void LList<Elem>:: next() { if ( == ) return; // No next Elem = ->next; leftcnt++; rightcnt--; grade.next(); leftcnt = 34 rightcnt = 45 Lists: Linked List Print Functions grade.print(); < > void LList<Elem>:: print() const { Link<Elem>* temp = ; cout << < ; while (temp!= ) { cout << temp->next->element << ; temp = temp->next; cout << ; while (temp->next!= ) { cout << temp->next->element << ; temp = temp->next; cout << >\n ; leftcnt = 4 rightcnt = 0 46

24 Lists: Linked List Print Functions grade.print(); < > void LList<Elem>:: print() const { Link<Elem>* temp = ; cout << < ; while (temp!= ) { cout << temp->next->element << ; temp = temp->next; cout << ; while (temp->next!= ) { cout << temp->next->element << ; temp = temp->next; cout << >\n ; leftcnt = 2 rightcnt = 2 47 Small Exercise LList height; height.append(163); height.append(1); height.setpos(1); height.insert(180); height.prev(); height.remove(temp);

25 Small Exercise Q1: Write a swap function to swap two adjacent elements (->next, ->next->next) of a linked list by adjusting only the pointers 49 Small Exercise 50

26 Small Exercise bool LList<Elem>:: swap() { if ( == ) return false; if (->next == ) return false; Link<Elem>* temp = ->next; ->next = temp->next; temp->next = ->next->next; ->next->next = temp; if (->next == ) = ->next; 51 Small Exercise Q2: Write a findndel function which deletes all the occurrence of a given ITEM in a linked list (Do no use remove() function) 52

27 Small Exercise bool LList<Elem>:: findndel(elem value) { if ( == ) return false; Link<Elem>* temp; Link<Elem>* ptr = ; bool beforefence = true; while (ptr <> ) { // check if the ptr passes by if (ptr == ->next) beforefence = false; if (ptr->next->element == value) { // check the list if ( == ptr->next) = ptr; if ( == ptr->next) = ptr; temp = ptr->next; ptr->next = ptr->next->next; delete temp; if (beforefence) leftcnt--; else rightcnt--; else ptr = ptr->next; 53 Lists: Comparison Array-Based Lists Insertion and deletion are Θ(n) Prev and direct access are Θ(1) Array must be allocated in advance No over if all array positions are full Linked Lists Insertion and deletion are Θ(1) Prev and direct access are Θ(n) Space grows with number of elements Every element requires over 54

28 Lists: Comparison Memory Space Array-Based Lists Linked Lists DE n(e + P) E: P: D: n: Space for data value Space for pointer Number of elements in array Number of current elements 55 Lists Singly Linked List Allows for direct access from a list node only to the next node in the list It is not convenient to access the previous element (Θ(n)) Doubly Linked List Allow convenient access the next node and also the previous node (Θ(1)) 56

29 Lists: Doubly Linked List // Doubly-linked list link node class Link { public: Elem element; // Value for this node Link *next; // Pointer to next node Link *prev; // Pointer to previous node Link(const Elem& e, Link* prevp=, Link* nextp=) { element=e; prev=prevp; next=nextp; Link(Link* prevp=, Link* nextp=) { prev=prevp; next=nextp; ; element Link temp; prev next 57 Lists: Doubly Linked List Insert Function // Insert at front of right partition bool LList<Elem>::insert(const Elem& item) { ->next = new Link<Elem>(item,, ->next); if (->next->next!= ) ->next->next->prev = ->next; if ( == ) // Appending new Elem = ->next; // Set rightcnt++; // Added to right grade.ins ert( ); 58

30 Lists: Doubly Linked List Insert Function // Insert at front of right partition bool LList<Elem>::insert(const Elem& item) { ->next = new Link<Elem>(item,, ->next); if (->next->next!= ) ->next->next->prev = ->next; if ( == ) // Appending new Elem = ->next; // Set rightcnt++; // Added to right grade.ins ert( ); grade.ins ert( ); 59 Lists: Doubly Linked List Remove Function // Remove, return first Elem in right part bool LList<Elem>::remove(Elem& it) { if (->next == ) return false; it = ->next->element; Link<Elem>* ltemp = ->next; if (ltemp->next!= ) ltemp->next->prev = ; else = ; // Reset ->next = ltemp->next; // Remove delete ltemp; // Reclaim space rightcnt--; // Removed from right grade.rem ove( mark); mark = 60

31 Lists: Doubly Linked List Remove Function void LList<Elem>::prev() { // no prev Elem if (!= ) { = ->prev; leftcnt--; rightcnt++; Doubly Linked List grade.prev(); void LList<Elem>::prev() { Link<Elem>* temp = ; // No prev Elem if ( == ) return; while (temp->next!=) temp=temp->next; = temp; leftcnt--; rightcnt++; Singly Linked List Stacks Restricted form of a List Insert only at the front Remove from the front TOP First-In-Last-Out (FILO) Notation Insert: PUSH Remove: POP The accessible element is called TOP 62

32 Stacks class AStac k: public St ack<elem> { priva te: int max Size; // Maximum size of stack int top; // Index for top element Elem *l ista rray; // Array holding stack elements publi c: AStack(int size =De faultlistsiz e) // Constructor { = size; top = 0; listarray = new Elem[size]; ~AStack() { delete [] listarray; // Destructor ; class LStac k: public St ack<elem> { priva te: Link<Elem>* top; // Pointer to first element int size; // Number o f elements publi c: LStack(int sz =DefaultListSize) { top = ; size = 0; ~LStack() { clear(); // Destructor Array-based Stack top=0 top Linked Stack 63 Stacks: Push Function Array-based Stack bool AStac k<elem>::push(const Elem& it) { // Put it on stack if (top == ) return false; // Stack is full else { listarray[top++] = it; bool LStack<Elem>:: push(const Elem& it) { // Put it on stack top = new Link<Elem>(it, top); size++; Linked Stack 42 top top= stack.push(); stack.push(42); 64

33 Stacks: Pop Function bool AStack<Elem>:: bool pop(elem& it) {// Pop top element if (top == 0) return false; else { it = listarray[--top]; Array-based Stack Linked Stack bool LStack<Elem>:: bool pop(elem& it) { // Remove it from stack if (size == 0) return false; it = top->element; Link<Elem>* ltemp = t op->next; delete top; top = ltemp; size--; 42 top= 12 value=42 stack.pop(val ue); value=42 top 65 Queues Restricted form of a list Insert at one end Remove from the other end First-In-First-Out (FIFO) Notation Insert: Enqueue Delete: Dequeue First Element: FRONT Last Element: REAR REAR Dequeue Enqueue FRONT 66

34 Queues class AQueu e: public Qu eue<elem> { priva te: int size; // Maximum size of queue int front; // Index of front element int rear; // Index of rear element Elem *l ista rray; // Array holding queue elements publi c: AQueue(int sz =DefaultListSize) { // Constructor // Make list array one position larger for empty slot size = sz+1; rear = 0; front = 1; listarray = new Elem[size]; Array-based Queue ~AQueue() { delete [] listarray; // Destructor rear front 67 Queues class LQueu e: public Qu eue<elem> { priva te: Link<Elem>* front; // Pointer to front queue node Link<Elem>* rear; // Pointer to rear queue node int size; // Number of elements in queue publi c: LQueue(int sz =DefaultListSize) // Constructor { front = ; rear = ; size = 0; Linked Queue ~LQueue() { clear(); // Destructor rear front 68

35 Queues: Enqueue Function Array-based Queue bool AQue ue <Elem>::enqueue(c onst Elem& it) { // Put element on rear if (rear == size) return false; // Full rear = rear+1; // increment listarray[rear] = it; Linked Queue bool LQue ue <Elem>::enqueue(c onst Elem& it) { // Put element on rear if (rear == ) // Empty queue front = rear = new Link<Elem>(it, ); else { // Append new node rear->next = new Link<Elem>(it, ); rear = rear->next; queue.enqueue(); size++; queue.enqueue(42); rear 42 front 42 front rear 69 Queues: Dequeue Function bool AQue ue <Elem>::dequeue(E lem& it) { // Take element out if (length() == 0) return false; // Empty it = listarray[front]; front = front+1; // Circular increment bool LQue ue <Elem>::dequeue(E lem& it) { // Remove element from front if (size == 0) return false; // Empty it = front->element; // Store dequeued valu e Link<Elem>* ltemp = front; // Hold dequeued link front = front->next; // Advance front delet e lt emp; // Delete link if (front == ) rear = ; // Dequeued last element size --; // Return element value queue.dequeue(value); rear Array-based Queue rear Linked Queue front value = value = front 70

36 Queues A problem of Array-based Queue front rear After some operations rear After some operations front rear The queue is FULL although some memory spaces are available! front 71 Queues Solution: rear front size-1 Circular Queue Pretend the array is circular size - 1 is defined to immediately precede position 0 0 rear size front

37 Queues: Circular Queue bool LQueue <Elem>:: enqueue(const Elem& it){ if (((rear+2) % size) == front) return false; // Full rear = (rear+1) % size; // Circular increment listarray[rear] = it; bool LQueue <Elem>:: dequeue(elem& it) { if (length() == 0) return false; // Empty it = listarray[front]; front = (front+1) % size; // Circular increment

Lists, Stacks, and Queues

Lists, Stacks, and Queues Data Structure Chapter 4 Lists, Stacks, and Queues Dr. Patrick Chan School of Computer Science and Engineering South China University of Technology Outline Lists (Ch 4.1) Introduction (4.1) Array-based

More information

Data Structures. Jia-Liang Lu. Slides based on the course notes of C.A. Shaffer

Data Structures. Jia-Liang Lu. Slides based on the course notes of C.A. Shaffer Data Structures Jia-Liang Lu Slides based on the course notes of C.A. Shaffer Part II: Fundamental Data Structure Chapter 4: Lists, Stacks, and Queues Lists A list is a finite, ordered sequence of data

More information

CS132 Algorithm. Instructor: Jialiang Lu Office: Information Center 703

CS132 Algorithm. Instructor: Jialiang Lu   Office: Information Center 703 CS132 Algorithm Instructor: Jialiang Lu Email: jialiang.lu@sjtu.edu.cn Office: Information Center 703 Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position.

More information

Data Structures and Algorithm Analysis Edition 3.2 (C++ Version)

Data Structures and Algorithm Analysis Edition 3.2 (C++ Version) Data Structures and Algorithm Analysis Edition 3.2 (C++ Version) Clifford A. Shaffer Department of Computer Science Virginia Tech Blacksburg, VA 24061 March 28, 2013 Update 3.2.0.10 For a list of changes,

More information

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage: Discussion 2C Notes (Week 3, January 21) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Abstraction In Homework 1, you were asked to build a class called Bag. Let

More information

Chapter 8 STRUCTURES IN C

Chapter 8 STRUCTURES IN C Chapter 8 STRUCTURES IN C 1 Structures Introduction Collections of related variables (aggregates) under one name Can contain variables of different data types Commonly used to define records to be stored

More information

Data Structure. Lecture#7: Lists, Stacks, and Queues (Chapter 4) U Kang Seoul National University. U Kang (2016) 1

Data Structure. Lecture#7: Lists, Stacks, and Queues (Chapter 4) U Kang Seoul National University. U Kang (2016) 1 Data Structure Lecture#7: Lists, Stacks, and Queues (Chapter 4) U Kang Seoul National University U Kang (2016) 1 In This Lecture Learn the List data structure Compare the array-based and link-based implementation

More information

Dynamic Data Structures

Dynamic Data Structures Dynamic Data Structures We have seen that the STL containers vector, deque, list, set and map can grow and shrink dynamically. We now examine how some of these containers can be implemented in C++. To

More information

! A data type for which: ! An ADT may be implemented using various. ! Examples:

! A data type for which: ! An ADT may be implemented using various. ! Examples: Stacks and Queues Unit 6 Chapter 19.1-2,4-5 CS 2308 Fall 2018 Jill Seaman 1 Abstract Data Type A data type for which: - only the properties of the data and the operations to be performed on the data are

More information

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive) Final Exam Exercises Chapters 1-7 + 11 Write C++ code to: l Determine if a number is odd or even CS 2308 Fall 2016 Jill Seaman l Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

Chapter 9 STACK, QUEUE

Chapter 9 STACK, QUEUE Chapter 9 STACK, QUEUE 1 LIFO: Last In, First Out. Stacks Restricted form of list: Insert and remove only at front of list. Notation: Insert: PUSH Remove: POP The accessible element is called TOP. Stack

More information

STACKS AND QUEUES. Problem Solving with Computers-II

STACKS AND QUEUES. Problem Solving with Computers-II STACKS AND QUEUES Problem Solving with Computers-II 2 Stacks container class available in the C++ STL Container class that uses the Last In First Out (LIFO) principle Methods i. push() ii. iii. iv. pop()

More information

1 P age DS & OOPS / UNIT II

1 P age DS & OOPS / UNIT II UNIT II Stacks: Definition operations - applications of stack. Queues: Definition - operations Priority queues - De que Applications of queue. Linked List: Singly Linked List, Doubly Linked List, Circular

More information

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S. Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S. Bowers 1 of 11 Doubly Linked Lists Each node has both a next and a prev pointer head \ v1 v2 v3 \ tail struct

More information

CS 216 Exam 1 Fall SOLUTION

CS 216 Exam 1 Fall SOLUTION CS 216 Exam 1 Fall 2004 - SOLUTION Name: Lab Section: Email Address: Student ID # This exam is closed note, closed book. You will have an hour and fifty minutes total to complete the exam. You may NOT

More information

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type Ch. 18: ADTs: Stacks and Queues CS 2308 Fall 2011 Jill Seaman Lecture 18 1 Abstract Data Type A data type for which: - only the properties of the data and the operations to be performed on the data are

More information

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue Queues CSE 2011 Fall 2009 9/28/2009 7:56 AM 1 Queues: FIFO Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue Applications,

More information

Data Structure. Lecture#8,9: Lists, Stacks, and Queues (Chapter 4) U Kang Seoul National University. U Kang 1

Data Structure. Lecture#8,9: Lists, Stacks, and Queues (Chapter 4) U Kang Seoul National University. U Kang 1 Data Structure Lecture#8,9: Lists, Stacks, and Queues (Chapter 4) U Kang Seoul National University U Kang 1 In This Lecture Learn the motivation and main idea of doubly linked list Learn the Stack and

More information

Object Oriented Programming COP3330 / CGS5409

Object Oriented Programming COP3330 / CGS5409 Object Oriented Programming COP3330 / CGS5409 Intro to Data Structures Vectors Linked Lists Queues Stacks C++ has some built-in methods of storing compound data in useful ways, like arrays and structs.

More information

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Queues and Priority Queues Kostas Alexis Implementations of the ADT Queue Like stacks, queues can have Array-based or Link-based implementation Can also use implementation

More information

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive) Final Exam Exercises Chapters 1-7 + 11 Write C++ code to:! Determine if a number is odd or even CS 2308 Fall 2018 Jill Seaman! Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering EXAM November 2016

UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering EXAM November 2016 UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering 16.322 EXAM 2 14 November 2016 Problem Weight Score 1 30 % 2 36 % 3 32 % Bonus 2 % TOTAL 100 % INSTRUCTIONS You may

More information

CS24 Week 4 Lecture 2

CS24 Week 4 Lecture 2 CS24 Week 4 Lecture 2 Kyle Dewey Overview Linked Lists Stacks Queues Linked Lists Linked Lists Idea: have each chunk (called a node) keep track of both a list element and another chunk Need to keep track

More information

Containers: Queue and List. Jordi Cortadella and Jordi Petit Department of Computer Science

Containers: Queue and List. Jordi Cortadella and Jordi Petit Department of Computer Science Containers: Queue and List Jordi Cortadella and Jordi Petit Department of Computer Science Queue A container in which insertion is done at one end (the tail) and deletion is done at the other end (the

More information

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples: Ch. 8: ADTs: Stacks and Queues Abstract Data Type A data type for which: CS 8 Fall Jill Seaman - only the properties of the data and the operations to be performed on the data are specific, - not concerned

More information

Data Structure. Recitation VII

Data Structure. Recitation VII Data Structure Recitation VII Recursion: Stack trace Queue Topic animation Trace Recursive factorial Executes factorial(4) Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4)

More information

4.1 Ordered Lists Revisited

4.1 Ordered Lists Revisited Chapter 4 Linked-lists 4.1 Ordered Lists Revisited Let us revisit our ordered-list. Let us assume that we use it for storing roll numbers of students in a class in ascending order. We can make the assumption

More information

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM

More information

Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison

Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison Linked lists Comp Sci 1575 Data Structures Outline 1 2 3 4 5 Linked list Linked lists are of a linear collection of data elements, called nodes, each pointing to the next node Each node is composed of

More information

CSE 230 Intermediate Programming in C and C++

CSE 230 Intermediate Programming in C and C++ CSE 230 Intermediate Programming in C and C++ Structures and List Processing Fall 2017 Stony Brook University Instructor: Shebuti Rayana http://www3.cs.stonybrook.edu/~cse230/ Self-referential Structure

More information

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013 Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every

More information

SCJ2013 Data Structure & Algorithms. Queue. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

SCJ2013 Data Structure & Algorithms. Queue. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi SCJ2013 Data Structure & Algorithms Queue Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi Course Objectives At the end of the lesson students are expected to be able to: Understand queue concepts and

More information

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive) Final Exam Exercises CS 2308 Spring 2014 Jill Seaman Chapters 1-7 + 11 Write C++ code to: Determine if a number is odd or even Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

CS350 - Exam 1 (100 Points)

CS350 - Exam 1 (100 Points) Spring 2013 Name CS350 - Exam 1 (100 Points) 1.(25 points) Stacks and Queues (a) (5) For the values 4, 8, 2, 5, 7, 3, 9 in that order, give a sequence of push() and pop() operations that produce the following

More information

Lecture 7: Data Structures. EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Lecture 7: Data Structures. EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology Lecture 7: Data Structures 1 Introduction: dynamic array Conventional array in C has fix number of elements Dynamic array is array with variable number of elements: actually a pointer and a variable indicating

More information

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the elements may locate at far positions

More information

Ashish Gupta, Data JUET, Guna

Ashish Gupta, Data JUET, Guna Categories of data structures Data structures are categories in two classes 1. Linear data structure: - organized into sequential fashion - elements are attached one after another - easy to implement because

More information

8. Fundamental Data Structures

8. Fundamental Data Structures 172 8. Fundamental Data Structures Abstract data types stack, queue, implementation variants for linked lists, [Ottman/Widmayer, Kap. 1.5.1-1.5.2, Cormen et al, Kap. 10.1.-10.2] Abstract Data Types 173

More information

ADT: Design & Implementation

ADT: Design & Implementation CPSC 250 Data Structures ADT: Design & Implementation Dr. Yingwu Zhu Abstract Data Type (ADT) ADT = data items + operations on the data Design of ADT Determine data members & operations Implementation

More information

Information Science 2

Information Science 2 Information Science 2 - Basic Data Structures- Week 02 College of Information Science and Engineering Ritsumeikan University Today s class outline l Basic data structures: Definitions and implementation

More information

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18 Stacks and Queues Week 9 Gaddis: Chapter 18 CS 5301 Fall 2015 Jill Seaman Introduction to the Stack Stack: a data structure that holds a collection of elements of the same type. - The elements are accessed

More information

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013 Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every

More information

Linked List. April 2, 2007 Programming and Data Structure 1

Linked List. April 2, 2007 Programming and Data Structure 1 Linked List April 2, 2007 Programming and Data Structure 1 Introduction head A linked list is a data structure which can change during execution. Successive elements are connected by pointers. Last element

More information

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50 Lists, Stacks, and Queues (Lists, Stacks, and Queues ) Data Structures and Programming Spring 2016 1 / 50 Abstract Data Types (ADT) Data type a set of objects + a set of operations Example: integer set

More information

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University Abstract Data Types CptS 223 Advanced Data Structures Larry Holder School of Electrical Engineering and Computer Science Washington State University 1 Purpose Abstract Data Types (ADTs) Lists Stacks Queues

More information

Data Structures and Algorithms(2)

Data Structures and Algorithms(2) Ming Zhang Data Structures and Algorithms Data Structures and Algorithms(2) Instructor: Ming Zhang Textbook Authors: Ming Zhang, Tengjiao Wang and Haiyan Zhao Higher Education Press, 2008.6 (the "Eleventh

More information

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours TED (10)-3071 Reg. No.. (REVISION-2010) (Maximum marks: 100) Signature. FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours PART

More information

Abstract Data Types 1

Abstract Data Types 1 Abstract Data Types 1 Purpose Abstract Data Types (ADTs) Lists Stacks Queues 2 Abstract Data Types (ADTs) ADT is a set of objects together with a set of operations. Abstract in that implementation of operations

More information

Abstract Data Types 1

Abstract Data Types 1 Abstract Data Types 1 Purpose Abstract Data Types (ADTs) Lists Stacks Queues 2 Primitive vs. Abstract Data Types Primitive DT: programmer ADT: programmer Interface (API) data Implementation (methods) Data

More information

Stack and Queue. Stack:

Stack and Queue. Stack: Stack and Queue Stack: Abstract Data Type A stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle. In the pushdown stacks only two operations

More information

How can we improve this? Queues 6. Topological ordering: given a sequence of. should occur prior to b, provide a schedule. Queues 5.

How can we improve this? Queues 6. Topological ordering: given a sequence of. should occur prior to b, provide a schedule. Queues 5. Queues 1 Queues A Queue is a sequential organization of items in which the first element entered is the first removed. They are often referred to as FIFO, which stands for first in first out. Examples:

More information

Queues. Gaddis 18.4, Molly A. O'Neil CS 2308 :: Spring 2016

Queues. Gaddis 18.4, Molly A. O'Neil CS 2308 :: Spring 2016 Queues Gaddis 18.4, 18.6 Molly A. O'Neil CS 2308 :: Spring 2016 The Queue ADT A queue is an abstract data type that stores a collection of elements of the same type The elements of a queue are accessed

More information

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure. 5 Linked Structures 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

More information

CMSC 341 Lecture 7 Lists

CMSC 341 Lecture 7 Lists CMSC 341 Lecture 7 Lists Today s Topics Linked Lists vs Arrays Nodes Using Linked Lists Supporting Actors (member variables) Overview Creation Traversal Deletion UMBC CMSC 341 Lists 2 Linked Lists vs Arrays

More information

csci 210: Data Structures Stacks and Queues

csci 210: Data Structures Stacks and Queues csci 210: Data Structures Stacks and Queues 1 Summary Topics Stacks and Queues as abstract data types ( ADT) Implementations arrays linked lists Analysis and comparison Applications: searching with stacks

More information

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures MIDTERM EXAMINATION Spring 2010 CS301- Data Structures Question No: 1 Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the

More information

Stacks, Queues (cont d)

Stacks, Queues (cont d) Stacks, Queues (cont d) CSE 2011 Winter 2007 February 1, 2007 1 The Adapter Pattern Using methods of one class to implement methods of another class Example: using List to implement Stack and Queue 2 1

More information

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

double d0, d1, d2, d3; double * dp = new double[4]; double da[4]; All multiple choice questions are equally weighted. You can generally assume that code shown in the questions is intended to be syntactically correct, unless something in the question or one of the answers

More information

Queue with Array Implementation

Queue with Array Implementation Queue with Array Implementation SCSJ2013 Data Structures & Algorithms Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi Faculty of Computing Objectives Queue concepts and applications. Queue structure and

More information

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3 UNIT 3 LINEAR DATA STRUCTURES 1. Define Data Structures Data Structures is defined as the way of organizing all data items that consider not only the elements stored but also stores the relationship between

More information

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2007 7p-9p, Thursday, March 1 Name: NetID: Lab Section

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Queues ArrayQueue Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 10, 2014 Abstract These lecture notes are meant to be looked

More information

Basic Data Structures (Version 7) Name:

Basic Data Structures (Version 7) Name: Prerequisite Concepts for Analysis of Algorithms Basic Data Structures (Version 7) Name: Email: Concept: mathematics notation 1. log 2 n is: Code: 21481 (A) o(log 10 n) (B) ω(log 10 n) (C) Θ(log 10 n)

More information

Largest Online Community of VU Students

Largest Online Community of VU Students WWW.VUPages.com WWW.VUTUBE.EDU.PK http://forum.vupages.com Largest Online Community of VU Students MIDTERM EXAMINATION SEMESTER FALL 2003 CS301-DATA STRUCTURE Total Marks:86 Duration: 60min Instructions

More information

Indexing. Outline. Introduction (Ch 10) Linear Index (Ch 10.1) Tree Index (Ch 10.3) 2-3 Tree (Ch 10.4) B-Tree (Ch 10.5) Data Structure Chapter 10

Indexing. Outline. Introduction (Ch 10) Linear Index (Ch 10.1) Tree Index (Ch 10.3) 2-3 Tree (Ch 10.4) B-Tree (Ch 10.5) Data Structure Chapter 10 http://125.216.243.100/csds/ Data Structure Chapter 10 Indexing Dr. Patrick Chan School of Computer Science and Engineering South China University of Technology Outline Introduction (Ch 10) Linear Index

More information

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10 Keeping Order:! Stacks, Queues, & Deques 1 Stacks 2 Stacks A stack is a last in, first out (LIFO) data structure Primary Operations: push() add item to top pop() return the top item and remove it peek()

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class Prof. amr Goneid, AUC 1 Dictionaries(1): A Key Table Class Prof. Amr Goneid, AUC 2 A Key Table

More information

Chapter 18: Stacks And Queues

Chapter 18: Stacks And Queues Chapter 18: Stacks And Queues 18.1 Introduction to the Stack ADT Introduction to the Stack ADT Stack: a LIFO (last in, first out) data structure Examples: plates in a cafeteria return addresses for function

More information

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print) Computer Science 62 Bruce/Mawhorter Fall 16 Midterm Examination October 5, 2016 Question Points Score 1 15 2 10 3 10 4 8 5 9 TOTAL 52 SOLUTIONS Your name (Please print) 1. Suppose you are given a singly-linked

More information

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001 CSE143 Exam with answers Problem numbering may differ from the test as given. All multiple choice questions are equally weighted. You can generally assume that code shown in the questions is intended to

More information

Indexing. Introduction. Outline. Search Approach. Linear Index (Ch 10.1) Tree Index (Ch 10.3) 2-3 Tree (Ch 10.4) B-Tree (Ch 10.5) Introduction (Ch 10)

Indexing. Introduction. Outline. Search Approach. Linear Index (Ch 10.1) Tree Index (Ch 10.3) 2-3 Tree (Ch 10.4) B-Tree (Ch 10.5) Introduction (Ch 10) http://125.216.243.100/csds/ Data Structure Chapter 10 Indexing Dr. Patrick Chan School of Computer Science and Engineering South China University of Technology Search Approach Sequential and List Method

More information

Name: I. 20 II. 20 III. 20 IV. 40. CMSC 341 Section 01 Fall 2016 Data Structures Exam 1. Instructions: 1. This is a closed-book, closed-notes exam.

Name: I. 20 II. 20 III. 20 IV. 40. CMSC 341 Section 01 Fall 2016 Data Structures Exam 1. Instructions: 1. This is a closed-book, closed-notes exam. CMSC 341 Section 01 Fall 2016 Data Structures Exam 1 Name: Score Max I. 20 II. 20 III. 20 IV. 40 Instructions: 1. This is a closed-book, closed-notes exam. 2. You have 75 minutes for the exam. 3. Calculators,

More information

Linked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil

Linked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil Linked Lists Gaddis Ch. 17 CS 2308 :: Spring 2016 Molly O'Neil List ADT A list is an abstract data type representing an ordered sequence of values For example, both MP3 Player assignments have used lists:

More information

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD DATA STRUCTURES AND ALGORITHMS LECTURE 08 S IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD S ABSTRACT DATA TYPE An Abstract Queue (Queue ADT) is an abstract data type that emphasizes specific

More information

Practice test for Midterm 1; solutions

Practice test for Midterm 1; solutions Practice test for Midterm 1; solutions November 1, 2 1 1 C++ review & UML diagrams Write a function which takes a vector of int and returns true if all the elements of the vector are positive ( 0) and

More information

"apple" "grape" "grape" "grape" "apple"

apple grape grape grape apple Test 1: CPS 100 Owen Astrachan and Dee Ramm February 21, 1997 Name: Honor code acknowledgment (signature) Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 Problem 6 TOTAL: value 10 pts. 9 pts. 21 pts.

More information

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues Copyright 2009 Pearson Education, Inc. Copyright Publishing as Pearson 2009 Addison-Wesley Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18.1 Introduction to

More information

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list Linked Lists Introduction : Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is

More information

Data Structures and Algorithms Winter Semester

Data Structures and Algorithms Winter Semester Page 0 German University in Cairo October 24, 2018 Media Engineering and Technology Faculty Prof. Dr. Slim Abdennadher Dr. Wael Abouelsadaat Data Structures and Algorithms Winter Semester 2018-2019 Midterm

More information

VALLIAMMAI ENGNIEERING COLLEGE SRM Nagar, Kattankulathur DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK

VALLIAMMAI ENGNIEERING COLLEGE SRM Nagar, Kattankulathur DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK VALLIAMMAI ENGNIEERING COLLEGE SRM Nagar, Kattankulathur 603203. DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK Degree & Branch : B.E E.C.E. Year & Semester : II / IV Section : ECE 1, 2 &

More information

Sequence Abstract Data Type

Sequence Abstract Data Type 1 Sequence Abstract Data Type Table of Contents Introduction.. 1 Objects for the sequence data type.. 2 The sequence as an object. 2.1 Sequence components. 2.2 Operations on sequences 3 Enquiry operations..

More information

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list Linked Lists Introduction : Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is

More information

CMPT 225. Lecture 6 linked list

CMPT 225. Lecture 6 linked list CMPT 225 Lecture 6 linked list 1 Last Lecture Class documentation Linked lists and its operations 2 Learning Outcomes At the end of this lecture, a student will be able to: define one of the concrete data

More information

INSTITUTE OF AERONAUTICAL ENGINEERING

INSTITUTE OF AERONAUTICAL ENGINEERING INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad - 500 043 COMPUTER SCIENCE AND ENGINEERING TUTORIAL QUESTION BANK Course Name Course Code Class Branch DATA STRUCTURES ACS002 B. Tech

More information

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE TED (10)-3071 Reg. No.. (REVISION-2010) Signature. FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours (Maximum marks: 100)

More information

CSCE 2014 Final Exam Spring Version A

CSCE 2014 Final Exam Spring Version A CSCE 2014 Final Exam Spring 2017 Version A Student Name: Student UAID: Instructions: This is a two-hour exam. Students are allowed one 8.5 by 11 page of study notes. Calculators, cell phones and computers

More information

Unit 4 Basic Collections

Unit 4 Basic Collections Unit 4 Basic Collections General Concepts Templates Exceptions Iterators Collection (or Container) Classes Vectors (or Arrays) Sets Lists Maps or Tables C++ Standard Template Library (STL Overview A program

More information

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Chapter 5: Stacks, ueues and Deques Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,

More information

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES CH4.2-4.3. ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER

More information

Matriculation number:

Matriculation number: Department of Informatics Prof. Dr. Michael Böhlen Binzmühlestrasse 14 8050 Zurich Phone: +41 44 635 4333 Email: boehlen@ifi.uzh.ch AlgoDat Repetition Exam Spring 2018 18.05.2018 Name: Matriculation number:

More information

Chapter 18: Stacks And Queues

Chapter 18: Stacks And Queues Chapter 18: Stacks And Queues 18.1 Introduction to the Stack ADT Introduction to the Stack ADT Stack a LIFO (last in, first out) data structure Examples plates in a cafeteria return addresses for function

More information

Computer Science 302 Spring 2007 Practice Final Examination: Part I

Computer Science 302 Spring 2007 Practice Final Examination: Part I Computer Science 302 Spring 2007 Practice Final Examination: Part I Name: This practice examination is much longer than the real final examination will be. If you can work all the problems here, you will

More information

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Queues. CITS2200 Data Structures and Algorithms. Topic 5 CITS2200 Data Structures and Algorithms Topic 5 Queues Implementations of the Queue ADT Queue specification Queue interface Block (array) representations of queues Recursive (linked) representations of

More information

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type.

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type. Stacks and Queues Week 9 Gaddis: Chapter 18 (8th ed.) Gaddis: Chapter 19 (9th ed.) CS 5301 Fall 2018 Jill Seaman Introduction to the Stack Stack: a data structure that holds a collection of elements of

More information

Motivation for Templates

Motivation for Templates Motivation for You want both: a list of Location objects a list of MazeMonster objects 1 How can you accomplish this by writing one LinkedList class? state all the ways you can think of doing this state

More information

CS S-04 Stacks and Queues 1. An Abstract Data Type is a definition of a type based on the operations that can be performed on it.

CS S-04 Stacks and Queues 1. An Abstract Data Type is a definition of a type based on the operations that can be performed on it. CS245-2017S-04 Stacks and Queues 1 04-0: Abstract Data Types An Abstract Data Type is a definition of a type based on the operations that can be performed on it. An ADT is an interface Data in an ADT cannot

More information

Data Structures. Lecture 5 : The Queues

Data Structures. Lecture 5 : The Queues 0 Data Structures Lecture 5 : Dr. Essam Halim Houssein Lecturer, Faculty of Computers and Informatics, Benha University http://bu.edu.eg/staff/esamhalim14 2 A queue is logically a first in first out (FIFO

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists Prof. amr Goneid, AUC 1 Linked Lists Prof. amr Goneid, AUC 2 Linked Lists The Linked List Structure Some Linked List

More information

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11. Lecture 11 1 More Data Structures In this lecture we will use a linked list to implement two abstract data types (ADT) An ADT provides the interface, or what a data structure does We can then use code

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms CS245-2017S-04 Stacks and Queues David Galles Department of Computer Science University of San Francisco 04-0: Abstract Data Types An Abstract Data Type is a definition of

More information