Question: this is the code : #include #include #include #include using namespace std; class Node { public: int data; Node* next; //Node * prev; }; class
this is the code :
#include
#include
#include
#include
using namespace std;
class Node
{
public:
int data;
Node* next;
//Node * prev;
};
class Stack {
public:
void push(Node** head_ref, int new_data);
Node* pop(Node** head_ref);
void peek(Node** head_ref);
};
// inserting node at the begining of the list
// Node * head pointer
// Node **head pointer of pointer
void Stack::push(Node** head_ref, int new_data) {
if (*head_ref == NULL) {
Node* new_node = new Node(); // dynamic allocation of a new memeory // heap
new_node->data = new_data;
new_node->next = NULL;
(*head_ref) = new_node;
return;
}
else {
Node* new_node = new Node(); // dynamic allocation of a new memeory // heap
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
return;
}
}
// poping node at the begining of the Stack
Node* Stack::pop(Node** head_ref) {
if (*head_ref != NULL) {
Node* poped_node = (*head_ref);
(*head_ref) = (*head_ref)->next;
return poped_node;
}
else {
printf("Error cannot pop from an empty Stack");
return *head_ref;
}
}
string readfile(string filename) {
string line;
ifstream myfile(filename);
if (myfile.is_open()) {
while (getline(myfile, line)) {
cout
}
myfile.close();
return line;
}
else {
cout
return line;
}
}
// poping node at the begining of the Stack
void Stack::peek(Node** head_ref) {
if (*head_ref != NULL) {
printf("top item in stack is %d ", (*head_ref)->data);
}
else {
printf("Error cannot peek an empty Stack");
}
}
int main() {
Node* head = NULL;
Node* result = NULL;
Stack stack1;
stack1.push(&head, 6);
stack1.push(&head, 4);
stack1.push(&head, 3);
stack1.push(&head, 10);
stack1.push(&head, 11);
stack1.peek(&head);
result = stack1.pop(&head);
cout data
Q1: In today's lecture, we saw how to implement Stack using linked list, based on that implement queue using linked list which include the following: 1) Write function to push (enqueue) an item into a queue, remember you need to use another pointer called tail in addition to head? 2) Write a function to pop (dequeue) an item from a queue? 3) Write a function to print items in the queue delete result;
stack1.peek(&head);
cout
system("PAUSE");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
