In case of c++ we have a different way of passing the head pointer to the push and pop functions |
- If we are using the **head notation to pass a reference to the head pointer, then we have to explicitly use *head in the functions and &head while calling the functions.
- But if we are using the *&head to pass the c++ compiler will take care of the extra *and we don't have to put the extra * and & while using the head reference in the function or passing the reference argument respectively.
|
1: #include <stdio.h>
2: #include <iostream>
3:
4: using namespace std;
5:
6: //stack in c++ using link list
7: //using c++ style reference passing for head pointer
8: //ie passing *&head
9:
10: #define INCREMENT 10
11: class stack
12: {
13: typedef struct node
14: {
15: struct node *next;
16: int data;
17: }stackNode;
18:
19: int m_nCapacity;
20: int m_nSize;
21: stackNode *m_pHead;
22: public:
23: stack(int capacity = INCREMENT);
24: stack(const stack &theStack);
25: stack& operator = (const stack &theStack);
26: bool push(stackNode *&head,int data);
27: int pop(stackNode *&head);
28: stackNode*& getHead(){return m_pHead;}
29: void show();
30: };
31:
32: stack::stack(int capacity):m_pHead(NULL),m_nCapacity(INCREMENT),m_nSize(0)
33: {
34: for(int i = 0 ; i < capacity ; i++)
35: push(m_pHead,i);
36: }
37:
38: bool stack::push(stackNode *&head,int val)
39: {
40: stackNode *newNode = new stackNode;
41: newNode->data = val;
42: newNode->next = NULL;
43:
44: if(head == NULL)
45: {
46: head = newNode;
47: m_nSize++;
48: }
49: else
50: {
51: newNode->next = head;
52: head = newNode;
53: m_nSize++;
54: }
55: }
56:
57: int stack::pop(stackNode *&head)
58: {
59: if(head == NULL)
60: return -1;
61:
62: int data;
63: stackNode *tempNode = head;
64: head = head->next;
65: data = tempNode->data;
66: delete(tempNode);
67: tempNode = NULL;
68: m_nSize--;
69: return data;
70: }
71:
72: void stack::show()
73: {
74: cout<<pop(m_pHead);
75: }
76:
77: int main(int argc, char **argv)
78: {
79: stack mystack;
80: mystack.show();
81: cout<<mystack.pop(mystack.getHead());
82:
83: printf("hello world\n");
84: getchar();
85: return 0;
86: }
87:
1: #include <stdio.h>
2: #include <iostream>
3:
4: using namespace std;
5:
6: //stack in c++ using link list
7: //using c style reference for passing head pointer
8: //ie passing **head
9: #define INCREMENT 10
10: class stack
11: {
12: typedef struct node
13: {
14: struct node *next;
15: int data;
16: }stackNode;
17:
18: int m_nCapacity;
19: int m_nSize;
20: stackNode *m_pHead;
21: public:
22: stack(int capacity = INCREMENT);
23: stack(const stack &theStack);
24: stack& operator = (const stack &theStack);
25: bool push(stackNode **head,int data);
26: int pop(stackNode **head);
27: stackNode** getHead(){return &m_pHead;}
28: void show();
29: };
30:
31: stack::stack(int capacity):m_pHead(NULL),m_nCapacity(INCREMENT),m_nSize(0)
32: {
33: for(int i = 0 ; i < capacity ; i++)
34: push(&m_pHead,i);//using &head
35: }
36:
37: bool stack::push(stackNode **head,int val)
38: {
39: stackNode *newNode = new stackNode;
40: newNode->data = val;
41: newNode->next = NULL;
42:
43: if(head == NULL)
44: {
45: *head = newNode;//the *head is the actual head
46: m_nSize++;
47: }
48: else
49: {
50: newNode->next = *head;
51: *head = newNode;
52: m_nSize++;
53: }
54: }
55:
56: int stack::pop(stackNode **head)
57: {
58: if(*head == NULL)
59: return -1;
60:
61: int data;
62: stackNode *tempNode = *head;
63: *head = (*head)->next;//braces are must
64: data = tempNode->data;
65: delete(tempNode);
66: tempNode = NULL;
67: m_nSize--;
68: return data;
69: }
70:
71: void stack::show()
72: {
73: cout<<pop(&m_pHead);//using &head
74: }
75:
76: int main(int argc, char **argv)
77: {
78: stack mystack;
79: mystack.show();
80: cout<<mystack.pop(mystack.getHead());
81:
82: printf("hello world\n");
83: getchar();
84: return 0;
85: }
86:
No comments:
Post a Comment