'Double stack linked list segmentation fault
So I've been asked to make a C++ program Double stack with linked list. I can't find any tutorial or example on Google, most of them use Array to do it. so I have to figure it out how to do this, and end up with Segmentation Fault output in the end, which I still have no idea which part of my code is wrong here
#include <iostream>
using namespace std;
struct Node{
public:
int data;
Node *next;
Node *prev;
};
Node *head = NULL;
Node *tail = NULL;
Node *del = NULL;
void push(Node **head_ref, int data){
Node *temp = new Node;
temp->data = data;
temp->next = head;
head->prev = temp;
head = temp;
}
void push2(Node **head_ref, int data){
Node *temp = new Node;
temp->data = data;
tail->next = temp;
temp->prev = tail;
tail = temp;
}
void pop(Node **head_ref, int data){
del = head;
head->next->prev = NULL;
head = del->next;
del = NULL;
}
void pop2(Node **head_ref, int data){
del = tail;
tail->prev->next = NULL;
tail = del->prev;
del = NULL;
}
int peek(Node **head_ref){
return head->data;
}
int main(){
system("clear");
push2(&head, 10);
}
any help is appreciated
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|