Help with this program in C++ A better storage structure for a two-stack data ty
ID: 3765940 • Letter: H
Question
Help with this program in C++
A better storage structure for a two-stack data type that would be to use a single array for the storage structure and let the stacks grow toward each other.
Design a class for this two-stack data type using this implementation and a dynamic array. In your functions for the basic stack operations, the number of the stack to be operated upon, 1 or 2, should be passed as a parameter. Also, the push operation should not fail because of a stack-full condition until all locations in the storage array have been used.
Explanation / Answer
#include <iostream.h> // input output stream header file
#include <stdio.h> // standard input output header file
#include <stdlib.h> // standard library header file
//#include <string.h> // header file with string function
#include <conio.h> // console input output header file
// Define the structure tag and node
// future variables can be created based on this structure
struct tag { // node = tag
int data;
struct tag *next;
} *node; // end struct tag // top = node
struct tag *first, *last, *head, *temp, *tail, *node1;
tag * funcPush(tag *node, int data1) {
tag *temp;
// allocate memory for a new node
temp = new ( struct tag ); // new is the equivalent of malloc
temp->data = data1;
temp -> next = node;
node = temp;
return node;
} // end func push
void funcDisplay(tag *node) {
tag *pointer;
pointer = node;
if (node != NULL) { // non empty stack can traverse
cout << " Items in the stack are as follows: " ;
while ( pointer != NULL) {
cout << pointer->next << " , ";
// progress on the linked list of stack
// pointer = pointer -> link;
} // end while
} // end if
} // end func display
int main() // start main
{
clrscr();
funcPush( node , 5);
funcPush( node , 6);
funcPush( node , 7);
funcPush( node , 9);
funcPush( node , 10);
funcDisplay(node);
return 0; // exit
} // end of main()
//////////////////////////**********************************////////////////////////
// makings of the program - the below can be ignored
//////////////////////////**********************************////////////////////////
/* temp->data = 5;
temp->next = null;
head = temp;
first = temp; // only for the first node hence out side the loop
*/
// following can go into a loop
// temp = (struct tag)malloc(sizeof(node)); // unlike C, C++ does not need malloc() = memory allocate, thanks to the new key word of C++
/* temp->data = 6;
temp->next = null;
head->next = temp;
head = temp;
*/
/*
temp->data = 7;
temp->next = null;
head->next = temp
head = temp;
*/
// loop again
/*
head = first;
int i;
for (i=1; i<=3; i++)
cout << " " << head->data << " , " ;
head = head->next;
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.