Homework 7 Andy Wang Object Oriented Programming in
Homework 7 Andy Wang Object Oriented Programming in C++ COP 3330
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
List insert. Middle(value, index) remove. Middle(value, index) Try to reuse other insertion/removal functions as much as possible
insert. Middle Check for an empty list 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
insert. Middle 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 (last. Ptr 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
Visual value next. Ptr first. Ptr List. Node last. Ptr List new. Ptr value next. Ptr List. Node
Visual value next. Ptr first. Ptr List. Node last. Ptr List new. Ptr value next. Ptr List. Node
Visual value next. Ptr first. Ptr List. Node last. Ptr List new. Ptr value next. Ptr List. Node
remove. Middle Check for an empty list Return nothing If the list has a single node If the index is 1, remove the item from the front/back Else, return nothing
remove. Middle 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
Visual value current. Ptr next. Ptr List. Node value next. Ptr first. Ptr List. Node last. Ptr List value next. Ptr List. Node temp. Ptr
Visual value current. Ptr next. Ptr List. Node value next. Ptr first. Ptr List. Node last. Ptr List value next. Ptr List. Node temp. Ptr
Visual value next. Ptr List. Node first. Ptr last. Ptr List value next. Ptr List. Node current. Ptr
- Slides: 13