Doubly-Linked Lists - PowerPoint PPT Presentation

About This Presentation
Title:

Doubly-Linked Lists

Description:

Example: recognizing palindromes. A palindrome is a string that reads the same forward and backward. ... 'That is a palindrome' endl; else. cout That ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 55
Provided by: ECC86
Learn more at: https://www.cs.bu.edu
Category:

less

Transcript and Presenter's Notes

Title: Doubly-Linked Lists


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
42
109
NULL
NULL
head_ptr
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?
  • Cursor can move forwards and backwards in list.

88
42
109
NULL
NULL
head_ptr
3
(No Transcript)
4
Stacks and Queues
  • Kruse and Ryba Ch 2 and 3

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

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

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

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

9
(No Transcript)
10
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 itemsMAX_ITEMS

11
Stack Implementation (cont.)
  • StackTypeStackType()
  • top -1
  • void StackTypeMakeEmpty()
  • top -1
  • bool StackTypeIsEmpty() const
  • return (top -1)

12
Stack Implementation (cont.)
  • bool StackTypeIsFull() const
  • return (top MAX_ITEMS-1)
  •  void StackTypePush(ItemType newItem)
  • top
  • itemstop newItem
  •  void StackTypePop(ItemType item)
  • item itemstop
  • top--

13
  • 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)

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

15
Implementing stacks using templates
(cont.)
  • templateltclass ItemTypegt
  • class StackType
  • public
  • StackType()
  • void MakeEmpty()
  • bool IsEmpty() const
  • bool IsFull() const
  • void Push(ItemType)
  • void Pop(ItemType)
  • private
  • int top
  • ItemType itemsMAX_ITEMS

16
Example using templates
  • // Client code
  • StackTypeltintgt myStack
  • StackTypeltfloatgt yourStack
  • StackTypeltStrTypegt 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.

17
Function templates
  • The definitions of the member functions must be
    rewritten as function templates.
  • templateltclass ItemTypegt
  • StackTypeltItemTypegtStackType()
  • top -1
  • templateltclass ItemTypegt
  • void StackTypeltItemTypegtMakeEmpty()
  • top -1

18
Function templates (cont.)
  •  templateltclass ItemTypegt
  • bool StackTypeltItemTypegtIsEmpty() const
  • return (top -1)
  • templateltclass ItemTypegt
  • bool StackTypeltItemTypegtIsFull() const
  • return (top MAX_ITEMS-1)
  •  
  • templateltclass ItemTypegt
  • void StackTypeltItemTypegtPush(ItemType newItem)
  • top
  • itemstop newItem

19
Function templates (cont.)
  • templateltclass ItemTypegt
  • void StackTypeltItemTypegtPop(ItemType item)
  • item itemstop
  • top--

20
Implementing stacks using dynamic array allocation
  • templateltclass ItemTypegt
  • 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
21
Implementing stacks using dynamic array
allocation (cont.)
  • templateltclass ItemTypegt
  • StackTypeltItemTypegtStackType(int max)
  • maxStack max
  • top -1
  • items new ItemTypemax
  • templateltclass ItemTypegt
  • StackTypeltItemTypegtStackType()
  • delete items

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

23
Example postfix expressions(cont.)
24
Postfix expressions Algorithm using stacks
(cont.)
25
Postfix expressionsAlgorithm 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)

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

27
Stack
tempStack
  • ItemType item
  • StackType tempStack
  • while (!Stack.IsEmpty())
  • Stack.Pop(item)
  • if (itemoldItem)
  • tempStack.Push(newItem)
  • else
  • tempStack.Push(item)
  • while (!tempStack.IsEmpty())
  • tempStack.Pop(item)
  • Stack.Push(item)

Stack
oldItem 2 newItem 5
28
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
tail
head
elements enter
elements exit
2
3
4
1
no changes of order
29
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)

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

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

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

33
(No Transcript)
34
(No Transcript)
35
Make front point to the element preceding the
front element in the queue (one memory location
will be wasted).
36
Initialize front and rear
37
Queue is empty now!! rear front
38
Queue Implementation
  • templateltclass ItemTypegt
  • 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
39
Queue Implementation (cont.)
  • templateltclass ItemTypegt
  • QueueTypeltItemTypegtQueueType(int max)
  • maxQue max 1
  • front maxQue - 1
  • rear maxQue - 1
  • items new ItemTypemaxQue

40
Queue Implementation (cont.)
  • templateltclass ItemTypegt
  • QueueTypeltItemTypegtQueueType()
  • delete items

41
Queue Implementation (cont.)
  • templateltclass ItemTypegt
  • void QueueTypeltItemTypegt MakeEmpty()
  • front maxQue - 1
  • rear maxQue - 1

42
Queue Implementation (cont.)
  • templateltclass ItemTypegt
  • bool QueueTypeltItemTypegtIsEmpty() const
  • return (rear front)
  • templateltclass ItemTypegt
  • bool QueueTypeltItemTypegtIsFull() const
  • return ( (rear 1) maxQue front)

43
Queue Implementation (cont.)
  • templateltclass ItemTypegt
  • void QueueTypeltItemTypegtEnqueue (ItemType
    newItem)
  • rear (rear 1) maxQue
  • itemsrear newItem

44
Queue Implementation (cont.)
  • templateltclass ItemTypegt
  • void QueueTypeltItemTypegtDequeue (ItemType
    item)
  • front (front 1) maxQue
  • item itemsfront

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

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

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

48
Example recognizing palindromes
49
Example recognizing palindromes
  • include ltiostream.hgt
  • include ltctype.hgt
  • include "stack.h"
  • include "queue.h
  • int main()
  • StackTypeltchargt s
  • QueTypeltchargt q
  • char ch
  • char sItem, qItem
  • int mismatches 0

cout ltlt "Enter string " ltlt endl  
while(cin.peek() ! '\\n')   cin gtgt ch
if(isalpha(ch))   if(!s.IsFull())
s.Push(toupper(ch))   if(!q.IsFull())
q.Enqueue(toupper(ch))
50
Example recognizing palindromes
  • while( (!q.IsEmpty()) (!s.IsEmpty()) )
  • s.Pop(sItem)
  • q.Dequeue(qItem)
  •  
  • if(sItem ! qItem)
  • mismatches
  • if (mismatches 0)
  • cout ltlt "That is a palindrome" ltlt endl
  • else
  • cout ltlt That is not a palindrome" ltlt endl
  •  
  • return 0

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

52
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

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

54
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com