Home › Forums › C Programming › Need desprate help. . .
- This topic has 1 reply, 2 voices, and was last updated 15 years, 3 months ago by GWILouisaxwzkla.
Viewing 1 reply thread
- AuthorPosts
- August 16, 2009 at 7:53 am #2223ZandraB68flxParticipant
I’m currently working on my assignment and i already have a working program.
But there’s something on my my assignment saying that, “Declare an Array called ItemArray that can store up to 100 ItemRec(which is my structure) records. ” Well it asked me to create an item tracking system using linked list.
Uhm any suggestions how I could do that?
Thanks ALOT! in advance :oops:
- August 16, 2009 at 3:21 pm #3605GWILouisaxwzklaParticipant
Heres a linked list program I had laying around:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303<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:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153<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 />
- AuthorPosts
Viewing 1 reply thread
- The forum ‘C Programming’ is closed to new topics and replies.