CSC 326-Lab 3: Implementing a Stack Using a Run-Time Array 1. Given the followin
ID: 3593846 • Letter: C
Question
CSC 326-Lab 3: Implementing a Stack Using a Run-Time Array 1. Given the following specification of the top operation: ItemType top Function Precondition Postconditions: Function value copy of item at top of stack. Stack is not changed. Returns copy of the last item put onto the stack. Stack is not empty. Write the function top as a nonmember function of the StackType. 2. Implement the following operation as a member function of the StackType class replace(ItemType oldltem, ItemType newItem) Function Precondition: Postconditions: Each occurrence of oldltem in stack has been changed to newltem. Replaces all occurrences of oldltem with newltem Stack has been initialized. Write the function replace(ltem Type oldltem, ItemType newltem) as a new member function of the StackType Write the function replace(ltemType oldltem, ItemType newltem) as a nonmember function of the StackType 3. Implement the following operation as a member function of the StackType class bool identical(StackType& stackl) Function Determines if stackI and self are identical. Precondition: Postconditions stackl and self have been initialized stackl and self are unchanged. Function value (self and stackl are identical). Write the function bool identical(StackTyped stackI) as a new member function of the StackType. Write a member function display to output the values stored in the stack. Write a test driver (epp) file to test all member and nonmember functions.Explanation / Answer
1)
ItemType top(StackType obj) function should be declared with friend keyword inside StackType like this
friend ItemType top(StackType obj) ;
//function definition outside class will have no friend keyword
//to access the top pointer which points to top of the Stack,, we need to declare top function as friend of StackType so that it can access data pointed y top pointer
ItemType top(StackType obj)
{
return obj.top->data;
}
2)
void replace(ItemType oldItem, ItemType newItem)
{
Node *cur = head;
while (cur != NULL)
{
if (cur->data == oldItem)
{
cur->data = newItem;
}
cur = cur->next;
}
}
3) below functions should be declared inside StackType class and also defined these functions inside class,,otherwise we need to use scope rsolution operator if defining these function outside class
void replace(ItemType oldItem, ItemType newItem)
{
Node *cur = head;
while (cur != NULL)
{
if (cur->data == oldItem)
{
cur->data = newItem;
}
cur = cur->next;
}
}
bool indenticle(StackType &obj)
{
if (obj.head == head && top == obj.top)
{
return true;
}
else
return false;
}
void display()
{
Node *cur = head;
while (cur != NULL)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf(" ");
}
-----------------------------------------------------------------------------------------
//complete program with testing of functions and output given
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef int ItemType;
struct node
{
ItemType data;
struct node *next;
};
typedef struct node Node;
class StackType
{
Node *head;
Node *top;
public:
StackType()
{
head = NULL;
top = NULL;
}
void push(ItemType val)
{
Node *cur = head, *newNode;
newNode = new Node;
newNode->data = val;
newNode->next = NULL;
if (head == NULL)
{
head = newNode;
top = head;
}
else
{
while (cur->next != NULL)
cur = cur->next;
cur->next = newNode;
top = newNode;
}
}
friend ItemType top(StackType obj);
void replace(ItemType oldItem, ItemType newItem)
{
Node *cur = head;
while (cur != NULL)
{
if (cur->data == oldItem)
{
cur->data = newItem;
}
cur = cur->next;
}
}
bool indenticle(StackType &obj)
{
if (obj.head == head && top == obj.top)
{
return true;
}
else
return false;
}
void display()
{
Node *cur = head;
while (cur != NULL)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf(" ");
}
};
//to access the top pointer which points to top of the Stack,, we need to declare top function as friend of StackType so that it can access data pointed y top pointer
ItemType top(StackType obj)
{
return obj.top->data;
}
int main(int argc, char **argv)
{
StackType st1, st2;
//push items on stack
st1.push(1);
st1.push(2);
st1.push(3);
st1.push(4);
//print st1
printf("Stack contains: ");
st1.display();
//push items on to st2
st2.push(5);
st2.push(6);
st2.push(7);
st2.push(8);
//print st1
printf("Stack contains: ");
st2.display();
//now print top of the st1 and st2 using non member function top
printf("top of the st1 = %d ",top(st1));
printf("top of the st2 = %d ",top(st2));
//now repace old items with new item
st1.push(3);
st1.push(10);
st1.display();
st1.replace(3,5);
st1.display();
//now test identicle function
if (st1.indenticle(st1) == true)
{
printf("stacks are identicle ");
}
else
{
printf("stacks are not identicle ");
}
if (st1.indenticle(st2) == true)
{
printf("stacks are identicle ");
}
else
{
printf("stacks are not identicle ");
}
}
--------------------------------------------------------------------------
//output
Stack contains: 1 2 3 4
Stack contains: 5 6 7 8
top of the st1 = 4
top of the st2 = 8
1 2 3 4 3 10
1 2 5 4 5 10
stacks are identicle
stacks are not identicle