I need to write a c++ program that when given a starting character and
an end character will print all possible combinations. it is based on
the concept of using a stack. it must also be based on the given
algorithm. i am including the information that i have been given for
this problem.
The Assignment:
1. You will use both array and link-list versions of stack class and
add to these classes the new method (member function) peek. The new
function differs from the top in ability to ?peek? not only the top
element but also any entry down the stack. Hence the new function is
taking positive integer parameter n between 1 and the size of the
stack and return the value of the entry n positions down the stack. If
no parameter is provided then by default the top entry is returned.
2. Using this extended version of stack class (you will have two
versions) you will write a program that will print to the screen all
strings with at most n characters in a given range first -- last.
For your convenience I am providing you with the algorithm that should
do that. Your job is to understand the algorithm and correctly
translate it to the C++ language.
The algorithm you need to translate:
Push the first character onto the stack
While the stack is not empty
Print the whole content of the stack using the new peek function
If the stack contains fewer than n letters -- push first onto the stack
Else
Pop characters off the stack, until either the stack is empty or there
is a character other than last on the top. In case when the top
character is not last -- nothing is popped out
If the stack is not empty
Pop the top one (which we may call the current)
Push the next letter (current + 1) onto the stack
Sample dialogs:
1.
Please enter the first character: a
Please enter the last character: b
Please enter the n: 3
a
aa
aaa
baa
ba
aba
bba
b
ab
aab
bab
bb
abb
bbb
this is the dynamic array stack template class:
// FILE: stack1.template
// TEMPLATE CLASS IMPLEMENTED: stack<Item> (see stack1.h for documentation)
// This file is included in the header file, and not compiled separately.
// INVARIANT for the Stack ADT:
// 1. The number of items in the Stack is in the member variable used.
// 2. The actual items of the Stack are stored in a partially-filled
// array data[0]..data[used-1]. The stack elements appear from the
// bottom (at data[0]) to the top (at data[used-1]).
#include <assert.h> // Provides assert
#include <stdlib.h> // Provides size_t
template <class Item>
void stack<Item>::push(const Item& entry)
// Library facilities used: assert.h
{
assert(size( ) < CAPACITY);
data[used] = entry;
used++;
}
template <class Item>
Item stack<Item>::pop( )
// Library facilities used: assert.h
{
assert(!is_empty( ));
used--;
return data[used];
}
//Implement the peek function here
this is the linked list stack template class that i am using:
// FILE: stack2.template
// TEMPLATE CLASS IMPLEMENTED: stack<Item> (see stack2.h for documentation)
// This file is included in the header file, and not compiled separately.
// INVARIANT for the stack ADT:
// 1. The items in the stack are stored in a linked list, with the top of the
// stack stored at the head node, down to the bottom of the stack at the
// final node.
// 2. The member variable top_ptr is the head pointer of the linked list.
#include <assert.h> // Provides assert
#include <stdlib.h> // Provides size_t
#include "linkplus.h" // Linked list toolkit
template <class Item>
stack<Item>::stack(const stack<Item>& source)
// Library facilities used: link2.h
{
node<Item> *tail_ptr; // Needed for argument of list_copy
list_copy(source.top_ptr, top_ptr, tail_ptr);
}
template <class Item>
void stack<Item>::push(const Item& entry)
// Library facilities used: link2.h
{
list_head_insert(top_ptr, entry);
}
template <class Item>
Item stack<Item>::pop( )
// Library facilities used: assert.h, link2.h
{
Item answer;
assert(!is_empty( ));
answer = top_ptr->data();
list_head_remove(top_ptr);
return answer;
}
template <class Item>
void stack<Item>::operator =(const stack<Item>& source)
// Library facilities used: link2.h
{
node<Item> *tail_ptr; // Needed for argument of list_copy
if (source.top_ptr == top_ptr) // Handle self-assignment
return;
list_clear(top_ptr);
list_copy(source.top_ptr, top_ptr, tail_ptr);
}
template <class Item>
Item stack<Item>::top( ) const
// Library facilities used: assert.h
{
assert(!is_empty( ));
return top_ptr->data();
}
//Implement here the peek function
this is the supporting linkplus file if needed for reference:
// FILE: linkplus.cpp
// IMPLEMENTS: The 10 functions of the expanded linked list toolkit
// (see linkplus.h). The four new functions implemented at the bottom of
// the file were implemented by __________(your name and email address)____.
#include <cassert> // Provides assert
#include <cstdlib> // Provides NULL and size_t
template<class Item>
node<Item>::node(const Item& init_data, node<Item>* init_link)
{
data_field = init_data;
link_field = init_link;
}
template<class Item>
void node<Item>::set_data(const Item& new_data)
{
data_field = new_data;
}
template<class Item>
void node<Item>::set_link(node<Item>* new_link)
{
link_field = new_link;
}
template<class Item>
Item node<Item>::data() const
{
return data_field;
}
template<class Item>
const node<Item>* node<Item>::link() const
{
return link_field;
}
template<class Item>
node<Item>* node<Item>::link()
{
return link_field;
}
template<class Item>
size_t list_length(const node<Item>* head_ptr)
// Library facilities used: stdlib.h
{
const node<Item> *cursor;
size_t answer;
answer = 0;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link())
answer++;
return answer;
}
template<class Item>
void list_head_insert(node<Item>*& head_ptr, const Item& entry)
{
head_ptr = new node<Item> (entry, head_ptr);
}
template<class Item>
void list_insert(node<Item>* previous_ptr, const Item& entry)
{
node<Item>* insert_ptr;
insert_ptr = new node<Item>;
insert_ptr->set_data(entry);
insert_ptr->set_link(previous_ptr->link());
previous_ptr->set_link(insert_ptr);
}
template<class Item>
node<Item>* list_search(node<Item>* head_ptr, const Item& target)
// Library facilities used: stdlib.h
{
node<Item> *cursor;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link())
if (target == cursor->data())
return cursor;
return NULL;
}
template<class Item>
node<Item>* list_locate(node<Item>* head_ptr, int position)
// Library facilities used: assert.h, stdlib.h
{
node<Item> *cursor;
size_t i;
assert (0 < position);
cursor = head_ptr;
for (i = 1; (i < position) && (cursor != NULL); i++)
cursor = cursor->link();
return cursor;
}
template<class Item>
const node<Item>* list_locate(const node<Item>* head_ptr, int position)
// Library facilities used: assert.h, stdlib.h
{
const node<Item> *cursor;
size_t i;
assert (0 < position);
cursor = head_ptr;
for (i = 1; (i < position) && (cursor != NULL); i++)
cursor = cursor->link();
return cursor;
}
template<class Item>
void list_head_remove(node<Item>*& head_ptr)
{
node<Item> *remove_ptr;
remove_ptr = head_ptr;
head_ptr = head_ptr->link();
delete remove_ptr;
}
template<class Item>
void list_remove(node<Item>* previous_ptr)
{
node<Item> *remove_ptr;
remove_ptr = previous_ptr->link();
previous_ptr->set_link(remove_ptr->link());
delete remove_ptr;
}
template<class Item>
void list_clear(node<Item>*& head_ptr)
// Library facilities used: stdlib.h
{
while (head_ptr != NULL)
list_head_remove(head_ptr);
}
template<class Item>
void list_copy(node<Item>* source_ptr, node<Item>*& head_ptr,
node<Item>*& tail_ptr)
// Library facilities used: stdlib.h
{
head_ptr = NULL;
tail_ptr = NULL;
// Handle the case of the empty list
if (source_ptr == NULL)
return;
// Make the head node for the newly created list, and put data in it
list_head_insert(head_ptr, source_ptr->data());
tail_ptr = head_ptr;
// Copy the rest of the nodes one at a time, adding at the tail of new list
source_ptr = source_ptr->link();
while(source_ptr !=NULL)
{
list_insert(tail_ptr, source_ptr->data());
tail_ptr = tail_ptr->link();
source_ptr = source_ptr->link();
}
}
template<class Item>
void list_piece(const node<Item>* start_ptr, const node<Item>*
end_ptr, node<Item>*& head_ptr, node<Item>*& tail_ptr)
// Library facilities used: stdlib.h
{
head_ptr = NULL;
tail_ptr = NULL;
// Handle the case of the empty list
if (start_ptr == NULL)
return;
// Make the head node for the newly created list, and put data in it
list_head_insert(head_ptr, start_ptr->data());
tail_ptr = head_ptr;
if (start_ptr == end_ptr)
return;
// Copy the rest of the nodes one at a time, adding at the tail of new list
for (start_ptr = start_ptr->link(); start_ptr != NULL; start_ptr =
start_ptr->link())
{
list_insert(tail_ptr, start_ptr->data());
tail_ptr = tail_ptr->link();
if (start_ptr == end_ptr)
return;
}
} |