doubly-linked lists same basic functions operate on list each node has a forward and backward link:...

54
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 42 109 NULL head_ptr

Post on 18-Dec-2015

229 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 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

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?

88NULL 42 109 NULL

head_ptr

Page 2: 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

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.

88NULL 42 109 NULL

head_ptr

Page 3: 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
Page 4: 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

Stacks and Queues

Kruse and Ryba Ch 2 and 3

Page 5: 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

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).

Page 6: 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

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)

Page 7: 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

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.

Page 8: 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

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.

Page 9: 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
Page 10: 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

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];};

Page 11: 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

Stack Implementation (cont.)StackType::StackType(){ top = -1;}

void StackType::MakeEmpty() { top = -1;}

bool StackType::IsEmpty() const{ return (top == -1);}

Page 12: 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

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--;}

Page 13: 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

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);

Page 14: 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

Implementing stacks using templates

• Templates allow the compiler to generate multiple versions of a class type or a function by allowing parameterized types.

Page 15: 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

Implementing stacks using templatestemplate<class 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];};

(cont.)

Page 16: 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

Example using templates // Client codeStackType<int> myStack;StackType<float> yourStack;StackType<StrType> 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.

Page 17: 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

Function templates • The definitions of the member functions must be

rewritten as function templates.

template<class ItemType>StackType<ItemType>::StackType(){ top = -1;}

template<class ItemType>void StackType<ItemType>::MakeEmpty(){ top = -1;}

Page 18: 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

Function templates (cont.) template<class ItemType>bool StackType<ItemType>::IsEmpty() const{

return (top == -1);}

template<class ItemType>bool StackType<ItemType>::IsFull() const{

return (top == MAX_ITEMS-1);} 

template<class ItemType>void StackType<ItemType>::Push(ItemType newItem){

top++; items[top] = newItem;}

Page 19: 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

Function templates (cont.)

template<class ItemType>void StackType<ItemType>::Pop(ItemType& item){ item = items[top]; top--;}

Page 20: 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

Implementing stacks using dynamic array allocation

template<class ItemType>

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;};

Page 21: 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

Implementing stacks using dynamic array allocation (cont.)

template<class ItemType>StackType<ItemType>::StackType(int max){

maxStack = max; top = -1; items = new ItemType[max];}

template<class ItemType>StackType<ItemType>::~StackType(){

delete [ ] items;}

Page 22: 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

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: 2 5 +

• Expressions are evaluated from left to right. 

• Precedence rules and parentheses are never needed!!

Page 23: 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

Example: postfix expressions(cont.)

Page 24: 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

Postfix expressions: Algorithm using stacks (cont.)

Page 25: 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

Postfix expressions:Algorithm using stacksWHILE 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)

Page 26: 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

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).

Page 27: 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

{

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);

}

}

1

2

3

3

5

1

1

5

3

Stack

Stack

tempStack

oldItem = 2

newItem = 5

Page 28: 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

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).

queueelements

enterno changes of order

elementsexit234 1

tail head

Page 29: 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

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)

Page 30: 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

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.

Page 31: 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

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.

Page 32: 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

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.

Page 33: 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
Page 34: 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
Page 35: 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

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

Page 36: 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

Initialize front and rear

Page 37: 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

Queue is empty now!!

rear == front

Page 38: 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

Queue Implementationtemplate<class ItemType>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;};

Page 39: 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

Queue Implementation (cont.)

template<class ItemType>QueueType<ItemType>::QueueType(int

max){

maxQue = max + 1; front = maxQue - 1; rear = maxQue - 1; items = new ItemType[maxQue];}

Page 40: 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

Queue Implementation (cont.)

template<class ItemType>

QueueType<ItemType>::~QueueType()

{

delete [] items;

}

Page 41: 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

Queue Implementation (cont.)

template<class ItemType>void QueueType<ItemType>::

MakeEmpty(){ front = maxQue - 1; rear = maxQue - 1;}

Page 42: 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

Queue Implementation (cont.)template<class ItemType>bool QueueType<ItemType>::IsEmpty() const{

return (rear == front);}

template<class ItemType>bool QueueType<ItemType>::IsFull() const{

return ( (rear + 1) % maxQue == front);}

Page 43: 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

Queue Implementation (cont.)

template<class ItemType>

void QueueType<ItemType>::Enqueue (ItemType newItem)

{

rear = (rear + 1) % maxQue;

items[rear] = newItem;

}

Page 44: 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

Queue Implementation (cont.)

template<class ItemType>

void QueueType<ItemType>::Dequeue (ItemType& item)

{

front = (front + 1) % maxQue;

item = items[front];

}

Page 45: 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

Queue overflow

• The condition resulting from trying to add an element onto a full queue.

 

if(!q.IsFull())

q.Enqueue(item);

Page 46: 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

Queue underflow

• The condition resulting from trying to remove an element from an empty queue.

 

if(!q.IsEmpty())

q.Dequeue(item);

Page 47: 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

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.

Page 48: 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

Example: recognizing palindromes

Page 49: 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

Example: recognizing palindromes#include <iostream.h>#include <ctype.h>#include "stack.h"#include "queue.h“int main(){ StackType<char> s; QueType<char> 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)); } }

Page 50: 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

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

Page 51: 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

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.

Page 52: 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

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

Page 53: 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

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.

Page 54: 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