pointers pass by value

I have two concerns I passed pointer as argument by value in the code below in
function initNode, addNode and several others the argument im thinking were pass by value. The changes to the link list were made successfully. I tried using same approach to changing insertFront and reverse member function but they did not change list. So i used a pointer to a pointer in parameter list and used the & operator and passed head the first node to list.
1) Why could i sucesfully init node and add node by pass by value and not reverse list or insertfront of list.
2)Usually in argument list with pointers you use & and variable name in argument list Im thinking the address is passed to function. What happens when the variable name is an address and the & operator is used (insertFront(&head,60))? Is the address of an address passed to function? Is that why I had to use pointer to pointer in parameter list.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
  
#include "stdafx.h"
#include<iostream>

	
class	node{
public:
	int  data;
		node *next;
	};

class list
{
private:
	//    node *head= new node;

public:
	void initNode(node *head,int nodeData);
        void addNode( node *head,int newdata);
	void insertFront(node **head,int frontData);
	 node *searchNode(node *head,int key);
	 bool deleteNode(node **head, node *delptr);
       void reverse(node **head );
	 node *copyLinkedlist(node *head);
        void deletelinklist(node **head);
	void display(node *head);
};

void list::initNode(node *head,int nodeData)
{
		head->data = nodeData;
		head->next = NULL;
}

void list::addNode(node *head,int newdata)
	{
		using namespace std;
		node *current = head; 
		
	while(current->next !=NULL)
	 {
        
		 current = current->next;
     }
      if (current->next == NULL)
		 {
          node *newNode = new node;
		  current->next = newNode;
		  newNode->data = newdata;
		  newNode->next = NULL;
		 } 
	}

void list::display( node *head)
	{
		using namespace std;
		node *cur = head;
		while(cur)
		{
          cout<<"data = "<< cur->data<< endl;
		  cur = cur->next;
		}
	} 
void list::insertFront( node **head,int frontData)
	{
		node *frontNode = new node;
		frontNode->next = *head;
		frontNode->data = frontData;
		*head = frontNode;

	}

  node *list::searchNode(node *head,int key)
	{
		node *cur = head;
		node *nodeFound = cur;
		using namespace std;
		while(cur)
		{
			if (cur->data == key)
				{
				nodeFound = cur;
		        return nodeFound;
			    }
			cur = cur->next;
		}	
		   
		return nodeFound;
	}

bool list::deleteNode( node **head, node *delptr)
	{
		node *cur = *head;
		node *tempNextptr= cur;
		using namespace std;
		//cout<<"cur= "<<cur<<endl;
		//cout<<"*head= "<<*head<<endl;

		 while(cur)
		{
			if (cur == delptr)
			{
		        tempNextptr->next = delptr->next;
				if(cur == *head)
					*head = cur->next;
				
				delete cur;
				return true;
			}
	
			    tempNextptr = cur;
			    cur = cur->next;
		}

          return false;
	}

void list::reverse( node **head )
	{
       node *parent = *head;
	   node *me = parent->next;
	   node *child = me->next;
	  
	   using namespace std;	    
	   parent->next = NULL;
	   while(child)
	   {
		me->next = parent;
		parent = me;
		me = child; 
        child = child->next;
	   }
	     me->next = parent;
       	 *head = me;
	}



 node	*list::copyLinkedlist( node *head)
	{
		using namespace std;
		node *cur = head;
		node *newHead;
		
		while(cur)
		{
           node *newNode = new node;
		   if(cur == head)
			  newHead = newNode;

		   newNode->data = cur->data;
           newNode->next = cur->next;
		   cur = cur->next;
		}
		return newHead;
	}

/*	comparelinklist()
	{

	}
*/
void list::deletelinklist(node **head)
	{
		node *cur = *head;
		node *tmptr; 
		while (cur)
		 {
			 if (cur->next == NULL)
				 delete cur;
			 else
			 {
			   tmptr = cur->next;
			   delete cur;
			 }
			 cur = tmptr;
		 }
	}


int main()
{
	using namespace std;
    node *head= new node;
	node *newList,*newNode;
    node *found;
	bool remove;

  list lnklst;

	lnklst.initNode(head,10);
	lnklst.display(head);
    lnklst.addNode(head,20);
	lnklst.display(head);
	lnklst.addNode(head,30);
	lnklst.display(head);
	found = lnklst.searchNode(head,30);
	//cout<<"search node = "<< found->data<< endl;
	remove = lnklst.deleteNode(&head,found);
	//cout<<"head= "<<head<<endl;
    lnklst.display(head);
	lnklst.addNode(head,40);
	lnklst.addNode(head,50);
	lnklst.display(head);
	newList = lnklst.copyLinkedlist(head);
	cout<<"newlist"<<endl;
	lnklst.display(newList);
	lnklst.insertFront(&head,60);
	lnklst.display(head);
	cout<<"newNode list"<<endl;
    newNode = lnklst.copyLinkedlist(head);
	lnklst.display(newNode);

    //deletelinklist(&head);
	//display(head);

	cout<<"reverse list"<<endl;
    lnklst.reverse(&head);
	lnklst.display(head);

	if(remove)
	  cout<<"true"<<endl;
	else
	  cout<<"false"<<endl;

         
	cin.clear();
	cin.ignore(255,'/n');
	cin.get();

	return 0;
}
Last edited on
Topic archived. No new replies allowed.