Home › Forums › C Programming › Need desprate help. . . › Re: Re: Need desprate help. . .
August 16, 2009 at 3:21 pm
#3605
GWILouisaxwzkla
Participant
Heres a linked list program I had laying around:
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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | <br /> /****************************************************************<br /> * File Name : cgt.cpp<br /> * Date : April,10,2007<br /> * Comments : new project<br /> * Compiler/Assembler : VC++ 6.0<br /> *<br /> *<br /> *<br /> *<br /> *<br /> * Program Shell Generated At: 2:59:58<br /> =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /> <br /> <br /> #include <br /> //#include <br /> //#include <br /> //#include <br /> <br /> using namespace std;<br /> <br /> struct node<br /> {<br /> <br /> int data;<br /> node * next;<br /> node * back;<br /> <br /> };<br /> <br /> <br /> //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br /> <br /> void readList ( node ** list );<br /> bool listsAreEqual ( node * left , node * right );<br /> node * copyList ( node * list );<br /> void printList ( node * list );<br /> void destroyList ( node ** list );<br /> bool listsContainEqualMembers ( node * left , node * right );<br /> <br /> //##################################################################################<br /> <br /> //main function ******************************<br /> <br /> int main ( )<br /> {<br /> <br /> node * head1 = 0;<br /> node * head2 = 0;<br /> <br /> cout << "enter first list :" << endl;<br /> readList ( & head1 );<br /> cout << "enter second list: " << endl;<br /> readList ( & head2 );<br /> <br /> if ( listsAreEqual ( head1 , head2 ) )<br /> cout << endl << "The lists are equal" << endl;<br /> else<br /> cout << endl << "The lists are not equal" << endl;<br /> <br /> cout << endl;<br /> if ( listsContainEqualMembers ( head1 , head2 ) )<br /> cout << endl << "lists contain equal members" << endl;<br /> else<br /> cout << endl << "lists do not contain equal members " << endl;<br /> <br /> <br /> node * copy = copyList ( head1 );<br /> cout << endl << "copy of first list is: ";<br /> printList ( copy );<br /> destroyList ( & head1 );<br /> destroyList ( & head2 );<br /> destroyList ( & copy );<br /> return 0 ;<br /> }<br /> <br /> <br /> /******************************* FUNCTION DEFINITION ******************************<br /> <br /> Name : readList<br /> Parameters :<br /> <br /> <br /> <br /> Returns: Void type<br /> Comments:<br /> <br /> <br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> void readList ( node ** list )<br /> {<br /> <br /> cout << "enter an integer ( -1 to stop ) ";<br /> int i;<br /> cin >> i;<br /> node * temp = 0;<br /> <br /> while ( i != -1 )<br /> {<br /> <br /> temp = new node;<br /> if ( temp == 0 )<br /> {<br /> cout << " allocation error" ;<br /> ( * list ) = 0;<br /> return;<br /> }<br /> temp -> data = i ;<br /> if ( ( * list ) == 0 )<br /> {<br /> ( * list ) = temp;<br /> temp -> next = 0;<br /> temp -> back = temp;<br /> <br /> }<br /> else<br /> {<br /> ( * list ) -> back -> next = temp;<br /> temp -> next = temp -> back = 0;<br /> ( * list ) -> back = temp;<br /> }<br /> cout << "enter a number ( -1 to stop ) ";<br /> cin >> i;<br /> }<br /> <br /> return;<br /> }<br /> /******************************* FUNCTION DEFINITION ******************************<br /> <br /> Name : listsAreEqual<br /> Parameters :<br /> <br /> <br /> <br /> Returns: user defined type , bool<br /> <br /> Comments:<br /> <br /> <br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> bool listsAreEqual ( node * left , node * right )<br /> {<br /> <br /> bool matches = true;<br /> <br /> while ( left != 0 && right != 0 && matches )<br /> {<br /> <br /> if ( left -> data != right -> data )<br /> matches = false;<br /> else<br /> {<br /> left = left -> next;<br /> right = right -> next;<br /> }<br /> }<br /> if ( ! left && ! right && matches )<br /> return true;<br /> <br /> return false;<br /> }<br /> /******************************* FUNCTION DEFINITION ******************************<br /> <br /> Name : copyList<br /> Parameters :<br /> <br /> <br /> <br /> Returns: user defined type , node *<br /> <br /> Comments:<br /> <br /> <br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> node * copyList ( node * list )<br /> {<br /> <br /> node * temp = 0;<br /> node * newList = 0;<br /> <br /> while ( list )<br /> {<br /> <br /> temp = new node;<br /> if ( ! temp )<br /> {<br /> cout << "allocation error! ";<br /> return 0;<br /> }<br /> if ( newList == 0 )<br /> {<br /> newList = temp;<br /> temp -> data = list -> data;<br /> newList -> next = 0;<br /> newList -> back = temp;<br /> }<br /> else<br /> {<br /> temp -> data = list -> data;<br /> newList -> back -> next = temp;<br /> newList -> back = temp;<br /> temp -> next = temp -> next = 0;<br /> }<br /> list = list -> next;<br /> }<br /> return newList;<br /> }<br /> void printList ( node * temp )<br /> {<br /> <br /> <br /> cout << "list read is " ;<br /> while ( temp )<br /> {<br /> cout < data << " ";<br /> temp = temp -> next;<br /> }<br /> cout << endl;<br /> }<br /> /******************************* FUNCTION DEFINITION ******************************<br /> <br /> Name : destroyList<br /> Parameters :<br /> <br /> <br /> <br /> Returns: user defined type , void<br /> <br /> Comments:<br /> <br /> <br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> void destroyList ( node ** list )<br /> {<br /> <br /> <br /> node * temp = ( * list );<br /> node * temp2 = 0;<br /> while ( temp )<br /> {<br /> <br /> temp2 = temp;<br /> temp = temp -> next;<br /> delete temp2;<br /> }<br /> <br /> ( * list ) = 0;<br /> }<br /> /******************************* FUNCTION DEFINITION ******************************<br /> <br /> Name : listsContainEqualMembers<br /> Parameters :<br /> <br /> <br /> <br /> Returns: Void type<br /> Comments:<br /> <br /> <br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> bool listsContainEqualMembers ( node * left , node * right )<br /> {<br /> <br /> <br /> node * temp1 = left;<br /> node * temp2 = right;<br /> bool matches ;<br /> int length1 = 0 , length2 = 0;<br /> int numItems1 = 0 , numItems2 = 0;<br /> <br /> while ( temp1 && matches )<br /> {<br /> temp2 = right;<br /> matches = false;<br /> length1 ++; //get length of first list<br /> while ( temp2 && ! matches )<br /> {<br /> if ( temp1 -> data == temp2 -> data )<br /> matches = true;<br /> if ( ! matches )<br /> temp2 = temp2 -> next;<br /> }<br /> if ( matches )<br /> temp1 = temp1 -> next;<br /> }<br /> temp2 = right;<br /> while ( temp2 )<br /> {<br /> length2 ++ ;<br /> temp2 = temp2 -> next;<br /> }<br /> if ( length1 == length2 && matches )<br /> return true;<br /> return false;<br /> }<br /> <br /> |
heres another one:
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 | <br /> <br /> <br /> #include <br /> #include <br /> //simple list<br /> <br /> typedef struct nodeType<br /> {<br /> int i;<br /> struct nodeType * next;<br /> } node;<br /> <br /> void print ( node * );<br /> void addBack ( int , node ** , node ** );<br /> void addFront ( int , node ** , node ** );<br /> void destroy ( node ** );<br /> <br /> void main()<br /> {<br /> <br /> node * head = NULL;<br /> node * tail = NULL;<br /> char choice;<br /> char newline;<br /> int i;<br /> do<br /> {<br /> printf ( "choose an operation: n" );<br /> printf ( "1. add a node to back of list n" );<br /> printf ( "2. add a node to front of list n" );<br /> printf ( "3. print the list. n" );<br /> printf ( "4. exit n" );<br /> choice = getchar ();<br /> newline = getchar ();<br /> printf ( "n" );<br /> switch ( choice )<br /> {<br /> case '1':<br /> {<br /> printf ( "enter an integer : n" );<br /> scanf ( "%d",& i );<br /> addBack ( i , & head , & tail );<br /> newline = getchar ();<br /> };<br /> break;<br /> case '2':<br /> {<br /> printf ( "enter an integer : n" );<br /> scanf ( "%d",& i );<br /> addFront ( i , & head , & tail );<br /> newline = getchar ();<br /> };<br /> break;<br /> case '3':<br /> {<br /> print ( head );<br /> };<br /> break;<br /> case '4':<br /> {<br /> printf ( "goodbye! n" );<br /> };<br /> break;<br /> default: printf ( "bad choice ! n" );<br /> <br /> };<br /> printf ( "n" );<br /> }<br /> while ( choice != '4' );<br /> destroy ( & head );<br /> <br /> <br /> <br /> <br /> <br /> }<br /> <br /> void print ( node * r )<br /> {<br /> <br /> node * t = r;<br /> <br /> printf ( "This is the list: n" );<br /> <br /> while ( t != NULL )<br /> {<br /> printf ( "%c" ," " );<br /> printf ( "%i" , t -> i );<br /> printf ( "%c" , " " );<br /> t = t -> next;<br /> }<br /> printf ( "n" );<br /> <br /> }<br /> <br /> void addBack ( int n , node ** h , node ** t )<br /> {<br /> <br /> if ( ( * h ) == NULL )<br /> {<br /> ( * h ) = ( * t ) = malloc ( sizeof ( node ) );<br /> ( * t ) -> i = n;<br /> ( * t ) -> next = NULL;<br /> <br /> }<br /> else<br /> {<br /> <br /> ( * t ) -> next = malloc ( sizeof ( node ) );<br /> ( * t ) -> next -> i = n;<br /> ( * t ) -> next -> next = NULL;<br /> ( * t ) = ( * t ) -> next;<br /> <br /> }<br /> }<br /> <br /> void addFront ( int n , node ** h , node ** t )<br /> {<br /> <br /> if ( ( * h ) == NULL ) //empty list<br /> {<br /> <br /> ( * h ) = ( * t ) = malloc ( sizeof ( node ) );<br /> ( * t ) -> i = n;<br /> ( * t ) -> next = NULL;<br /> <br /> }<br /> else //add to front<br /> {<br /> node * temp = ( * h );<br /> <br /> ( * h ) = malloc ( sizeof ( node ) );<br /> ( * h ) -> i = n;<br /> ( * h ) -> next = temp;<br /> }<br /> }<br /> <br /> void destroy ( node ** h )<br /> {<br /> <br /> node * temp;<br /> <br /> while ( ( * h ) != NULL )<br /> {<br /> temp = ( * h );<br /> ( * h ) = ( * h ) -> next;<br /> free ( temp );<br /> }<br /> }<br /> <br /> <br /> |