Download presentation
Presentation is loading. Please wait.
1
Andy Wang Object Oriented Programming in C++ COP 3330
Homework 7 Andy Wang Object Oriented Programming in C++ COP 3330
2
Palindrome #include “stack.h”
Use Stack<char> to declare a stack of characters Use getline to fetch input Skip non alphabets (use cstring library) Use the same upper/lower case One algorithm Go through the input line forward, push each character to stack 1 Go through the input line backward, push each character to stack 2 Loop and pop both at the same time to check for palindrome
3
List insertMiddle(value, index) removeMiddle(value, index)
Try to reuse other insertion/removal functions as much as possible
4
insertMiddle Check for an empty list If the list has one node
Just allocate a new node and set first and last pointers to the new node If the list has one node Check whether the insertion index less or equal to one Insert to the front Else insert to the back
5
insertMiddle If the list has more than one node
Check whether the index is less or equal to one Insert to the front Traverse the list to find the node before the insertion point Check if the index is out of bound (lastPtr is reached) Insert at the back Else Create a new node Make the new node point to the next node Make the current node point to the new node Update the last pointer as needed
6
Visual value nextPtr ListNode firstPtr lastPtr List newPtr value
7
Visual value nextPtr ListNode firstPtr lastPtr List newPtr value
8
Visual value nextPtr ListNode firstPtr lastPtr List newPtr value
9
removeMiddle Check for an empty list If the list has a single node
Return nothing If the list has a single node If the index is 1, remove the item from the front/back Else, return nothing
10
removeMiddle If the list has more than one node
Check if the index is 1 Remove from the front Else traverse through the list and locate the node before the node to be removed If the node to be removed is the last node, just remove the last node If the index is out of bound, return nothing Use a temporary pointer to track the node to be deleted Make the current node point to the next node of the node to be deleted Extract data and deallocate the temp node
11
Visual value nextPtr ListNode currentPtr firstPtr lastPtr List value
tempPtr value nextPtr ListNode
12
Visual value nextPtr ListNode currentPtr firstPtr lastPtr List value
tempPtr value nextPtr ListNode
13
Visual value nextPtr ListNode currentPtr firstPtr lastPtr List value
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.