linked lists c++, pass in and pass out

the code works, but im having a little trouble with the output.

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
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>


typedef
struct node{
int data1;
node *p;
} nodetype;

void deleteAll(nodetype *start){
nodetype *t1 = start;
nodetype *t2 = start;
int size = 0;

for(int i=0; i<size; i++){
t2=t2->p;
delete t1;
t1=t2;
}

start=NULL;
}

void startlist(nodetype **start){
nodetype *firstone = new nodetype;
nodetype *temp;
(*start)=firstone;
for(int i=0; i<10; i++){
temp=new nodetype;
temp->data1=random(1000);
if(firstone == NULL){
firstone=temp;
firstone=temp;
} // if
else{
firstone->p=temp;
firstone=temp;
} // else
} //for
temp->p = NULL;
} // startlist



void addNode(nodetype *start, int pos, int value){
nodetype *n=new nodetype;
n->data1=value;
nodetype *temp=start;

if(pos==1){ n->p=start;
start=n; } // if

else{
for(int i=0; i<pos-2; i++) temp=temp->p;
n->p = temp->p; temp->p=n;
} // else

} // addnode

void deleteNode(nodetype *start, int pos){
nodetype *temp=start;
nodetype *d = new nodetype;
if(pos==1){
start=start->p;
delete temp;
} //if
else{
for(int i=0; i<pos-1; i++){
d = temp;
temp = temp->p;
} //for
d->p=temp->p;
} //else

} // deletenode

void changeNode(nodetype *start, int pos, int value){
nodetype *temp=start;
for(int i=0; i<pos-1; i++){
temp=temp->p;
}
start->data1=value;

}


void printall(nodetype *start){
nodetype *temp;
temp=start;

if(start==NULL){
cout << "\n\n\tError";
} // if
else{
while(temp!=NULL){
cout << " " << temp->data1;
temp=temp->p;
} // while
} // else

} // printall

int listsize(nodetype *start){
nodetype *temp = start;
int size=0;
for(;;){
if(temp==NULL) break;
temp=temp->p;
size++;
}
cout << "\n\n\tList size = " << size;

return size;
}

int main(){
clrscr();
int pos, value;
char c1;
nodetype *list=NULL;
cout << "\n\n\tstartlist() ";
startlist(&list);
printall(list);
getch();
cout << "\n\n\t Insert Node (Y/N)? ";
c1=getche();
if((c1=='Y')||(c1=='y')){
cout << "\n\tNode# = ";
cin >> pos;
cout << "\n\tValue = ";
cin >> value;
addNode(list, pos,value);
}
cout << "\n\n\t";
printall(list);
listsize(list);
cout << "\n\n\t Change Value (Y/N)? ";
c1=getche();
if((c1=='Y')||(c1=='y')){
cout << "\n\tNode# = ";
cin >> pos;
cout << "\n\tValue = ";
cin >> value;
changeNode(list,pos,value);
}
cout << "\n\n\t";
printall(list);
listsize(list);
cout << "\n\n\t Delete Node (Y/N/ALL)? ";
c1=getche();
if((c1=='Y')||(c1=='y')){
cout << "\n\tNode# = ";
cin >> pos;
deleteNode(list,pos);
}
else{
((c1=='A')||(c1=='a'));
cout << "\n\n\tDeleting Nodes";
deleteAll(list);
}
printall(list);
listsize(list);


cout << "\n\n\tComplete";

return 0;
} //MAIN 



for deleteall i want it to just delete all of the nodes then printout zero, but it just printsout the full numbers. any idea how to fix that, then also there is the problem that i get a memory printout when i printout the random numbers. any help is appreciated

here is the output



        startlist()   4318456  549  604  986  944  712  283  556  734  286  861

         Insert Node (Y/N)? y
        Node# =
3

        Value = 333


          4318456  549  333  604  986  944  712  283  556  734  286  861

        List size = 12

         Change Value (Y/N)? y
        Node# = 2

        Value = 222


          222  549  333  604  986  944  712  283  556  734  286  861

        List size = 12

         Delete Node (Y/N/ALL)? a

        Deleting Nodes  222  549  333  604  986  944  712  283  556  734  286  8
61

        List size = 12

        Complete


as you can see, i just want all the nodes to delete and the list size to equal zero. above that i have the memory being pritned out.
hello chee,

that format isn't easy to read. Does your code always look like that?

You want to manipulate 'start' directly so you have to write instead of void deleteNode(nodetype *start, int pos) -> void deleteNode(nodetype **start, int pos) Note that second '*' (like in 'startlist')

What's this line all about nodetype *d = new nodetype;? new in delete?

So that you use always 'pos' it doesn't look like you want a single linked list? Using vector might probably be easier for your purpose
Topic archived. No new replies allowed.