A) Static Stack Create an array of student records (at least 15) and put all the
ID: 3671733 • Letter: A
Question
A) Static Stack
Create an array of student records (at least 15) and put all the related student records information in an array: Student ID(Axxxxxxxx), Name, Address and average of test scores and a Pointer for Test Scores (at least 10 scores).
-Display the student records in the array (do not display pointers).
-Push all student records from the array to a static stack
-Pop 5 student records from the stack and display the popped records.
-Display the remaining student records in the static stack.
(Loops are needed, Stack Class definition must be used, using template and container is optional. (C++))
please help me, i'm lost on this code, please?
Explanation / Answer
Stack.cpp
#include "stack.h"
#include <cstdlib>
#include <iostream>
#ifndef NULL
#define NULL 0
#endif
using namespace std;
stack::stack()
{
/* initialize our stack */
for(int i = 0 ; i < STACK_SIZE ; i++)
{
stackarray[i] = NULL;
}
index = -1;
}
stack::~stack()
{
for(int i = 0 ; i < STACK_SIZE ; i++)
{
delete stackarray[i];
}
// delete stackarray;
}
void stack::push(Student* stu)
{
if(index == STACK_SIZE - 1)
{
cout << "There you go, trying to smash the stack..." << endl;
return;
}
else
{
stackarray[++index] = stu;
}
}
Student* stack::pop()
{
if(index < 0)
{
cout << "Stack is empty. " << endl;
exit(-1);
}
else
{
Student* temp;
temp = stackarray[index];
stackarray[index] = NULL;
index--;
return temp;
}
}
bool stack::isEmpty()
{
if(index < 0)
{
return true;
}
else
{
return false;
}
}
stack.h
#ifndef stack_h_
#define stack_h_
#include "student.h"
class stack
{
private:
static const int STACK_SIZE = 100;
Student* stackarray[STACK_SIZE]; /* 100 stack frames oughta be enough for anybody... */
int index;
public:
stack();
virtual ~stack();
void push(Student*);
Student* pop();
bool isEmpty();
};
#endif
student.h
#ifndef student_h_
#define student_h_
/* This will be the student object */
#include <string>
#include <vector>
class Student
{
private:
std::string id;
std::string name;
std::string address;
double gpa;
std::vector<double> grades;
public:
Student();
Student(std::string,std::string,std::string,double);
virtual ~Student();
Student(const Student&);
Student& operator=(const Student&);
void display();
/* The rubric calls for an "APPEND" function. Not sure exactly what we
would be "appending" to a student object. Editing perhaps, but not
appending.
*/
void setInfo(std::string,std::string,std::string,double);
//This won't actually work atm...
//void getInfo();
void setId(std::string);
void setName(std::string);
void setAddress(std::string);
void setGpa(double);
void addGrade(double);
std::string getId();
std::string getName();
std::string getAddress();
double getGpa();
const std::vector<double> getGrades();
};
#endif
main.cpp
#include "stack_ll.h"
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>
#include "../lab1/ll.h"
using namespace std;
void addFileToStack(ifstream&,stack_ll*);
void addFileToList(ifstream&,ll*);
int main(int argc, char *argv[])
{
stack_ll* st = new stack_ll;
ll* list = new ll;
ifstream ifs;
ifs.open("input.txt");
cout << " Adding student objects to our linked list. ";
addFileToList(ifs, list);
cout << " Proceeding to push 5 students from list onto stack. ";
for(int i = 0; i < 5 ; i++)
{
Student* temp;
temp = list->getStudent(i);
st->push(temp);
}
cout << " Pushing remaining students onto stack ";
for(int i = 5 ; i < 20 ; i++)
{
Student* temp;
temp = list->getStudent(i);
st->push(temp);
}
cout << " Proceeding to pop all students off the stack ";
for(int i = 0 ; i < 20 ; i++)
{
Student* temp;
temp = st->pop();
temp->display();
delete temp;
}
delete st;
delete list;
return 0;
}
void addFileToStack(ifstream& input,stack_ll* sp)
{
while( input.good() && input.peek() != EOF )
{
string id,name,address,gpastring;
getline(input,id,';');
getline(input,name,';');
getline(input,address,';');
getline(input,gpastring,' ');
double gpa;
gpa = atof(gpastring.c_str());
Student *temp = new Student(id,name,address,gpa);
sp->push(temp);
}
/* Put ifs seeker back to beginning of file stream */
input.seekg(0, ios::beg);
}
void addFileToList(ifstream& input,ll* list)
{
while( input.good() && input.peek() != EOF )
{
string id,name,address,gpastring;
getline(input,id,';');
getline(input,name,';');
getline(input,address,';');
getline(input,gpastring,' ');
double gpa;
gpa = atof(gpastring.c_str());
Student *temp = new Student(id,name,address,gpa);
list->insertTail(temp);
}
/* Put ifs seeker back to beginning of file stream */
input.seekg(0, ios::beg);
}
stack_11.h
#ifndef stack_ll_h_
#define stack_ll_h_
/* This implementation of the stack is a singly-linked list */
#include "../student.h"
#include <iostream>
using namespace std;
class stack_ll
{
private:
struct node {
Student *student;
node *next;
~node(){delete student;}
};
node *head;
// node *tail;
public:
stack_ll();
virtual ~stack_ll();
void push(Student*);
Student* pop();
void deleteStackContents();
};
#endif
stack_11.cpp
/* Doubly Linked List */
#include "stack_ll.h"
#include <iostream>
#include <string>
#include <cstdlib>
#ifndef NULL
#define NULL 0
#endif
using namespace std;
stack_ll::stack_ll()
{
head = NULL;
// tail = NULL;
}
stack_ll::~stack_ll()
{
cout << "Called stack_ll destructor" << endl;
deleteStackContents();
delete head;
// delete tail;
}
void stack_ll::push(Student* stu)
{
/* Test if empty list */
if (head == NULL)
{
node *temp = new node;
temp->student = stu;
temp->next = NULL;
head = temp;
return;
}
/* add (first) to tail */
else
if (head->next == NULL)
{
node *temp = new node;
temp->student = stu;
temp->next = NULL;
head->next = temp;
return;
}
/* add to tail */
node *temp;
temp = head;
while(temp->next != NULL)
{
temp = temp->next;
}
node *temp2 = new node;
temp2->student = stu;
temp2->next = NULL;
temp->next = temp2;
return;
}
Student* stack_ll::pop()
{
node *temp;
temp = head;
/* newtop will hold a pointer to the new top of the stack */
node *newtop;
newtop = NULL;
/* Empty list check */
if(temp == NULL)
{
cout << "List is empty. Can't return anything." << endl;
exit(-1);
}
/* Find the top of the stack */
while(temp->next != NULL)
{
newtop = temp;
temp = temp->next;
}
/* We are returning a pointer to a copy of the top of the stack */
/* In this case, we return temp3 */
Student temp2;
temp2 = *(temp->student);
Student* temp3 = new Student(temp2);
/* So long as there is more than one element in stack, this is evaluated */
if(newtop != NULL)
{
newtop->next = NULL; /* Make sure new top of stack's ->next is NULL */
}
else
{
head = newtop; /* newtop is NULL here; we've popped the last element */
}
/* We now delete the top of the stack */
delete temp;
/* And here we actually return our copy (non-const, so is manipulable)*/
return temp3;
}
void stack_ll::deleteStackContents()
{
cout << "Deleting list..." << endl;
if(head == NULL)
{
cout << "Empty list. Nothing to delete." << endl;
return;
/* What if head get's set to NULL inappropriately? Then
* memory still isn't free'd and we can't get to it!
* Gotta make sure that doesn't happen.
*/
}
node *temp;
while(head->next != NULL)
{
temp = head->next;
head->next = temp->next;
delete temp;
}
cout << "Attempting to delete head... " << endl;
delete head;
head = NULL;
cout << "Head deleted." << endl;
/* Don't need to delete tail. We deleted its contents in the while loop
* above
*/
// tail = NULL;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.