This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int Stack::pop() | |
{ | |
return Q1.deque(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Stack | |
{ | |
private: | |
// two queue | |
Queue Q1, Q2; | |
public: | |
// push method to add data element | |
void push(int); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Stack :: push(int x) | |
{ | |
// move all elements in Q1 to Q2 | |
while(!Q1.isEmpty()) | |
{ | |
int temp = Q1.deque(); | |
Q2.enque(temp); | |
} | |
// add the element which is pushed into Stack |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct node | |
{ | |
int data; // Data | |
node *prev; // A reference to the previous node | |
node *next; // A reference to the next node | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Doubly_Linked_List | |
{ | |
private: | |
node *front; // points to first node of list | |
node *end; // points to first las of list | |
public: | |
Doubly_Linked_List() | |
{ | |
front = NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Doubly_Linked_List::add_before(node *n, int d) | |
{ | |
node *temp; | |
temp = new node(); | |
temp->data = d; | |
temp->next = n; | |
temp->prev = n->prev; | |
n->prev = temp; | |
//if node is to be inserted before the first node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Doubly_Linked_List :: add_after(node *n, int d) | |
{ | |
node *temp; | |
temp = new node(); | |
temp->data = d; | |
temp->prev = n; | |
temp->next = n->next; | |
n->next = temp; | |
//if node is to be inserted after the last node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Doubly_Linked_List::add_end(int d) | |
{ | |
// create new node | |
node *temp; | |
temp = new node(); | |
temp->data = d; | |
temp->prev = end; | |
temp->next = NULL; | |
// if list is empty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Doubly_Linked_List::delete_node(node *n) | |
{ | |
// if node to be deleted is first node of list | |
if( n->prev == NULL ) | |
{ | |
front = n->next; //the next node will be front of list | |
front->prev = NULL; | |
} | |
// if node to be deleted is last node of list | |
else if( n->next == NULL ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Doubly_Linked_List::forward_traverse() | |
{ | |
node *trav; | |
trav = front; | |
while( trav != NULL ) | |
{ | |
cout << trav -> data << endl; | |
trav = trav -> next; | |
} | |
} |