So I have to implement deque using doubly linked list and am supposed to put objects of class Students but I keep getting a sedimentation error. I am basically trying to put data into the object before pushing it. please help as it would mean a lot to me
#include <iostream>
#include<string>
using namespace std;
struct Students
{
string name;
int snum;
void getdata()
{
cout<<"Enter name of student: ";
getline(cin,name);
cout<<"\nEnter Student number: ";;
cin>>snum;
}
void showdata()
{
cout<<"Name: "<<name;
cout<<"\nStudent Number: "<<snum<<"\n";
}
};
struct node
{
Students s;
node *prev, *next;
static node* getnode(Students s)
{
node* newnode = (node*)malloc(sizeof(Students));
newnode->s = s;
newnode->prev = newnode->next = NULL;
return newnode;
}
};
class deque
{
node* front;
node* rear;
int Count;
public:
deque()
{
front = rear = NULL;
Count = 0;
}
void pushFront(Students s);
void pushBack(Students s);
void popFront();
void popBack();
Students peekFront();
Students peekBack();
int count();
bool isEmpty();
void clear();
};
bool deque::isEmpty()
{
return (front == NULL);
}
int deque::count()
{
return Count;
}
void deque::pushFront(Students s)
{
node* newnode = node::getnode(s);
if (newnode == NULL)
cout << "OverFlow\n";
else
{
if (front == NULL)
rear = front = newnode;
//puts node at front
else
{
newnode->next = front;
front->prev = newnode;
front = newnode;
}
// increase by 1
Count++;
}
}
void deque::pushBack(Students s)
{
node* newnode = node::getnode(s);
if (newnode == NULL)
cout << "OverFlow\n";
else
{
if (rear == NULL)
front = rear = newnode;
else
{
newnode->prev = rear;
rear->next = newnode;
rear = newnode;
}
Count++;
}
}
void deque::popFront()
{
if (isEmpty())
cout << "UnderFlow\n";
else
{
node* temp = front;
front = front->next;
if (front == NULL)
rear = NULL;
else
front->prev = NULL;
free(temp);
Count--;
}
}
void deque::popBack()
{
if (isEmpty())
cout << "UnderFlow\n";
else
{
node* temp = rear;
rear = rear->prev;
if (rear == NULL)
front = NULL;
else
rear->next = NULL;
free(temp);
Count--;
}
}
Students deque::peekFront()
{
if (isEmpty())
abort() ;
return front->s;
// s.showdata();
}
Students deque::peekBack()
{
if (isEmpty())
abort();
return rear->s;
}
void deque::clear()
{
rear = NULL;
while (front != NULL)
{
node* temp = front;
front = front->next;
free(temp);
}
Count = 0;
}
// main
int main()
{
deque dq;
Students s1,s2,s3,s4;
s1.getdata();
dq.pushBack(s1);
s2.getdata();
dq.pushBack(s2);
s3.getdata();
dq.pushBack(s3);
s4.getdata();
dq.pushBack(s4);
return 0;
}
I am pretty sure the problem lies in getnode() at the start but I am not entirely sure