Home › Forums › C Programming › Can You Help Me Plz! › Reply To: Can You Help Me Plz!
August 13, 2008 at 11:32 am
#3415
Humayan
Participant
Heres a quick translation into c:
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 | #include < stdlib.h ><br /> #include < stdio.h ><br /> //simple list<br /> typedef struct nodeType<br /> {<br /> int i;<br /> struct nodeType * next;<br /> } node;<br /> void print ( node * );<br /> void addBack ( int , node ** , node ** );<br /> void addFront ( int , node ** , node ** );<br /> void destroy ( node ** );<br /> void main()<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 /> void print ( node * r )<br /> {<br /> node * t = r;<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 /> void addBack ( int n , node ** h , node ** t )<br /> {<br /> if ( ( * h ) == NULL )<br /> {<br /> ( * h ) = ( * t ) = malloc ( sizeof ( node ) );<br /> ( * t ) -> i = n;<br /> ( * t ) -> next = NULL;<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 /> void addFront ( int n , node ** h , node ** t )<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 /> 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 /> void destroy ( node ** h )<br /> {<br /> node * temp;<br /> while ( ( * h ) != NULL )<br /> {<br /> temp = ( * h );<br /> ( * h ) = ( * h ) -> next;<br /> free ( temp );<br /> }<br /> } |