Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Stack & Queues COP 3502.
Lists + CS3240, L. Grewe 1. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked.
Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
What is a Queue? A queue is a FIFO “first in, first out” structure.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Chapter 5 ADTs Stack and Queue. 2 Goals Describe a stack and its operations at a logical level Demonstrate the effect of stack operations using a particular.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Introduction to Stacks & Queues.
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Inheritance CS 308 – Data Structures “the mechanism by which one class acquires the properties of another class” the properties of another class”
1 Chapter 4 Stack and Queue ADT. 2 Stacks of Coins and Bills.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
Implementing a Stack as a Linked Structure CS 308 – Data Structures.
Stacks CS 308 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
What is a Stack? n Logical (or ADT) level: A stack is an ordered group of homogeneous items in which the removal and addition of items can take place only.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
Chapter 18: Stacks and Queues
Inheritance CS 302 – Data Structures Section 2.4 (pp ) and Section 6.7.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
Data Structures: Stacks Queues 1. 2 Stack ADT: What is a Stack? a stack is a varying-length, collection of homogeneous elements Insertion and Deletion.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
1 Data Structures and Algorithms Stacks and Queues.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 17: Stacks and Queues. Objectives In this chapter, you will: – Learn about stacks – Examine various stack operations – Learn how to implement.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Chapter 5 (Part 1) ADT Stack 1 Fall Stacks TOP OF THE STACK.
1 C++ Plus Data Structures Abstract Data Types Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by.
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.
CS505 Data Structures and Algorithms
CC 215 Data Structures Queue ADT
Queues.
Homework 4 questions???.
C++ Plus Data Structures
Stack and Queue APURBO DATTA.
Chapter 5 ADTs Stack and Queue.
Queues.
C++ Plus Data Structures
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.
Stacks.
Stacks and Queues 1.
C++ Plus Data Structures
CSI 1340 Introduction to Computer Science II
Queues.
CSCS-200 Data Structure and Algorithms
Presentation transcript:

Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88 NULL NULL head_ptr

Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? Cursor can move forwards and backwards in list. 88 NULL NULL head_ptr

Stacks and Queues Kruse and Ryba Ch 2 and 3

What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the top of the stack (the most recently added items are at the top of the stack). The last element to be added is the first to be removed (LIFO: Last In, First Out).

Stack Specification Definitions: (provided by the user) –MAX_ITEMS: Max number of items that might be on the stack –ItemType: Data type of the items on the stack Operations –MakeEmpty –Boolean IsEmpty –Boolean IsFull –Push (ItemType newItem) –Pop (ItemType& item) (or pop and top)

Push (ItemType newItem) Function: Adds newItem to the top of the stack. Preconditions: Stack has been initialized and is not full. Postconditions: newItem is at the top of the stack.

Pop (ItemType& item) Function: Removes topItem from stack and returns it in item. Preconditions: Stack has been initialized and is not empty. Postconditions: Top element has been removed from stack and item is a copy of the removed element.

Stack Implementation #include "ItemType.h" // Must be provided by the user of the class // Contains definitions for MAX_ITEMS and ItemType class StackType { public: StackType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType); void Pop(ItemType&); private: int top; ItemType items[MAX_ITEMS]; };

Stack Implementation (cont.) StackType::StackType() { top = -1; } void StackType::MakeEmpty() { top = -1; } bool StackType::IsEmpty() const { return (top == -1); }

Stack Implementation (cont.) bool StackType::IsFull() const { return (top == MAX_ITEMS-1); } void StackType::Push(ItemType newItem) { top++; items[top] = newItem; } void StackType::Pop(ItemType& item) { item = items[top]; top--; }

Stack overflow The condition resulting from trying to push an element onto a full stack. if(!stack.IsFull()) stack.Push(item); Stack underflow The condition resulting from trying to pop an empty stack. if(!stack.IsEmpty()) stack.Pop(item);

Implementing stacks using templates Templates allow the compiler to generate multiple versions of a class type or a function by allowing parameterized types.

Implementing stacks using templates template class StackType { public: StackType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType); void Pop(ItemType&); private: int top; ItemType items[MAX_ITEMS]; }; (cont.)

Example using templates // Client code StackType myStack; StackType yourStack; StackType anotherStack; myStack.Push(35); yourStack.Push(584.39); The compiler generates distinct class types and gives its own internal name to each of the types.

Function templates The definitions of the member functions must be rewritten as function templates. template StackType ::StackType() { top = -1; } template void StackType ::MakeEmpty() { top = -1; }

Function templates (cont.) template bool StackType ::IsEmpty() const { return (top == -1); } template bool StackType ::IsFull() const { return (top == MAX_ITEMS-1); } template void StackType ::Push(ItemType newItem) { top++; items[top] = newItem; }

Function templates (cont.) template void StackType ::Pop(ItemType& item) { item = items[top]; top--; }

Implementing stacks using dynamic array allocation template class StackType { public: StackType(int); ~StackType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType); void Pop(ItemType&); private: int top; int maxStack; ItemType *items; };

Implementing stacks using dynamic array allocation (cont.) template StackType ::StackType(int max) { maxStack = max; top = -1; items = new ItemType[max]; } template StackType ::~StackType() { delete [ ] items; }

Example: postfix expressions Postfix notation is another way of writing arithmetic expressions. In postfix notation, the operator is written after the two operands. infix: 2+5 postfix: Expressions are evaluated from left to right. Precedence rules and parentheses are never needed!!

Example: postfix expressions (cont.)

Postfix expressions: Algorithm using stacks (cont.)

Postfix expressions: Algorithm using stacks WHILE more input items exist Get an item IF item is an operand stack.Push(item) ELSE stack.Pop(operand2) stack.Pop(operand1) Compute result stack.Push(result) stack.Pop(result)

Write the body for a function that replaces each copy of an item in a stack with another item. Use the following specification. (this function is a client program). ReplaceItem(StackType& stack, ItemType oldItem, ItemType newItem) Function: Replaces all occurrences of oldItem with newItem. Precondition: stack has been initialized. Postconditions: Each occurrence of oldItem in stack has been replaced by newItem. (You may use any of the member functions of the StackType, but you may not assume any knowledge of how the stack is implemented).

{ ItemType item; StackType tempStack; while (!Stack.IsEmpty()) { Stack.Pop(item); if (item==oldItem) tempStack.Push(newItem); else tempStack.Push(item); } while (!tempStack.IsEmpty()) { tempStack.Pop(item); Stack.Push(item); } Stack tempStack oldItem = 2 newItem = 5

What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at one end. –Elements are removed from the other end. The element added first is also removed first (FIFO: First In, First Out). queue elements enter no changes of order elements exit 2341 tailhead

Queue Specification Definitions: (provided by the user) –MAX_ITEMS: Max number of items that might be on the queue –ItemType: Data type of the items on the queue Operations –MakeEmpty –Boolean IsEmpty –Boolean IsFull –Enqueue (ItemType newItem) –Dequeue (ItemType& item) (serve and retrieve)

Enqueue (ItemType newItem) Function: Adds newItem to the rear of the queue. Preconditions: Queue has been initialized and is not full. Postconditions: newItem is at rear of queue.

Dequeue (ItemType& item) Function: Removes front item from queue and returns it in item. Preconditions: Queue has been initialized and is not empty. Postconditions: Front element has been removed from queue and item is a copy of removed element.

Implementation issues Implement the queue as a circular structure. How do we know if a queue is full or empty? Initialization of front and rear. Testing for a full or empty queue.

Make front point to the element preceding the front element in the queue (one memory location will be wasted).

Initialize front and rear

Queue is empty now!! rear == front

Queue Implementation template class QueueType { public: QueueType(int); QueueType(); ~QueueType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType); void Dequeue(ItemType&); private: int front; int rear; ItemType* items; int maxQue; };

Queue Implementation (cont.) template QueueType ::QueueType(int max) { maxQue = max + 1; front = maxQue - 1; rear = maxQue - 1; items = new ItemType[maxQue]; }

Queue Implementation (cont.) template QueueType ::~QueueType() { delete [] items; }

Queue Implementation (cont.) template void QueueType :: MakeEmpty() { front = maxQue - 1; rear = maxQue - 1; }

Queue Implementation (cont.) template bool QueueType ::IsEmpty() const { return (rear == front); } template bool QueueType ::IsFull() const { return ( (rear + 1) % maxQue == front); }

Queue Implementation (cont.) template void QueueType ::Enqueue (ItemType newItem) { rear = (rear + 1) % maxQue; items[rear] = newItem; }

Queue Implementation (cont.) template void QueueType ::Dequeue (ItemType& item) { front = (front + 1) % maxQue; item = items[front]; }

Queue overflow The condition resulting from trying to add an element onto a full queue. if(!q.IsFull()) q.Enqueue(item);

Queue underflow The condition resulting from trying to remove an element from an empty queue. if(!q.IsEmpty()) q.Dequeue(item);

Example: recognizing palindromes A palindrome is a string that reads the same forward and backward. Able was I ere I saw Elba We will read the line of text into both a stack and a queue. Compare the contents of the stack and the queue character-by-character to see if they would produce the same string of characters.

Example: recognizing palindromes

#include #include "stack.h" #include "queue.h“ int main() { StackType s; QueType q; char ch; char sItem, qItem; int mismatches = 0; cout << "Enter string: " << endl; while(cin.peek() != '\\n') { cin >> ch; if(isalpha(ch)) { if(!s.IsFull()) s.Push(toupper(ch)); if(!q.IsFull()) q.Enqueue(toupper(ch)); }

while( (!q.IsEmpty()) && (!s.IsEmpty()) ) { s.Pop(sItem); q.Dequeue(qItem); if(sItem != qItem) ++mismatches; } if (mismatches == 0) cout << "That is a palindrome" << endl; else cout << That is not a palindrome" << endl; return 0; } Example: recognizing palindromes

Case Study: Simulation Queuing System: consists of servers and queues of objects to be served. Simulation: a program that determines how long items must wait in line before being served.

Case Study: Simulation (cont.) Inputs to the simulation: (1) the length of the simulation (2) the average transaction time (3) the number of servers (4) the average time between job arrivals

Case Study: Simulation (cont.) Parameters the simulation must vary: (1) number of servers (2) time between arrivals of items Output of simulation: average wait time.