#include <iostream>
usingnamespace std;
class Stack
{
private:
/* structure of a linked list node */
struct node
{
int data;
node *next;
};
node* head;
public:
//initialize the stack (constructor)
Stack()
{
head = NULL;
}
/* deleteNode() function deletes first node that matches data */
void deleteNode(int data)
{
node* n = head;
if(n->data == data) // when node to be deleted is head node
{
n->data = n->next->data; // copy the data of next node to head
}
else // when not head node
{ // traverse stack to find the node that matches data
while(n->next != NULL && n->next->data != data)
{
n = n->next;
}
}
if(n->next == NULL) // if node not in Linked List
{
cout << " [ERROR: given data is not present in linked list] ";
return;
}
node *temp = n->next; // store address of next node
n->next = n->next->next; // remove node from Linked List
delete(temp);
return;
}
/* push() function inserts a node at the begining */
void push(int new_data)
{
node *new_head = new node;
new_head->data = new_data;
new_head->next = head;
head = new_head;
}
/* peek() function returns data of head node */
int peek()
{
return head->data;
}
/* Utility function to print a linked list */
void printList()
{
node* n = head;
while(n!=NULL)
{
cout << n->data;
n=n->next;
}
}
};
class A
{
private:
static Stack stack;
public:
void test()
{
stack.push(2);
stack.deleteNode(2);
}
};
// initialize static variable out of class
void A::stack.push(1); // compile error: expected initializer before '.' token
int main()
{
A a;
a.test();
}
would not make sense, since the class has no non-static member named stack ( but a static one )
OMG! This is embarrassing. I'm wrong w/ this part. I guess i need to read back my books again :DD
#include <iostream>
usingnamespace std;
class Stack
{
private:
/* structure of a linked list node */
struct node
{
int data;
node *next;
};
node* head;
public:
//initialize the stack (constructor)
Stack()
{
head = NULL;
push(1); // base node
}
/* deleteNode() function deletes first node that matches data */
void deleteNode(int data)
{
node* n = head;
if(n->data == data) // when node to be deleted is head node
{
n->data = n->next->data; // copy the data of next node to head
}
else // when not head node
{ // traverse stack to find the node that matches data
while(n->next != NULL && n->next->data != data)
{
n = n->next;
}
}
if(n->next == NULL) // if node not in Linked List
{
cout << " [ERROR: given data is not present in linked list] ";
return;
}
node *temp = n->next; // store address of next node
n->next = n->next->next; // remove node from Linked List
delete(temp);
return;
}
/* push() function inserts a node at the begining */
void push(int new_data)
{
node *new_head = new node;
new_head->data = new_data;
new_head->next = head;
head = new_head;
}
/* peek() function returns data of head node */
int peek()
{
return head->data;
}
/* Utility function to print a linked list */
void printList()
{
node* n = head;
cout << " head[ ";
while(n!=NULL)
{
cout << n->data << " ";
n=n->next;
}
cout << "]base" << endl;
}
};
class A
{
private:
public:
static Stack stack;
staticvoid test()
{
stack.push(2);
stack.push(3);
stack.push(4);
cout << "Given Linked List: ";
stack.printList();
cout << "attempt to delete non-existent node" << endl;
stack.deleteNode(9); cout << endl;
cout << "deleting base node";
stack.deleteNode(1);
cout << "\nModified Linked List: ";
stack.printList();
cout << "Deleting node with value 3 ";
stack.deleteNode(3);
cout << "\nModified Linked List: ";
stack.printList();
cout << "head node is " << stack.peek();
cout << "\nDeleting head node ";
stack.deleteNode(stack.peek());
cout << "\nModified Linked List: ";
stack.printList();
cout << "head node is " << stack.peek();
}
};
// initialize static variable out of class
Stack A::stack;
int main()
{
A a;
A b;
cout << "test a:" << endl;
a.test();
cout << endl << endl << "test b:" << endl;
b.test();
}
output:
test a:
Given Linked List: head[ 4 3 2 1 ]base
attempt to delete non-existent node
[ERROR: given data is not present in linked list]
deleting base node
Modified Linked List: head[ 4 3 2 ]base
Deleting node with value 3
Modified Linked List: head[ 4 2 ]base
head node is 4
Deleting head node
Modified Linked List: head[ 2 ]base
head node is 2
test b:
Given Linked List: head[ 4 3 2 2 ]base
attempt to delete non-existent node
[ERROR: given data is not present in linked list]
deleting base node [ERROR: given data is not present in linked list]
Modified Linked List: head[ 4 3 2 2 ]base
Deleting node with value 3
Modified Linked List: head[ 4 2 2 ]base
head node is 4
Deleting head node
Modified Linked List: head[ 2 2 ]base
head node is 2